[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" |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 65 | #include "net/http/http_auth_scheme.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 66 | #include "net/http/http_basic_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 67 | #include "net/http/http_network_session.h" |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 68 | #include "net/http/http_network_session_peer.h" |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 69 | #include "net/http/http_proxy_connect_job.h" |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 70 | #include "net/http/http_request_headers.h" |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 71 | #include "net/http/http_response_info.h" |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 72 | #include "net/http/http_server_properties.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 73 | #include "net/http/http_stream.h" |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 74 | #include "net/http/http_stream_factory.h" |
[email protected] | c41737d | 2014-05-14 07:47:19 | [diff] [blame] | 75 | #include "net/http/http_transaction_test_util.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 76 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 77 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 78 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 79 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 80 | #include "net/log/test_net_log_util.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 81 | #include "net/proxy_resolution/mock_proxy_resolver.h" |
| 82 | #include "net/proxy_resolution/proxy_config_service_fixed.h" |
| 83 | #include "net/proxy_resolution/proxy_info.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 84 | #include "net/proxy_resolution/proxy_resolution_service.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 85 | #include "net/proxy_resolution/proxy_resolver.h" |
| 86 | #include "net/proxy_resolution/proxy_resolver_factory.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 87 | #include "net/socket/client_socket_factory.h" |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 88 | #include "net/socket/client_socket_pool.h" |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 89 | #include "net/socket/client_socket_pool_manager.h" |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 90 | #include "net/socket/connect_job.h" |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 91 | #include "net/socket/connection_attempts.h" |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 92 | #include "net/socket/mock_client_socket_pool_manager.h" |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 93 | #include "net/socket/next_proto.h" |
Paul Jensen | a457017a | 2018-01-19 23:52:04 | [diff] [blame] | 94 | #include "net/socket/socket_tag.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 95 | #include "net/socket/socket_test_util.h" |
Matt Menke | 4b412da | 2019-01-25 19:31:12 | [diff] [blame] | 96 | #include "net/socket/socks_connect_job.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 97 | #include "net/socket/ssl_client_socket.h" |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 98 | #include "net/spdy/spdy_session.h" |
| 99 | #include "net/spdy/spdy_session_pool.h" |
| 100 | #include "net/spdy/spdy_test_util_common.h" |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 101 | #include "net/ssl/client_cert_identity_test_util.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 102 | #include "net/ssl/ssl_cert_request_info.h" |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 103 | #include "net/ssl/ssl_config.h" |
[email protected] | e86839fd | 2013-08-14 18:29:03 | [diff] [blame] | 104 | #include "net/ssl/ssl_config_service.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 105 | #include "net/ssl/ssl_info.h" |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 106 | #include "net/ssl/ssl_private_key.h" |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 107 | #include "net/ssl/test_ssl_config_service.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 108 | #include "net/test/cert_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 109 | #include "net/test/gtest_util.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 110 | #include "net/test/test_data_directory.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 111 | #include "net/test/test_with_task_environment.h" |
Victor Vasiliev | 27cc771 | 2019-01-24 11:50:14 | [diff] [blame] | 112 | #include "net/third_party/quiche/src/spdy/core/spdy_framer.h" |
[email protected] | baee31a | 2018-01-18 06:10:23 | [diff] [blame] | 113 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 114 | #include "net/url_request/static_http_user_agent_settings.h" |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 115 | #include "net/websockets/websocket_handshake_stream_base.h" |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 116 | #include "net/websockets/websocket_test_util.h" |
bnc | f458840 | 2015-11-24 13:33:18 | [diff] [blame] | 117 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 118 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 119 | #include "testing/platform_test.h" |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 120 | #include "url/gurl.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 121 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 122 | #if defined(NTLM_PORTABLE) |
| 123 | #include "base/base64.h" |
| 124 | #include "net/ntlm/ntlm_test_data.h" |
| 125 | #endif |
| 126 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 127 | #if BUILDFLAG(ENABLE_REPORTING) |
| 128 | #include "net/network_error_logging/network_error_logging_service.h" |
| 129 | #include "net/network_error_logging/network_error_logging_test_util.h" |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 130 | #include "net/reporting/reporting_cache.h" |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 131 | #include "net/reporting/reporting_endpoint.h" |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 132 | #include "net/reporting/reporting_header_parser.h" |
| 133 | #include "net/reporting/reporting_service.h" |
| 134 | #include "net/reporting/reporting_test_util.h" |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 135 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 136 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 137 | using net::test::IsError; |
| 138 | using net::test::IsOk; |
| 139 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 140 | using base::ASCIIToUTF16; |
| 141 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 142 | using testing::AnyOf; |
| 143 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 144 | //----------------------------------------------------------------------------- |
| 145 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 146 | namespace net { |
| 147 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 148 | namespace { |
| 149 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 150 | const base::string16 kBar(ASCIIToUTF16("bar")); |
| 151 | const base::string16 kBar2(ASCIIToUTF16("bar2")); |
| 152 | const base::string16 kBar3(ASCIIToUTF16("bar3")); |
| 153 | const base::string16 kBaz(ASCIIToUTF16("baz")); |
| 154 | const base::string16 kFirst(ASCIIToUTF16("first")); |
| 155 | const base::string16 kFoo(ASCIIToUTF16("foo")); |
| 156 | const base::string16 kFoo2(ASCIIToUTF16("foo2")); |
| 157 | const base::string16 kFoo3(ASCIIToUTF16("foo3")); |
| 158 | const base::string16 kFou(ASCIIToUTF16("fou")); |
| 159 | const base::string16 kSecond(ASCIIToUTF16("second")); |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 160 | const base::string16 kWrongPassword(ASCIIToUTF16("wrongpassword")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 161 | |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 162 | const char kAlternativeServiceHttpHeader[] = |
| 163 | "Alt-Svc: h2=\"mail.example.org:443\"\r\n"; |
| 164 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 165 | int GetIdleSocketCountInTransportSocketPool(HttpNetworkSession* session) { |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 166 | return session |
| 167 | ->GetSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 168 | ProxyServer::Direct()) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 169 | ->IdleSocketCount(); |
[email protected] | e5c02664 | 2012-03-17 00:14:02 | [diff] [blame] | 170 | } |
| 171 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 172 | bool IsTransportSocketPoolStalled(HttpNetworkSession* session) { |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 173 | return session |
| 174 | ->GetSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 175 | ProxyServer::Direct()) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 176 | ->IsStalled(); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 179 | // Takes in a Value created from a NetLogHttpResponseParameter, and returns |
| 180 | // a JSONified list of headers as a single string. Uses single quotes instead |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 181 | // of double quotes for easier comparison. |
| 182 | std::string GetHeaders(const base::Value& params) { |
| 183 | if (!params.is_dict()) |
| 184 | return ""; |
| 185 | const base::Value* header_list = params.FindListKey("headers"); |
| 186 | if (!header_list) |
| 187 | return ""; |
| 188 | std::string headers; |
| 189 | base::JSONWriter::Write(*header_list, &headers); |
| 190 | base::ReplaceChars(headers, "\"", "'", &headers); |
| 191 | return headers; |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 194 | // Tests LoadTimingInfo in the case a socket is reused and no PAC script is |
| 195 | // used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 196 | void TestLoadTimingReused(const LoadTimingInfo& load_timing_info) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 197 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 198 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 199 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 200 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 201 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 202 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 203 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 204 | EXPECT_FALSE(load_timing_info.send_start.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 205 | |
| 206 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 207 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 208 | // Set at a higher level. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 209 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 210 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 211 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 214 | // Tests LoadTimingInfo in the case a new socket is used and no PAC script is |
| 215 | // used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 216 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 217 | int connect_timing_flags) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 218 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 219 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 220 | |
| 221 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 222 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 223 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 224 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 225 | connect_timing_flags); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 226 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 227 | load_timing_info.send_start); |
| 228 | |
| 229 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 230 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 231 | // Set at a higher level. |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 232 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 233 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 234 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // Tests LoadTimingInfo in the case a socket is reused and a PAC script is |
| 238 | // used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 239 | void TestLoadTimingReusedWithPac(const LoadTimingInfo& load_timing_info) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 240 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 241 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 242 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 243 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 244 | |
| 245 | EXPECT_FALSE(load_timing_info.proxy_resolve_start.is_null()); |
| 246 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 247 | load_timing_info.proxy_resolve_end); |
| 248 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 249 | load_timing_info.send_start); |
| 250 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 251 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 252 | // Set at a higher level. |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 253 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 254 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 255 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // Tests LoadTimingInfo in the case a new socket is used and a PAC script is |
| 259 | // used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 260 | void TestLoadTimingNotReusedWithPac(const LoadTimingInfo& load_timing_info, |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 261 | int connect_timing_flags) { |
| 262 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 263 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 264 | |
| 265 | EXPECT_FALSE(load_timing_info.proxy_resolve_start.is_null()); |
| 266 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 267 | load_timing_info.proxy_resolve_end); |
| 268 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 269 | load_timing_info.connect_timing.connect_start); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 270 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 271 | connect_timing_flags); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 272 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 273 | load_timing_info.send_start); |
| 274 | |
| 275 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 276 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 277 | // Set at a higher level. |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 278 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 279 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 280 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 281 | } |
| 282 | |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 283 | // ProxyResolver that records URLs passed to it, and that can be told what |
| 284 | // result to return. |
| 285 | class CapturingProxyResolver : public ProxyResolver { |
| 286 | public: |
| 287 | CapturingProxyResolver() |
| 288 | : proxy_server_(ProxyServer::SCHEME_HTTP, HostPortPair("myproxy", 80)) {} |
| 289 | ~CapturingProxyResolver() override = default; |
| 290 | |
| 291 | int GetProxyForURL(const GURL& url, |
| 292 | ProxyInfo* results, |
| 293 | CompletionOnceCallback callback, |
| 294 | std::unique_ptr<Request>* request, |
| 295 | const NetLogWithSource& net_log) override { |
| 296 | results->UseProxyServer(proxy_server_); |
| 297 | resolved_.push_back(url); |
| 298 | return OK; |
| 299 | } |
| 300 | |
| 301 | // Sets whether the resolver should use direct connections, instead of a |
| 302 | // proxy. |
| 303 | void set_proxy_server(ProxyServer proxy_server) { |
| 304 | proxy_server_ = proxy_server; |
| 305 | } |
| 306 | |
| 307 | const std::vector<GURL>& resolved() const { return resolved_; } |
| 308 | |
| 309 | private: |
| 310 | std::vector<GURL> resolved_; |
| 311 | |
| 312 | ProxyServer proxy_server_; |
| 313 | |
| 314 | DISALLOW_COPY_AND_ASSIGN(CapturingProxyResolver); |
| 315 | }; |
| 316 | |
| 317 | class CapturingProxyResolverFactory : public ProxyResolverFactory { |
| 318 | public: |
| 319 | explicit CapturingProxyResolverFactory(CapturingProxyResolver* resolver) |
| 320 | : ProxyResolverFactory(false), resolver_(resolver) {} |
| 321 | |
| 322 | int CreateProxyResolver(const scoped_refptr<PacFileData>& pac_script, |
| 323 | std::unique_ptr<ProxyResolver>* resolver, |
| 324 | CompletionOnceCallback callback, |
| 325 | std::unique_ptr<Request>* request) override { |
| 326 | *resolver = std::make_unique<ForwardingProxyResolver>(resolver_); |
| 327 | return OK; |
| 328 | } |
| 329 | |
| 330 | private: |
| 331 | ProxyResolver* resolver_; |
| 332 | }; |
| 333 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 334 | std::unique_ptr<HttpNetworkSession> CreateSession( |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 335 | SpdySessionDependencies* session_deps) { |
[email protected] | c6bf815 | 2012-12-02 07:43:34 | [diff] [blame] | 336 | return SpdySessionDependencies::SpdyCreateSession(session_deps); |
[email protected] | e8d53619 | 2008-10-17 22:21:14 | [diff] [blame] | 337 | } |
| 338 | |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 339 | class FailingProxyResolverFactory : public ProxyResolverFactory { |
| 340 | public: |
| 341 | FailingProxyResolverFactory() : ProxyResolverFactory(false) {} |
| 342 | |
| 343 | // ProxyResolverFactory override. |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 344 | int CreateProxyResolver(const scoped_refptr<PacFileData>& script_data, |
| 345 | std::unique_ptr<ProxyResolver>* result, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 346 | CompletionOnceCallback callback, |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 347 | std::unique_ptr<Request>* request) override { |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 348 | return ERR_PAC_SCRIPT_FAILED; |
| 349 | } |
| 350 | }; |
| 351 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 352 | } // namespace |
| 353 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 354 | class HttpNetworkTransactionTest : public PlatformTest, |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 355 | public WithTaskEnvironment { |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 356 | public: |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 357 | ~HttpNetworkTransactionTest() override { |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 358 | // Important to restore the per-pool limit first, since the pool limit must |
| 359 | // always be greater than group limit, and the tests reduce both limits. |
| 360 | ClientSocketPoolManager::set_max_sockets_per_pool( |
| 361 | HttpNetworkSession::NORMAL_SOCKET_POOL, old_max_pool_sockets_); |
| 362 | ClientSocketPoolManager::set_max_sockets_per_group( |
| 363 | HttpNetworkSession::NORMAL_SOCKET_POOL, old_max_group_sockets_); |
| 364 | } |
| 365 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 366 | protected: |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 367 | HttpNetworkTransactionTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 368 | : WithTaskEnvironment(base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 369 | dummy_connect_job_params_( |
| 370 | nullptr /* client_socket_factory */, |
| 371 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 372 | nullptr /* http_auth_cache */, |
| 373 | nullptr /* http_auth_handler_factory */, |
| 374 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 375 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 376 | nullptr /* quic_stream_factory */, |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 377 | nullptr /* proxy_delegate */, |
| 378 | nullptr /* http_user_agent_settings */, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 379 | nullptr /* ssl_client_context */, |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 380 | nullptr /* socket_performance_watcher_factory */, |
| 381 | nullptr /* network_quality_estimator */, |
| 382 | nullptr /* net_log */, |
| 383 | nullptr /* websocket_endpoint_lock_manager */), |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 384 | ssl_(ASYNC, OK), |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 385 | old_max_group_sockets_(ClientSocketPoolManager::max_sockets_per_group( |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 386 | HttpNetworkSession::NORMAL_SOCKET_POOL)), |
| 387 | old_max_pool_sockets_(ClientSocketPoolManager::max_sockets_per_pool( |
| 388 | HttpNetworkSession::NORMAL_SOCKET_POOL)) { |
bnc | a86731e | 2017-04-17 12:31:28 | [diff] [blame] | 389 | session_deps_.enable_http2_alternative_service = true; |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 390 | } |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 391 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 392 | struct SimpleGetHelperResult { |
| 393 | int rv; |
| 394 | std::string status_line; |
| 395 | std::string response_data; |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 396 | int64_t total_received_bytes; |
| 397 | int64_t total_sent_bytes; |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 398 | LoadTimingInfo load_timing_info; |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 399 | ConnectionAttempts connection_attempts; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 400 | IPEndPoint remote_endpoint_after_start; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 401 | }; |
| 402 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 403 | void SetUp() override { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 404 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 405 | base::RunLoop().RunUntilIdle(); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 406 | // Set an initial delay to ensure that the first call to TimeTicks::Now() |
| 407 | // before incrementing the counter does not return a null value. |
Matt Menke | 6dc0823 | 2019-10-03 18:00:28 | [diff] [blame] | 408 | FastForwardBy(base::TimeDelta::FromSeconds(1)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 409 | } |
| 410 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 411 | void TearDown() override { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 412 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 413 | base::RunLoop().RunUntilIdle(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 414 | // Empty the current queue. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 415 | base::RunLoop().RunUntilIdle(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 416 | PlatformTest::TearDown(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 417 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 418 | base::RunLoop().RunUntilIdle(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 419 | } |
| 420 | |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 421 | void Check100ResponseTiming(bool use_spdy); |
| 422 | |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 423 | // Either |write_failure| specifies a write failure or |read_failure| |
| 424 | // specifies a read failure when using a reused socket. In either case, the |
| 425 | // failure should cause the network transaction to resend the request, and the |
| 426 | // other argument should be NULL. |
| 427 | void KeepAliveConnectionResendRequestTest(const MockWrite* write_failure, |
| 428 | const MockRead* read_failure); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 429 | |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [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 PreconnectErrorResendRequestTest(const MockWrite* write_failure, |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 435 | const MockRead* read_failure, |
| 436 | bool use_spdy); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 437 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 438 | SimpleGetHelperResult SimpleGetHelperForData( |
| 439 | base::span<StaticSocketDataProvider*> providers) { |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 440 | SimpleGetHelperResult out; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 441 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 442 | HttpRequestInfo request; |
| 443 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 444 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 445 | request.traffic_annotation = |
| 446 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 447 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 448 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 449 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 450 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 451 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 452 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 453 | for (auto* provider : providers) { |
| 454 | session_deps_.socket_factory->AddSocketDataProvider(provider); |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 455 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 456 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 457 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 458 | |
eroman | 24bc6a1 | 2015-05-06 19:55:48 | [diff] [blame] | 459 | EXPECT_TRUE(log.bound().IsCapturing()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 460 | int rv = trans.Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 461 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 462 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 463 | out.rv = callback.WaitForResult(); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 464 | out.total_received_bytes = trans.GetTotalReceivedBytes(); |
| 465 | out.total_sent_bytes = trans.GetTotalSentBytes(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 466 | |
| 467 | // Even in the failure cases that use this function, connections are always |
| 468 | // successfully established before the error. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 469 | EXPECT_TRUE(trans.GetLoadTimingInfo(&out.load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 470 | TestLoadTimingNotReused(out.load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 471 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 472 | if (out.rv != OK) |
| 473 | return out; |
| 474 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 475 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 476 | // Can't use ASSERT_* inside helper functions like this, so |
| 477 | // return an error. |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 478 | if (!response || !response->headers) { |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 479 | out.rv = ERR_UNEXPECTED; |
| 480 | return out; |
| 481 | } |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 482 | out.status_line = response->headers->GetStatusLine(); |
| 483 | |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 484 | EXPECT_EQ("127.0.0.1", response->remote_endpoint.ToStringWithoutPort()); |
| 485 | EXPECT_EQ(80, response->remote_endpoint.port()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 486 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 487 | bool got_endpoint = |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 488 | trans.GetRemoteEndpoint(&out.remote_endpoint_after_start); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 489 | EXPECT_EQ(got_endpoint, |
| 490 | out.remote_endpoint_after_start.address().size() > 0); |
| 491 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 492 | rv = ReadTransaction(&trans, &out.response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 493 | EXPECT_THAT(rv, IsOk()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 494 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 495 | auto entries = log.GetEntries(); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 496 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 497 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
| 498 | NetLogEventPhase::NONE); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 499 | ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 500 | entries, pos, NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 501 | NetLogEventPhase::NONE); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 502 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 503 | EXPECT_EQ("GET / HTTP/1.1\r\n", |
| 504 | GetStringValueFromParams(entries[pos], "line")); |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 505 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 506 | EXPECT_EQ("['Host: www.example.org','Connection: keep-alive']", |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 507 | GetHeaders(entries[pos].params)); |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 508 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 509 | out.total_received_bytes = trans.GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 510 | // The total number of sent bytes should not have changed. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 511 | EXPECT_EQ(out.total_sent_bytes, trans.GetTotalSentBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 512 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 513 | trans.GetConnectionAttempts(&out.connection_attempts); |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 514 | return out; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 515 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 516 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 517 | SimpleGetHelperResult SimpleGetHelper(base::span<const MockRead> data_reads) { |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 518 | MockWrite data_writes[] = { |
| 519 | MockWrite("GET / HTTP/1.1\r\n" |
| 520 | "Host: www.example.org\r\n" |
| 521 | "Connection: keep-alive\r\n\r\n"), |
| 522 | }; |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 523 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 524 | StaticSocketDataProvider reads(data_reads, data_writes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 525 | StaticSocketDataProvider* data[] = {&reads}; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 526 | SimpleGetHelperResult out = SimpleGetHelperForData(data); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 527 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 528 | EXPECT_EQ(CountWriteBytes(data_writes), out.total_sent_bytes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 529 | return out; |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 530 | } |
| 531 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 532 | void AddSSLSocketData() { |
| 533 | ssl_.next_proto = kProtoHTTP2; |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 534 | ssl_.ssl_info.cert = |
| 535 | ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"); |
| 536 | ASSERT_TRUE(ssl_.ssl_info.cert); |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 537 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_); |
| 538 | } |
| 539 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 540 | void ConnectStatusHelperWithExpectedStatus(const MockRead& status, |
| 541 | int expected_status); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 542 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 543 | void ConnectStatusHelper(const MockRead& status); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 544 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 545 | void CheckErrorIsPassedBack(int error, IoMode mode); |
| 546 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 547 | base::RepeatingClosure FastForwardByCallback(base::TimeDelta delta) { |
| 548 | return base::BindRepeating(&HttpNetworkTransactionTest::FastForwardBy, |
| 549 | base::Unretained(this), delta); |
| 550 | } |
| 551 | |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 552 | const CommonConnectJobParams dummy_connect_job_params_; |
| 553 | |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 554 | // These clocks are defined here, even though they're only used in the |
| 555 | // Reporting tests below, since they need to be destroyed after |
| 556 | // |session_deps_|. |
| 557 | base::SimpleTestClock clock_; |
| 558 | base::SimpleTestTickClock tick_clock_; |
| 559 | |
[email protected] | 4bd4622 | 2013-05-14 19:32:23 | [diff] [blame] | 560 | SpdyTestUtil spdy_util_; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 561 | SpdySessionDependencies session_deps_; |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 562 | SSLSocketDataProvider ssl_; |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 563 | |
| 564 | // Original socket limits. Some tests set these. Safest to always restore |
| 565 | // them once each test has been run. |
| 566 | int old_max_group_sockets_; |
| 567 | int old_max_pool_sockets_; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 568 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 569 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 570 | namespace { |
| 571 | |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 572 | class BeforeHeadersSentHandler { |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 573 | public: |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 574 | BeforeHeadersSentHandler() |
| 575 | : observed_before_headers_sent_with_proxy_(false), |
| 576 | observed_before_headers_sent_(false) {} |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 577 | |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 578 | void OnBeforeHeadersSent(const ProxyInfo& proxy_info, |
| 579 | HttpRequestHeaders* request_headers) { |
| 580 | observed_before_headers_sent_ = true; |
| 581 | if (!proxy_info.is_http() && !proxy_info.is_https() && |
| 582 | !proxy_info.is_quic()) { |
| 583 | return; |
| 584 | } |
| 585 | observed_before_headers_sent_with_proxy_ = true; |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 586 | observed_proxy_server_uri_ = proxy_info.proxy_server().ToURI(); |
| 587 | } |
| 588 | |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 589 | bool observed_before_headers_sent_with_proxy() const { |
| 590 | return observed_before_headers_sent_with_proxy_; |
| 591 | } |
| 592 | |
| 593 | bool observed_before_headers_sent() const { |
| 594 | return observed_before_headers_sent_; |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | std::string observed_proxy_server_uri() const { |
| 598 | return observed_proxy_server_uri_; |
| 599 | } |
| 600 | |
| 601 | private: |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 602 | bool observed_before_headers_sent_with_proxy_; |
| 603 | bool observed_before_headers_sent_; |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 604 | std::string observed_proxy_server_uri_; |
| 605 | |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 606 | DISALLOW_COPY_AND_ASSIGN(BeforeHeadersSentHandler); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 607 | }; |
| 608 | |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 609 | // Fill |str| with a long header list that consumes >= |size| bytes. |
| 610 | void FillLargeHeadersString(std::string* str, int size) { |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 611 | const char row[] = |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 612 | "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; |
| 613 | const int sizeof_row = strlen(row); |
| 614 | const int num_rows = static_cast<int>( |
| 615 | ceil(static_cast<float>(size) / sizeof_row)); |
| 616 | const int sizeof_data = num_rows * sizeof_row; |
| 617 | DCHECK(sizeof_data >= size); |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 618 | str->reserve(sizeof_data); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 619 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 620 | for (int i = 0; i < num_rows; ++i) |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 621 | str->append(row, sizeof_row); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 622 | } |
| 623 | |
thakis | 84dff94 | 2015-07-28 20:47:38 | [diff] [blame] | 624 | #if defined(NTLM_PORTABLE) |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 625 | uint64_t MockGetMSTime() { |
| 626 | // Tue, 23 May 2017 20:13:07 +0000 |
| 627 | return 131400439870000000; |
| 628 | } |
| 629 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 630 | // Alternative functions that eliminate randomness and dependency on the local |
| 631 | // host name so that the generated NTLM messages are reproducible. |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 632 | void MockGenerateRandom(uint8_t* output, size_t n) { |
| 633 | // This is set to 0xaa because the client challenge for testing in |
| 634 | // [MS-NLMP] Section 4.2.1 is 8 bytes of 0xaa. |
| 635 | memset(output, 0xaa, n); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 636 | } |
| 637 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 638 | std::string MockGetHostName() { |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 639 | return ntlm::test::kHostnameAscii; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 640 | } |
thakis | 84dff94 | 2015-07-28 20:47:38 | [diff] [blame] | 641 | #endif // defined(NTLM_PORTABLE) |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 642 | |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 643 | class CaptureGroupIdTransportSocketPool : public TransportClientSocketPool { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 644 | public: |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 645 | explicit CaptureGroupIdTransportSocketPool( |
| 646 | const CommonConnectJobParams* common_connect_job_params) |
| 647 | : TransportClientSocketPool(0, |
| 648 | 0, |
| 649 | base::TimeDelta(), |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 650 | ProxyServer::Direct(), |
| 651 | false /* is_for_websockets */, |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 652 | common_connect_job_params) {} |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 653 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 654 | const ClientSocketPool::GroupId& last_group_id_received() const { |
| 655 | return last_group_id_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 656 | } |
| 657 | |
Tarun Bansal | 162eabe5 | 2018-01-20 01:16:39 | [diff] [blame] | 658 | bool socket_requested() const { return socket_requested_; } |
| 659 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 660 | int RequestSocket( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 661 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 662 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 663 | const base::Optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 664 | RequestPriority priority, |
| 665 | const SocketTag& socket_tag, |
| 666 | ClientSocketPool::RespectLimits respect_limits, |
| 667 | ClientSocketHandle* handle, |
| 668 | CompletionOnceCallback callback, |
| 669 | const ClientSocketPool::ProxyAuthCallback& proxy_auth_callback, |
| 670 | const NetLogWithSource& net_log) override { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 671 | last_group_id_ = group_id; |
Tarun Bansal | 162eabe5 | 2018-01-20 01:16:39 | [diff] [blame] | 672 | socket_requested_ = true; |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 673 | return ERR_IO_PENDING; |
| 674 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 675 | void CancelRequest(const ClientSocketPool::GroupId& group_id, |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 676 | ClientSocketHandle* handle, |
| 677 | bool cancel_connect_job) override {} |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 678 | void ReleaseSocket(const ClientSocketPool::GroupId& group_id, |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 679 | std::unique_ptr<StreamSocket> socket, |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 680 | int64_t generation) override {} |
dmichael | d6e570d | 2014-12-18 22:30:57 | [diff] [blame] | 681 | void CloseIdleSockets() override {} |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 682 | void CloseIdleSocketsInGroup( |
| 683 | const ClientSocketPool::GroupId& group_id) override {} |
dmichael | d6e570d | 2014-12-18 22:30:57 | [diff] [blame] | 684 | int IdleSocketCount() const override { return 0; } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 685 | size_t IdleSocketCountInGroup( |
| 686 | const ClientSocketPool::GroupId& group_id) const override { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 687 | return 0; |
| 688 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 689 | LoadState GetLoadState(const ClientSocketPool::GroupId& group_id, |
dmichael | d6e570d | 2014-12-18 22:30:57 | [diff] [blame] | 690 | const ClientSocketHandle* handle) const override { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 691 | return LOAD_STATE_IDLE; |
| 692 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 693 | |
| 694 | private: |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 695 | ClientSocketPool::GroupId last_group_id_; |
Tarun Bansal | 162eabe5 | 2018-01-20 01:16:39 | [diff] [blame] | 696 | bool socket_requested_ = false; |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 697 | }; |
| 698 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 699 | //----------------------------------------------------------------------------- |
| 700 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 701 | // Helper functions for validating that AuthChallengeInfo's are correctly |
| 702 | // configured for common cases. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 703 | bool CheckBasicServerAuth( |
| 704 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 705 | if (!auth_challenge) |
| 706 | return false; |
| 707 | EXPECT_FALSE(auth_challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 708 | EXPECT_EQ("http://www.example.org", auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 709 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 710 | EXPECT_EQ(kBasicAuthScheme, auth_challenge->scheme); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 711 | return true; |
| 712 | } |
| 713 | |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 714 | bool CheckBasicSecureServerAuth( |
| 715 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
| 716 | if (!auth_challenge) |
| 717 | return false; |
| 718 | EXPECT_FALSE(auth_challenge->is_proxy); |
| 719 | EXPECT_EQ("https://www.example.org", auth_challenge->challenger.Serialize()); |
| 720 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
| 721 | EXPECT_EQ(kBasicAuthScheme, auth_challenge->scheme); |
| 722 | return true; |
| 723 | } |
| 724 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 725 | bool CheckBasicProxyAuth( |
| 726 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 727 | if (!auth_challenge) |
| 728 | return false; |
| 729 | EXPECT_TRUE(auth_challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 730 | EXPECT_EQ("http://myproxy:70", auth_challenge->challenger.Serialize()); |
| 731 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
| 732 | EXPECT_EQ(kBasicAuthScheme, auth_challenge->scheme); |
| 733 | return true; |
| 734 | } |
| 735 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 736 | bool CheckBasicSecureProxyAuth( |
| 737 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 738 | if (!auth_challenge) |
| 739 | return false; |
| 740 | EXPECT_TRUE(auth_challenge->is_proxy); |
| 741 | EXPECT_EQ("https://myproxy:70", auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 742 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 743 | EXPECT_EQ(kBasicAuthScheme, auth_challenge->scheme); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 744 | return true; |
| 745 | } |
| 746 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 747 | bool CheckDigestServerAuth( |
| 748 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 749 | if (!auth_challenge) |
| 750 | return false; |
| 751 | EXPECT_FALSE(auth_challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 752 | EXPECT_EQ("http://www.example.org", auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 753 | EXPECT_EQ("digestive", auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 754 | EXPECT_EQ(kDigestAuthScheme, auth_challenge->scheme); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 755 | return true; |
| 756 | } |
| 757 | |
thakis | 84dff94 | 2015-07-28 20:47:38 | [diff] [blame] | 758 | #if defined(NTLM_PORTABLE) |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 759 | bool CheckNTLMServerAuth( |
| 760 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 761 | if (!auth_challenge) |
| 762 | return false; |
| 763 | EXPECT_FALSE(auth_challenge->is_proxy); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 764 | EXPECT_EQ("https://server", auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 765 | EXPECT_EQ(std::string(), auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 766 | EXPECT_EQ(kNtlmAuthScheme, auth_challenge->scheme); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 767 | return true; |
| 768 | } |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 769 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 770 | bool CheckNTLMProxyAuth( |
| 771 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 772 | if (!auth_challenge) |
| 773 | return false; |
| 774 | EXPECT_TRUE(auth_challenge->is_proxy); |
| 775 | EXPECT_EQ("http://server", auth_challenge->challenger.Serialize()); |
| 776 | EXPECT_EQ(std::string(), auth_challenge->realm); |
| 777 | EXPECT_EQ(kNtlmAuthScheme, auth_challenge->scheme); |
| 778 | return true; |
| 779 | } |
thakis | 84dff94 | 2015-07-28 20:47:38 | [diff] [blame] | 780 | #endif // defined(NTLM_PORTABLE) |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 781 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 782 | } // namespace |
| 783 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 784 | // TODO(950069): Add testing for frame_origin in NetworkIsolationKey |
| 785 | // using kAppendInitiatingFrameOriginToNetworkIsolationKey. |
| 786 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 787 | TEST_F(HttpNetworkTransactionTest, Basic) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 788 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 789 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 790 | } |
| 791 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 792 | TEST_F(HttpNetworkTransactionTest, SimpleGET) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 793 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 794 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 795 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 796 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 797 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 798 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 799 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 800 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 801 | EXPECT_EQ("hello world", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 802 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 803 | EXPECT_EQ(reads_size, out.total_received_bytes); |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 804 | EXPECT_EQ(0u, out.connection_attempts.size()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 805 | |
| 806 | EXPECT_FALSE(out.remote_endpoint_after_start.address().empty()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | // Response with no status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 810 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 811 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 812 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 813 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 814 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 815 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 816 | EXPECT_THAT(out.rv, IsOk()); |
| 817 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 818 | EXPECT_EQ("hello world", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 819 | int64_t reads_size = CountReadBytes(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 820 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 821 | } |
| 822 | |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 823 | // Response with no status line, and a weird port. Should fail by default. |
| 824 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeadersWeirdPort) { |
| 825 | MockRead data_reads[] = { |
| 826 | MockRead("hello world"), MockRead(SYNCHRONOUS, OK), |
| 827 | }; |
| 828 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 829 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 830 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 831 | |
| 832 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 833 | |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 834 | HttpRequestInfo request; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 835 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 836 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 837 | |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 838 | request.method = "GET"; |
| 839 | request.url = GURL("http://www.example.com:2000/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 840 | request.traffic_annotation = |
| 841 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 842 | |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 843 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 844 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 845 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_INVALID_HTTP_RESPONSE)); |
| 846 | } |
| 847 | |
Shivani Sharma | fdcaefd | 2017-11-02 00:12:26 | [diff] [blame] | 848 | // Tests that request info can be destroyed after the headers phase is complete. |
| 849 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoReadDestroyRequestInfo) { |
| 850 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 851 | auto trans = |
| 852 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 853 | |
| 854 | MockRead data_reads[] = { |
| 855 | MockRead("HTTP/1.0 200 OK\r\n"), MockRead("Connection: keep-alive\r\n"), |
| 856 | MockRead("Content-Length: 100\r\n\r\n"), MockRead(SYNCHRONOUS, 0), |
| 857 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 858 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
Shivani Sharma | fdcaefd | 2017-11-02 00:12:26 | [diff] [blame] | 859 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 860 | |
| 861 | TestCompletionCallback callback; |
| 862 | |
| 863 | { |
| 864 | auto request = std::make_unique<HttpRequestInfo>(); |
| 865 | request->method = "GET"; |
| 866 | request->url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 867 | request->traffic_annotation = |
| 868 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Shivani Sharma | fdcaefd | 2017-11-02 00:12:26 | [diff] [blame] | 869 | |
| 870 | int rv = |
| 871 | trans->Start(request.get(), callback.callback(), NetLogWithSource()); |
| 872 | |
| 873 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 874 | } // Let request info be destroyed. |
| 875 | |
| 876 | trans.reset(); |
| 877 | } |
| 878 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 879 | // Allow up to 4 bytes of junk to precede status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 880 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk3Bytes) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 881 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 882 | MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 883 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 884 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 885 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 886 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 887 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 888 | EXPECT_EQ("DATA", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 889 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 890 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | // Allow up to 4 bytes of junk to precede status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 894 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 895 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 896 | MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 897 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 898 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 899 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 900 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 901 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 902 | EXPECT_EQ("DATA", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 903 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 904 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | // Beyond 4 bytes of slop and it should fail to find a status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 908 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 909 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 910 | MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 911 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 912 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 913 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 914 | EXPECT_THAT(out.rv, IsOk()); |
| 915 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 916 | EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 917 | int64_t reads_size = CountReadBytes(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 918 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | // Same as StatusLineJunk4Bytes, except the read chunks are smaller. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 922 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 923 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 924 | MockRead("\n"), |
| 925 | MockRead("\n"), |
| 926 | MockRead("Q"), |
| 927 | MockRead("J"), |
| 928 | MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 929 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 930 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 931 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 932 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 933 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 934 | EXPECT_EQ("DATA", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 935 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 936 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | // Close the connection before enough bytes to have a status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 940 | TEST_F(HttpNetworkTransactionTest, StatusLinePartial) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 941 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 942 | MockRead("HTT"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 943 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 944 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 945 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 946 | EXPECT_THAT(out.rv, IsOk()); |
| 947 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 948 | EXPECT_EQ("HTT", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 949 | int64_t reads_size = CountReadBytes(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 950 | EXPECT_EQ(reads_size, out.total_received_bytes); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 951 | } |
| 952 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 953 | // Simulate a 204 response, lacking a Content-Length header, sent over a |
| 954 | // persistent connection. The response should still terminate since a 204 |
| 955 | // cannot have a response body. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 956 | TEST_F(HttpNetworkTransactionTest, StopsReading204) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 957 | char junk[] = "junk"; |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 958 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 959 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 960 | MockRead(junk), // Should not be read!! |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 961 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 962 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 963 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 964 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 965 | EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line); |
| 966 | EXPECT_EQ("", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 967 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 968 | int64_t response_size = reads_size - strlen(junk); |
| 969 | EXPECT_EQ(response_size, out.total_received_bytes); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 970 | } |
| 971 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 972 | // A simple request using chunked encoding with some extra data after. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 973 | TEST_F(HttpNetworkTransactionTest, ChunkedEncoding) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 974 | std::string final_chunk = "0\r\n\r\n"; |
| 975 | std::string extra_data = "HTTP/1.1 200 OK\r\n"; |
| 976 | std::string last_read = final_chunk + extra_data; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 977 | MockRead data_reads[] = { |
| 978 | MockRead("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"), |
| 979 | MockRead("5\r\nHello\r\n"), |
| 980 | MockRead("1\r\n"), |
| 981 | MockRead(" \r\n"), |
| 982 | MockRead("5\r\nworld\r\n"), |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 983 | MockRead(last_read.data()), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 984 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 985 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 986 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 987 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 988 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 989 | EXPECT_EQ("Hello world", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 990 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 991 | int64_t response_size = reads_size - extra_data.size(); |
| 992 | EXPECT_EQ(response_size, out.total_received_bytes); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 993 | } |
| 994 | |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 995 | // Next tests deal with http://crbug.com/56344. |
| 996 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 997 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 998 | MultipleContentLengthHeadersNoTransferEncoding) { |
| 999 | MockRead data_reads[] = { |
| 1000 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1001 | MockRead("Content-Length: 10\r\n"), |
| 1002 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1003 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1004 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1005 | EXPECT_THAT(out.rv, IsError(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH)); |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 1006 | } |
| 1007 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1008 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1009 | DuplicateContentLengthHeadersNoTransferEncoding) { |
| 1010 | MockRead data_reads[] = { |
| 1011 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1012 | MockRead("Content-Length: 5\r\n"), |
| 1013 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1014 | MockRead("Hello"), |
| 1015 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1016 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1017 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1018 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1019 | EXPECT_EQ("Hello", out.response_data); |
| 1020 | } |
| 1021 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1022 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1023 | ComplexContentLengthHeadersNoTransferEncoding) { |
| 1024 | // More than 2 dupes. |
| 1025 | { |
| 1026 | MockRead data_reads[] = { |
| 1027 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1028 | MockRead("Content-Length: 5\r\n"), |
| 1029 | MockRead("Content-Length: 5\r\n"), |
| 1030 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1031 | MockRead("Hello"), |
| 1032 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1033 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1034 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1035 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1036 | EXPECT_EQ("Hello", out.response_data); |
| 1037 | } |
| 1038 | // HTTP/1.0 |
| 1039 | { |
| 1040 | MockRead data_reads[] = { |
| 1041 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 1042 | MockRead("Content-Length: 5\r\n"), |
| 1043 | MockRead("Content-Length: 5\r\n"), |
| 1044 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1045 | MockRead("Hello"), |
| 1046 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1047 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1048 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1049 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 1050 | EXPECT_EQ("Hello", out.response_data); |
| 1051 | } |
| 1052 | // 2 dupes and one mismatched. |
| 1053 | { |
| 1054 | MockRead data_reads[] = { |
| 1055 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1056 | MockRead("Content-Length: 10\r\n"), |
| 1057 | MockRead("Content-Length: 10\r\n"), |
| 1058 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1059 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1060 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1061 | EXPECT_THAT(out.rv, IsError(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH)); |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1065 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 1066 | MultipleContentLengthHeadersTransferEncoding) { |
| 1067 | MockRead data_reads[] = { |
| 1068 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1069 | MockRead("Content-Length: 666\r\n"), |
| 1070 | MockRead("Content-Length: 1337\r\n"), |
| 1071 | MockRead("Transfer-Encoding: chunked\r\n\r\n"), |
| 1072 | MockRead("5\r\nHello\r\n"), |
| 1073 | MockRead("1\r\n"), |
| 1074 | MockRead(" \r\n"), |
| 1075 | MockRead("5\r\nworld\r\n"), |
| 1076 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1077 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 1078 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1079 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1080 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 1081 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1082 | EXPECT_EQ("Hello world", out.response_data); |
| 1083 | } |
| 1084 | |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1085 | // Next tests deal with http://crbug.com/98895. |
| 1086 | |
| 1087 | // Checks that a single Content-Disposition header results in no error. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1088 | TEST_F(HttpNetworkTransactionTest, SingleContentDispositionHeader) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1089 | MockRead data_reads[] = { |
| 1090 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1091 | MockRead("Content-Disposition: attachment;filename=\"salutations.txt\"r\n"), |
| 1092 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1093 | MockRead("Hello"), |
| 1094 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1095 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1096 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1097 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1098 | EXPECT_EQ("Hello", out.response_data); |
| 1099 | } |
| 1100 | |
[email protected] | 54a9c6e5 | 2012-03-21 20:10:59 | [diff] [blame] | 1101 | // Checks that two identical Content-Disposition headers result in no error. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1102 | TEST_F(HttpNetworkTransactionTest, TwoIdenticalContentDispositionHeaders) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1103 | MockRead data_reads[] = { |
| 1104 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1105 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 1106 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 1107 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1108 | MockRead("Hello"), |
| 1109 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1110 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1111 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 54a9c6e5 | 2012-03-21 20:10:59 | [diff] [blame] | 1112 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1113 | EXPECT_EQ("Hello", out.response_data); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | // Checks that two distinct Content-Disposition headers result in an error. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1117 | TEST_F(HttpNetworkTransactionTest, TwoDistinctContentDispositionHeaders) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1118 | MockRead data_reads[] = { |
| 1119 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1120 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 1121 | MockRead("Content-Disposition: attachment;filename=\"hi.txt\"r\n"), |
| 1122 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1123 | MockRead("Hello"), |
| 1124 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1125 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1126 | EXPECT_THAT(out.rv, |
| 1127 | IsError(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION)); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1128 | } |
| 1129 | |
[email protected] | 54a9c6e5 | 2012-03-21 20:10:59 | [diff] [blame] | 1130 | // Checks that two identical Location headers result in no error. |
| 1131 | // Also tests Location header behavior. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1132 | TEST_F(HttpNetworkTransactionTest, TwoIdenticalLocationHeaders) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1133 | MockRead data_reads[] = { |
| 1134 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 1135 | MockRead("Location: http://good.com/\r\n"), |
[email protected] | 54a9c6e5 | 2012-03-21 20:10:59 | [diff] [blame] | 1136 | MockRead("Location: http://good.com/\r\n"), |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1137 | MockRead("Content-Length: 0\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1138 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1139 | }; |
| 1140 | |
| 1141 | HttpRequestInfo request; |
| 1142 | request.method = "GET"; |
| 1143 | request.url = GURL("http://redirect.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1144 | request.traffic_annotation = |
| 1145 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1146 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1147 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1148 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1149 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1150 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1151 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1152 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1153 | TestCompletionCallback callback; |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1154 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1155 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1156 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1157 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1158 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1159 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1160 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1161 | ASSERT_TRUE(response); |
| 1162 | ASSERT_TRUE(response->headers); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1163 | EXPECT_EQ("HTTP/1.1 302 Redirect", response->headers->GetStatusLine()); |
| 1164 | std::string url; |
| 1165 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 1166 | EXPECT_EQ("http://good.com/", url); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 1167 | EXPECT_TRUE(response->proxy_server.is_direct()); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1168 | } |
| 1169 | |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1170 | // Checks that two distinct Location headers result in an error. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1171 | TEST_F(HttpNetworkTransactionTest, TwoDistinctLocationHeaders) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1172 | MockRead data_reads[] = { |
| 1173 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 1174 | MockRead("Location: http://good.com/\r\n"), |
| 1175 | MockRead("Location: http://evil.com/\r\n"), |
| 1176 | MockRead("Content-Length: 0\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1177 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1178 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1179 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1180 | EXPECT_THAT(out.rv, IsError(ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION)); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1181 | } |
| 1182 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1183 | // Do a request using the HEAD method. Verify that we don't try to read the |
| 1184 | // message body (since HEAD has none). |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1185 | TEST_F(HttpNetworkTransactionTest, Head) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1186 | HttpRequestInfo request; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1187 | request.method = "HEAD"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 1188 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1189 | request.traffic_annotation = |
| 1190 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1191 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1192 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1193 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 1194 | BeforeHeadersSentHandler headers_handler; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1195 | trans.SetBeforeHeadersSentCallback( |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 1196 | base::Bind(&BeforeHeadersSentHandler::OnBeforeHeadersSent, |
| 1197 | base::Unretained(&headers_handler))); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1198 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1199 | MockWrite data_writes1[] = { |
csharrison | f473dd19 | 2015-08-18 13:54:13 | [diff] [blame] | 1200 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 1201 | "Host: www.example.org\r\n" |
| 1202 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1203 | }; |
| 1204 | MockRead data_reads1[] = { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 1205 | MockRead("HTTP/1.1 404 Not Found\r\n"), MockRead("Server: Blah\r\n"), |
| 1206 | MockRead("Content-Length: 1234\r\n\r\n"), |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1207 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 1208 | // No response body because the test stops reading here. |
| 1209 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1210 | }; |
| 1211 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1212 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1213 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1214 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1215 | TestCompletionCallback callback1; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1216 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1217 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1218 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1219 | |
| 1220 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1221 | EXPECT_THAT(rv, IsOk()); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1222 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1223 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1224 | ASSERT_TRUE(response); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1225 | |
| 1226 | // Check that the headers got parsed. |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1227 | EXPECT_TRUE(response->headers); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1228 | EXPECT_EQ(1234, response->headers->GetContentLength()); |
| 1229 | EXPECT_EQ("HTTP/1.1 404 Not Found", response->headers->GetStatusLine()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 1230 | EXPECT_TRUE(response->proxy_server.is_direct()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 1231 | EXPECT_TRUE(headers_handler.observed_before_headers_sent()); |
| 1232 | EXPECT_FALSE(headers_handler.observed_before_headers_sent_with_proxy()); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1233 | |
| 1234 | std::string server_header; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 1235 | size_t iter = 0; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1236 | bool has_server_header = response->headers->EnumerateHeader( |
| 1237 | &iter, "Server", &server_header); |
| 1238 | EXPECT_TRUE(has_server_header); |
| 1239 | EXPECT_EQ("Blah", server_header); |
| 1240 | |
| 1241 | // Reading should give EOF right away, since there is no message body |
| 1242 | // (despite non-zero content-length). |
| 1243 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1244 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1245 | EXPECT_THAT(rv, IsOk()); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1246 | EXPECT_EQ("", response_data); |
| 1247 | } |
| 1248 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1249 | TEST_F(HttpNetworkTransactionTest, ReuseConnection) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1250 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1251 | |
| 1252 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1253 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1254 | MockRead("hello"), |
| 1255 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1256 | MockRead("world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1257 | MockRead(SYNCHRONOUS, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1258 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1259 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1260 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1261 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1262 | const char* const kExpectedResponseData[] = { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1263 | "hello", "world" |
| 1264 | }; |
| 1265 | |
| 1266 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1267 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1268 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 1269 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1270 | request.traffic_annotation = |
| 1271 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1272 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1273 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1274 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1275 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1276 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1277 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1278 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1279 | |
| 1280 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1281 | EXPECT_THAT(rv, IsOk()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1282 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1283 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1284 | ASSERT_TRUE(response); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1285 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1286 | EXPECT_TRUE(response->headers); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1287 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 1288 | EXPECT_TRUE(response->proxy_server.is_direct()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1289 | |
| 1290 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1291 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1292 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1293 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1297 | TEST_F(HttpNetworkTransactionTest, Ignores100) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1298 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 1299 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 1300 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 1301 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 1302 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1303 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1304 | request.method = "POST"; |
| 1305 | request.url = GURL("http://www.foo.com/"); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 1306 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1307 | request.traffic_annotation = |
| 1308 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1309 | |
shivanisha | b9a14395 | 2016-09-19 17:23:41 | [diff] [blame] | 1310 | // Check the upload progress returned before initialization is correct. |
| 1311 | UploadProgress progress = request.upload_data_stream->GetUploadProgress(); |
| 1312 | EXPECT_EQ(0u, progress.size()); |
| 1313 | EXPECT_EQ(0u, progress.position()); |
| 1314 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1315 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1316 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1317 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1318 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1319 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 1320 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 1321 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1322 | MockRead(SYNCHRONOUS, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1323 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1324 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1325 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1326 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1327 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1328 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1329 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1330 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1331 | |
| 1332 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1333 | EXPECT_THAT(rv, IsOk()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1334 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1335 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1336 | ASSERT_TRUE(response); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1337 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1338 | EXPECT_TRUE(response->headers); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1339 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1340 | |
| 1341 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1342 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1343 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1344 | EXPECT_EQ("hello world", response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1345 | } |
| 1346 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1347 | // This test is almost the same as Ignores100 above, but the response contains |
| 1348 | // 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] | 1349 | // HTTP/1.1 and the two status headers are read in one read. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1350 | TEST_F(HttpNetworkTransactionTest, Ignores1xx) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1351 | HttpRequestInfo request; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1352 | request.method = "GET"; |
| 1353 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1354 | request.traffic_annotation = |
| 1355 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1356 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1357 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1358 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1359 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1360 | MockRead data_reads[] = { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1361 | MockRead("HTTP/1.1 102 Unspecified status code\r\n\r\n" |
| 1362 | "HTTP/1.1 200 OK\r\n\r\n"), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1363 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1364 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1365 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1366 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1367 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1368 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1369 | TestCompletionCallback callback; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1370 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1371 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1372 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1373 | |
| 1374 | rv = callback.WaitForResult(); |
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 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1377 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1378 | ASSERT_TRUE(response); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1379 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1380 | EXPECT_TRUE(response->headers); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1381 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1382 | |
| 1383 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1384 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1385 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1386 | EXPECT_EQ("hello world", response_data); |
| 1387 | } |
| 1388 | |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1389 | TEST_F(HttpNetworkTransactionTest, LoadTimingMeasuresTimeToFirstByteForHttp) { |
| 1390 | static const base::TimeDelta kDelayAfterFirstByte = |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1391 | base::TimeDelta::FromMilliseconds(10); |
| 1392 | |
| 1393 | HttpRequestInfo request; |
| 1394 | request.method = "GET"; |
| 1395 | request.url = GURL("http://www.foo.com/"); |
| 1396 | request.traffic_annotation = |
| 1397 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 1398 | |
| 1399 | std::vector<MockWrite> data_writes = { |
| 1400 | MockWrite(ASYNC, 0, |
| 1401 | "GET / HTTP/1.1\r\n" |
| 1402 | "Host: www.foo.com\r\n" |
| 1403 | "Connection: keep-alive\r\n\r\n"), |
| 1404 | }; |
| 1405 | |
| 1406 | std::vector<MockRead> data_reads = { |
| 1407 | // Write one byte of the status line, followed by a pause. |
| 1408 | MockRead(ASYNC, 1, "H"), |
| 1409 | MockRead(ASYNC, ERR_IO_PENDING, 2), |
| 1410 | MockRead(ASYNC, 3, "TTP/1.1 200 OK\r\n\r\n"), |
| 1411 | MockRead(ASYNC, 4, "hello world"), |
| 1412 | MockRead(SYNCHRONOUS, OK, 5), |
| 1413 | }; |
| 1414 | |
| 1415 | SequencedSocketData data(data_reads, data_writes); |
| 1416 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 1417 | |
| 1418 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1419 | |
| 1420 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 1421 | |
| 1422 | TestCompletionCallback callback; |
| 1423 | |
| 1424 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 1425 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1426 | |
| 1427 | data.RunUntilPaused(); |
| 1428 | ASSERT_TRUE(data.IsPaused()); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1429 | FastForwardBy(kDelayAfterFirstByte); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1430 | data.Resume(); |
| 1431 | |
| 1432 | rv = callback.WaitForResult(); |
| 1433 | EXPECT_THAT(rv, IsOk()); |
| 1434 | |
| 1435 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 1436 | ASSERT_TRUE(response); |
| 1437 | |
| 1438 | EXPECT_TRUE(response->headers); |
| 1439 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1440 | |
| 1441 | LoadTimingInfo load_timing_info; |
| 1442 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
| 1443 | EXPECT_FALSE(load_timing_info.receive_headers_start.is_null()); |
| 1444 | EXPECT_FALSE(load_timing_info.connect_timing.connect_end.is_null()); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1445 | // Ensure we didn't include the delay in the TTFB time. |
| 1446 | EXPECT_EQ(load_timing_info.receive_headers_start, |
| 1447 | load_timing_info.connect_timing.connect_end); |
| 1448 | // Ensure that the mock clock advanced at all. |
| 1449 | EXPECT_EQ(base::TimeTicks::Now() - load_timing_info.receive_headers_start, |
| 1450 | kDelayAfterFirstByte); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1451 | |
| 1452 | std::string response_data; |
| 1453 | rv = ReadTransaction(&trans, &response_data); |
| 1454 | EXPECT_THAT(rv, IsOk()); |
| 1455 | EXPECT_EQ("hello world", response_data); |
| 1456 | } |
| 1457 | |
| 1458 | // Tests that the time-to-first-byte reported in a transaction's load timing |
| 1459 | // info uses the first response, even if 1XX/informational. |
| 1460 | void HttpNetworkTransactionTest::Check100ResponseTiming(bool use_spdy) { |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1461 | static const base::TimeDelta kDelayAfter100Response = |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1462 | base::TimeDelta::FromMilliseconds(10); |
| 1463 | |
| 1464 | HttpRequestInfo request; |
| 1465 | request.method = "GET"; |
| 1466 | request.url = GURL("https://www.foo.com/"); |
| 1467 | request.traffic_annotation = |
| 1468 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 1469 | |
| 1470 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 1471 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 1472 | |
| 1473 | std::vector<MockWrite> data_writes; |
| 1474 | std::vector<MockRead> data_reads; |
| 1475 | |
| 1476 | spdy::SpdySerializedFrame spdy_req( |
| 1477 | spdy_util_.ConstructSpdyGet(request.url.spec().c_str(), 1, LOWEST)); |
| 1478 | |
| 1479 | spdy::SpdyHeaderBlock spdy_resp1_headers; |
| 1480 | spdy_resp1_headers[spdy::kHttp2StatusHeader] = "100"; |
| 1481 | spdy::SpdySerializedFrame spdy_resp1( |
| 1482 | spdy_util_.ConstructSpdyReply(1, spdy_resp1_headers.Clone())); |
| 1483 | spdy::SpdySerializedFrame spdy_resp2( |
| 1484 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 1485 | spdy::SpdySerializedFrame spdy_data( |
| 1486 | spdy_util_.ConstructSpdyDataFrame(1, "hello world", true)); |
| 1487 | |
| 1488 | if (use_spdy) { |
| 1489 | ssl.next_proto = kProtoHTTP2; |
| 1490 | |
| 1491 | data_writes = {CreateMockWrite(spdy_req, 0)}; |
| 1492 | |
| 1493 | data_reads = { |
| 1494 | CreateMockRead(spdy_resp1, 1), MockRead(ASYNC, ERR_IO_PENDING, 2), |
| 1495 | CreateMockRead(spdy_resp2, 3), CreateMockRead(spdy_data, 4), |
| 1496 | MockRead(SYNCHRONOUS, OK, 5), |
| 1497 | }; |
| 1498 | } else { |
| 1499 | data_writes = { |
| 1500 | MockWrite(ASYNC, 0, |
| 1501 | "GET / HTTP/1.1\r\n" |
| 1502 | "Host: www.foo.com\r\n" |
| 1503 | "Connection: keep-alive\r\n\r\n"), |
| 1504 | }; |
| 1505 | |
| 1506 | data_reads = { |
| 1507 | MockRead(ASYNC, 1, "HTTP/1.1 100 Continue\r\n\r\n"), |
| 1508 | MockRead(ASYNC, ERR_IO_PENDING, 2), |
| 1509 | |
| 1510 | MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n\r\n"), |
| 1511 | MockRead(ASYNC, 4, "hello world"), |
| 1512 | MockRead(SYNCHRONOUS, OK, 5), |
| 1513 | }; |
| 1514 | } |
| 1515 | |
| 1516 | SequencedSocketData data(data_reads, data_writes); |
| 1517 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 1518 | |
| 1519 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1520 | |
| 1521 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 1522 | |
| 1523 | TestCompletionCallback callback; |
| 1524 | |
| 1525 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 1526 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1527 | |
| 1528 | data.RunUntilPaused(); |
| 1529 | // We should now have parsed the 100 response and hit ERR_IO_PENDING. Insert |
| 1530 | // the delay before parsing the 200 response. |
| 1531 | ASSERT_TRUE(data.IsPaused()); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1532 | FastForwardBy(kDelayAfter100Response); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1533 | data.Resume(); |
| 1534 | |
| 1535 | rv = callback.WaitForResult(); |
| 1536 | EXPECT_THAT(rv, IsOk()); |
| 1537 | |
| 1538 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 1539 | ASSERT_TRUE(response); |
| 1540 | |
| 1541 | LoadTimingInfo load_timing_info; |
| 1542 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
| 1543 | EXPECT_FALSE(load_timing_info.receive_headers_start.is_null()); |
| 1544 | EXPECT_FALSE(load_timing_info.connect_timing.connect_end.is_null()); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1545 | // Ensure we didn't include the delay in the TTFB time. |
| 1546 | EXPECT_EQ(load_timing_info.receive_headers_start, |
| 1547 | load_timing_info.connect_timing.connect_end); |
| 1548 | // Ensure that the mock clock advanced at all. |
| 1549 | EXPECT_EQ(base::TimeTicks::Now() - load_timing_info.receive_headers_start, |
| 1550 | kDelayAfter100Response); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1551 | |
| 1552 | std::string response_data; |
| 1553 | rv = ReadTransaction(&trans, &response_data); |
| 1554 | EXPECT_THAT(rv, IsOk()); |
| 1555 | EXPECT_EQ("hello world", response_data); |
| 1556 | } |
| 1557 | |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1558 | TEST_F(HttpNetworkTransactionTest, MeasuresTimeToFirst100ResponseForHttp) { |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1559 | Check100ResponseTiming(false /* use_spdy */); |
| 1560 | } |
| 1561 | |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1562 | TEST_F(HttpNetworkTransactionTest, MeasuresTimeToFirst100ResponseForSpdy) { |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1563 | Check100ResponseTiming(true /* use_spdy */); |
| 1564 | } |
| 1565 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1566 | TEST_F(HttpNetworkTransactionTest, Incomplete100ThenEOF) { |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1567 | HttpRequestInfo request; |
| 1568 | request.method = "POST"; |
| 1569 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1570 | request.traffic_annotation = |
| 1571 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1572 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1573 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1574 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1575 | |
| 1576 | MockRead data_reads[] = { |
| 1577 | MockRead(SYNCHRONOUS, "HTTP/1.0 100 Continue\r\n"), |
| 1578 | MockRead(ASYNC, 0), |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1579 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1580 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1581 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1582 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1583 | TestCompletionCallback callback; |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1584 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1585 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1586 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1587 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1588 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1589 | EXPECT_THAT(rv, IsOk()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1590 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1591 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1592 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1593 | EXPECT_THAT(rv, IsOk()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1594 | EXPECT_EQ("", response_data); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1595 | } |
| 1596 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1597 | TEST_F(HttpNetworkTransactionTest, EmptyResponse) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1598 | HttpRequestInfo request; |
| 1599 | request.method = "POST"; |
| 1600 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1601 | request.traffic_annotation = |
| 1602 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1603 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1604 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1605 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1606 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1607 | MockRead data_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1608 | MockRead(ASYNC, 0), |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1609 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1610 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1611 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1612 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1613 | TestCompletionCallback callback; |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1614 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1615 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1616 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1617 | |
| 1618 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1619 | EXPECT_THAT(rv, IsError(ERR_EMPTY_RESPONSE)); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1620 | } |
| 1621 | |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 1622 | void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1623 | const MockWrite* write_failure, |
| 1624 | const MockRead* read_failure) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1625 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1626 | request.method = "GET"; |
| 1627 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1628 | request.traffic_annotation = |
| 1629 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1630 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1631 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1632 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1633 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1634 | |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1635 | // Written data for successfully sending both requests. |
| 1636 | MockWrite data1_writes[] = { |
| 1637 | MockWrite("GET / HTTP/1.1\r\n" |
| 1638 | "Host: www.foo.com\r\n" |
| 1639 | "Connection: keep-alive\r\n\r\n"), |
| 1640 | MockWrite("GET / HTTP/1.1\r\n" |
| 1641 | "Host: www.foo.com\r\n" |
| 1642 | "Connection: keep-alive\r\n\r\n") |
| 1643 | }; |
| 1644 | |
| 1645 | // Read results for the first request. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1646 | MockRead data1_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1647 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1648 | MockRead("hello"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1649 | MockRead(ASYNC, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1650 | }; |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1651 | |
| 1652 | if (write_failure) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1653 | ASSERT_FALSE(read_failure); |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1654 | data1_writes[1] = *write_failure; |
| 1655 | } else { |
| 1656 | ASSERT_TRUE(read_failure); |
| 1657 | data1_reads[2] = *read_failure; |
| 1658 | } |
| 1659 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1660 | StaticSocketDataProvider data1(data1_reads, data1_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1661 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1662 | |
| 1663 | MockRead data2_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1664 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1665 | MockRead("world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1666 | MockRead(ASYNC, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1667 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1668 | StaticSocketDataProvider data2(data2_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1669 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1670 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 1671 | const char* const kExpectedResponseData[] = { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1672 | "hello", "world" |
| 1673 | }; |
| 1674 | |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 1675 | uint32_t first_socket_log_id = NetLogSource::kInvalidId; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1676 | for (int i = 0; i < 2; ++i) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1677 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1678 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1679 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1680 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1681 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1682 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1683 | |
| 1684 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1685 | EXPECT_THAT(rv, IsOk()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1686 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1687 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1688 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1689 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1690 | if (i == 0) { |
| 1691 | first_socket_log_id = load_timing_info.socket_log_id; |
| 1692 | } else { |
| 1693 | // The second request should be using a new socket. |
| 1694 | EXPECT_NE(first_socket_log_id, load_timing_info.socket_log_id); |
| 1695 | } |
| 1696 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1697 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1698 | ASSERT_TRUE(response); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1699 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1700 | EXPECT_TRUE(response->headers); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 1701 | EXPECT_TRUE(response->proxy_server.is_direct()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1702 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1703 | |
| 1704 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1705 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1706 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1707 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1708 | } |
| 1709 | } |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1710 | |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1711 | void HttpNetworkTransactionTest::PreconnectErrorResendRequestTest( |
| 1712 | const MockWrite* write_failure, |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1713 | const MockRead* read_failure, |
| 1714 | bool use_spdy) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1715 | HttpRequestInfo request; |
| 1716 | request.method = "GET"; |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1717 | request.url = GURL("https://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1718 | request.traffic_annotation = |
| 1719 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1720 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1721 | TestNetLog net_log; |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1722 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1723 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1724 | |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1725 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 1726 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 1727 | if (use_spdy) { |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 1728 | ssl1.next_proto = kProtoHTTP2; |
| 1729 | ssl2.next_proto = kProtoHTTP2; |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1730 | } |
| 1731 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 1732 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1733 | |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1734 | // SPDY versions of the request and response. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1735 | spdy::SpdySerializedFrame spdy_request(spdy_util_.ConstructSpdyGet( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 1736 | request.url.spec().c_str(), 1, DEFAULT_PRIORITY)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1737 | spdy::SpdySerializedFrame spdy_response( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1738 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1739 | spdy::SpdySerializedFrame spdy_data( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 1740 | spdy_util_.ConstructSpdyDataFrame(1, "hello", true)); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1741 | |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1742 | // HTTP/1.1 versions of the request and response. |
| 1743 | const char kHttpRequest[] = "GET / HTTP/1.1\r\n" |
| 1744 | "Host: www.foo.com\r\n" |
| 1745 | "Connection: keep-alive\r\n\r\n"; |
| 1746 | const char kHttpResponse[] = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"; |
| 1747 | const char kHttpData[] = "hello"; |
| 1748 | |
| 1749 | std::vector<MockRead> data1_reads; |
| 1750 | std::vector<MockWrite> data1_writes; |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1751 | if (write_failure) { |
| 1752 | ASSERT_FALSE(read_failure); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1753 | data1_writes.push_back(*write_failure); |
| 1754 | data1_reads.push_back(MockRead(ASYNC, OK)); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1755 | } else { |
| 1756 | ASSERT_TRUE(read_failure); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1757 | if (use_spdy) { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 1758 | data1_writes.push_back(CreateMockWrite(spdy_request)); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1759 | } else { |
| 1760 | data1_writes.push_back(MockWrite(kHttpRequest)); |
| 1761 | } |
| 1762 | data1_reads.push_back(*read_failure); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1763 | } |
| 1764 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1765 | StaticSocketDataProvider data1(data1_reads, data1_writes); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1766 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 1767 | |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1768 | std::vector<MockRead> data2_reads; |
| 1769 | std::vector<MockWrite> data2_writes; |
| 1770 | |
| 1771 | if (use_spdy) { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 1772 | data2_writes.push_back(CreateMockWrite(spdy_request, 0, ASYNC)); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1773 | |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 1774 | data2_reads.push_back(CreateMockRead(spdy_response, 1, ASYNC)); |
| 1775 | data2_reads.push_back(CreateMockRead(spdy_data, 2, ASYNC)); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1776 | data2_reads.push_back(MockRead(ASYNC, OK, 3)); |
| 1777 | } else { |
| 1778 | data2_writes.push_back( |
| 1779 | MockWrite(ASYNC, kHttpRequest, strlen(kHttpRequest), 0)); |
| 1780 | |
| 1781 | data2_reads.push_back( |
| 1782 | MockRead(ASYNC, kHttpResponse, strlen(kHttpResponse), 1)); |
| 1783 | data2_reads.push_back(MockRead(ASYNC, kHttpData, strlen(kHttpData), 2)); |
| 1784 | data2_reads.push_back(MockRead(ASYNC, OK, 3)); |
| 1785 | } |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1786 | SequencedSocketData data2(data2_reads, data2_writes); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1787 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 1788 | |
| 1789 | // Preconnect a socket. |
nharper | 8cdb0fb | 2016-04-22 21:34:59 | [diff] [blame] | 1790 | session->http_stream_factory()->PreconnectStreams(1, request); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1791 | // Wait for the preconnect to complete. |
| 1792 | // TODO(davidben): Some way to wait for an idle socket count might be handy. |
| 1793 | base::RunLoop().RunUntilIdle(); |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 1794 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1795 | |
| 1796 | // Make the request. |
| 1797 | TestCompletionCallback callback; |
| 1798 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1799 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1800 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1801 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1802 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1803 | |
| 1804 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1805 | EXPECT_THAT(rv, IsOk()); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1806 | |
| 1807 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1808 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1809 | TestLoadTimingNotReused( |
| 1810 | load_timing_info, |
| 1811 | CONNECT_TIMING_HAS_DNS_TIMES|CONNECT_TIMING_HAS_SSL_TIMES); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1812 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1813 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1814 | ASSERT_TRUE(response); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1815 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1816 | EXPECT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 1817 | if (response->was_fetched_via_spdy) { |
| 1818 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 1819 | } else { |
| 1820 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1821 | } |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1822 | |
| 1823 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1824 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1825 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1826 | EXPECT_EQ(kHttpData, response_data); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1827 | } |
| 1828 | |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1829 | // 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] | 1830 | // ERR_HTTP2_PING_FAILED, ERR_HTTP2_SERVER_REFUSED_STREAM, |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1831 | // ERR_QUIC_HANDSHAKE_FAILED or ERR_QUIC_PROTOCOL_ERROR. |
| 1832 | TEST_F(HttpNetworkTransactionTest, FiniteRetriesOnIOError) { |
| 1833 | HttpRequestInfo request; |
| 1834 | request.method = "GET"; |
| 1835 | request.url = GURL("https://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1836 | request.traffic_annotation = |
| 1837 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1838 | |
| 1839 | // Check whether we give up after the third try. |
| 1840 | |
| 1841 | // Construct an HTTP2 request and a "Go away" response. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1842 | spdy::SpdySerializedFrame spdy_request(spdy_util_.ConstructSpdyGet( |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1843 | request.url.spec().c_str(), 1, DEFAULT_PRIORITY)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1844 | spdy::SpdySerializedFrame spdy_response_go_away( |
| 1845 | spdy_util_.ConstructSpdyGoAway(0)); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1846 | MockRead data_read1[] = {CreateMockRead(spdy_response_go_away)}; |
| 1847 | MockWrite data_write[] = {CreateMockWrite(spdy_request, 0)}; |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1848 | |
| 1849 | // Three go away responses. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1850 | StaticSocketDataProvider data1(data_read1, data_write); |
| 1851 | StaticSocketDataProvider data2(data_read1, data_write); |
| 1852 | StaticSocketDataProvider data3(data_read1, data_write); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1853 | |
| 1854 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 1855 | AddSSLSocketData(); |
| 1856 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 1857 | AddSSLSocketData(); |
| 1858 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 1859 | AddSSLSocketData(); |
| 1860 | |
| 1861 | TestCompletionCallback callback; |
| 1862 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1863 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 1864 | |
| 1865 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 1866 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1867 | |
| 1868 | rv = callback.WaitForResult(); |
Bence Béky | d0d6950 | 2019-06-25 19:47:18 | [diff] [blame] | 1869 | EXPECT_THAT(rv, IsError(ERR_HTTP2_SERVER_REFUSED_STREAM)); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | TEST_F(HttpNetworkTransactionTest, RetryTwiceOnIOError) { |
| 1873 | HttpRequestInfo request; |
| 1874 | request.method = "GET"; |
| 1875 | request.url = GURL("https://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 1876 | request.traffic_annotation = |
| 1877 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1878 | |
| 1879 | // Check whether we try atleast thrice before giving up. |
| 1880 | |
| 1881 | // Construct an HTTP2 request and a "Go away" response. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1882 | spdy::SpdySerializedFrame spdy_request(spdy_util_.ConstructSpdyGet( |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1883 | request.url.spec().c_str(), 1, DEFAULT_PRIORITY)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1884 | spdy::SpdySerializedFrame spdy_response_go_away( |
| 1885 | spdy_util_.ConstructSpdyGoAway(0)); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1886 | MockRead data_read1[] = {CreateMockRead(spdy_response_go_away)}; |
| 1887 | MockWrite data_write[] = {CreateMockWrite(spdy_request, 0)}; |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1888 | |
| 1889 | // Construct a non error HTTP2 response. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1890 | spdy::SpdySerializedFrame spdy_response_no_error( |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1891 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1892 | spdy::SpdySerializedFrame spdy_data( |
| 1893 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1894 | MockRead data_read2[] = {CreateMockRead(spdy_response_no_error, 1), |
| 1895 | CreateMockRead(spdy_data, 2)}; |
| 1896 | |
| 1897 | // Two error responses. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1898 | StaticSocketDataProvider data1(data_read1, data_write); |
| 1899 | StaticSocketDataProvider data2(data_read1, data_write); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1900 | // Followed by a success response. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1901 | SequencedSocketData data3(data_read2, data_write); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1902 | |
| 1903 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 1904 | AddSSLSocketData(); |
| 1905 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 1906 | AddSSLSocketData(); |
| 1907 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 1908 | AddSSLSocketData(); |
| 1909 | |
| 1910 | TestCompletionCallback callback; |
| 1911 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1912 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 1913 | |
| 1914 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 1915 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1916 | |
| 1917 | rv = callback.WaitForResult(); |
| 1918 | EXPECT_THAT(rv, IsOk()); |
| 1919 | } |
| 1920 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1921 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionNotConnectedOnWrite) { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1922 | MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1923 | KeepAliveConnectionResendRequestTest(&write_failure, nullptr); |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1924 | } |
| 1925 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1926 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1927 | MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1928 | KeepAliveConnectionResendRequestTest(nullptr, &read_failure); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1929 | } |
| 1930 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1931 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1932 | MockRead read_failure(SYNCHRONOUS, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1933 | KeepAliveConnectionResendRequestTest(nullptr, &read_failure); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1934 | } |
| 1935 | |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1936 | // Make sure that on a 408 response (Request Timeout), the request is retried, |
| 1937 | // if the socket was a reused keep alive socket. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1938 | TEST_F(HttpNetworkTransactionTest, KeepAlive408) { |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1939 | MockRead read_failure(SYNCHRONOUS, |
| 1940 | "HTTP/1.1 408 Request Timeout\r\n" |
| 1941 | "Connection: Keep-Alive\r\n" |
| 1942 | "Content-Length: 6\r\n\r\n" |
| 1943 | "Pickle"); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1944 | KeepAliveConnectionResendRequestTest(nullptr, &read_failure); |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1945 | } |
| 1946 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1947 | TEST_F(HttpNetworkTransactionTest, PreconnectErrorNotConnectedOnWrite) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1948 | MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1949 | PreconnectErrorResendRequestTest(&write_failure, nullptr, false); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1950 | } |
| 1951 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1952 | TEST_F(HttpNetworkTransactionTest, PreconnectErrorReset) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1953 | MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1954 | PreconnectErrorResendRequestTest(nullptr, &read_failure, false); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1955 | } |
| 1956 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1957 | TEST_F(HttpNetworkTransactionTest, PreconnectErrorEOF) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1958 | MockRead read_failure(SYNCHRONOUS, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1959 | PreconnectErrorResendRequestTest(nullptr, &read_failure, false); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1960 | } |
| 1961 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1962 | TEST_F(HttpNetworkTransactionTest, PreconnectErrorAsyncEOF) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1963 | MockRead read_failure(ASYNC, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1964 | PreconnectErrorResendRequestTest(nullptr, &read_failure, false); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1965 | } |
| 1966 | |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1967 | // Make sure that on a 408 response (Request Timeout), the request is retried, |
| 1968 | // if the socket was a preconnected (UNUSED_IDLE) socket. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1969 | TEST_F(HttpNetworkTransactionTest, RetryOnIdle408) { |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1970 | MockRead read_failure(SYNCHRONOUS, |
| 1971 | "HTTP/1.1 408 Request Timeout\r\n" |
| 1972 | "Connection: Keep-Alive\r\n" |
| 1973 | "Content-Length: 6\r\n\r\n" |
| 1974 | "Pickle"); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1975 | KeepAliveConnectionResendRequestTest(nullptr, &read_failure); |
| 1976 | PreconnectErrorResendRequestTest(nullptr, &read_failure, false); |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1977 | } |
| 1978 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1979 | TEST_F(HttpNetworkTransactionTest, SpdyPreconnectErrorNotConnectedOnWrite) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1980 | MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1981 | PreconnectErrorResendRequestTest(&write_failure, nullptr, 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, SpdyPreconnectErrorReset) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1985 | MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1986 | PreconnectErrorResendRequestTest(nullptr, &read_failure, true); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1987 | } |
| 1988 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1989 | TEST_F(HttpNetworkTransactionTest, SpdyPreconnectErrorEOF) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1990 | MockRead read_failure(SYNCHRONOUS, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1991 | PreconnectErrorResendRequestTest(nullptr, &read_failure, true); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1992 | } |
| 1993 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1994 | TEST_F(HttpNetworkTransactionTest, SpdyPreconnectErrorAsyncEOF) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1995 | MockRead read_failure(ASYNC, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1996 | PreconnectErrorResendRequestTest(nullptr, &read_failure, true); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1997 | } |
| 1998 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1999 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2000 | HttpRequestInfo request; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2001 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2002 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2003 | request.traffic_annotation = |
| 2004 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2005 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2006 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2007 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2008 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2009 | MockRead data_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2010 | MockRead(ASYNC, ERR_CONNECTION_RESET), |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 2011 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 2012 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2013 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2014 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2015 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2016 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2017 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2018 | TestCompletionCallback callback; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2019 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2020 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2021 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2022 | |
| 2023 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2024 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2025 | |
| 2026 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2027 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2028 | EXPECT_LT(0u, endpoint.address().size()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | // What do various browsers do when the server closes a non-keepalive |
| 2032 | // connection without sending any response header or body? |
| 2033 | // |
| 2034 | // IE7: error page |
| 2035 | // Safari 3.1.2 (Windows): error page |
| 2036 | // Firefox 3.0.1: blank page |
| 2037 | // Opera 9.52: after five attempts, blank page |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2038 | // Us with WinHTTP: error page (ERR_INVALID_RESPONSE) |
| 2039 | // Us: error page (EMPTY_RESPONSE) |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2040 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) { |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2041 | MockRead data_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2042 | MockRead(SYNCHRONOUS, OK), // EOF |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 2043 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 2044 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2045 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2046 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2047 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2048 | EXPECT_THAT(out.rv, IsError(ERR_EMPTY_RESPONSE)); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2049 | } |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 2050 | |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2051 | // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression |
| 2052 | // tests. There was a bug causing HttpNetworkTransaction to hang in the |
| 2053 | // destructor in such situations. |
| 2054 | // See http://crbug.com/154712 and http://crbug.com/156609. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2055 | TEST_F(HttpNetworkTransactionTest, KeepAliveEarlyClose) { |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2056 | HttpRequestInfo request; |
| 2057 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2058 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2059 | request.traffic_annotation = |
| 2060 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2061 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2062 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2063 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2064 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2065 | |
| 2066 | MockRead data_reads[] = { |
| 2067 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2068 | MockRead("Connection: keep-alive\r\n"), |
| 2069 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2070 | MockRead("hello"), |
| 2071 | MockRead(SYNCHRONOUS, 0), |
| 2072 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2073 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2074 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2075 | |
| 2076 | TestCompletionCallback callback; |
| 2077 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2078 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2079 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2080 | |
| 2081 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2082 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2083 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 2084 | scoped_refptr<IOBufferWithSize> io_buf = |
| 2085 | base::MakeRefCounted<IOBufferWithSize>(100); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2086 | rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2087 | if (rv == ERR_IO_PENDING) |
| 2088 | rv = callback.WaitForResult(); |
| 2089 | EXPECT_EQ(5, rv); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2090 | rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2091 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2092 | |
| 2093 | trans.reset(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 2094 | base::RunLoop().RunUntilIdle(); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2095 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 2096 | } |
| 2097 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2098 | TEST_F(HttpNetworkTransactionTest, KeepAliveEarlyClose2) { |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2099 | HttpRequestInfo request; |
| 2100 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2101 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2102 | request.traffic_annotation = |
| 2103 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2104 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2105 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2106 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2107 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2108 | |
| 2109 | MockRead data_reads[] = { |
| 2110 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2111 | MockRead("Connection: keep-alive\r\n"), |
| 2112 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2113 | MockRead(SYNCHRONOUS, 0), |
| 2114 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2115 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2116 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2117 | |
| 2118 | TestCompletionCallback callback; |
| 2119 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2120 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2121 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2122 | |
| 2123 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2124 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2125 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 2126 | scoped_refptr<IOBufferWithSize> io_buf( |
| 2127 | base::MakeRefCounted<IOBufferWithSize>(100)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2128 | rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2129 | if (rv == ERR_IO_PENDING) |
| 2130 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2131 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2132 | |
| 2133 | trans.reset(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 2134 | base::RunLoop().RunUntilIdle(); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2135 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 2136 | } |
| 2137 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2138 | // Test that we correctly reuse a keep-alive connection after not explicitly |
| 2139 | // reading the body. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2140 | TEST_F(HttpNetworkTransactionTest, KeepAliveAfterUnreadBody) { |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2141 | HttpRequestInfo request; |
| 2142 | request.method = "GET"; |
| 2143 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2144 | request.traffic_annotation = |
| 2145 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2146 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2147 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2148 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2149 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2150 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2151 | const char* request_data = |
| 2152 | "GET / HTTP/1.1\r\n" |
| 2153 | "Host: www.foo.com\r\n" |
| 2154 | "Connection: keep-alive\r\n\r\n"; |
| 2155 | MockWrite data_writes[] = { |
| 2156 | MockWrite(ASYNC, 0, request_data), MockWrite(ASYNC, 2, request_data), |
| 2157 | MockWrite(ASYNC, 4, request_data), MockWrite(ASYNC, 6, request_data), |
| 2158 | MockWrite(ASYNC, 8, request_data), MockWrite(ASYNC, 10, request_data), |
| 2159 | MockWrite(ASYNC, 12, request_data), MockWrite(ASYNC, 14, request_data), |
| 2160 | MockWrite(ASYNC, 17, request_data), MockWrite(ASYNC, 20, request_data), |
| 2161 | }; |
| 2162 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2163 | // Note that because all these reads happen in the same |
| 2164 | // StaticSocketDataProvider, it shows that the same socket is being reused for |
| 2165 | // all transactions. |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2166 | MockRead data_reads[] = { |
| 2167 | MockRead(ASYNC, 1, "HTTP/1.1 204 No Content\r\n\r\n"), |
| 2168 | MockRead(ASYNC, 3, "HTTP/1.1 205 Reset Content\r\n\r\n"), |
| 2169 | MockRead(ASYNC, 5, "HTTP/1.1 304 Not Modified\r\n\r\n"), |
| 2170 | MockRead(ASYNC, 7, |
| 2171 | "HTTP/1.1 302 Found\r\n" |
| 2172 | "Content-Length: 0\r\n\r\n"), |
| 2173 | MockRead(ASYNC, 9, |
| 2174 | "HTTP/1.1 302 Found\r\n" |
| 2175 | "Content-Length: 5\r\n\r\n" |
| 2176 | "hello"), |
| 2177 | MockRead(ASYNC, 11, |
| 2178 | "HTTP/1.1 301 Moved Permanently\r\n" |
| 2179 | "Content-Length: 0\r\n\r\n"), |
| 2180 | MockRead(ASYNC, 13, |
| 2181 | "HTTP/1.1 301 Moved Permanently\r\n" |
| 2182 | "Content-Length: 5\r\n\r\n" |
| 2183 | "hello"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2184 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2185 | // In the next two rounds, IsConnectedAndIdle returns false, due to |
| 2186 | // the set_busy_before_sync_reads(true) call, while the |
| 2187 | // HttpNetworkTransaction is being shut down, but the socket is still |
| 2188 | // reuseable. See http://crbug.com/544255. |
| 2189 | MockRead(ASYNC, 15, |
| 2190 | "HTTP/1.1 200 Hunky-Dory\r\n" |
| 2191 | "Content-Length: 5\r\n\r\n"), |
| 2192 | MockRead(SYNCHRONOUS, 16, "hello"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2193 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2194 | MockRead(ASYNC, 18, |
| 2195 | "HTTP/1.1 200 Hunky-Dory\r\n" |
| 2196 | "Content-Length: 5\r\n\r\n" |
| 2197 | "he"), |
| 2198 | MockRead(SYNCHRONOUS, 19, "llo"), |
| 2199 | |
| 2200 | // The body of the final request is actually read. |
| 2201 | MockRead(ASYNC, 21, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 2202 | MockRead(ASYNC, 22, "hello"), |
| 2203 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2204 | SequencedSocketData data(data_reads, data_writes); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2205 | data.set_busy_before_sync_reads(true); |
| 2206 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 2207 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 2208 | const int kNumUnreadBodies = base::size(data_writes) - 1; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2209 | std::string response_lines[kNumUnreadBodies]; |
| 2210 | |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 2211 | uint32_t first_socket_log_id = NetLogSource::kInvalidId; |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2212 | for (size_t i = 0; i < kNumUnreadBodies; ++i) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2213 | TestCompletionCallback callback; |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2214 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2215 | auto trans = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2216 | session.get()); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2217 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2218 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2219 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2220 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2221 | LoadTimingInfo load_timing_info; |
| 2222 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 2223 | if (i == 0) { |
| 2224 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2225 | first_socket_log_id = load_timing_info.socket_log_id; |
| 2226 | } else { |
| 2227 | TestLoadTimingReused(load_timing_info); |
| 2228 | EXPECT_EQ(first_socket_log_id, load_timing_info.socket_log_id); |
| 2229 | } |
| 2230 | |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2231 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2232 | ASSERT_TRUE(response); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2233 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2234 | ASSERT_TRUE(response->headers); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2235 | response_lines[i] = response->headers->GetStatusLine(); |
| 2236 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2237 | // Delete the transaction without reading the response bodies. Then spin |
| 2238 | // the message loop, so the response bodies are drained. |
| 2239 | trans.reset(); |
| 2240 | base::RunLoop().RunUntilIdle(); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2241 | } |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2242 | |
| 2243 | const char* const kStatusLines[] = { |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2244 | "HTTP/1.1 204 No Content", |
| 2245 | "HTTP/1.1 205 Reset Content", |
| 2246 | "HTTP/1.1 304 Not Modified", |
| 2247 | "HTTP/1.1 302 Found", |
| 2248 | "HTTP/1.1 302 Found", |
| 2249 | "HTTP/1.1 301 Moved Permanently", |
| 2250 | "HTTP/1.1 301 Moved Permanently", |
| 2251 | "HTTP/1.1 200 Hunky-Dory", |
| 2252 | "HTTP/1.1 200 Hunky-Dory", |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2253 | }; |
| 2254 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 2255 | static_assert(kNumUnreadBodies == base::size(kStatusLines), |
mostynb | 91e0da98 | 2015-01-20 19:17:27 | [diff] [blame] | 2256 | "forgot to update kStatusLines"); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2257 | |
| 2258 | for (int i = 0; i < kNumUnreadBodies; ++i) |
| 2259 | EXPECT_EQ(kStatusLines[i], response_lines[i]); |
| 2260 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2261 | TestCompletionCallback callback; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2262 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2263 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2264 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2265 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2266 | ASSERT_TRUE(response); |
| 2267 | ASSERT_TRUE(response->headers); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2268 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2269 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2270 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2271 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2272 | EXPECT_EQ("hello", response_data); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2273 | } |
| 2274 | |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2275 | // Sockets that receive extra data after a response is complete should not be |
| 2276 | // reused. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2277 | TEST_F(HttpNetworkTransactionTest, KeepAliveWithUnusedData1) { |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2278 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2279 | MockWrite data_writes1[] = { |
| 2280 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 2281 | "Host: www.borked.com\r\n" |
| 2282 | "Connection: keep-alive\r\n\r\n"), |
| 2283 | }; |
| 2284 | |
| 2285 | MockRead data_reads1[] = { |
| 2286 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2287 | "Connection: keep-alive\r\n" |
| 2288 | "Content-Length: 22\r\n\r\n" |
| 2289 | "This server is borked."), |
| 2290 | }; |
| 2291 | |
| 2292 | MockWrite data_writes2[] = { |
| 2293 | MockWrite("GET /foo HTTP/1.1\r\n" |
| 2294 | "Host: www.borked.com\r\n" |
| 2295 | "Connection: keep-alive\r\n\r\n"), |
| 2296 | }; |
| 2297 | |
| 2298 | MockRead data_reads2[] = { |
| 2299 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2300 | "Content-Length: 3\r\n\r\n" |
| 2301 | "foo"), |
| 2302 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2303 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2304 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2305 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2306 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 2307 | |
| 2308 | TestCompletionCallback callback; |
| 2309 | HttpRequestInfo request1; |
| 2310 | request1.method = "HEAD"; |
| 2311 | request1.url = GURL("http://www.borked.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2312 | request1.traffic_annotation = |
| 2313 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2314 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2315 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2316 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2317 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2318 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2319 | |
| 2320 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
| 2321 | ASSERT_TRUE(response1); |
| 2322 | ASSERT_TRUE(response1->headers); |
| 2323 | EXPECT_EQ(200, response1->headers->response_code()); |
| 2324 | EXPECT_TRUE(response1->headers->IsKeepAlive()); |
| 2325 | |
| 2326 | std::string response_data1; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2327 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2328 | EXPECT_EQ("", response_data1); |
| 2329 | // Deleting the transaction attempts to release the socket back into the |
| 2330 | // socket pool. |
| 2331 | trans1.reset(); |
| 2332 | |
| 2333 | HttpRequestInfo request2; |
| 2334 | request2.method = "GET"; |
| 2335 | request2.url = GURL("http://www.borked.com/foo"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2336 | request2.traffic_annotation = |
| 2337 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2338 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2339 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2340 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2341 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2342 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2343 | |
| 2344 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
| 2345 | ASSERT_TRUE(response2); |
| 2346 | ASSERT_TRUE(response2->headers); |
| 2347 | EXPECT_EQ(200, response2->headers->response_code()); |
| 2348 | |
| 2349 | std::string response_data2; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2350 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2351 | EXPECT_EQ("foo", response_data2); |
| 2352 | } |
| 2353 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2354 | TEST_F(HttpNetworkTransactionTest, KeepAliveWithUnusedData2) { |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2355 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2356 | MockWrite data_writes1[] = { |
| 2357 | MockWrite("GET / HTTP/1.1\r\n" |
| 2358 | "Host: www.borked.com\r\n" |
| 2359 | "Connection: keep-alive\r\n\r\n"), |
| 2360 | }; |
| 2361 | |
| 2362 | MockRead data_reads1[] = { |
| 2363 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2364 | "Connection: keep-alive\r\n" |
| 2365 | "Content-Length: 22\r\n\r\n" |
| 2366 | "This server is borked." |
| 2367 | "Bonus data!"), |
| 2368 | }; |
| 2369 | |
| 2370 | MockWrite data_writes2[] = { |
| 2371 | MockWrite("GET /foo HTTP/1.1\r\n" |
| 2372 | "Host: www.borked.com\r\n" |
| 2373 | "Connection: keep-alive\r\n\r\n"), |
| 2374 | }; |
| 2375 | |
| 2376 | MockRead data_reads2[] = { |
| 2377 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2378 | "Content-Length: 3\r\n\r\n" |
| 2379 | "foo"), |
| 2380 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2381 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2382 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2383 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2384 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 2385 | |
| 2386 | TestCompletionCallback callback; |
| 2387 | HttpRequestInfo request1; |
| 2388 | request1.method = "GET"; |
| 2389 | request1.url = GURL("http://www.borked.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2390 | request1.traffic_annotation = |
| 2391 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2392 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2393 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2394 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2395 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2396 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2397 | |
| 2398 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
| 2399 | ASSERT_TRUE(response1); |
| 2400 | ASSERT_TRUE(response1->headers); |
| 2401 | EXPECT_EQ(200, response1->headers->response_code()); |
| 2402 | EXPECT_TRUE(response1->headers->IsKeepAlive()); |
| 2403 | |
| 2404 | std::string response_data1; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2405 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2406 | EXPECT_EQ("This server is borked.", response_data1); |
| 2407 | // Deleting the transaction attempts to release the socket back into the |
| 2408 | // socket pool. |
| 2409 | trans1.reset(); |
| 2410 | |
| 2411 | HttpRequestInfo request2; |
| 2412 | request2.method = "GET"; |
| 2413 | request2.url = GURL("http://www.borked.com/foo"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2414 | request2.traffic_annotation = |
| 2415 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2416 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2417 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2418 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2419 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2420 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2421 | |
| 2422 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
| 2423 | ASSERT_TRUE(response2); |
| 2424 | ASSERT_TRUE(response2->headers); |
| 2425 | EXPECT_EQ(200, response2->headers->response_code()); |
| 2426 | |
| 2427 | std::string response_data2; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2428 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2429 | EXPECT_EQ("foo", response_data2); |
| 2430 | } |
| 2431 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2432 | TEST_F(HttpNetworkTransactionTest, KeepAliveWithUnusedData3) { |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2433 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2434 | MockWrite data_writes1[] = { |
| 2435 | MockWrite("GET / HTTP/1.1\r\n" |
| 2436 | "Host: www.borked.com\r\n" |
| 2437 | "Connection: keep-alive\r\n\r\n"), |
| 2438 | }; |
| 2439 | |
| 2440 | MockRead data_reads1[] = { |
| 2441 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2442 | "Connection: keep-alive\r\n" |
| 2443 | "Transfer-Encoding: chunked\r\n\r\n"), |
| 2444 | MockRead("16\r\nThis server is borked.\r\n"), |
| 2445 | MockRead("0\r\n\r\nBonus data!"), |
| 2446 | }; |
| 2447 | |
| 2448 | MockWrite data_writes2[] = { |
| 2449 | MockWrite("GET /foo HTTP/1.1\r\n" |
| 2450 | "Host: www.borked.com\r\n" |
| 2451 | "Connection: keep-alive\r\n\r\n"), |
| 2452 | }; |
| 2453 | |
| 2454 | MockRead data_reads2[] = { |
| 2455 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2456 | "Content-Length: 3\r\n\r\n" |
| 2457 | "foo"), |
| 2458 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2459 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2460 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2461 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2462 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 2463 | |
| 2464 | TestCompletionCallback callback; |
| 2465 | HttpRequestInfo request1; |
| 2466 | request1.method = "GET"; |
| 2467 | request1.url = GURL("http://www.borked.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2468 | request1.traffic_annotation = |
| 2469 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2470 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2471 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2472 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2473 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2474 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2475 | |
| 2476 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
| 2477 | ASSERT_TRUE(response1); |
| 2478 | ASSERT_TRUE(response1->headers); |
| 2479 | EXPECT_EQ(200, response1->headers->response_code()); |
| 2480 | EXPECT_TRUE(response1->headers->IsKeepAlive()); |
| 2481 | |
| 2482 | std::string response_data1; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2483 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2484 | EXPECT_EQ("This server is borked.", response_data1); |
| 2485 | // Deleting the transaction attempts to release the socket back into the |
| 2486 | // socket pool. |
| 2487 | trans1.reset(); |
| 2488 | |
| 2489 | HttpRequestInfo request2; |
| 2490 | request2.method = "GET"; |
| 2491 | request2.url = GURL("http://www.borked.com/foo"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2492 | request2.traffic_annotation = |
| 2493 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2494 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2495 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2496 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2497 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2498 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2499 | |
| 2500 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
| 2501 | ASSERT_TRUE(response2); |
| 2502 | ASSERT_TRUE(response2->headers); |
| 2503 | EXPECT_EQ(200, response2->headers->response_code()); |
| 2504 | |
| 2505 | std::string response_data2; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2506 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2507 | EXPECT_EQ("foo", response_data2); |
| 2508 | } |
| 2509 | |
| 2510 | // This is a little different from the others - it tests the case that the |
| 2511 | // HttpStreamParser doesn't know if there's extra data on a socket or not when |
| 2512 | // the HttpNetworkTransaction is torn down, because the response body hasn't |
| 2513 | // been read from yet, but the request goes through the HttpResponseBodyDrainer. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2514 | TEST_F(HttpNetworkTransactionTest, KeepAliveWithUnusedData4) { |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2515 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2516 | MockWrite data_writes1[] = { |
| 2517 | MockWrite("GET / HTTP/1.1\r\n" |
| 2518 | "Host: www.borked.com\r\n" |
| 2519 | "Connection: keep-alive\r\n\r\n"), |
| 2520 | }; |
| 2521 | |
| 2522 | MockRead data_reads1[] = { |
| 2523 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2524 | "Connection: keep-alive\r\n" |
| 2525 | "Transfer-Encoding: chunked\r\n\r\n"), |
| 2526 | MockRead("16\r\nThis server is borked.\r\n"), |
| 2527 | MockRead("0\r\n\r\nBonus data!"), |
| 2528 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2529 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2530 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 2531 | |
| 2532 | TestCompletionCallback callback; |
| 2533 | HttpRequestInfo request1; |
| 2534 | request1.method = "GET"; |
| 2535 | request1.url = GURL("http://www.borked.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2536 | request1.traffic_annotation = |
| 2537 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2538 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2539 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2540 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2541 | int rv = trans->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2542 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2543 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2544 | const HttpResponseInfo* response1 = trans->GetResponseInfo(); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2545 | ASSERT_TRUE(response1); |
| 2546 | ASSERT_TRUE(response1->headers); |
| 2547 | EXPECT_EQ(200, response1->headers->response_code()); |
| 2548 | EXPECT_TRUE(response1->headers->IsKeepAlive()); |
| 2549 | |
| 2550 | // Deleting the transaction creates an HttpResponseBodyDrainer to read the |
| 2551 | // response body. |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2552 | trans.reset(); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2553 | |
| 2554 | // Let the HttpResponseBodyDrainer drain the socket. It should determine the |
| 2555 | // socket can't be reused, rather than returning it to the socket pool. |
| 2556 | base::RunLoop().RunUntilIdle(); |
| 2557 | |
| 2558 | // There should be no idle sockets in the pool. |
| 2559 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 2560 | } |
| 2561 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2562 | // Test the request-challenge-retry sequence for basic auth. |
| 2563 | // (basic auth is the easiest to mock, because it has no randomness). |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2564 | TEST_F(HttpNetworkTransactionTest, BasicAuth) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2565 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2566 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2567 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2568 | request.traffic_annotation = |
| 2569 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2570 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2571 | TestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2572 | session_deps_.net_log = &log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2573 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2574 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2575 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2576 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2577 | MockWrite( |
| 2578 | "GET / HTTP/1.1\r\n" |
| 2579 | "Host: www.example.org\r\n" |
| 2580 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2581 | }; |
| 2582 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2583 | MockRead data_reads1[] = { |
| 2584 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2585 | // Give a couple authenticate options (only the middle one is actually |
| 2586 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 2587 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2588 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2589 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2590 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2591 | // Large content-length -- won't matter, as connection will be reset. |
| 2592 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2593 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2594 | }; |
| 2595 | |
| 2596 | // After calling trans->RestartWithAuth(), this is the request we should |
| 2597 | // be issuing -- the final header line contains the credentials. |
| 2598 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2599 | MockWrite( |
| 2600 | "GET / HTTP/1.1\r\n" |
| 2601 | "Host: www.example.org\r\n" |
| 2602 | "Connection: keep-alive\r\n" |
| 2603 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2604 | }; |
| 2605 | |
| 2606 | // Lastly, the server responds with the actual content. |
| 2607 | MockRead data_reads2[] = { |
| 2608 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2609 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2610 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2611 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2612 | }; |
| 2613 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2614 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 2615 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2616 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 2617 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2618 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2619 | TestCompletionCallback callback1; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2620 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2621 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2622 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2623 | |
| 2624 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2625 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2626 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2627 | LoadTimingInfo load_timing_info1; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2628 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info1)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2629 | TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2630 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2631 | int64_t writes_size1 = CountWriteBytes(data_writes1); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2632 | EXPECT_EQ(writes_size1, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2633 | int64_t reads_size1 = CountReadBytes(data_reads1); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2634 | EXPECT_EQ(reads_size1, trans.GetTotalReceivedBytes()); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 2635 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2636 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 2637 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2638 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2639 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2640 | TestCompletionCallback callback2; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2641 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2642 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2643 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2644 | |
| 2645 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2646 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2647 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2648 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2649 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info2)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2650 | TestLoadTimingNotReused(load_timing_info2, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2651 | // The load timing after restart should have a new socket ID, and times after |
| 2652 | // those of the first load timing. |
| 2653 | EXPECT_LE(load_timing_info1.receive_headers_end, |
| 2654 | load_timing_info2.connect_timing.connect_start); |
| 2655 | EXPECT_NE(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
| 2656 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2657 | int64_t writes_size2 = CountWriteBytes(data_writes2); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2658 | EXPECT_EQ(writes_size1 + writes_size2, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2659 | int64_t reads_size2 = CountReadBytes(data_reads2); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2660 | EXPECT_EQ(reads_size1 + reads_size2, trans.GetTotalReceivedBytes()); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 2661 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2662 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 2663 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2664 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2665 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2666 | } |
| 2667 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2668 | // Test the request-challenge-retry sequence for basic auth. |
| 2669 | // (basic auth is the easiest to mock, because it has no randomness). |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2670 | TEST_F(HttpNetworkTransactionTest, BasicAuthWithAddressChange) { |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2671 | HttpRequestInfo request; |
| 2672 | request.method = "GET"; |
| 2673 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2674 | request.traffic_annotation = |
| 2675 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2676 | |
| 2677 | TestNetLog log; |
| 2678 | MockHostResolver* resolver = new MockHostResolver(); |
| 2679 | session_deps_.net_log = &log; |
| 2680 | session_deps_.host_resolver.reset(resolver); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2681 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2682 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2683 | |
| 2684 | resolver->rules()->ClearRules(); |
| 2685 | resolver->rules()->AddRule("www.example.org", "127.0.0.1"); |
| 2686 | |
| 2687 | MockWrite data_writes1[] = { |
| 2688 | MockWrite("GET / HTTP/1.1\r\n" |
| 2689 | "Host: www.example.org\r\n" |
| 2690 | "Connection: keep-alive\r\n\r\n"), |
| 2691 | }; |
| 2692 | |
| 2693 | MockRead data_reads1[] = { |
| 2694 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2695 | // Give a couple authenticate options (only the middle one is actually |
| 2696 | // supported). |
| 2697 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
| 2698 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2699 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2700 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2701 | // Large content-length -- won't matter, as connection will be reset. |
| 2702 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 2703 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 2704 | }; |
| 2705 | |
| 2706 | // After calling trans->RestartWithAuth(), this is the request we should |
| 2707 | // be issuing -- the final header line contains the credentials. |
| 2708 | MockWrite data_writes2[] = { |
| 2709 | MockWrite("GET / HTTP/1.1\r\n" |
| 2710 | "Host: www.example.org\r\n" |
| 2711 | "Connection: keep-alive\r\n" |
| 2712 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2713 | }; |
| 2714 | |
| 2715 | // Lastly, the server responds with the actual content. |
| 2716 | MockRead data_reads2[] = { |
| 2717 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2718 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2719 | MockRead("Content-Length: 100\r\n\r\n"), MockRead(SYNCHRONOUS, OK), |
| 2720 | }; |
| 2721 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2722 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 2723 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2724 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 2725 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 2726 | |
| 2727 | TestCompletionCallback callback1; |
| 2728 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2729 | EXPECT_EQ(OK, callback1.GetResult(trans.Start(&request, callback1.callback(), |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2730 | NetLogWithSource()))); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2731 | |
| 2732 | LoadTimingInfo load_timing_info1; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2733 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info1)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2734 | TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2735 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2736 | int64_t writes_size1 = CountWriteBytes(data_writes1); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2737 | EXPECT_EQ(writes_size1, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2738 | int64_t reads_size1 = CountReadBytes(data_reads1); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2739 | EXPECT_EQ(reads_size1, trans.GetTotalReceivedBytes()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2740 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2741 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2742 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2743 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2744 | |
| 2745 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2746 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2747 | ASSERT_FALSE(endpoint.address().empty()); |
| 2748 | EXPECT_EQ("127.0.0.1:80", endpoint.ToString()); |
| 2749 | |
| 2750 | resolver->rules()->ClearRules(); |
| 2751 | resolver->rules()->AddRule("www.example.org", "127.0.0.2"); |
| 2752 | |
| 2753 | TestCompletionCallback callback2; |
| 2754 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2755 | EXPECT_EQ(OK, callback2.GetResult(trans.RestartWithAuth( |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2756 | AuthCredentials(kFoo, kBar), callback2.callback()))); |
| 2757 | |
| 2758 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2759 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info2)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2760 | TestLoadTimingNotReused(load_timing_info2, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2761 | // The load timing after restart should have a new socket ID, and times after |
| 2762 | // those of the first load timing. |
| 2763 | EXPECT_LE(load_timing_info1.receive_headers_end, |
| 2764 | load_timing_info2.connect_timing.connect_start); |
| 2765 | EXPECT_NE(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
| 2766 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2767 | int64_t writes_size2 = CountWriteBytes(data_writes2); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2768 | EXPECT_EQ(writes_size1 + writes_size2, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2769 | int64_t reads_size2 = CountReadBytes(data_reads2); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2770 | EXPECT_EQ(reads_size1 + reads_size2, trans.GetTotalReceivedBytes()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2771 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2772 | response = trans.GetResponseInfo(); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2773 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2774 | EXPECT_FALSE(response->auth_challenge.has_value()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2775 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2776 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2777 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2778 | ASSERT_FALSE(endpoint.address().empty()); |
| 2779 | EXPECT_EQ("127.0.0.2:80", endpoint.ToString()); |
| 2780 | } |
| 2781 | |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 2782 | // Test that, if the server requests auth indefinitely, HttpNetworkTransaction |
| 2783 | // will eventually give up. |
| 2784 | TEST_F(HttpNetworkTransactionTest, BasicAuthForever) { |
| 2785 | HttpRequestInfo request; |
| 2786 | request.method = "GET"; |
| 2787 | request.url = GURL("http://www.example.org/"); |
| 2788 | request.traffic_annotation = |
| 2789 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2790 | |
| 2791 | TestNetLog log; |
| 2792 | session_deps_.net_log = &log; |
| 2793 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2794 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 2795 | |
| 2796 | MockWrite data_writes[] = { |
| 2797 | MockWrite("GET / HTTP/1.1\r\n" |
| 2798 | "Host: www.example.org\r\n" |
| 2799 | "Connection: keep-alive\r\n\r\n"), |
| 2800 | }; |
| 2801 | |
| 2802 | MockRead data_reads[] = { |
| 2803 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2804 | // Give a couple authenticate options (only the middle one is actually |
| 2805 | // supported). |
| 2806 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
| 2807 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2808 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2809 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2810 | // Large content-length -- won't matter, as connection will be reset. |
| 2811 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 2812 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 2813 | }; |
| 2814 | |
| 2815 | // After calling trans->RestartWithAuth(), this is the request we should |
| 2816 | // be issuing -- the final header line contains the credentials. |
| 2817 | MockWrite data_writes_restart[] = { |
| 2818 | MockWrite("GET / HTTP/1.1\r\n" |
| 2819 | "Host: www.example.org\r\n" |
| 2820 | "Connection: keep-alive\r\n" |
| 2821 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2822 | }; |
| 2823 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2824 | StaticSocketDataProvider data(data_reads, data_writes); |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 2825 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 2826 | |
| 2827 | TestCompletionCallback callback; |
| 2828 | int rv = callback.GetResult( |
| 2829 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
| 2830 | |
| 2831 | std::vector<std::unique_ptr<StaticSocketDataProvider>> data_restarts; |
| 2832 | for (int i = 0; i < 32; i++) { |
| 2833 | // Check the previous response was a 401. |
| 2834 | EXPECT_THAT(rv, IsOk()); |
| 2835 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 2836 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2837 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 2838 | |
| 2839 | data_restarts.push_back(std::make_unique<StaticSocketDataProvider>( |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2840 | data_reads, data_writes_restart)); |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 2841 | session_deps_.socket_factory->AddSocketDataProvider( |
| 2842 | data_restarts.back().get()); |
| 2843 | rv = callback.GetResult(trans.RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 2844 | callback.callback())); |
| 2845 | } |
| 2846 | |
| 2847 | // After too many tries, the transaction should have given up. |
| 2848 | EXPECT_THAT(rv, IsError(ERR_TOO_MANY_RETRIES)); |
| 2849 | } |
| 2850 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2851 | TEST_F(HttpNetworkTransactionTest, DoNotSendAuth) { |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2852 | HttpRequestInfo request; |
| 2853 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2854 | request.url = GURL("http://www.example.org/"); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2855 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2856 | request.traffic_annotation = |
| 2857 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2858 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2859 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2860 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2861 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2862 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2863 | MockWrite( |
| 2864 | "GET / HTTP/1.1\r\n" |
| 2865 | "Host: www.example.org\r\n" |
| 2866 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2867 | }; |
| 2868 | |
| 2869 | MockRead data_reads[] = { |
| 2870 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2871 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2872 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2873 | // Large content-length -- won't matter, as connection will be reset. |
| 2874 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2875 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2876 | }; |
| 2877 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2878 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2879 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2880 | TestCompletionCallback callback; |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2881 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2882 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2883 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2884 | |
| 2885 | rv = callback.WaitForResult(); |
| 2886 | EXPECT_EQ(0, rv); |
| 2887 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2888 | int64_t writes_size = CountWriteBytes(data_writes); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2889 | EXPECT_EQ(writes_size, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2890 | int64_t reads_size = CountReadBytes(data_reads); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2891 | EXPECT_EQ(reads_size, trans.GetTotalReceivedBytes()); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 2892 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2893 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 2894 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2895 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2896 | } |
| 2897 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2898 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 2899 | // connection. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2900 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) { |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2901 | // On the second pass, the body read of the auth challenge is synchronous, so |
| 2902 | // IsConnectedAndIdle returns false. The socket should still be drained and |
| 2903 | // reused. See http://crbug.com/544255. |
| 2904 | for (int i = 0; i < 2; ++i) { |
| 2905 | HttpRequestInfo request; |
| 2906 | request.method = "GET"; |
| 2907 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2908 | request.traffic_annotation = |
| 2909 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2910 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2911 | TestNetLog log; |
| 2912 | session_deps_.net_log = &log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2913 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2914 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2915 | MockWrite data_writes[] = { |
| 2916 | MockWrite(ASYNC, 0, |
| 2917 | "GET / HTTP/1.1\r\n" |
| 2918 | "Host: www.example.org\r\n" |
| 2919 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2920 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2921 | // After calling trans.RestartWithAuth(), this is the request we should |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2922 | // be issuing -- the final header line contains the credentials. |
| 2923 | MockWrite(ASYNC, 6, |
| 2924 | "GET / HTTP/1.1\r\n" |
| 2925 | "Host: www.example.org\r\n" |
| 2926 | "Connection: keep-alive\r\n" |
| 2927 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2928 | }; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2929 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2930 | MockRead data_reads[] = { |
| 2931 | MockRead(ASYNC, 1, "HTTP/1.1 401 Unauthorized\r\n"), |
| 2932 | MockRead(ASYNC, 2, "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2933 | MockRead(ASYNC, 3, "Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2934 | MockRead(ASYNC, 4, "Content-Length: 14\r\n\r\n"), |
| 2935 | MockRead(i == 0 ? ASYNC : SYNCHRONOUS, 5, "Unauthorized\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2936 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2937 | // Lastly, the server responds with the actual content. |
| 2938 | MockRead(ASYNC, 7, "HTTP/1.1 200 OK\r\n"), |
| 2939 | MockRead(ASYNC, 8, "Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2940 | MockRead(ASYNC, 9, "Content-Length: 5\r\n\r\n"), |
| 2941 | MockRead(ASYNC, 10, "Hello"), |
| 2942 | }; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2943 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2944 | SequencedSocketData data(data_reads, data_writes); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2945 | data.set_busy_before_sync_reads(true); |
| 2946 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 2947 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2948 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2949 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2950 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2951 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2952 | ASSERT_THAT(callback1.GetResult(rv), IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2953 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2954 | LoadTimingInfo load_timing_info1; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2955 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info1)); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2956 | TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2957 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2958 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2959 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2960 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2961 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2962 | TestCompletionCallback callback2; |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2963 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2964 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 2965 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2966 | ASSERT_THAT(callback2.GetResult(rv), IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2967 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2968 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2969 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info2)); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2970 | TestLoadTimingReused(load_timing_info2); |
| 2971 | // The load timing after restart should have the same socket ID, and times |
| 2972 | // those of the first load timing. |
| 2973 | EXPECT_LE(load_timing_info1.receive_headers_end, |
| 2974 | load_timing_info2.send_start); |
| 2975 | 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] | 2976 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2977 | response = trans.GetResponseInfo(); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2978 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2979 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2980 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2981 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2982 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2983 | EXPECT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2984 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2985 | int64_t writes_size = CountWriteBytes(data_writes); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2986 | EXPECT_EQ(writes_size, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2987 | int64_t reads_size = CountReadBytes(data_reads); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2988 | EXPECT_EQ(reads_size, trans.GetTotalReceivedBytes()); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2989 | } |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 2993 | // connection and with no response body to drain. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2994 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2995 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2996 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2997 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 2998 | request.traffic_annotation = |
| 2999 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3000 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3001 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3002 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3003 | MockWrite data_writes1[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3004 | MockWrite("GET / HTTP/1.1\r\n" |
| 3005 | "Host: www.example.org\r\n" |
| 3006 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3007 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3008 | // After calling trans.RestartWithAuth(), this is the request we should |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3009 | // be issuing -- the final header line contains the credentials. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3010 | MockWrite("GET / HTTP/1.1\r\n" |
| 3011 | "Host: www.example.org\r\n" |
| 3012 | "Connection: keep-alive\r\n" |
| 3013 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3014 | }; |
| 3015 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3016 | MockRead data_reads1[] = { |
| 3017 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 3018 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3019 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3020 | |
| 3021 | // Lastly, the server responds with the actual content. |
| 3022 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3023 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3024 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3025 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3026 | }; |
| 3027 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 3028 | // An incorrect reconnect would cause this to be read. |
| 3029 | MockRead data_reads2[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3030 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 3031 | }; |
| 3032 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3033 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 3034 | StaticSocketDataProvider data2(data_reads2, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3035 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 3036 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3037 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3038 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3039 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3040 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3041 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3042 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3043 | |
| 3044 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3045 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3046 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3047 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3048 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3049 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3050 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3051 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3052 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3053 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3054 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3055 | |
| 3056 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3057 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3058 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3059 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3060 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3061 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3062 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 3066 | // connection and with a large response body to drain. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3067 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3068 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3069 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3070 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 3071 | request.traffic_annotation = |
| 3072 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3073 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3074 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3075 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3076 | MockWrite data_writes1[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3077 | MockWrite("GET / HTTP/1.1\r\n" |
| 3078 | "Host: www.example.org\r\n" |
| 3079 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3080 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3081 | // After calling trans.RestartWithAuth(), this is the request we should |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3082 | // be issuing -- the final header line contains the credentials. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3083 | MockWrite("GET / HTTP/1.1\r\n" |
| 3084 | "Host: www.example.org\r\n" |
| 3085 | "Connection: keep-alive\r\n" |
| 3086 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3087 | }; |
| 3088 | |
| 3089 | // Respond with 5 kb of response body. |
| 3090 | std::string large_body_string("Unauthorized"); |
| 3091 | large_body_string.append(5 * 1024, ' '); |
| 3092 | large_body_string.append("\r\n"); |
| 3093 | |
| 3094 | MockRead data_reads1[] = { |
| 3095 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 3096 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3097 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3098 | // 5134 = 12 + 5 * 1024 + 2 |
| 3099 | MockRead("Content-Length: 5134\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3100 | MockRead(ASYNC, large_body_string.data(), large_body_string.size()), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3101 | |
| 3102 | // Lastly, the server responds with the actual content. |
| 3103 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3104 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3105 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3106 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3107 | }; |
| 3108 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 3109 | // An incorrect reconnect would cause this to be read. |
| 3110 | MockRead data_reads2[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3111 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 3112 | }; |
| 3113 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3114 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 3115 | StaticSocketDataProvider data2(data_reads2, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3116 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 3117 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3118 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3119 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3120 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3121 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3122 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3123 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3124 | |
| 3125 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3126 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3127 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3128 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3129 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3130 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3131 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3132 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3133 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3134 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3135 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3136 | |
| 3137 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3138 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3139 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3140 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3141 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3142 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3143 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3147 | // connection, but the server gets impatient and closes the connection. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3148 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveImpatientServer) { |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3149 | HttpRequestInfo request; |
| 3150 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3151 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 3152 | request.traffic_annotation = |
| 3153 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3154 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3155 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3156 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3157 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3158 | MockWrite( |
| 3159 | "GET / HTTP/1.1\r\n" |
| 3160 | "Host: www.example.org\r\n" |
| 3161 | "Connection: keep-alive\r\n\r\n"), |
| 3162 | // This simulates the seemingly successful write to a closed connection |
| 3163 | // if the bug is not fixed. |
| 3164 | MockWrite( |
| 3165 | "GET / HTTP/1.1\r\n" |
| 3166 | "Host: www.example.org\r\n" |
| 3167 | "Connection: keep-alive\r\n" |
| 3168 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3169 | }; |
| 3170 | |
| 3171 | MockRead data_reads1[] = { |
| 3172 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 3173 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3174 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3175 | MockRead("Content-Length: 14\r\n\r\n"), |
| 3176 | // Tell MockTCPClientSocket to simulate the server closing the connection. |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3177 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3178 | MockRead("Unauthorized\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3179 | MockRead(SYNCHRONOUS, OK), // The server closes the connection. |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3180 | }; |
| 3181 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3182 | // After calling trans.RestartWithAuth(), this is the request we should |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3183 | // be issuing -- the final header line contains the credentials. |
| 3184 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3185 | MockWrite( |
| 3186 | "GET / HTTP/1.1\r\n" |
| 3187 | "Host: www.example.org\r\n" |
| 3188 | "Connection: keep-alive\r\n" |
| 3189 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3190 | }; |
| 3191 | |
| 3192 | // Lastly, the server responds with the actual content. |
| 3193 | MockRead data_reads2[] = { |
| 3194 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3195 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3196 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3197 | MockRead("hello"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3198 | }; |
| 3199 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3200 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 3201 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3202 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 3203 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3204 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3205 | TestCompletionCallback callback1; |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3206 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3207 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3208 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3209 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3210 | |
| 3211 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3212 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3213 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3214 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3215 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3216 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3217 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3218 | TestCompletionCallback callback2; |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3219 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3220 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3221 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3222 | |
| 3223 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3224 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3225 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3226 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3227 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3228 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3229 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3230 | } |
| 3231 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3232 | // Test the request-challenge-retry sequence for basic auth, over a connection |
| 3233 | // that requires a restart when setting up an SSL tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3234 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyNoKeepAliveHttp10) { |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3235 | HttpRequestInfo request; |
| 3236 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3237 | request.url = GURL("https://www.example.org/"); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3238 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3239 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 3240 | request.traffic_annotation = |
| 3241 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3242 | |
| 3243 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3244 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3245 | ProxyResolutionService::CreateFixedFromPacResult( |
| 3246 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 3247 | BoundTestNetLog log; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3248 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3249 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3250 | |
| 3251 | // Since we have proxy, should try to establish tunnel. |
| 3252 | MockWrite data_writes1[] = { |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3253 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3254 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3255 | "Proxy-Connection: keep-alive\r\n\r\n"), |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3256 | }; |
| 3257 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3258 | // The proxy responds to the connect with a 407, using a non-persistent |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3259 | // connection. |
| 3260 | MockRead data_reads1[] = { |
| 3261 | // No credentials. |
| 3262 | MockRead("HTTP/1.0 407 Proxy Authentication Required\r\n"), |
| 3263 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"), |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3264 | }; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3265 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3266 | // Since the first connection couldn't be reused, need to establish another |
| 3267 | // once given credentials. |
| 3268 | MockWrite data_writes2[] = { |
| 3269 | // After calling trans->RestartWithAuth(), this is the request we should |
| 3270 | // be issuing -- the final header line contains the credentials. |
| 3271 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3272 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3273 | "Proxy-Connection: keep-alive\r\n" |
| 3274 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3275 | |
| 3276 | MockWrite("GET / HTTP/1.1\r\n" |
| 3277 | "Host: www.example.org\r\n" |
| 3278 | "Connection: keep-alive\r\n\r\n"), |
| 3279 | }; |
| 3280 | |
| 3281 | MockRead data_reads2[] = { |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3282 | MockRead("HTTP/1.0 200 Connection Established\r\n\r\n"), |
| 3283 | |
| 3284 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3285 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3286 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3287 | MockRead(SYNCHRONOUS, "hello"), |
| 3288 | }; |
| 3289 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3290 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3291 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3292 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3293 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3294 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 3295 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 3296 | |
| 3297 | TestCompletionCallback callback1; |
| 3298 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 3299 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 3300 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3301 | |
| 3302 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3303 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3304 | |
| 3305 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3306 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3307 | auto entries = log.GetEntries(); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3308 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3309 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3310 | NetLogEventPhase::NONE); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3311 | ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3312 | entries, pos, |
| 3313 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3314 | NetLogEventPhase::NONE); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3315 | |
| 3316 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3317 | ASSERT_TRUE(response); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3318 | EXPECT_FALSE(response->headers->IsKeepAlive()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3319 | ASSERT_TRUE(response->headers); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3320 | EXPECT_EQ(407, response->headers->response_code()); |
| 3321 | EXPECT_TRUE(HttpVersion(1, 0) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3322 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3323 | |
| 3324 | LoadTimingInfo load_timing_info; |
| 3325 | // CONNECT requests and responses are handled at the connect job level, so |
| 3326 | // the transaction does not yet have a connection. |
| 3327 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3328 | |
| 3329 | TestCompletionCallback callback2; |
| 3330 | |
| 3331 | rv = |
| 3332 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3333 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3334 | |
| 3335 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3336 | EXPECT_THAT(rv, IsOk()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3337 | |
| 3338 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3339 | ASSERT_TRUE(response); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3340 | |
| 3341 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3342 | EXPECT_EQ(200, response->headers->response_code()); |
| 3343 | EXPECT_EQ(5, response->headers->GetContentLength()); |
| 3344 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 3345 | |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 3346 | // Check that credentials were successfully cached, with the right target. |
| 3347 | HttpAuthCache::Entry* entry = session->http_auth_cache()->Lookup( |
| 3348 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 3349 | HttpAuth::AUTH_SCHEME_BASIC, NetworkIsolationKey()); |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 3350 | ASSERT_TRUE(entry); |
| 3351 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 3352 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 3353 | |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3354 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3355 | EXPECT_FALSE(response->auth_challenge.has_value()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3356 | |
| 3357 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3358 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 3359 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 3360 | |
| 3361 | trans.reset(); |
| 3362 | session->CloseAllConnections(); |
| 3363 | } |
| 3364 | |
| 3365 | // Test the request-challenge-retry sequence for basic auth, over a connection |
| 3366 | // that requires a restart when setting up an SSL tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3367 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyNoKeepAliveHttp11) { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3368 | HttpRequestInfo request; |
| 3369 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3370 | request.url = GURL("https://www.example.org/"); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3371 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3372 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 3373 | request.traffic_annotation = |
| 3374 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3375 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3376 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3377 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3378 | ProxyResolutionService::CreateFixedFromPacResult( |
| 3379 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 3380 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3381 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3382 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3383 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3384 | // Since we have proxy, should try to establish tunnel. |
| 3385 | MockWrite data_writes1[] = { |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3386 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3387 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3388 | "Proxy-Connection: keep-alive\r\n\r\n"), |
mmenke | e0b5c88 | 2015-08-26 20:29:11 | [diff] [blame] | 3389 | }; |
| 3390 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3391 | // The proxy responds to the connect with a 407, using a non-persistent |
mmenke | 0fd148d | 2015-09-30 23:00:08 | [diff] [blame] | 3392 | // connection. |
| 3393 | MockRead data_reads1[] = { |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3394 | // No credentials. |
| 3395 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 3396 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3397 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 3398 | }; |
mmenke | e0b5c88 | 2015-08-26 20:29:11 | [diff] [blame] | 3399 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3400 | MockWrite data_writes2[] = { |
| 3401 | // After calling trans->RestartWithAuth(), this is the request we should |
| 3402 | // be issuing -- the final header line contains the credentials. |
| 3403 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3404 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3405 | "Proxy-Connection: keep-alive\r\n" |
| 3406 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
mmenke | 0fd148d | 2015-09-30 23:00:08 | [diff] [blame] | 3407 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3408 | MockWrite("GET / HTTP/1.1\r\n" |
| 3409 | "Host: www.example.org\r\n" |
| 3410 | "Connection: keep-alive\r\n\r\n"), |
| 3411 | }; |
| 3412 | |
| 3413 | MockRead data_reads2[] = { |
| 3414 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 3415 | |
| 3416 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3417 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3418 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3419 | MockRead(SYNCHRONOUS, "hello"), |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3420 | }; |
| 3421 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3422 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3423 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3424 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3425 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3426 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3427 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3428 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3429 | TestCompletionCallback callback1; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3430 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 3431 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 3432 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3433 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3434 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3435 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3436 | |
| 3437 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3438 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3439 | auto entries = log.GetEntries(); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3440 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3441 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3442 | NetLogEventPhase::NONE); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3443 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 3444 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3445 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3446 | NetLogEventPhase::NONE); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3447 | |
| 3448 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3449 | ASSERT_TRUE(response); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3450 | EXPECT_FALSE(response->headers->IsKeepAlive()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3451 | ASSERT_TRUE(response->headers); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3452 | EXPECT_EQ(407, response->headers->response_code()); |
| 3453 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3454 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3455 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 3456 | LoadTimingInfo load_timing_info; |
| 3457 | // CONNECT requests and responses are handled at the connect job level, so |
| 3458 | // the transaction does not yet have a connection. |
| 3459 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3460 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3461 | TestCompletionCallback callback2; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3462 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3463 | rv = trans->RestartWithAuth( |
| 3464 | AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3465 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3466 | |
| 3467 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3468 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3469 | |
| 3470 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3471 | ASSERT_TRUE(response); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3472 | |
| 3473 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3474 | EXPECT_EQ(200, response->headers->response_code()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3475 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3476 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 3477 | |
| 3478 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3479 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3480 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 3481 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3482 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 3483 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 3484 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3485 | trans.reset(); |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 3486 | session->CloseAllConnections(); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3487 | } |
| 3488 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3489 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3490 | // proxy connection with HTTP/1.0 responses, when setting up an SSL tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3491 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAliveHttp10) { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3492 | // On the second pass, the body read of the auth challenge is synchronous, so |
| 3493 | // IsConnectedAndIdle returns false. The socket should still be drained and |
| 3494 | // reused. See http://crbug.com/544255. |
| 3495 | for (int i = 0; i < 2; ++i) { |
| 3496 | HttpRequestInfo request; |
| 3497 | request.method = "GET"; |
| 3498 | request.url = GURL("https://www.example.org/"); |
| 3499 | // Ensure that proxy authentication is attempted even |
| 3500 | // when the no authentication data flag is set. |
| 3501 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 3502 | request.traffic_annotation = |
| 3503 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3504 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3505 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3506 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3507 | ProxyResolutionService::CreateFixed("myproxy:70", |
| 3508 | TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3509 | BoundTestNetLog log; |
| 3510 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3511 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3512 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3513 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3514 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3515 | // Since we have proxy, should try to establish tunnel. |
| 3516 | MockWrite data_writes1[] = { |
| 3517 | MockWrite(ASYNC, 0, |
| 3518 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3519 | "Host: www.example.org:443\r\n" |
| 3520 | "Proxy-Connection: keep-alive\r\n\r\n"), |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3521 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3522 | // After calling trans.RestartWithAuth(), this is the request we should |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3523 | // be issuing -- the final header line contains the credentials. |
| 3524 | MockWrite(ASYNC, 3, |
| 3525 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3526 | "Host: www.example.org:443\r\n" |
| 3527 | "Proxy-Connection: keep-alive\r\n" |
| 3528 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 3529 | }; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3530 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3531 | // The proxy responds to the connect with a 407, using a persistent |
| 3532 | // connection. (Since it's HTTP/1.0, keep-alive has to be explicit.) |
| 3533 | MockRead data_reads1[] = { |
| 3534 | // No credentials. |
| 3535 | MockRead(ASYNC, 1, |
| 3536 | "HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 3537 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3538 | "Proxy-Connection: keep-alive\r\n" |
| 3539 | "Content-Length: 10\r\n\r\n"), |
| 3540 | MockRead(i == 0 ? ASYNC : SYNCHRONOUS, 2, "0123456789"), |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3541 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3542 | // Wrong credentials (wrong password). |
| 3543 | MockRead(ASYNC, 4, |
| 3544 | "HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 3545 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3546 | "Proxy-Connection: keep-alive\r\n" |
| 3547 | "Content-Length: 10\r\n\r\n"), |
| 3548 | // No response body because the test stops reading here. |
| 3549 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED, 5), |
| 3550 | }; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3551 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3552 | SequencedSocketData data1(data_reads1, data_writes1); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3553 | data1.set_busy_before_sync_reads(true); |
| 3554 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3555 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3556 | TestCompletionCallback callback1; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3557 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3558 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3559 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3560 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3561 | auto entries = log.GetEntries(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3562 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3563 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3564 | NetLogEventPhase::NONE); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3565 | ExpectLogContainsSomewhere( |
| 3566 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3567 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3568 | NetLogEventPhase::NONE); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3569 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3570 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3571 | ASSERT_TRUE(response); |
| 3572 | ASSERT_TRUE(response->headers); |
| 3573 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3574 | EXPECT_EQ(407, response->headers->response_code()); |
| 3575 | EXPECT_EQ(10, response->headers->GetContentLength()); |
| 3576 | EXPECT_TRUE(HttpVersion(1, 0) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3577 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3578 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3579 | TestCompletionCallback callback2; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3580 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3581 | // Wrong password (should be "bar"). |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3582 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBaz), |
| 3583 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3584 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3585 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3586 | response = trans.GetResponseInfo(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3587 | ASSERT_TRUE(response); |
| 3588 | ASSERT_TRUE(response->headers); |
| 3589 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3590 | EXPECT_EQ(407, response->headers->response_code()); |
| 3591 | EXPECT_EQ(10, response->headers->GetContentLength()); |
| 3592 | EXPECT_TRUE(HttpVersion(1, 0) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3593 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3594 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3595 | // Flush the idle socket before the NetLog and HttpNetworkTransaction go |
| 3596 | // out of scope. |
| 3597 | session->CloseAllConnections(); |
| 3598 | } |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 3602 | // proxy connection with HTTP/1.1 responses, when setting up an SSL tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3603 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAliveHttp11) { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3604 | // On the second pass, the body read of the auth challenge is synchronous, so |
| 3605 | // IsConnectedAndIdle returns false. The socket should still be drained and |
| 3606 | // reused. See http://crbug.com/544255. |
| 3607 | for (int i = 0; i < 2; ++i) { |
| 3608 | HttpRequestInfo request; |
| 3609 | request.method = "GET"; |
| 3610 | request.url = GURL("https://www.example.org/"); |
| 3611 | // Ensure that proxy authentication is attempted even |
| 3612 | // when the no authentication data flag is set. |
| 3613 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 3614 | request.traffic_annotation = |
| 3615 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3616 | |
| 3617 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3618 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3619 | ProxyResolutionService::CreateFixed("myproxy:70", |
| 3620 | TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3621 | BoundTestNetLog log; |
| 3622 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3623 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3624 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3625 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3626 | |
| 3627 | // Since we have proxy, should try to establish tunnel. |
| 3628 | MockWrite data_writes1[] = { |
| 3629 | MockWrite(ASYNC, 0, |
| 3630 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3631 | "Host: www.example.org:443\r\n" |
| 3632 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 3633 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3634 | // After calling trans.RestartWithAuth(), this is the request we should |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3635 | // be issuing -- the final header line contains the credentials. |
| 3636 | MockWrite(ASYNC, 3, |
| 3637 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3638 | "Host: www.example.org:443\r\n" |
| 3639 | "Proxy-Connection: keep-alive\r\n" |
| 3640 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 3641 | }; |
| 3642 | |
| 3643 | // The proxy responds to the connect with a 407, using a persistent |
| 3644 | // connection. (Since it's HTTP/1.0, keep-alive has to be explicit.) |
| 3645 | MockRead data_reads1[] = { |
| 3646 | // No credentials. |
| 3647 | MockRead(ASYNC, 1, |
| 3648 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 3649 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3650 | "Content-Length: 10\r\n\r\n"), |
| 3651 | MockRead(i == 0 ? ASYNC : SYNCHRONOUS, 2, "0123456789"), |
| 3652 | |
| 3653 | // Wrong credentials (wrong password). |
| 3654 | MockRead(ASYNC, 4, |
| 3655 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 3656 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3657 | "Content-Length: 10\r\n\r\n"), |
| 3658 | // No response body because the test stops reading here. |
| 3659 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED, 5), |
| 3660 | }; |
| 3661 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3662 | SequencedSocketData data1(data_reads1, data_writes1); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3663 | data1.set_busy_before_sync_reads(true); |
| 3664 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 3665 | |
| 3666 | TestCompletionCallback callback1; |
| 3667 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3668 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3669 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3670 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3671 | auto entries = log.GetEntries(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3672 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3673 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3674 | NetLogEventPhase::NONE); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3675 | ExpectLogContainsSomewhere( |
| 3676 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3677 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3678 | NetLogEventPhase::NONE); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3679 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3680 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3681 | ASSERT_TRUE(response); |
| 3682 | ASSERT_TRUE(response->headers); |
| 3683 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3684 | EXPECT_EQ(407, response->headers->response_code()); |
| 3685 | EXPECT_EQ(10, response->headers->GetContentLength()); |
| 3686 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3687 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 3688 | EXPECT_FALSE(response->did_use_http_auth); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3689 | |
| 3690 | TestCompletionCallback callback2; |
| 3691 | |
| 3692 | // Wrong password (should be "bar"). |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3693 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBaz), |
| 3694 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3695 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3696 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3697 | response = trans.GetResponseInfo(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3698 | ASSERT_TRUE(response); |
| 3699 | ASSERT_TRUE(response->headers); |
| 3700 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3701 | EXPECT_EQ(407, response->headers->response_code()); |
| 3702 | EXPECT_EQ(10, response->headers->GetContentLength()); |
| 3703 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3704 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 3705 | EXPECT_TRUE(response->did_use_http_auth); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3706 | |
| 3707 | // Flush the idle socket before the NetLog and HttpNetworkTransaction go |
| 3708 | // out of scope. |
| 3709 | session->CloseAllConnections(); |
| 3710 | } |
| 3711 | } |
| 3712 | |
| 3713 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 3714 | // proxy connection with HTTP/1.1 responses, when setting up an SSL tunnel, in |
| 3715 | // the case the server sends extra data on the original socket, so it can't be |
| 3716 | // reused. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3717 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAliveExtraData) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3718 | HttpRequestInfo request; |
| 3719 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3720 | request.url = GURL("https://www.example.org/"); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3721 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3722 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 3723 | request.traffic_annotation = |
| 3724 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3725 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3726 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3727 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3728 | ProxyResolutionService::CreateFixedFromPacResult( |
| 3729 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 3730 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3731 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3732 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3733 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3734 | // Since we have proxy, should try to establish tunnel. |
| 3735 | MockWrite data_writes1[] = { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3736 | MockWrite(ASYNC, 0, |
| 3737 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3738 | "Host: www.example.org:443\r\n" |
| 3739 | "Proxy-Connection: keep-alive\r\n\r\n"), |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3740 | }; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3741 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3742 | // The proxy responds to the connect with a 407, using a persistent, but sends |
| 3743 | // extra data, so the socket cannot be reused. |
| 3744 | MockRead data_reads1[] = { |
| 3745 | // No credentials. |
| 3746 | MockRead(ASYNC, 1, |
| 3747 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 3748 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3749 | "Content-Length: 10\r\n\r\n"), |
| 3750 | MockRead(SYNCHRONOUS, 2, "0123456789"), |
| 3751 | MockRead(SYNCHRONOUS, 3, "I'm broken!"), |
| 3752 | }; |
| 3753 | |
| 3754 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3755 | // After calling trans->RestartWithAuth(), this is the request we should |
| 3756 | // be issuing -- the final header line contains the credentials. |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3757 | MockWrite(ASYNC, 0, |
| 3758 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3759 | "Host: www.example.org:443\r\n" |
| 3760 | "Proxy-Connection: keep-alive\r\n" |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3761 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3762 | |
| 3763 | MockWrite(ASYNC, 2, |
| 3764 | "GET / HTTP/1.1\r\n" |
| 3765 | "Host: www.example.org\r\n" |
| 3766 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3767 | }; |
| 3768 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3769 | MockRead data_reads2[] = { |
| 3770 | MockRead(ASYNC, 1, "HTTP/1.1 200 Connection Established\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3771 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3772 | MockRead(ASYNC, 3, |
| 3773 | "HTTP/1.1 200 OK\r\n" |
| 3774 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 3775 | "Content-Length: 5\r\n\r\n"), |
| 3776 | // No response body because the test stops reading here. |
| 3777 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED, 4), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3778 | }; |
| 3779 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3780 | SequencedSocketData data1(data_reads1, data_writes1); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3781 | data1.set_busy_before_sync_reads(true); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3782 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3783 | SequencedSocketData data2(data_reads2, data_writes2); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3784 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 3785 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 3786 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3787 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3788 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3789 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 3790 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 3791 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3792 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3793 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3794 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3795 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3796 | auto entries = log.GetEntries(); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 3797 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3798 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3799 | NetLogEventPhase::NONE); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 3800 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 3801 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3802 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3803 | NetLogEventPhase::NONE); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3804 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3805 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 3806 | ASSERT_TRUE(response); |
| 3807 | ASSERT_TRUE(response->headers); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3808 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3809 | EXPECT_EQ(407, response->headers->response_code()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3810 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3811 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3812 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3813 | LoadTimingInfo load_timing_info; |
| 3814 | // CONNECT requests and responses are handled at the connect job level, so |
| 3815 | // the transaction does not yet have a connection. |
| 3816 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3817 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3818 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3819 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3820 | rv = |
| 3821 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3822 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3823 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3824 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3825 | EXPECT_EQ(200, response->headers->response_code()); |
| 3826 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3827 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 3828 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3829 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3830 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3831 | |
| 3832 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3833 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 3834 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 3835 | |
| 3836 | trans.reset(); |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 3837 | session->CloseAllConnections(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3838 | } |
| 3839 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3840 | // Test the case a proxy closes a socket while the challenge body is being |
| 3841 | // drained. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3842 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAliveHangupDuringBody) { |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3843 | HttpRequestInfo request; |
| 3844 | request.method = "GET"; |
| 3845 | request.url = GURL("https://www.example.org/"); |
| 3846 | // Ensure that proxy authentication is attempted even |
| 3847 | // when the no authentication data flag is set. |
| 3848 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 3849 | request.traffic_annotation = |
| 3850 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3851 | |
| 3852 | // Configure against proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3853 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 3854 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3855 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3856 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3857 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3858 | |
| 3859 | // Since we have proxy, should try to establish tunnel. |
| 3860 | MockWrite data_writes1[] = { |
| 3861 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3862 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3863 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 3864 | }; |
| 3865 | |
| 3866 | // The proxy responds to the connect with a 407, using a persistent |
| 3867 | // connection. |
| 3868 | MockRead data_reads1[] = { |
| 3869 | // No credentials. |
| 3870 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 3871 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3872 | MockRead("Content-Length: 10\r\n\r\n"), MockRead("spam!"), |
| 3873 | // Server hands up in the middle of the body. |
| 3874 | MockRead(ASYNC, ERR_CONNECTION_CLOSED), |
| 3875 | }; |
| 3876 | |
| 3877 | MockWrite data_writes2[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3878 | // After calling trans.RestartWithAuth(), this is the request we should |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3879 | // be issuing -- the final header line contains the credentials. |
| 3880 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3881 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3882 | "Proxy-Connection: keep-alive\r\n" |
| 3883 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3884 | |
| 3885 | MockWrite("GET / HTTP/1.1\r\n" |
| 3886 | "Host: www.example.org\r\n" |
| 3887 | "Connection: keep-alive\r\n\r\n"), |
| 3888 | }; |
| 3889 | |
| 3890 | MockRead data_reads2[] = { |
| 3891 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 3892 | |
| 3893 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3894 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3895 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3896 | MockRead(SYNCHRONOUS, "hello"), |
| 3897 | }; |
| 3898 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3899 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3900 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3901 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3902 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 3903 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 3904 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 3905 | |
| 3906 | TestCompletionCallback callback; |
| 3907 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3908 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3909 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3910 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3911 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3912 | ASSERT_TRUE(response); |
| 3913 | ASSERT_TRUE(response->headers); |
| 3914 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3915 | EXPECT_EQ(407, response->headers->response_code()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3916 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3917 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3918 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3919 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3920 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3921 | response = trans.GetResponseInfo(); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3922 | ASSERT_TRUE(response); |
| 3923 | ASSERT_TRUE(response->headers); |
| 3924 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3925 | EXPECT_EQ(200, response->headers->response_code()); |
| 3926 | std::string body; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3927 | EXPECT_THAT(ReadTransaction(&trans, &body), IsOk()); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3928 | EXPECT_EQ("hello", body); |
| 3929 | } |
| 3930 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3931 | // Test that we don't read the response body when we fail to establish a tunnel, |
| 3932 | // even if the user cancels the proxy's auth attempt. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3933 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3934 | HttpRequestInfo request; |
| 3935 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3936 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 3937 | request.traffic_annotation = |
| 3938 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3939 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3940 | // Configure against proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3941 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 3942 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3943 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3944 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3945 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3946 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3947 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3948 | // Since we have proxy, should try to establish tunnel. |
| 3949 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3950 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3951 | "Host: www.example.org:443\r\n" |
| 3952 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3953 | }; |
| 3954 | |
| 3955 | // The proxy responds to the connect with a 407. |
| 3956 | MockRead data_reads[] = { |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 3957 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 3958 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3959 | MockRead("Content-Length: 10\r\n\r\n"), |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3960 | MockRead("0123456789"), |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 3961 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3962 | }; |
| 3963 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3964 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3965 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3966 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3967 | TestCompletionCallback callback; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3968 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3969 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3970 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3971 | |
| 3972 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3973 | EXPECT_THAT(rv, IsOk()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3974 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3975 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 3976 | ASSERT_TRUE(response); |
| 3977 | ASSERT_TRUE(response->headers); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3978 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3979 | EXPECT_EQ(407, response->headers->response_code()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3980 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3981 | |
| 3982 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3983 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3984 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 3985 | |
| 3986 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 3987 | session->CloseAllConnections(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3988 | } |
| 3989 | |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 3990 | // Test the no-tunnel HTTP auth case where proxy and server origins and realms |
| 3991 | // are the same, but the user/passwords are different. Serves to verify |
| 3992 | // credentials are correctly separated based on HttpAuth::Target. |
| 3993 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyMatchesServerAuthNoTunnel) { |
| 3994 | HttpRequestInfo request; |
| 3995 | request.method = "GET"; |
| 3996 | request.url = GURL("http://myproxy:70/"); |
| 3997 | request.traffic_annotation = |
| 3998 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 3999 | |
| 4000 | // Proxy matches request URL. |
| 4001 | session_deps_.proxy_resolution_service = |
| 4002 | ProxyResolutionService::CreateFixedFromPacResult( |
| 4003 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 4004 | BoundTestNetLog log; |
| 4005 | session_deps_.net_log = log.bound().net_log(); |
| 4006 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 4007 | |
| 4008 | MockWrite data_writes[] = { |
| 4009 | // Initial request gets a proxy auth challenge. |
| 4010 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4011 | "Host: myproxy:70\r\n" |
| 4012 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4013 | // Retry with proxy auth credentials, which will result in a server auth |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 4014 | // challenge. |
| 4015 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4016 | "Host: myproxy:70\r\n" |
| 4017 | "Proxy-Connection: keep-alive\r\n" |
| 4018 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4019 | // Retry with proxy and server auth credentials, which gets a response. |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 4020 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4021 | "Host: myproxy:70\r\n" |
| 4022 | "Proxy-Connection: keep-alive\r\n" |
| 4023 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 4024 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4025 | // A second request should preemptively send the correct proxy and server |
| 4026 | // auth headers. |
| 4027 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4028 | "Host: myproxy:70\r\n" |
| 4029 | "Proxy-Connection: keep-alive\r\n" |
| 4030 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 4031 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4032 | }; |
| 4033 | |
| 4034 | MockRead data_reads[] = { |
| 4035 | // Proxy auth challenge. |
| 4036 | MockRead("HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 4037 | "Proxy-Connection: keep-alive\r\n" |
| 4038 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4039 | "Content-Length: 0\r\n\r\n"), |
| 4040 | // Server auth challenge. |
| 4041 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4042 | "Proxy-Connection: keep-alive\r\n" |
| 4043 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4044 | "Content-Length: 0\r\n\r\n"), |
| 4045 | // Response. |
| 4046 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4047 | "Proxy-Connection: keep-alive\r\n" |
| 4048 | "Content-Length: 5\r\n\r\n" |
| 4049 | "hello"), |
| 4050 | // Response to second request. |
| 4051 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4052 | "Proxy-Connection: keep-alive\r\n" |
| 4053 | "Content-Length: 2\r\n\r\n" |
| 4054 | "hi"), |
| 4055 | }; |
| 4056 | |
| 4057 | StaticSocketDataProvider data(data_reads, data_writes); |
| 4058 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 4059 | |
| 4060 | TestCompletionCallback callback; |
| 4061 | |
| 4062 | auto trans = |
| 4063 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4064 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4065 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4066 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4067 | ASSERT_TRUE(response); |
| 4068 | ASSERT_TRUE(response->headers); |
| 4069 | EXPECT_EQ(407, response->headers->response_code()); |
| 4070 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
| 4071 | |
| 4072 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 4073 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4074 | response = trans->GetResponseInfo(); |
| 4075 | ASSERT_TRUE(response); |
| 4076 | EXPECT_EQ(401, response->headers->response_code()); |
| 4077 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4078 | EXPECT_EQ("http://myproxy:70", |
| 4079 | response->auth_challenge->challenger.Serialize()); |
| 4080 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4081 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4082 | |
| 4083 | rv = trans->RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 4084 | callback.callback()); |
| 4085 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4086 | response = trans->GetResponseInfo(); |
| 4087 | ASSERT_TRUE(response); |
| 4088 | EXPECT_EQ(200, response->headers->response_code()); |
| 4089 | // The password prompt info should not be set. |
| 4090 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4091 | |
| 4092 | std::string response_data; |
| 4093 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4094 | EXPECT_EQ("hello", response_data); |
| 4095 | |
| 4096 | // Check that the credentials were cached correctly. |
| 4097 | HttpAuthCache::Entry* entry = session->http_auth_cache()->Lookup( |
| 4098 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4099 | HttpAuth::AUTH_SCHEME_BASIC, NetworkIsolationKey()); |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 4100 | ASSERT_TRUE(entry); |
| 4101 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4102 | ASSERT_EQ(kBar, entry->credentials().password()); |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4103 | entry = session->http_auth_cache()->Lookup( |
| 4104 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4105 | HttpAuth::AUTH_SCHEME_BASIC, NetworkIsolationKey()); |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 4106 | ASSERT_TRUE(entry); |
| 4107 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4108 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4109 | |
| 4110 | // Make another request, which should automatically send the correct proxy and |
| 4111 | // server auth credentials and get another response. |
| 4112 | trans = |
| 4113 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4114 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4115 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4116 | response = trans->GetResponseInfo(); |
| 4117 | ASSERT_TRUE(response); |
| 4118 | EXPECT_EQ(200, response->headers->response_code()); |
| 4119 | // The password prompt info should not be set. |
| 4120 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4121 | |
| 4122 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4123 | EXPECT_EQ("hi", response_data); |
| 4124 | |
| 4125 | trans.reset(); |
| 4126 | session->CloseAllConnections(); |
| 4127 | } |
| 4128 | |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4129 | // Test the no-tunnel HTTP auth case where proxy and server origins and realms |
| 4130 | // are the same, but the user/passwords are different, and with different |
| 4131 | // NetworkIsolationKeys. Sends one request with a NIK, response to both proxy |
| 4132 | // and auth challenges, sends another request with another NIK, expecting only |
| 4133 | // the proxy credentials to be cached, and thus sees only a server auth |
| 4134 | // challenge. Then sends a request with the original NIK, expecting cached proxy |
| 4135 | // and auth credentials that match the ones used in the first request. |
| 4136 | // |
| 4137 | // Serves to verify credentials are correctly separated based on |
| 4138 | // HttpAuth::Target and NetworkIsolationKeys, but NetworkIsolationKey only |
| 4139 | // affects server credentials, not proxy credentials. |
| 4140 | TEST_F(HttpNetworkTransactionTest, |
| 4141 | BasicAuthProxyMatchesServerAuthWithNetworkIsolationKeyNoTunnel) { |
| 4142 | const url::Origin kOrigin1 = url::Origin::Create(GURL("https://foo.test/")); |
| 4143 | const net::NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 4144 | const url::Origin kOrigin2 = url::Origin::Create(GURL("https://bar.test/")); |
| 4145 | const net::NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
| 4146 | |
| 4147 | // This test would need to use a single socket without this option enabled. |
| 4148 | // Best to use this option when it would affect a test, as it will eventually |
| 4149 | // become the default behavior. |
| 4150 | base::test::ScopedFeatureList feature_list; |
| 4151 | feature_list.InitAndEnableFeature( |
| 4152 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 4153 | |
| 4154 | // Proxy matches request URL. |
| 4155 | session_deps_.proxy_resolution_service = |
| 4156 | ProxyResolutionService::CreateFixedFromPacResult( |
| 4157 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 4158 | BoundTestNetLog log; |
| 4159 | session_deps_.net_log = log.bound().net_log(); |
| 4160 | session_deps_.key_auth_cache_server_entries_by_network_isolation_key = true; |
| 4161 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 4162 | |
| 4163 | MockWrite data_writes[] = { |
| 4164 | // Initial request gets a proxy auth challenge. |
| 4165 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4166 | "Host: myproxy:70\r\n" |
| 4167 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4168 | // Retry with proxy auth credentials, which will result in a server auth |
| 4169 | // challenge. |
| 4170 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4171 | "Host: myproxy:70\r\n" |
| 4172 | "Proxy-Connection: keep-alive\r\n" |
| 4173 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4174 | // Retry with proxy and server auth credentials, which gets a response. |
| 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 | // Another request to the same server and using the same NIK should |
| 4181 | // preemptively send the correct cached proxy and server |
| 4182 | // auth headers. |
| 4183 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4184 | "Host: myproxy:70\r\n" |
| 4185 | "Proxy-Connection: keep-alive\r\n" |
| 4186 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 4187 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4188 | }; |
| 4189 | |
| 4190 | MockRead data_reads[] = { |
| 4191 | // Proxy auth challenge. |
| 4192 | MockRead("HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 4193 | "Proxy-Connection: keep-alive\r\n" |
| 4194 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4195 | "Content-Length: 0\r\n\r\n"), |
| 4196 | // Server auth challenge. |
| 4197 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4198 | "Proxy-Connection: keep-alive\r\n" |
| 4199 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4200 | "Content-Length: 0\r\n\r\n"), |
| 4201 | // Response. |
| 4202 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4203 | "Proxy-Connection: keep-alive\r\n" |
| 4204 | "Content-Length: 5\r\n\r\n" |
| 4205 | "hello"), |
| 4206 | // Response to second request. |
| 4207 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4208 | "Proxy-Connection: keep-alive\r\n" |
| 4209 | "Content-Length: 2\r\n\r\n" |
| 4210 | "hi"), |
| 4211 | }; |
| 4212 | |
| 4213 | StaticSocketDataProvider data(data_reads, data_writes); |
| 4214 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 4215 | |
| 4216 | MockWrite data_writes2[] = { |
| 4217 | // Initial request using a different NetworkIsolationKey includes the |
| 4218 | // cached proxy credentials, but not server credentials. |
| 4219 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4220 | "Host: myproxy:70\r\n" |
| 4221 | "Proxy-Connection: keep-alive\r\n" |
| 4222 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4223 | // Retry with proxy and new server auth credentials, which gets a |
| 4224 | // response. |
| 4225 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4226 | "Host: myproxy:70\r\n" |
| 4227 | "Proxy-Connection: keep-alive\r\n" |
| 4228 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 4229 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 4230 | }; |
| 4231 | |
| 4232 | MockRead data_reads2[] = { |
| 4233 | // Server auth challenge. |
| 4234 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4235 | "Proxy-Connection: keep-alive\r\n" |
| 4236 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4237 | "Content-Length: 0\r\n\r\n"), |
| 4238 | // Response. |
| 4239 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4240 | "Proxy-Connection: keep-alive\r\n" |
| 4241 | "Content-Length: 9\r\n\r\n" |
| 4242 | "greetings"), |
| 4243 | }; |
| 4244 | |
| 4245 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 4246 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 4247 | |
| 4248 | TestCompletionCallback callback; |
| 4249 | |
| 4250 | HttpRequestInfo request; |
| 4251 | request.method = "GET"; |
| 4252 | request.url = GURL("http://myproxy:70/"); |
| 4253 | request.traffic_annotation = |
| 4254 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 4255 | request.network_isolation_key = kNetworkIsolationKey1; |
| 4256 | |
| 4257 | auto trans = |
| 4258 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4259 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4260 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4261 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4262 | ASSERT_TRUE(response); |
| 4263 | ASSERT_TRUE(response->headers); |
| 4264 | EXPECT_EQ(407, response->headers->response_code()); |
| 4265 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
| 4266 | |
| 4267 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 4268 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4269 | response = trans->GetResponseInfo(); |
| 4270 | ASSERT_TRUE(response); |
| 4271 | EXPECT_EQ(401, response->headers->response_code()); |
| 4272 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4273 | EXPECT_EQ("http://myproxy:70", |
| 4274 | response->auth_challenge->challenger.Serialize()); |
| 4275 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4276 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4277 | |
| 4278 | rv = trans->RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 4279 | callback.callback()); |
| 4280 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4281 | response = trans->GetResponseInfo(); |
| 4282 | ASSERT_TRUE(response); |
| 4283 | EXPECT_EQ(200, response->headers->response_code()); |
| 4284 | // The password prompt info should not be set. |
| 4285 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4286 | std::string response_data; |
| 4287 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4288 | EXPECT_EQ("hello", response_data); |
| 4289 | |
| 4290 | // Check that the proxy credentials were cached correctly. The should be |
| 4291 | // accessible with any NetworkIsolationKey. |
| 4292 | HttpAuthCache::Entry* entry = session->http_auth_cache()->Lookup( |
| 4293 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4294 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4295 | ASSERT_TRUE(entry); |
| 4296 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4297 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 4298 | EXPECT_EQ(entry, |
| 4299 | session->http_auth_cache()->Lookup( |
| 4300 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4301 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4302 | |
| 4303 | // Check that the server credentials were cached correctly. The should be |
| 4304 | // accessible with only kNetworkIsolationKey1. |
| 4305 | entry = session->http_auth_cache()->Lookup( |
| 4306 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4307 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4308 | ASSERT_TRUE(entry); |
| 4309 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4310 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4311 | // Looking up the server entry with another NetworkIsolationKey should fail. |
| 4312 | EXPECT_FALSE(session->http_auth_cache()->Lookup( |
| 4313 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4314 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4315 | |
| 4316 | // Make another request with a different NetworkIsolationKey. It should use |
| 4317 | // another socket, reuse the cached proxy credentials, but result in a server |
| 4318 | // auth challenge. |
| 4319 | request.network_isolation_key = kNetworkIsolationKey2; |
| 4320 | trans = |
| 4321 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4322 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4323 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4324 | response = trans->GetResponseInfo(); |
| 4325 | ASSERT_TRUE(response); |
| 4326 | EXPECT_EQ(401, response->headers->response_code()); |
| 4327 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4328 | EXPECT_EQ("http://myproxy:70", |
| 4329 | response->auth_challenge->challenger.Serialize()); |
| 4330 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4331 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4332 | |
| 4333 | rv = trans->RestartWithAuth(AuthCredentials(kFoo3, kBar3), |
| 4334 | callback.callback()); |
| 4335 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4336 | response = trans->GetResponseInfo(); |
| 4337 | ASSERT_TRUE(response); |
| 4338 | EXPECT_EQ(200, response->headers->response_code()); |
| 4339 | // The password prompt info should not be set. |
| 4340 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4341 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4342 | EXPECT_EQ("greetings", response_data); |
| 4343 | |
| 4344 | // Check that the proxy credentials are still cached. |
| 4345 | entry = session->http_auth_cache()->Lookup( |
| 4346 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4347 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4348 | ASSERT_TRUE(entry); |
| 4349 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4350 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 4351 | EXPECT_EQ(entry, |
| 4352 | session->http_auth_cache()->Lookup( |
| 4353 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4354 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4355 | |
| 4356 | // Check that the correct server credentials are cached for each |
| 4357 | // NetworkIsolationKey. |
| 4358 | entry = session->http_auth_cache()->Lookup( |
| 4359 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4360 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4361 | ASSERT_TRUE(entry); |
| 4362 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4363 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4364 | entry = session->http_auth_cache()->Lookup( |
| 4365 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4366 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2); |
| 4367 | ASSERT_TRUE(entry); |
| 4368 | ASSERT_EQ(kFoo3, entry->credentials().username()); |
| 4369 | ASSERT_EQ(kBar3, entry->credentials().password()); |
| 4370 | |
| 4371 | // Make a request with the original NetworkIsolationKey. It should reuse the |
| 4372 | // first socket, and the proxy credentials sent on the first socket. |
| 4373 | request.network_isolation_key = kNetworkIsolationKey1; |
| 4374 | trans = |
| 4375 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4376 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4377 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4378 | response = trans->GetResponseInfo(); |
| 4379 | ASSERT_TRUE(response); |
| 4380 | EXPECT_EQ(200, response->headers->response_code()); |
| 4381 | // The password prompt info should not be set. |
| 4382 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4383 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4384 | EXPECT_EQ("hi", response_data); |
| 4385 | |
| 4386 | trans.reset(); |
| 4387 | session->CloseAllConnections(); |
| 4388 | } |
| 4389 | |
| 4390 | // Much like the test above, but uses tunnelled connections. |
| 4391 | TEST_F(HttpNetworkTransactionTest, |
| 4392 | BasicAuthProxyMatchesServerAuthWithNetworkIsolationKeyWithTunnel) { |
| 4393 | const url::Origin kOrigin1 = url::Origin::Create(GURL("https://foo.test/")); |
| 4394 | const net::NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 4395 | const url::Origin kOrigin2 = url::Origin::Create(GURL("https://bar.test/")); |
| 4396 | const net::NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
| 4397 | |
| 4398 | // This test would need to use a single socket without this option enabled. |
| 4399 | // Best to use this option when it would affect a test, as it will eventually |
| 4400 | // become the default behavior. |
| 4401 | base::test::ScopedFeatureList feature_list; |
| 4402 | feature_list.InitAndEnableFeature( |
| 4403 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 4404 | |
| 4405 | // Proxy matches request URL. |
| 4406 | session_deps_.proxy_resolution_service = |
| 4407 | ProxyResolutionService::CreateFixedFromPacResult( |
| 4408 | "HTTPS myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 4409 | BoundTestNetLog log; |
| 4410 | session_deps_.net_log = log.bound().net_log(); |
| 4411 | session_deps_.key_auth_cache_server_entries_by_network_isolation_key = true; |
| 4412 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 4413 | |
| 4414 | MockWrite data_writes[] = { |
| 4415 | // Initial tunnel request gets a proxy auth challenge. |
| 4416 | MockWrite("CONNECT myproxy:70 HTTP/1.1\r\n" |
| 4417 | "Host: myproxy:70\r\n" |
| 4418 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4419 | // Retry with proxy auth credentials, which will result in establishing a |
| 4420 | // tunnel. |
| 4421 | MockWrite("CONNECT myproxy:70 HTTP/1.1\r\n" |
| 4422 | "Host: myproxy:70\r\n" |
| 4423 | "Proxy-Connection: keep-alive\r\n" |
| 4424 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4425 | // Request over the tunnel, which gets a server auth challenge. |
| 4426 | MockWrite("GET / HTTP/1.1\r\n" |
| 4427 | "Host: myproxy:70\r\n" |
| 4428 | "Connection: keep-alive\r\n\r\n"), |
| 4429 | // Retry with server auth credentials, which gets a response. |
| 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 | // Another request to the same server and using the same NIK should |
| 4435 | // preemptively send the correct cached server |
| 4436 | // auth header. Since a tunnel was already established, the proxy headers |
| 4437 | // won't be sent again except when establishing another tunnel. |
| 4438 | MockWrite("GET / HTTP/1.1\r\n" |
| 4439 | "Host: myproxy:70\r\n" |
| 4440 | "Connection: keep-alive\r\n" |
| 4441 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4442 | }; |
| 4443 | |
| 4444 | MockRead data_reads[] = { |
| 4445 | // Proxy auth challenge. |
| 4446 | MockRead("HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 4447 | "Proxy-Connection: keep-alive\r\n" |
| 4448 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4449 | "Content-Length: 0\r\n\r\n"), |
| 4450 | // Tunnel success |
| 4451 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 4452 | // Server auth challenge. |
| 4453 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4454 | "Connection: keep-alive\r\n" |
| 4455 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4456 | "Content-Length: 0\r\n\r\n"), |
| 4457 | // Response. |
| 4458 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4459 | "Connection: keep-alive\r\n" |
| 4460 | "Content-Length: 5\r\n\r\n" |
| 4461 | "hello"), |
| 4462 | // Response to second request. |
| 4463 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4464 | "Connection: keep-alive\r\n" |
| 4465 | "Content-Length: 2\r\n\r\n" |
| 4466 | "hi"), |
| 4467 | }; |
| 4468 | |
| 4469 | StaticSocketDataProvider data(data_reads, data_writes); |
| 4470 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 4471 | // One for the proxy connection, one of the server connection. |
| 4472 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 4473 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 4474 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 4475 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 4476 | |
| 4477 | MockWrite data_writes2[] = { |
| 4478 | // Initial request using a different NetworkIsolationKey includes the |
| 4479 | // cached proxy credentials when establishing a tunnel. |
| 4480 | MockWrite("CONNECT myproxy:70 HTTP/1.1\r\n" |
| 4481 | "Host: myproxy:70\r\n" |
| 4482 | "Proxy-Connection: keep-alive\r\n" |
| 4483 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4484 | // Request over the tunnel, which gets a server auth challenge. Cached |
| 4485 | // credentials cannot be used, since the NIK is different. |
| 4486 | MockWrite("GET / HTTP/1.1\r\n" |
| 4487 | "Host: myproxy:70\r\n" |
| 4488 | "Connection: keep-alive\r\n\r\n"), |
| 4489 | // Retry with server auth credentials, which gets a response. |
| 4490 | MockWrite("GET / HTTP/1.1\r\n" |
| 4491 | "Host: myproxy:70\r\n" |
| 4492 | "Connection: keep-alive\r\n" |
| 4493 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 4494 | }; |
| 4495 | |
| 4496 | MockRead data_reads2[] = { |
| 4497 | // Tunnel success |
| 4498 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 4499 | // Server auth challenge. |
| 4500 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4501 | "Connection: keep-alive\r\n" |
| 4502 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4503 | "Content-Length: 0\r\n\r\n"), |
| 4504 | // Response. |
| 4505 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4506 | "Connection: keep-alive\r\n" |
| 4507 | "Content-Length: 9\r\n\r\n" |
| 4508 | "greetings"), |
| 4509 | }; |
| 4510 | |
| 4511 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 4512 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 4513 | // One for the proxy connection, one of the server connection. |
| 4514 | SSLSocketDataProvider ssl3(ASYNC, OK); |
| 4515 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
| 4516 | SSLSocketDataProvider ssl4(ASYNC, OK); |
| 4517 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl4); |
| 4518 | |
| 4519 | TestCompletionCallback callback; |
| 4520 | |
| 4521 | HttpRequestInfo request; |
| 4522 | request.method = "GET"; |
| 4523 | request.url = GURL("https://myproxy:70/"); |
| 4524 | request.traffic_annotation = |
| 4525 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 4526 | request.network_isolation_key = kNetworkIsolationKey1; |
| 4527 | |
| 4528 | auto trans = |
| 4529 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4530 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4531 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4532 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4533 | ASSERT_TRUE(response); |
| 4534 | ASSERT_TRUE(response->headers); |
| 4535 | EXPECT_EQ(407, response->headers->response_code()); |
| 4536 | EXPECT_TRUE(CheckBasicSecureProxyAuth(response->auth_challenge)); |
| 4537 | |
| 4538 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 4539 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4540 | response = trans->GetResponseInfo(); |
| 4541 | ASSERT_TRUE(response); |
| 4542 | EXPECT_EQ(401, response->headers->response_code()); |
| 4543 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4544 | EXPECT_EQ("https://myproxy:70", |
| 4545 | response->auth_challenge->challenger.Serialize()); |
| 4546 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4547 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4548 | |
| 4549 | rv = trans->RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 4550 | callback.callback()); |
| 4551 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4552 | response = trans->GetResponseInfo(); |
| 4553 | ASSERT_TRUE(response); |
| 4554 | EXPECT_EQ(200, response->headers->response_code()); |
| 4555 | // The password prompt info should not be set. |
| 4556 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4557 | std::string response_data; |
| 4558 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4559 | EXPECT_EQ("hello", response_data); |
| 4560 | |
| 4561 | // Check that the proxy credentials were cached correctly. The should be |
| 4562 | // accessible with any NetworkIsolationKey. |
| 4563 | HttpAuthCache::Entry* entry = session->http_auth_cache()->Lookup( |
| 4564 | GURL("https://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4565 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4566 | ASSERT_TRUE(entry); |
| 4567 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4568 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 4569 | EXPECT_EQ(entry, |
| 4570 | session->http_auth_cache()->Lookup( |
| 4571 | GURL("https://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4572 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4573 | |
| 4574 | // Check that the server credentials were cached correctly. The should be |
| 4575 | // accessible with only kNetworkIsolationKey1. |
| 4576 | entry = session->http_auth_cache()->Lookup( |
| 4577 | GURL("https://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4578 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4579 | ASSERT_TRUE(entry); |
| 4580 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4581 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4582 | // Looking up the server entry with another NetworkIsolationKey should fail. |
| 4583 | EXPECT_FALSE(session->http_auth_cache()->Lookup( |
| 4584 | GURL("https://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4585 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4586 | |
| 4587 | // Make another request with a different NetworkIsolationKey. It should use |
| 4588 | // another socket, reuse the cached proxy credentials, but result in a server |
| 4589 | // auth challenge. |
| 4590 | request.network_isolation_key = kNetworkIsolationKey2; |
| 4591 | trans = |
| 4592 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4593 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4594 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4595 | response = trans->GetResponseInfo(); |
| 4596 | ASSERT_TRUE(response); |
| 4597 | EXPECT_EQ(401, response->headers->response_code()); |
| 4598 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4599 | EXPECT_EQ("https://myproxy:70", |
| 4600 | response->auth_challenge->challenger.Serialize()); |
| 4601 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4602 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4603 | |
| 4604 | rv = trans->RestartWithAuth(AuthCredentials(kFoo3, kBar3), |
| 4605 | callback.callback()); |
| 4606 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4607 | response = trans->GetResponseInfo(); |
| 4608 | ASSERT_TRUE(response); |
| 4609 | EXPECT_EQ(200, response->headers->response_code()); |
| 4610 | // The password prompt info should not be set. |
| 4611 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4612 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4613 | EXPECT_EQ("greetings", response_data); |
| 4614 | |
| 4615 | // Check that the proxy credentials are still cached. |
| 4616 | entry = session->http_auth_cache()->Lookup( |
| 4617 | GURL("https://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4618 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4619 | ASSERT_TRUE(entry); |
| 4620 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4621 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 4622 | EXPECT_EQ(entry, |
| 4623 | session->http_auth_cache()->Lookup( |
| 4624 | GURL("https://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4625 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4626 | |
| 4627 | // Check that the correct server credentials are cached for each |
| 4628 | // NetworkIsolationKey. |
| 4629 | entry = session->http_auth_cache()->Lookup( |
| 4630 | GURL("https://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4631 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4632 | ASSERT_TRUE(entry); |
| 4633 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4634 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4635 | entry = session->http_auth_cache()->Lookup( |
| 4636 | GURL("https://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4637 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2); |
| 4638 | ASSERT_TRUE(entry); |
| 4639 | ASSERT_EQ(kFoo3, entry->credentials().username()); |
| 4640 | ASSERT_EQ(kBar3, entry->credentials().password()); |
| 4641 | |
| 4642 | // Make a request with the original NetworkIsolationKey. It should reuse the |
| 4643 | // first socket, and the proxy credentials sent on the first socket. |
| 4644 | request.network_isolation_key = kNetworkIsolationKey1; |
| 4645 | trans = |
| 4646 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4647 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4648 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4649 | response = trans->GetResponseInfo(); |
| 4650 | ASSERT_TRUE(response); |
| 4651 | EXPECT_EQ(200, response->headers->response_code()); |
| 4652 | // The password prompt info should not be set. |
| 4653 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4654 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4655 | EXPECT_EQ("hi", response_data); |
| 4656 | |
| 4657 | trans.reset(); |
| 4658 | session->CloseAllConnections(); |
| 4659 | } |
| 4660 | |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4661 | // Test that we don't pass extraneous headers from the proxy's response to the |
| 4662 | // caller when the proxy responds to CONNECT with 407. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4663 | TEST_F(HttpNetworkTransactionTest, SanitizeProxyAuthHeaders) { |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4664 | HttpRequestInfo request; |
| 4665 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 4666 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 4667 | request.traffic_annotation = |
| 4668 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4669 | |
| 4670 | // Configure against proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 4671 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 4672 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4673 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4674 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4675 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4676 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4677 | |
| 4678 | // Since we have proxy, should try to establish tunnel. |
| 4679 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4680 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 4681 | "Host: www.example.org:443\r\n" |
| 4682 | "Proxy-Connection: keep-alive\r\n\r\n"), |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4683 | }; |
| 4684 | |
| 4685 | // The proxy responds to the connect with a 407. |
| 4686 | MockRead data_reads[] = { |
| 4687 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4688 | MockRead("X-Foo: bar\r\n"), |
| 4689 | MockRead("Set-Cookie: foo=bar\r\n"), |
| 4690 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4691 | MockRead("Content-Length: 10\r\n\r\n"), |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 4692 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4693 | }; |
| 4694 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4695 | StaticSocketDataProvider data(data_reads, data_writes); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4696 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 4697 | |
| 4698 | TestCompletionCallback callback; |
| 4699 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 4700 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4701 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4702 | |
| 4703 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4704 | EXPECT_THAT(rv, IsOk()); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4705 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4706 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4707 | ASSERT_TRUE(response); |
| 4708 | ASSERT_TRUE(response->headers); |
| 4709 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 4710 | EXPECT_EQ(407, response->headers->response_code()); |
| 4711 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4712 | EXPECT_FALSE(response->headers->HasHeader("X-Foo")); |
| 4713 | EXPECT_FALSE(response->headers->HasHeader("Set-Cookie")); |
| 4714 | |
| 4715 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4716 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4717 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4718 | |
| 4719 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
| 4720 | session->CloseAllConnections(); |
| 4721 | } |
| 4722 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4723 | // Test when a server (non-proxy) returns a 407 (proxy-authenticate). |
| 4724 | // The request should fail with ERR_UNEXPECTED_PROXY_AUTH. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4725 | TEST_F(HttpNetworkTransactionTest, UnexpectedProxyAuth) { |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4726 | HttpRequestInfo request; |
| 4727 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 4728 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 4729 | request.traffic_annotation = |
| 4730 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4731 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4732 | // We are using a DIRECT connection (i.e. no proxy) for this session. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4733 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4734 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4735 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4736 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 4737 | MockWrite( |
| 4738 | "GET / HTTP/1.1\r\n" |
| 4739 | "Host: www.example.org\r\n" |
| 4740 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4741 | }; |
| 4742 | |
| 4743 | MockRead data_reads1[] = { |
| 4744 | MockRead("HTTP/1.0 407 Proxy Auth required\r\n"), |
| 4745 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4746 | // Large content-length -- won't matter, as connection will be reset. |
| 4747 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 4748 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4749 | }; |
| 4750 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4751 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 4752 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4753 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4754 | TestCompletionCallback callback; |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4755 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 4756 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4757 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4758 | |
| 4759 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4760 | EXPECT_THAT(rv, IsError(ERR_UNEXPECTED_PROXY_AUTH)); |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4761 | } |
| 4762 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4763 | // Tests when an HTTPS server (non-proxy) returns a 407 (proxy-authentication) |
| 4764 | // through a non-authenticating proxy. The request should fail with |
| 4765 | // ERR_UNEXPECTED_PROXY_AUTH. |
| 4766 | // Note that it is impossible to detect if an HTTP server returns a 407 through |
| 4767 | // a non-authenticating proxy - there is nothing to indicate whether the |
| 4768 | // response came from the proxy or the server, so it is treated as if the proxy |
| 4769 | // issued the challenge. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4770 | TEST_F(HttpNetworkTransactionTest, HttpsServerRequestsProxyAuthThroughProxy) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4771 | HttpRequestInfo request; |
| 4772 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 4773 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 4774 | request.traffic_annotation = |
| 4775 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4776 | |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 4777 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 4778 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4779 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 4780 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4781 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4782 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4783 | // Since we have proxy, should try to establish tunnel. |
| 4784 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4785 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 4786 | "Host: www.example.org:443\r\n" |
| 4787 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4788 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4789 | MockWrite("GET / HTTP/1.1\r\n" |
| 4790 | "Host: www.example.org\r\n" |
| 4791 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4792 | }; |
| 4793 | |
| 4794 | MockRead data_reads1[] = { |
| 4795 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 4796 | |
| 4797 | MockRead("HTTP/1.1 407 Unauthorized\r\n"), |
| 4798 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4799 | MockRead("\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 4800 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4801 | }; |
| 4802 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4803 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 4804 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 4805 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 4806 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4807 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4808 | TestCompletionCallback callback1; |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4809 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4810 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4811 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4812 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4813 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4814 | |
| 4815 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4816 | EXPECT_THAT(rv, IsError(ERR_UNEXPECTED_PROXY_AUTH)); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 4817 | auto entries = log.GetEntries(); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4818 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 4819 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 4820 | NetLogEventPhase::NONE); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4821 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 4822 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 4823 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 4824 | NetLogEventPhase::NONE); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4825 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4826 | |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4827 | // Test a proxy auth scheme that allows default credentials and a proxy server |
| 4828 | // that uses non-persistent connections. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4829 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4830 | AuthAllowsDefaultCredentialsTunnelConnectionClose) { |
| 4831 | HttpRequestInfo request; |
| 4832 | request.method = "GET"; |
| 4833 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 4834 | request.traffic_annotation = |
| 4835 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4836 | |
| 4837 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 4838 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 4839 | ProxyResolutionService::CreateFixedFromPacResult( |
| 4840 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4841 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4842 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4843 | auth_handler_factory->set_do_init_from_challenge(true); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4844 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4845 | mock_handler->set_allows_default_credentials(true); |
| 4846 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 4847 | HttpAuth::AUTH_PROXY); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4848 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4849 | |
| 4850 | // Add NetLog just so can verify load timing information gets a NetLog ID. |
| 4851 | NetLog net_log; |
| 4852 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4853 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4854 | |
| 4855 | // Since we have proxy, should try to establish tunnel. |
| 4856 | MockWrite data_writes1[] = { |
| 4857 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4858 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4859 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4860 | }; |
| 4861 | |
| 4862 | // The proxy responds to the connect with a 407, using a non-persistent |
| 4863 | // connection. |
| 4864 | MockRead data_reads1[] = { |
| 4865 | // No credentials. |
| 4866 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4867 | MockRead("Proxy-Authenticate: Mock\r\n"), |
| 4868 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 4869 | }; |
| 4870 | |
| 4871 | // Since the first connection couldn't be reused, need to establish another |
| 4872 | // once given credentials. |
| 4873 | MockWrite data_writes2[] = { |
| 4874 | // After calling trans->RestartWithAuth(), this is the request we should |
| 4875 | // be issuing -- the final header line contains the credentials. |
| 4876 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4877 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4878 | "Proxy-Connection: keep-alive\r\n" |
| 4879 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 4880 | |
| 4881 | MockWrite("GET / HTTP/1.1\r\n" |
| 4882 | "Host: www.example.org\r\n" |
| 4883 | "Connection: keep-alive\r\n\r\n"), |
| 4884 | }; |
| 4885 | |
| 4886 | MockRead data_reads2[] = { |
| 4887 | MockRead("HTTP/1.0 200 Connection Established\r\n\r\n"), |
| 4888 | |
| 4889 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 4890 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4891 | MockRead("Content-Length: 5\r\n\r\n"), |
| 4892 | MockRead(SYNCHRONOUS, "hello"), |
| 4893 | }; |
| 4894 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4895 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4896 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4897 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4898 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 4899 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 4900 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 4901 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 4902 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4903 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4904 | |
| 4905 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 4906 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4907 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4908 | |
| 4909 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4910 | ASSERT_TRUE(response); |
| 4911 | ASSERT_TRUE(response->headers); |
| 4912 | EXPECT_FALSE(response->headers->IsKeepAlive()); |
| 4913 | EXPECT_EQ(407, response->headers->response_code()); |
| 4914 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4915 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 4916 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4917 | |
| 4918 | LoadTimingInfo load_timing_info; |
| 4919 | // CONNECT requests and responses are handled at the connect job level, so |
| 4920 | // the transaction does not yet have a connection. |
| 4921 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 4922 | |
| 4923 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4924 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4925 | response = trans->GetResponseInfo(); |
| 4926 | ASSERT_TRUE(response); |
| 4927 | ASSERT_TRUE(response->headers); |
| 4928 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 4929 | EXPECT_EQ(200, response->headers->response_code()); |
| 4930 | EXPECT_EQ(5, response->headers->GetContentLength()); |
| 4931 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4932 | |
| 4933 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 4934 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4935 | |
| 4936 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 4937 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 4938 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 4939 | |
| 4940 | trans.reset(); |
| 4941 | session->CloseAllConnections(); |
| 4942 | } |
| 4943 | |
| 4944 | // Test a proxy auth scheme that allows default credentials and a proxy server |
| 4945 | // that hangs up when credentials are initially sent. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4946 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4947 | AuthAllowsDefaultCredentialsTunnelServerClosesConnection) { |
| 4948 | HttpRequestInfo request; |
| 4949 | request.method = "GET"; |
| 4950 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 4951 | request.traffic_annotation = |
| 4952 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4953 | |
| 4954 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 4955 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 4956 | ProxyResolutionService::CreateFixedFromPacResult( |
| 4957 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4958 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4959 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4960 | auth_handler_factory->set_do_init_from_challenge(true); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4961 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4962 | mock_handler->set_allows_default_credentials(true); |
| 4963 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 4964 | HttpAuth::AUTH_PROXY); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4965 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4966 | |
| 4967 | // Add NetLog just so can verify load timing information gets a NetLog ID. |
| 4968 | NetLog net_log; |
| 4969 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4970 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4971 | |
| 4972 | // Should try to establish tunnel. |
| 4973 | MockWrite data_writes1[] = { |
| 4974 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4975 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4976 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4977 | |
| 4978 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4979 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4980 | "Proxy-Connection: keep-alive\r\n" |
| 4981 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 4982 | }; |
| 4983 | |
| 4984 | // The proxy responds to the connect with a 407, using a non-persistent |
| 4985 | // connection. |
| 4986 | MockRead data_reads1[] = { |
| 4987 | // No credentials. |
| 4988 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4989 | MockRead("Proxy-Authenticate: Mock\r\n\r\n"), |
| 4990 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED), |
| 4991 | }; |
| 4992 | |
| 4993 | // Since the first connection was closed, need to establish another once given |
| 4994 | // credentials. |
| 4995 | MockWrite data_writes2[] = { |
| 4996 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4997 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4998 | "Proxy-Connection: keep-alive\r\n" |
| 4999 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 5000 | |
| 5001 | MockWrite("GET / HTTP/1.1\r\n" |
| 5002 | "Host: www.example.org\r\n" |
| 5003 | "Connection: keep-alive\r\n\r\n"), |
| 5004 | }; |
| 5005 | |
| 5006 | MockRead data_reads2[] = { |
| 5007 | MockRead("HTTP/1.0 200 Connection Established\r\n\r\n"), |
| 5008 | |
| 5009 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5010 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5011 | MockRead("Content-Length: 5\r\n\r\n"), |
| 5012 | MockRead(SYNCHRONOUS, "hello"), |
| 5013 | }; |
| 5014 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5015 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5016 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5017 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5018 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5019 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 5020 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 5021 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5022 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5023 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5024 | |
| 5025 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 5026 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5027 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5028 | |
| 5029 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5030 | ASSERT_TRUE(response); |
| 5031 | ASSERT_TRUE(response->headers); |
| 5032 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 5033 | EXPECT_EQ(407, response->headers->response_code()); |
| 5034 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5035 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5036 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5037 | |
| 5038 | LoadTimingInfo load_timing_info; |
| 5039 | // CONNECT requests and responses are handled at the connect job level, so |
| 5040 | // the transaction does not yet have a connection. |
| 5041 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 5042 | |
| 5043 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5044 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5045 | |
| 5046 | response = trans->GetResponseInfo(); |
| 5047 | ASSERT_TRUE(response); |
| 5048 | ASSERT_TRUE(response->headers); |
| 5049 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 5050 | EXPECT_EQ(200, response->headers->response_code()); |
| 5051 | EXPECT_EQ(5, response->headers->GetContentLength()); |
| 5052 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5053 | |
| 5054 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5055 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5056 | |
| 5057 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 5058 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 5059 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 5060 | |
| 5061 | trans.reset(); |
| 5062 | session->CloseAllConnections(); |
| 5063 | } |
| 5064 | |
| 5065 | // Test a proxy auth scheme that allows default credentials and a proxy server |
| 5066 | // that hangs up when credentials are initially sent, and hangs up again when |
| 5067 | // they are retried. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 5068 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5069 | AuthAllowsDefaultCredentialsTunnelServerClosesConnectionTwice) { |
| 5070 | HttpRequestInfo request; |
| 5071 | request.method = "GET"; |
| 5072 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 5073 | request.traffic_annotation = |
| 5074 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5075 | |
| 5076 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 5077 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5078 | ProxyResolutionService::CreateFixedFromPacResult( |
| 5079 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5080 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5081 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5082 | auth_handler_factory->set_do_init_from_challenge(true); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5083 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5084 | mock_handler->set_allows_default_credentials(true); |
| 5085 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5086 | HttpAuth::AUTH_PROXY); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5087 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5088 | |
| 5089 | // Add NetLog just so can verify load timing information gets a NetLog ID. |
| 5090 | NetLog net_log; |
| 5091 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 5092 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5093 | |
| 5094 | // Should try to establish tunnel. |
| 5095 | MockWrite data_writes1[] = { |
| 5096 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5097 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5098 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5099 | |
| 5100 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5101 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5102 | "Proxy-Connection: keep-alive\r\n" |
| 5103 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 5104 | }; |
| 5105 | |
| 5106 | // The proxy responds to the connect with a 407, and then hangs up after the |
| 5107 | // second request is sent. |
| 5108 | MockRead data_reads1[] = { |
| 5109 | // No credentials. |
| 5110 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5111 | MockRead("Content-Length: 0\r\n"), |
| 5112 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 5113 | MockRead("Proxy-Authenticate: Mock\r\n\r\n"), |
| 5114 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED), |
| 5115 | }; |
| 5116 | |
| 5117 | // HttpNetworkTransaction sees a reused connection that was closed with |
| 5118 | // ERR_CONNECTION_CLOSED, realized it might have been a race, so retries the |
| 5119 | // request. |
| 5120 | MockWrite data_writes2[] = { |
| 5121 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5122 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5123 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5124 | }; |
| 5125 | |
| 5126 | // The proxy, having had more than enough of us, just hangs up. |
| 5127 | MockRead data_reads2[] = { |
| 5128 | // No credentials. |
| 5129 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED), |
| 5130 | }; |
| 5131 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5132 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5133 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5134 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5135 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5136 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5137 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5138 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5139 | |
| 5140 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 5141 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5142 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5143 | |
| 5144 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5145 | ASSERT_TRUE(response); |
| 5146 | ASSERT_TRUE(response->headers); |
| 5147 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 5148 | EXPECT_EQ(407, response->headers->response_code()); |
| 5149 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5150 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5151 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5152 | |
| 5153 | LoadTimingInfo load_timing_info; |
| 5154 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 5155 | |
| 5156 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5157 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_EMPTY_RESPONSE)); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5158 | |
| 5159 | trans.reset(); |
| 5160 | session->CloseAllConnections(); |
| 5161 | } |
| 5162 | |
Asanka Herath | bc3f8f6 | 2018-11-16 23:08:30 | [diff] [blame] | 5163 | // This test exercises an odd edge case where the proxy closes the connection |
| 5164 | // after the authentication handshake is complete. Presumably this technique is |
| 5165 | // used in lieu of returning a 403 or 5xx status code when the authentication |
| 5166 | // succeeds, but the user is not authorized to connect to the destination |
| 5167 | // server. There's no standard for what a proxy should do to indicate a blocked |
| 5168 | // site. |
| 5169 | TEST_F(HttpNetworkTransactionTest, |
| 5170 | AuthAllowsDefaultCredentialsTunnelConnectionClosesBeforeBody) { |
| 5171 | HttpRequestInfo request; |
| 5172 | request.method = "GET"; |
| 5173 | request.url = GURL("https://www.example.org/"); |
| 5174 | request.traffic_annotation = |
| 5175 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 5176 | |
| 5177 | // Configure against proxy server "myproxy:70". |
| 5178 | session_deps_.proxy_resolution_service = |
| 5179 | ProxyResolutionService::CreateFixedFromPacResult( |
| 5180 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 5181 | |
Asanka Herath | bc3f8f6 | 2018-11-16 23:08:30 | [diff] [blame] | 5182 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
| 5183 | auth_handler_factory->set_do_init_from_challenge(true); |
| 5184 | |
| 5185 | // Create two mock AuthHandlers. This is because the transaction gets retried |
| 5186 | // after the first ERR_CONNECTION_CLOSED since it's ambiguous whether there |
| 5187 | // was a real network error. |
| 5188 | // |
| 5189 | // The handlers support both default and explicit credentials. The retry |
| 5190 | // mentioned above should be able to reuse the default identity. Thus there |
| 5191 | // should never be a need to prompt for explicit credentials. |
| 5192 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
| 5193 | mock_handler->set_allows_default_credentials(true); |
| 5194 | mock_handler->set_allows_explicit_credentials(true); |
| 5195 | mock_handler->set_connection_based(true); |
| 5196 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5197 | HttpAuth::AUTH_PROXY); |
| 5198 | mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
| 5199 | mock_handler->set_allows_default_credentials(true); |
| 5200 | mock_handler->set_allows_explicit_credentials(true); |
| 5201 | mock_handler->set_connection_based(true); |
| 5202 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5203 | HttpAuth::AUTH_PROXY); |
| 5204 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
| 5205 | |
| 5206 | NetLog net_log; |
| 5207 | session_deps_.net_log = &net_log; |
| 5208 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 5209 | |
| 5210 | // Data for both sockets. |
| 5211 | // |
| 5212 | // Writes are for the tunnel establishment attempts and the |
| 5213 | // authentication handshake. |
| 5214 | MockWrite data_writes1[] = { |
| 5215 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5216 | "Host: www.example.org:443\r\n" |
| 5217 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5218 | |
| 5219 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5220 | "Host: www.example.org:443\r\n" |
| 5221 | "Proxy-Connection: keep-alive\r\n" |
| 5222 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 5223 | |
| 5224 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5225 | "Host: www.example.org:443\r\n" |
| 5226 | "Proxy-Connection: keep-alive\r\n" |
| 5227 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 5228 | }; |
| 5229 | |
| 5230 | // The server side of the authentication handshake. Note that the response to |
| 5231 | // the final CONNECT request is ERR_CONNECTION_CLOSED. |
| 5232 | MockRead data_reads1[] = { |
| 5233 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5234 | MockRead("Content-Length: 0\r\n"), |
| 5235 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 5236 | MockRead("Proxy-Authenticate: Mock\r\n\r\n"), |
| 5237 | |
| 5238 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5239 | MockRead("Content-Length: 0\r\n"), |
| 5240 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 5241 | MockRead("Proxy-Authenticate: Mock foo\r\n\r\n"), |
| 5242 | |
| 5243 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED), |
| 5244 | }; |
| 5245 | |
| 5246 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 5247 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 5248 | |
| 5249 | // The second socket is for the reconnection attempt. Data is identical to the |
| 5250 | // first attempt. |
| 5251 | StaticSocketDataProvider data2(data_reads1, data_writes1); |
| 5252 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5253 | |
| 5254 | auto trans = |
| 5255 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 5256 | |
| 5257 | TestCompletionCallback callback; |
| 5258 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 5259 | |
| 5260 | // Two rounds per handshake. After one retry, the error is propagated up the |
| 5261 | // stack. |
| 5262 | for (int i = 0; i < 4; ++i) { |
| 5263 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5264 | |
| 5265 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5266 | ASSERT_TRUE(response); |
| 5267 | ASSERT_TRUE(response->headers); |
| 5268 | EXPECT_EQ(407, response->headers->response_code()); |
| 5269 | ASSERT_TRUE(trans->IsReadyToRestartForAuth()); |
| 5270 | |
| 5271 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
| 5272 | } |
| 5273 | |
| 5274 | // One shall be the number thou shalt retry, and the number of the retrying |
| 5275 | // shall be one. Two shalt thou not retry, neither retry thou zero, excepting |
| 5276 | // that thou then proceed to one. Three is right out. Once the number one, |
| 5277 | // being the first number, be reached, then lobbest thou thy |
| 5278 | // ERR_CONNECTION_CLOSED towards they network transaction, who shall snuff it. |
| 5279 | EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.GetResult(rv)); |
| 5280 | |
| 5281 | trans.reset(); |
| 5282 | session->CloseAllConnections(); |
| 5283 | } |
| 5284 | |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5285 | // Test a proxy auth scheme that allows default credentials and a proxy server |
| 5286 | // that hangs up when credentials are initially sent, and sends a challenge |
| 5287 | // again they are retried. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 5288 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5289 | AuthAllowsDefaultCredentialsTunnelServerChallengesTwice) { |
| 5290 | HttpRequestInfo request; |
| 5291 | request.method = "GET"; |
| 5292 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 5293 | request.traffic_annotation = |
| 5294 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5295 | |
| 5296 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 5297 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5298 | ProxyResolutionService::CreateFixedFromPacResult( |
| 5299 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5300 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5301 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5302 | auth_handler_factory->set_do_init_from_challenge(true); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5303 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5304 | mock_handler->set_allows_default_credentials(true); |
| 5305 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5306 | HttpAuth::AUTH_PROXY); |
| 5307 | // Add another handler for the second challenge. It supports default |
| 5308 | // credentials, but they shouldn't be used, since they were already tried. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5309 | mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5310 | mock_handler->set_allows_default_credentials(true); |
| 5311 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5312 | HttpAuth::AUTH_PROXY); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5313 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5314 | |
| 5315 | // Add NetLog just so can verify load timing information gets a NetLog ID. |
| 5316 | NetLog net_log; |
| 5317 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 5318 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5319 | |
| 5320 | // Should try to establish tunnel. |
| 5321 | MockWrite data_writes1[] = { |
| 5322 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5323 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5324 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5325 | }; |
| 5326 | |
| 5327 | // The proxy responds to the connect with a 407, using a non-persistent |
| 5328 | // connection. |
| 5329 | MockRead data_reads1[] = { |
| 5330 | // No credentials. |
| 5331 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5332 | MockRead("Proxy-Authenticate: Mock\r\n"), |
| 5333 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 5334 | }; |
| 5335 | |
| 5336 | // Since the first connection was closed, need to establish another once given |
| 5337 | // credentials. |
| 5338 | MockWrite data_writes2[] = { |
| 5339 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5340 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5341 | "Proxy-Connection: keep-alive\r\n" |
| 5342 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 5343 | }; |
| 5344 | |
| 5345 | MockRead data_reads2[] = { |
| 5346 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5347 | MockRead("Proxy-Authenticate: Mock\r\n"), |
| 5348 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 5349 | }; |
| 5350 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5351 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5352 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5353 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5354 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5355 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 5356 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 5357 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5358 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5359 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5360 | |
| 5361 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 5362 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5363 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5364 | |
| 5365 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5366 | ASSERT_TRUE(response); |
| 5367 | ASSERT_TRUE(response->headers); |
| 5368 | EXPECT_EQ(407, response->headers->response_code()); |
| 5369 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5370 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5371 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5372 | |
| 5373 | LoadTimingInfo load_timing_info; |
| 5374 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 5375 | |
| 5376 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5377 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5378 | response = trans->GetResponseInfo(); |
| 5379 | ASSERT_TRUE(response); |
| 5380 | ASSERT_TRUE(response->headers); |
| 5381 | EXPECT_EQ(407, response->headers->response_code()); |
| 5382 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5383 | EXPECT_TRUE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5384 | |
| 5385 | trans.reset(); |
| 5386 | session->CloseAllConnections(); |
| 5387 | } |
| 5388 | |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5389 | // A more nuanced test than GenerateAuthToken test which asserts that |
| 5390 | // ERR_INVALID_AUTH_CREDENTIALS does not cause the auth scheme to be |
| 5391 | // unnecessarily invalidated, and that if the server co-operates, the |
| 5392 | // authentication handshake can continue with the same scheme but with a |
| 5393 | // different identity. |
| 5394 | TEST_F(HttpNetworkTransactionTest, NonPermanentGenerateAuthTokenError) { |
| 5395 | HttpRequestInfo request; |
| 5396 | request.method = "GET"; |
| 5397 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 5398 | request.traffic_annotation = |
| 5399 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5400 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5401 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5402 | auth_handler_factory->set_do_init_from_challenge(true); |
| 5403 | |
| 5404 | // First handler. Uses default credentials, but barfs at generate auth token. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5405 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5406 | mock_handler->set_allows_default_credentials(true); |
| 5407 | mock_handler->set_allows_explicit_credentials(true); |
| 5408 | mock_handler->set_connection_based(true); |
| 5409 | mock_handler->SetGenerateExpectation(true, ERR_INVALID_AUTH_CREDENTIALS); |
| 5410 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5411 | HttpAuth::AUTH_SERVER); |
| 5412 | |
| 5413 | // Add another handler for the second challenge. It supports default |
| 5414 | // credentials, but they shouldn't be used, since they were already tried. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5415 | mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5416 | mock_handler->set_allows_default_credentials(true); |
| 5417 | mock_handler->set_allows_explicit_credentials(true); |
| 5418 | mock_handler->set_connection_based(true); |
| 5419 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5420 | HttpAuth::AUTH_SERVER); |
| 5421 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
| 5422 | |
| 5423 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 5424 | |
| 5425 | MockWrite data_writes1[] = { |
| 5426 | MockWrite("GET / HTTP/1.1\r\n" |
| 5427 | "Host: www.example.org\r\n" |
| 5428 | "Connection: keep-alive\r\n\r\n"), |
| 5429 | }; |
| 5430 | |
| 5431 | MockRead data_reads1[] = { |
| 5432 | MockRead("HTTP/1.1 401 Authentication Required\r\n" |
| 5433 | "WWW-Authenticate: Mock\r\n" |
| 5434 | "Connection: keep-alive\r\n\r\n"), |
| 5435 | }; |
| 5436 | |
| 5437 | // Identical to data_writes1[]. The AuthHandler encounters a |
| 5438 | // ERR_INVALID_AUTH_CREDENTIALS during the GenerateAuthToken stage, so the |
| 5439 | // transaction procceds without an authorization header. |
| 5440 | MockWrite data_writes2[] = { |
| 5441 | MockWrite("GET / HTTP/1.1\r\n" |
| 5442 | "Host: www.example.org\r\n" |
| 5443 | "Connection: keep-alive\r\n\r\n"), |
| 5444 | }; |
| 5445 | |
| 5446 | MockRead data_reads2[] = { |
| 5447 | MockRead("HTTP/1.1 401 Authentication Required\r\n" |
| 5448 | "WWW-Authenticate: Mock\r\n" |
| 5449 | "Connection: keep-alive\r\n\r\n"), |
| 5450 | }; |
| 5451 | |
| 5452 | MockWrite data_writes3[] = { |
| 5453 | MockWrite("GET / HTTP/1.1\r\n" |
| 5454 | "Host: www.example.org\r\n" |
| 5455 | "Connection: keep-alive\r\n" |
| 5456 | "Authorization: auth_token\r\n\r\n"), |
| 5457 | }; |
| 5458 | |
| 5459 | MockRead data_reads3[] = { |
| 5460 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5461 | "Content-Length: 5\r\n" |
| 5462 | "Content-Type: text/plain\r\n" |
| 5463 | "Connection: keep-alive\r\n\r\n" |
| 5464 | "Hello"), |
| 5465 | }; |
| 5466 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5467 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5468 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 5469 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5470 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5471 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5472 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5473 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5474 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 5475 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5476 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5477 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5478 | |
| 5479 | TestCompletionCallback callback; |
| 5480 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 5481 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5482 | |
| 5483 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5484 | ASSERT_TRUE(response); |
| 5485 | ASSERT_TRUE(response->headers); |
| 5486 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5487 | |
| 5488 | // The following three tests assert that an authentication challenge was |
| 5489 | // received and that the stack is ready to respond to the challenge using |
| 5490 | // ambient credentials. |
| 5491 | EXPECT_EQ(401, response->headers->response_code()); |
| 5492 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5493 | EXPECT_FALSE(response->auth_challenge.has_value()); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5494 | |
| 5495 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
| 5496 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5497 | response = trans->GetResponseInfo(); |
| 5498 | ASSERT_TRUE(response); |
| 5499 | ASSERT_TRUE(response->headers); |
| 5500 | |
| 5501 | // The following three tests assert that an authentication challenge was |
| 5502 | // received and that the stack needs explicit credentials before it is ready |
| 5503 | // to respond to the challenge. |
| 5504 | EXPECT_EQ(401, response->headers->response_code()); |
| 5505 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5506 | EXPECT_TRUE(response->auth_challenge.has_value()); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5507 | |
| 5508 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
| 5509 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5510 | response = trans->GetResponseInfo(); |
| 5511 | ASSERT_TRUE(response); |
| 5512 | ASSERT_TRUE(response->headers); |
| 5513 | EXPECT_EQ(200, response->headers->response_code()); |
| 5514 | |
| 5515 | trans.reset(); |
| 5516 | session->CloseAllConnections(); |
| 5517 | } |
| 5518 | |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5519 | // Proxy resolver that returns a proxy with the same host and port for different |
| 5520 | // schemes, based on the path of the URL being requests. |
| 5521 | class SameProxyWithDifferentSchemesProxyResolver : public ProxyResolver { |
| 5522 | public: |
| 5523 | SameProxyWithDifferentSchemesProxyResolver() {} |
| 5524 | ~SameProxyWithDifferentSchemesProxyResolver() override {} |
| 5525 | |
| 5526 | static std::string ProxyHostPortPairAsString() { return "proxy.test:10000"; } |
| 5527 | |
| 5528 | static HostPortPair ProxyHostPortPair() { |
| 5529 | return HostPortPair::FromString(ProxyHostPortPairAsString()); |
| 5530 | } |
| 5531 | |
| 5532 | // ProxyResolver implementation. |
| 5533 | int GetProxyForURL(const GURL& url, |
| 5534 | ProxyInfo* results, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 5535 | CompletionOnceCallback callback, |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5536 | std::unique_ptr<Request>* request, |
| 5537 | const NetLogWithSource& /*net_log*/) override { |
| 5538 | *results = ProxyInfo(); |
Ramin Halavati | 921731ea | 2018-03-16 08:24:57 | [diff] [blame] | 5539 | results->set_traffic_annotation( |
| 5540 | MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5541 | if (url.path() == "/socks4") { |
| 5542 | results->UsePacString("SOCKS " + ProxyHostPortPairAsString()); |
| 5543 | return OK; |
| 5544 | } |
| 5545 | if (url.path() == "/socks5") { |
| 5546 | results->UsePacString("SOCKS5 " + ProxyHostPortPairAsString()); |
| 5547 | return OK; |
| 5548 | } |
| 5549 | if (url.path() == "/http") { |
| 5550 | results->UsePacString("PROXY " + ProxyHostPortPairAsString()); |
| 5551 | return OK; |
| 5552 | } |
| 5553 | if (url.path() == "/https") { |
| 5554 | results->UsePacString("HTTPS " + ProxyHostPortPairAsString()); |
| 5555 | return OK; |
| 5556 | } |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5557 | if (url.path() == "/https_trusted") { |
| 5558 | results->UseProxyServer(ProxyServer(ProxyServer::SCHEME_HTTPS, |
| 5559 | ProxyHostPortPair(), |
| 5560 | true /* is_trusted_proxy */)); |
| 5561 | return OK; |
| 5562 | } |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5563 | NOTREACHED(); |
| 5564 | return ERR_NOT_IMPLEMENTED; |
| 5565 | } |
| 5566 | |
| 5567 | private: |
| 5568 | DISALLOW_COPY_AND_ASSIGN(SameProxyWithDifferentSchemesProxyResolver); |
| 5569 | }; |
| 5570 | |
| 5571 | class SameProxyWithDifferentSchemesProxyResolverFactory |
| 5572 | : public ProxyResolverFactory { |
| 5573 | public: |
| 5574 | SameProxyWithDifferentSchemesProxyResolverFactory() |
| 5575 | : ProxyResolverFactory(false) {} |
| 5576 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 5577 | int CreateProxyResolver(const scoped_refptr<PacFileData>& pac_script, |
| 5578 | std::unique_ptr<ProxyResolver>* resolver, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 5579 | CompletionOnceCallback callback, |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 5580 | std::unique_ptr<Request>* request) override { |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5581 | *resolver = std::make_unique<SameProxyWithDifferentSchemesProxyResolver>(); |
| 5582 | return OK; |
| 5583 | } |
| 5584 | |
| 5585 | private: |
| 5586 | DISALLOW_COPY_AND_ASSIGN(SameProxyWithDifferentSchemesProxyResolverFactory); |
| 5587 | }; |
| 5588 | |
| 5589 | // 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] | 5590 | // same address, the connections are not grouped together. i.e., a request to |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5591 | // foo.com using proxy.com as an HTTPS proxy won't use the same socket as a |
| 5592 | // request to foo.com using proxy.com as an HTTP proxy. |
| 5593 | TEST_F(HttpNetworkTransactionTest, SameDestinationForDifferentProxyTypes) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5594 | session_deps_.proxy_resolution_service = |
| 5595 | std::make_unique<ProxyResolutionService>( |
| 5596 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 5597 | ProxyConfig::CreateAutoDetect(), TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 5598 | std::make_unique<SameProxyWithDifferentSchemesProxyResolverFactory>(), |
| 5599 | nullptr); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5600 | |
| 5601 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 5602 | |
| 5603 | MockWrite socks_writes[] = { |
| 5604 | MockWrite(SYNCHRONOUS, kSOCKS4OkRequestLocalHostPort80, |
| 5605 | kSOCKS4OkRequestLocalHostPort80Length), |
| 5606 | MockWrite(SYNCHRONOUS, |
| 5607 | "GET /socks4 HTTP/1.1\r\n" |
| 5608 | "Host: test\r\n" |
| 5609 | "Connection: keep-alive\r\n\r\n"), |
| 5610 | }; |
| 5611 | MockRead socks_reads[] = { |
| 5612 | MockRead(SYNCHRONOUS, kSOCKS4OkReply, kSOCKS4OkReplyLength), |
| 5613 | MockRead("HTTP/1.0 200 OK\r\n" |
| 5614 | "Connection: keep-alive\r\n" |
| 5615 | "Content-Length: 15\r\n\r\n" |
| 5616 | "SOCKS4 Response"), |
| 5617 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5618 | StaticSocketDataProvider socks_data(socks_reads, socks_writes); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5619 | session_deps_.socket_factory->AddSocketDataProvider(&socks_data); |
| 5620 | |
| 5621 | const char kSOCKS5Request[] = { |
| 5622 | 0x05, // Version |
| 5623 | 0x01, // Command (CONNECT) |
| 5624 | 0x00, // Reserved |
| 5625 | 0x03, // Address type (DOMAINNAME) |
| 5626 | 0x04, // Length of domain (4) |
| 5627 | 't', 'e', 's', 't', // Domain string |
| 5628 | 0x00, 0x50, // 16-bit port (80) |
| 5629 | }; |
| 5630 | MockWrite socks5_writes[] = { |
| 5631 | MockWrite(ASYNC, kSOCKS5GreetRequest, kSOCKS5GreetRequestLength), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 5632 | MockWrite(ASYNC, kSOCKS5Request, base::size(kSOCKS5Request)), |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5633 | MockWrite(SYNCHRONOUS, |
| 5634 | "GET /socks5 HTTP/1.1\r\n" |
| 5635 | "Host: test\r\n" |
| 5636 | "Connection: keep-alive\r\n\r\n"), |
| 5637 | }; |
| 5638 | MockRead socks5_reads[] = { |
| 5639 | MockRead(ASYNC, kSOCKS5GreetResponse, kSOCKS5GreetResponseLength), |
| 5640 | MockRead(ASYNC, kSOCKS5OkResponse, kSOCKS5OkResponseLength), |
| 5641 | MockRead("HTTP/1.0 200 OK\r\n" |
| 5642 | "Connection: keep-alive\r\n" |
| 5643 | "Content-Length: 15\r\n\r\n" |
| 5644 | "SOCKS5 Response"), |
| 5645 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5646 | StaticSocketDataProvider socks5_data(socks5_reads, socks5_writes); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5647 | session_deps_.socket_factory->AddSocketDataProvider(&socks5_data); |
| 5648 | |
| 5649 | MockWrite http_writes[] = { |
| 5650 | MockWrite(SYNCHRONOUS, |
| 5651 | "GET http://test/http HTTP/1.1\r\n" |
| 5652 | "Host: test\r\n" |
| 5653 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5654 | }; |
| 5655 | MockRead http_reads[] = { |
| 5656 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5657 | "Proxy-Connection: keep-alive\r\n" |
| 5658 | "Content-Length: 13\r\n\r\n" |
| 5659 | "HTTP Response"), |
| 5660 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5661 | StaticSocketDataProvider http_data(http_reads, http_writes); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5662 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 5663 | |
| 5664 | MockWrite https_writes[] = { |
| 5665 | MockWrite(SYNCHRONOUS, |
| 5666 | "GET http://test/https HTTP/1.1\r\n" |
| 5667 | "Host: test\r\n" |
| 5668 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5669 | }; |
| 5670 | MockRead https_reads[] = { |
| 5671 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5672 | "Proxy-Connection: keep-alive\r\n" |
| 5673 | "Content-Length: 14\r\n\r\n" |
| 5674 | "HTTPS Response"), |
| 5675 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5676 | StaticSocketDataProvider https_data(https_reads, https_writes); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5677 | session_deps_.socket_factory->AddSocketDataProvider(&https_data); |
| 5678 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 5679 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 5680 | |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5681 | MockWrite https_trusted_writes[] = { |
| 5682 | MockWrite(SYNCHRONOUS, |
| 5683 | "GET http://test/https_trusted HTTP/1.1\r\n" |
| 5684 | "Host: test\r\n" |
| 5685 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5686 | }; |
| 5687 | MockRead https_trusted_reads[] = { |
| 5688 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5689 | "Proxy-Connection: keep-alive\r\n" |
| 5690 | "Content-Length: 22\r\n\r\n" |
| 5691 | "HTTPS Trusted Response"), |
| 5692 | }; |
| 5693 | StaticSocketDataProvider trusted_https_data(https_trusted_reads, |
| 5694 | https_trusted_writes); |
| 5695 | session_deps_.socket_factory->AddSocketDataProvider(&trusted_https_data); |
| 5696 | SSLSocketDataProvider ssl2(SYNCHRONOUS, OK); |
| 5697 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 5698 | |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5699 | struct TestCase { |
| 5700 | GURL url; |
| 5701 | std::string expected_response; |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5702 | // 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] | 5703 | // after the test. |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5704 | int expected_idle_socks4_sockets; |
| 5705 | int expected_idle_socks5_sockets; |
| 5706 | // How many idle sockets there should be in the HTTP/HTTPS proxy socket |
| 5707 | // pools after the test. |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5708 | int expected_idle_http_sockets; |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5709 | int expected_idle_https_sockets; |
| 5710 | // How many idle sockets there should be in the HTTPS proxy socket pool with |
| 5711 | // the ProxyServer's |is_trusted_proxy| bit set after the test. |
| 5712 | int expected_idle_trusted_https_sockets; |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5713 | } const kTestCases[] = { |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5714 | {GURL("http://test/socks4"), "SOCKS4 Response", 1, 0, 0, 0, 0}, |
| 5715 | {GURL("http://test/socks5"), "SOCKS5 Response", 1, 1, 0, 0, 0}, |
| 5716 | {GURL("http://test/http"), "HTTP Response", 1, 1, 1, 0, 0}, |
| 5717 | {GURL("http://test/https"), "HTTPS Response", 1, 1, 1, 1, 0}, |
| 5718 | {GURL("http://test/https_trusted"), "HTTPS Trusted Response", 1, 1, 1, 1, |
| 5719 | 1}, |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5720 | }; |
| 5721 | |
| 5722 | for (const auto& test_case : kTestCases) { |
| 5723 | HttpRequestInfo request; |
| 5724 | request.method = "GET"; |
| 5725 | request.url = test_case.url; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 5726 | request.traffic_annotation = |
| 5727 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5728 | std::unique_ptr<HttpNetworkTransaction> trans = |
| 5729 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 5730 | session.get()); |
| 5731 | TestCompletionCallback callback; |
| 5732 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 5733 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5734 | |
| 5735 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5736 | ASSERT_TRUE(response); |
| 5737 | ASSERT_TRUE(response->headers); |
| 5738 | EXPECT_EQ(200, response->headers->response_code()); |
| 5739 | std::string response_data; |
| 5740 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 5741 | EXPECT_EQ(test_case.expected_response, response_data); |
| 5742 | |
| 5743 | // Return the socket to the socket pool, so can make sure it's not used for |
| 5744 | // the next requests. |
| 5745 | trans.reset(); |
| 5746 | base::RunLoop().RunUntilIdle(); |
| 5747 | |
| 5748 | // Check the number of idle sockets in the pool, to make sure that used |
| 5749 | // sockets are indeed being returned to the socket pool. If each request |
| 5750 | // doesn't return an idle socket to the pool, the test would incorrectly |
| 5751 | // pass. |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5752 | EXPECT_EQ(test_case.expected_idle_socks4_sockets, |
| 5753 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5754 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5755 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5756 | ProxyServer(ProxyServer::SCHEME_SOCKS4, |
| 5757 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5758 | ProxyHostPortPair())) |
| 5759 | ->IdleSocketCount()); |
| 5760 | EXPECT_EQ(test_case.expected_idle_socks5_sockets, |
| 5761 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5762 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5763 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5764 | ProxyServer(ProxyServer::SCHEME_SOCKS5, |
| 5765 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5766 | ProxyHostPortPair())) |
| 5767 | ->IdleSocketCount()); |
| 5768 | EXPECT_EQ(test_case.expected_idle_http_sockets, |
| 5769 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5770 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5771 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5772 | ProxyServer(ProxyServer::SCHEME_HTTP, |
| 5773 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5774 | ProxyHostPortPair())) |
| 5775 | ->IdleSocketCount()); |
| 5776 | EXPECT_EQ(test_case.expected_idle_https_sockets, |
| 5777 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5778 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5779 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5780 | ProxyServer(ProxyServer::SCHEME_HTTPS, |
| 5781 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5782 | ProxyHostPortPair())) |
| 5783 | ->IdleSocketCount()); |
| 5784 | EXPECT_EQ(test_case.expected_idle_trusted_https_sockets, |
| 5785 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5786 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5787 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5788 | ProxyServer(ProxyServer::SCHEME_HTTPS, |
| 5789 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5790 | ProxyHostPortPair(), |
| 5791 | true /* is_trusted_proxy */)) |
| 5792 | ->IdleSocketCount()); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5793 | } |
| 5794 | } |
| 5795 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5796 | // Test the load timing for HTTPS requests with an HTTP proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 5797 | TEST_F(HttpNetworkTransactionTest, HttpProxyLoadTimingNoPacTwoRequests) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5798 | HttpRequestInfo request1; |
| 5799 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 5800 | request1.url = GURL("https://www.example.org/1"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 5801 | request1.traffic_annotation = |
| 5802 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5803 | |
| 5804 | HttpRequestInfo request2; |
| 5805 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 5806 | request2.url = GURL("https://www.example.org/2"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 5807 | request2.traffic_annotation = |
| 5808 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5809 | |
| 5810 | // Configure against proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5811 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 5812 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5813 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5814 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 5815 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5816 | |
| 5817 | // Since we have proxy, should try to establish tunnel. |
| 5818 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5819 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5820 | "Host: www.example.org:443\r\n" |
| 5821 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5822 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5823 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 5824 | "Host: www.example.org\r\n" |
| 5825 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5826 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5827 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 5828 | "Host: www.example.org\r\n" |
| 5829 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5830 | }; |
| 5831 | |
| 5832 | // The proxy responds to the connect with a 407, using a persistent |
| 5833 | // connection. |
| 5834 | MockRead data_reads1[] = { |
| 5835 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 5836 | |
| 5837 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5838 | MockRead("Content-Length: 1\r\n\r\n"), |
| 5839 | MockRead(SYNCHRONOUS, "1"), |
| 5840 | |
| 5841 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5842 | MockRead("Content-Length: 2\r\n\r\n"), |
| 5843 | MockRead(SYNCHRONOUS, "22"), |
| 5844 | }; |
| 5845 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5846 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5847 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5848 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5849 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5850 | |
| 5851 | TestCompletionCallback callback1; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5852 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5853 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5854 | |
| 5855 | int rv = trans1->Start(&request1, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5856 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5857 | |
| 5858 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5859 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5860 | |
| 5861 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5862 | ASSERT_TRUE(response1); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 5863 | EXPECT_TRUE(response1->proxy_server.is_http()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5864 | ASSERT_TRUE(response1->headers); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5865 | EXPECT_EQ(1, response1->headers->GetContentLength()); |
| 5866 | |
| 5867 | LoadTimingInfo load_timing_info1; |
| 5868 | EXPECT_TRUE(trans1->GetLoadTimingInfo(&load_timing_info1)); |
| 5869 | TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_SSL_TIMES); |
| 5870 | |
| 5871 | trans1.reset(); |
| 5872 | |
| 5873 | TestCompletionCallback callback2; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5874 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5875 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5876 | |
| 5877 | rv = trans2->Start(&request2, callback2.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5878 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5879 | |
| 5880 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5881 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5882 | |
| 5883 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5884 | ASSERT_TRUE(response2); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 5885 | EXPECT_TRUE(response2->proxy_server.is_http()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5886 | ASSERT_TRUE(response2->headers); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5887 | EXPECT_EQ(2, response2->headers->GetContentLength()); |
| 5888 | |
| 5889 | LoadTimingInfo load_timing_info2; |
| 5890 | EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); |
| 5891 | TestLoadTimingReused(load_timing_info2); |
| 5892 | |
| 5893 | EXPECT_EQ(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
| 5894 | |
| 5895 | trans2.reset(); |
| 5896 | session->CloseAllConnections(); |
| 5897 | } |
| 5898 | |
| 5899 | // 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] | 5900 | TEST_F(HttpNetworkTransactionTest, HttpProxyLoadTimingWithPacTwoRequests) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5901 | HttpRequestInfo request1; |
| 5902 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 5903 | request1.url = GURL("https://www.example.org/1"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 5904 | request1.traffic_annotation = |
| 5905 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5906 | |
| 5907 | HttpRequestInfo request2; |
| 5908 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 5909 | request2.url = GURL("https://www.example.org/2"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 5910 | request2.traffic_annotation = |
| 5911 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5912 | |
| 5913 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 5914 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5915 | ProxyResolutionService::CreateFixedFromPacResult( |
| 5916 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5917 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5918 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 5919 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5920 | |
| 5921 | // Since we have proxy, should try to establish tunnel. |
| 5922 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5923 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5924 | "Host: www.example.org:443\r\n" |
| 5925 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5926 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5927 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 5928 | "Host: www.example.org\r\n" |
| 5929 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5930 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5931 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 5932 | "Host: www.example.org\r\n" |
| 5933 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5934 | }; |
| 5935 | |
| 5936 | // The proxy responds to the connect with a 407, using a persistent |
| 5937 | // connection. |
| 5938 | MockRead data_reads1[] = { |
| 5939 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 5940 | |
| 5941 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5942 | MockRead("Content-Length: 1\r\n\r\n"), |
| 5943 | MockRead(SYNCHRONOUS, "1"), |
| 5944 | |
| 5945 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5946 | MockRead("Content-Length: 2\r\n\r\n"), |
| 5947 | MockRead(SYNCHRONOUS, "22"), |
| 5948 | }; |
| 5949 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5950 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5951 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5952 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5953 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5954 | |
| 5955 | TestCompletionCallback callback1; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5956 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5957 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5958 | |
| 5959 | int rv = trans1->Start(&request1, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5960 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5961 | |
| 5962 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5963 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5964 | |
| 5965 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5966 | ASSERT_TRUE(response1); |
| 5967 | ASSERT_TRUE(response1->headers); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5968 | EXPECT_EQ(1, response1->headers->GetContentLength()); |
| 5969 | |
| 5970 | LoadTimingInfo load_timing_info1; |
| 5971 | EXPECT_TRUE(trans1->GetLoadTimingInfo(&load_timing_info1)); |
| 5972 | TestLoadTimingNotReusedWithPac(load_timing_info1, |
| 5973 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 5974 | |
| 5975 | trans1.reset(); |
| 5976 | |
| 5977 | TestCompletionCallback callback2; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5978 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5979 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5980 | |
| 5981 | rv = trans2->Start(&request2, callback2.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5982 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5983 | |
| 5984 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5985 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5986 | |
| 5987 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5988 | ASSERT_TRUE(response2); |
| 5989 | ASSERT_TRUE(response2->headers); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5990 | EXPECT_EQ(2, response2->headers->GetContentLength()); |
| 5991 | |
| 5992 | LoadTimingInfo load_timing_info2; |
| 5993 | EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); |
| 5994 | TestLoadTimingReusedWithPac(load_timing_info2); |
| 5995 | |
| 5996 | EXPECT_EQ(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
| 5997 | |
| 5998 | trans2.reset(); |
| 5999 | session->CloseAllConnections(); |
| 6000 | } |
| 6001 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6002 | // Test a simple get through an HTTPS Proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6003 | TEST_F(HttpNetworkTransactionTest, HttpsProxyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6004 | HttpRequestInfo request; |
| 6005 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6006 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6007 | request.traffic_annotation = |
| 6008 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6009 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6010 | // Configure against https proxy server "proxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6011 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6012 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6013 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6014 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6015 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6016 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6017 | // Since we have proxy, should use full url |
| 6018 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6019 | MockWrite( |
| 6020 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 6021 | "Host: www.example.org\r\n" |
| 6022 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6023 | }; |
| 6024 | |
| 6025 | MockRead data_reads1[] = { |
| 6026 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 6027 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 6028 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6029 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6030 | }; |
| 6031 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6032 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6033 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6034 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6035 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6036 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6037 | TestCompletionCallback callback1; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6038 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6039 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6040 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6041 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6042 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6043 | |
| 6044 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6045 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6046 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6047 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6048 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6049 | TestLoadTimingNotReused(load_timing_info, |
| 6050 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 6051 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6052 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6053 | ASSERT_TRUE(response); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6054 | |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 6055 | EXPECT_TRUE(response->proxy_server.is_https()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6056 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 6057 | EXPECT_EQ(200, response->headers->response_code()); |
| 6058 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 6059 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 6060 | |
| 6061 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6062 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6063 | } |
| 6064 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6065 | // Test a SPDY get through an HTTPS Proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6066 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6067 | HttpRequestInfo request; |
| 6068 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6069 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6070 | request.traffic_annotation = |
| 6071 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6072 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6073 | // Configure against https proxy server "proxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6074 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6075 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6076 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6077 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6078 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6079 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6080 | // fetch http://www.example.org/ via SPDY |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6081 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 6082 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6083 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6084 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6085 | spdy::SpdySerializedFrame resp( |
| 6086 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 6087 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6088 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6089 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6090 | }; |
| 6091 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6092 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6093 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6094 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6095 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6096 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6097 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6098 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6099 | TestCompletionCallback callback1; |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [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] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6105 | |
| 6106 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6107 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [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); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 6116 | EXPECT_TRUE(response->proxy_server.is_https()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6117 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 6118 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6119 | |
| 6120 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6121 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 6122 | EXPECT_EQ(kUploadData, response_data); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6123 | } |
| 6124 | |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6125 | // Verifies that a session which races and wins against the owning transaction |
| 6126 | // (completing prior to host resolution), doesn't fail the transaction. |
| 6127 | // Regression test for crbug.com/334413. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6128 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGetWithSessionRace) { |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [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 | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6132 | request.traffic_annotation = |
| 6133 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6134 | |
| 6135 | // Configure SPDY proxy server "proxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6136 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6137 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6138 | BoundTestNetLog log; |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6139 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6140 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6141 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6142 | // Fetch http://www.example.org/ through the SPDY proxy. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6143 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 6144 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6145 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6146 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6147 | spdy::SpdySerializedFrame resp( |
| 6148 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6149 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6150 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6151 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6152 | }; |
| 6153 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6154 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6155 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
| 6156 | |
| 6157 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6158 | ssl.next_proto = kProtoHTTP2; |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6159 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 6160 | |
| 6161 | TestCompletionCallback callback1; |
| 6162 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6163 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6164 | |
| 6165 | // Stall the hostname resolution begun by the transaction. |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6166 | session_deps_.host_resolver->set_ondemand_mode(true); |
| 6167 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6168 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6169 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6170 | |
| 6171 | // Race a session to the proxy, which completes first. |
| 6172 | session_deps_.host_resolver->set_ondemand_mode(false); |
Paul Jensen | a457017a | 2018-01-19 23:52:04 | [diff] [blame] | 6173 | SpdySessionKey key(HostPortPair("proxy", 70), ProxyServer::Direct(), |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 6174 | PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 6175 | SpdySessionKey::IsProxySession::kTrue, SocketTag(), |
| 6176 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6177 | base::WeakPtr<SpdySession> spdy_session = |
Bence Béky | 0ef1556e | 2017-06-30 19:52:52 | [diff] [blame] | 6178 | CreateSpdySession(session.get(), key, log.bound()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6179 | |
| 6180 | // Unstall the resolution begun by the transaction. |
| 6181 | session_deps_.host_resolver->set_ondemand_mode(true); |
| 6182 | session_deps_.host_resolver->ResolveAllPending(); |
| 6183 | |
| 6184 | EXPECT_FALSE(callback1.have_result()); |
| 6185 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6186 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6187 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6188 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6189 | ASSERT_TRUE(response); |
| 6190 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 6191 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6192 | |
| 6193 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6194 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6195 | EXPECT_EQ(kUploadData, response_data); |
| 6196 | } |
| 6197 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6198 | // Test a SPDY get through an HTTPS Proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6199 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGetWithProxyAuth) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6200 | HttpRequestInfo request; |
| 6201 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6202 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6203 | request.traffic_annotation = |
| 6204 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6205 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6206 | // Configure against https proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6207 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6208 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6209 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6210 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6211 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6212 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6213 | // The first request will be a bare GET, the second request will be a |
| 6214 | // GET with a Proxy-Authorization header. |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 6215 | spdy_util_.set_default_url(request.url); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6216 | spdy::SpdySerializedFrame req_get( |
Bence Béky | 27ad0a1 | 2018-02-08 00:35:48 | [diff] [blame] | 6217 | spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 6218 | spdy_util_.UpdateWithStreamDestruction(1); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6219 | const char* const kExtraAuthorizationHeaders[] = { |
[email protected] | cdf8f7e7 | 2013-05-23 10:56:46 | [diff] [blame] | 6220 | "proxy-authorization", "Basic Zm9vOmJhcg==" |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6221 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6222 | spdy::SpdySerializedFrame req_get_authorization(spdy_util_.ConstructSpdyGet( |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 6223 | kExtraAuthorizationHeaders, base::size(kExtraAuthorizationHeaders) / 2, 3, |
Bence Béky | 27ad0a1 | 2018-02-08 00:35:48 | [diff] [blame] | 6224 | LOWEST)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6225 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6226 | CreateMockWrite(req_get, 0), CreateMockWrite(req_get_authorization, 3), |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6227 | }; |
| 6228 | |
| 6229 | // The first response is a 407 proxy authentication challenge, and the second |
| 6230 | // response will be a 200 response since the second request includes a valid |
| 6231 | // Authorization header. |
| 6232 | const char* const kExtraAuthenticationHeaders[] = { |
[email protected] | cdf8f7e7 | 2013-05-23 10:56:46 | [diff] [blame] | 6233 | "proxy-authenticate", "Basic realm=\"MyRealm1\"" |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6234 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6235 | spdy::SpdySerializedFrame resp_authentication( |
| 6236 | spdy_util_.ConstructSpdyReplyError( |
| 6237 | "407", kExtraAuthenticationHeaders, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 6238 | base::size(kExtraAuthenticationHeaders) / 2, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6239 | spdy::SpdySerializedFrame body_authentication( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6240 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6241 | spdy::SpdySerializedFrame resp_data( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6242 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6243 | spdy::SpdySerializedFrame body_data( |
| 6244 | spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6245 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6246 | CreateMockRead(resp_authentication, 1), |
bnc | eb9aa711 | 2017-01-05 01:03:46 | [diff] [blame] | 6247 | CreateMockRead(body_authentication, 2, SYNCHRONOUS), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6248 | CreateMockRead(resp_data, 4), |
| 6249 | CreateMockRead(body_data, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 6250 | MockRead(ASYNC, 0, 6), |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6251 | }; |
| 6252 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6253 | SequencedSocketData data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6254 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6255 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6256 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6257 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6258 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6259 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6260 | TestCompletionCallback callback1; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6261 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6262 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6263 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6264 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6265 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6266 | |
| 6267 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6268 | EXPECT_THAT(rv, IsOk()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6269 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6270 | const HttpResponseInfo* const response = trans.GetResponseInfo(); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6271 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6272 | ASSERT_TRUE(response); |
| 6273 | ASSERT_TRUE(response->headers); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6274 | EXPECT_EQ(407, response->headers->response_code()); |
| 6275 | EXPECT_TRUE(response->was_fetched_via_spdy); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6276 | EXPECT_TRUE(CheckBasicSecureProxyAuth(response->auth_challenge)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6277 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6278 | TestCompletionCallback callback2; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6279 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6280 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6281 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6282 | |
| 6283 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6284 | EXPECT_THAT(rv, IsOk()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6285 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6286 | const HttpResponseInfo* const response_restart = trans.GetResponseInfo(); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6287 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6288 | ASSERT_TRUE(response_restart); |
| 6289 | ASSERT_TRUE(response_restart->headers); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6290 | EXPECT_EQ(200, response_restart->headers->response_code()); |
| 6291 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6292 | EXPECT_FALSE(response_restart->auth_challenge.has_value()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6293 | } |
| 6294 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6295 | // Test a SPDY CONNECT through an HTTPS Proxy to an HTTPS (non-SPDY) Server. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6296 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectHttps) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6297 | HttpRequestInfo request; |
| 6298 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6299 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6300 | request.traffic_annotation = |
| 6301 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6302 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6303 | // Configure against https proxy server "proxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6304 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6305 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6306 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6307 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6308 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6309 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6310 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6311 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6312 | // CONNECT to www.example.org:443 via SPDY |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6313 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6314 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6315 | HostPortPair("www.example.org", 443))); |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6316 | // fetch https://www.example.org/ via HTTP |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6317 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6318 | const char get[] = |
| 6319 | "GET / HTTP/1.1\r\n" |
| 6320 | "Host: www.example.org\r\n" |
| 6321 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6322 | spdy::SpdySerializedFrame wrapped_get( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6323 | spdy_util_.ConstructSpdyDataFrame(1, get, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6324 | spdy::SpdySerializedFrame conn_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6325 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6326 | const char resp[] = "HTTP/1.1 200 OK\r\n" |
| 6327 | "Content-Length: 10\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6328 | spdy::SpdySerializedFrame wrapped_get_resp( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6329 | spdy_util_.ConstructSpdyDataFrame(1, resp, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6330 | spdy::SpdySerializedFrame wrapped_body( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6331 | spdy_util_.ConstructSpdyDataFrame(1, "1234567890", false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6332 | spdy::SpdySerializedFrame window_update( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6333 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp.size())); |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6334 | |
| 6335 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6336 | CreateMockWrite(connect, 0), CreateMockWrite(wrapped_get, 2), |
| 6337 | CreateMockWrite(window_update, 6), |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6338 | }; |
| 6339 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6340 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6341 | CreateMockRead(conn_resp, 1, ASYNC), |
| 6342 | CreateMockRead(wrapped_get_resp, 3, ASYNC), |
| 6343 | CreateMockRead(wrapped_body, 4, ASYNC), |
| 6344 | CreateMockRead(wrapped_body, 5, ASYNC), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 6345 | MockRead(ASYNC, 0, 7), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6346 | }; |
| 6347 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6348 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6349 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6350 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6351 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6352 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6353 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6354 | SSLSocketDataProvider ssl2(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6355 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6356 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6357 | TestCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6358 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6359 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6360 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6361 | |
| 6362 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6363 | ASSERT_THAT(rv, IsOk()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6364 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6365 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6366 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6367 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); |
| 6368 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6369 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6370 | ASSERT_TRUE(response); |
| 6371 | ASSERT_TRUE(response->headers); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6372 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6373 | |
| 6374 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6375 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6376 | EXPECT_EQ("1234567890", response_data); |
| 6377 | } |
| 6378 | |
| 6379 | // Test a SPDY CONNECT through an HTTPS Proxy to a SPDY server. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6380 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectSpdy) { |
| 6381 | SpdyTestUtil spdy_util_wrapped; |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 6382 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6383 | HttpRequestInfo request; |
| 6384 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6385 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6386 | request.traffic_annotation = |
| 6387 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6388 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6389 | // Configure against https proxy server "proxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6390 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6391 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6392 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6393 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6394 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6395 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6396 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6397 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6398 | // CONNECT to www.example.org:443 via SPDY |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6399 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6400 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6401 | HostPortPair("www.example.org", 443))); |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6402 | // fetch https://www.example.org/ via SPDY |
| 6403 | const char kMyUrl[] = "https://www.example.org/"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6404 | spdy::SpdySerializedFrame get( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 6405 | spdy_util_wrapped.ConstructSpdyGet(kMyUrl, 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6406 | spdy::SpdySerializedFrame wrapped_get( |
| 6407 | spdy_util_.ConstructWrappedSpdyFrame(get, 1)); |
| 6408 | spdy::SpdySerializedFrame conn_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6409 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6410 | spdy::SpdySerializedFrame get_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6411 | spdy_util_wrapped.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6412 | spdy::SpdySerializedFrame wrapped_get_resp( |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 6413 | spdy_util_.ConstructWrappedSpdyFrame(get_resp, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6414 | spdy::SpdySerializedFrame body( |
| 6415 | spdy_util_wrapped.ConstructSpdyDataFrame(1, true)); |
| 6416 | spdy::SpdySerializedFrame wrapped_body( |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 6417 | spdy_util_.ConstructWrappedSpdyFrame(body, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6418 | spdy::SpdySerializedFrame window_update_get_resp( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6419 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp.size())); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6420 | spdy::SpdySerializedFrame window_update_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6421 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_body.size())); |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6422 | |
| 6423 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6424 | CreateMockWrite(connect, 0), CreateMockWrite(wrapped_get, 2), |
| 6425 | CreateMockWrite(window_update_get_resp, 6), |
| 6426 | CreateMockWrite(window_update_body, 7), |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6427 | }; |
| 6428 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6429 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6430 | CreateMockRead(conn_resp, 1, ASYNC), |
rch | 3232084 | 2015-05-16 15:57:09 | [diff] [blame] | 6431 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6432 | CreateMockRead(wrapped_get_resp, 4, ASYNC), |
| 6433 | CreateMockRead(wrapped_body, 5, ASYNC), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 6434 | MockRead(ASYNC, 0, 8), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6435 | }; |
| 6436 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6437 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6438 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6439 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6440 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6441 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6442 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6443 | SSLSocketDataProvider ssl2(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6444 | ssl2.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6445 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6446 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6447 | TestCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6448 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6449 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6450 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6451 | |
rch | 3232084 | 2015-05-16 15:57:09 | [diff] [blame] | 6452 | // Allow the SpdyProxyClientSocket's write callback to complete. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 6453 | base::RunLoop().RunUntilIdle(); |
rch | 3232084 | 2015-05-16 15:57:09 | [diff] [blame] | 6454 | // Now allow the read of the response to complete. |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 6455 | spdy_data.Resume(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6456 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6457 | EXPECT_THAT(rv, IsOk()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6458 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6459 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6460 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6461 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); |
| 6462 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6463 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6464 | ASSERT_TRUE(response); |
| 6465 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 6466 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6467 | |
| 6468 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6469 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 6470 | EXPECT_EQ(kUploadData, response_data); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6471 | } |
| 6472 | |
| 6473 | // Test a SPDY CONNECT failure through an HTTPS Proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6474 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectFailure) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6475 | HttpRequestInfo request; |
| 6476 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6477 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6478 | request.traffic_annotation = |
| 6479 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6480 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6481 | // Configure against https proxy server "proxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6482 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6483 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6484 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6485 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6486 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6487 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6488 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6489 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6490 | // CONNECT to www.example.org:443 via SPDY |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6491 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6492 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6493 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6494 | spdy::SpdySerializedFrame get( |
| 6495 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_CANCEL)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6496 | |
| 6497 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6498 | CreateMockWrite(connect, 0), CreateMockWrite(get, 2), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6499 | }; |
| 6500 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6501 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyReplyError(1)); |
| 6502 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6503 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6504 | CreateMockRead(resp, 1, ASYNC), MockRead(ASYNC, 0, 3), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6505 | }; |
| 6506 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6507 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6508 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6509 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6510 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6511 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6512 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6513 | SSLSocketDataProvider ssl2(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6514 | ssl2.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6515 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6516 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6517 | TestCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6518 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6519 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6520 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6521 | |
| 6522 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6523 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6524 | |
ttuttle | 960fcbf | 2016-04-19 13:26:32 | [diff] [blame] | 6525 | // TODO(juliatuttle): Anything else to check here? |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6526 | } |
| 6527 | |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6528 | // 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] | 6529 | // is observed, but does exist by the time auth credentials are provided. In |
| 6530 | // this case, auth and SSL are fully negotated on the second request, but then |
| 6531 | // the socket is discarded to use the shared session. |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6532 | TEST_F(HttpNetworkTransactionTest, ProxiedH2SessionAppearsDuringAuth) { |
| 6533 | ProxyConfig proxy_config; |
| 6534 | proxy_config.set_auto_detect(true); |
| 6535 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 6536 | |
| 6537 | CapturingProxyResolver capturing_proxy_resolver; |
| 6538 | capturing_proxy_resolver.set_proxy_server( |
| 6539 | ProxyServer(ProxyServer::SCHEME_HTTP, HostPortPair("myproxy", 70))); |
| 6540 | session_deps_.proxy_resolution_service = |
| 6541 | std::make_unique<ProxyResolutionService>( |
| 6542 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 6543 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 6544 | std::make_unique<CapturingProxyResolverFactory>( |
| 6545 | &capturing_proxy_resolver), |
| 6546 | nullptr); |
| 6547 | |
| 6548 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 6549 | |
| 6550 | const char kMyUrl[] = "https://www.example.org/"; |
| 6551 | spdy::SpdySerializedFrame get(spdy_util_.ConstructSpdyGet(kMyUrl, 1, LOWEST)); |
| 6552 | spdy::SpdySerializedFrame get_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6553 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6554 | spdy::SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true)); |
| 6555 | |
| 6556 | spdy_util_.UpdateWithStreamDestruction(1); |
| 6557 | spdy::SpdySerializedFrame get2( |
| 6558 | spdy_util_.ConstructSpdyGet(kMyUrl, 3, LOWEST)); |
| 6559 | spdy::SpdySerializedFrame get_resp2( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6560 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6561 | spdy::SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, true)); |
| 6562 | |
| 6563 | MockWrite auth_challenge_writes[] = { |
| 6564 | MockWrite(ASYNC, 0, |
| 6565 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 6566 | "Host: www.example.org:443\r\n" |
| 6567 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 6568 | MockWrite(ASYNC, 2, |
| 6569 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 6570 | "Host: www.example.org:443\r\n" |
| 6571 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6572 | }; |
| 6573 | |
| 6574 | MockRead auth_challenge_reads[] = { |
| 6575 | MockRead(ASYNC, 1, |
| 6576 | "HTTP/1.1 407 Authentication Required\r\n" |
| 6577 | "Content-Length: 0\r\n" |
| 6578 | "Proxy-Connection: close\r\n" |
| 6579 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"), |
| 6580 | }; |
| 6581 | |
| 6582 | MockWrite spdy_writes[] = { |
| 6583 | MockWrite(ASYNC, 0, |
| 6584 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 6585 | "Host: www.example.org:443\r\n" |
| 6586 | "Proxy-Connection: keep-alive\r\n" |
| 6587 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 6588 | CreateMockWrite(get, 2), |
| 6589 | CreateMockWrite(get2, 5), |
| 6590 | }; |
| 6591 | |
| 6592 | MockRead spdy_reads[] = { |
| 6593 | MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n\r\n"), |
| 6594 | CreateMockRead(get_resp, 3, ASYNC), |
| 6595 | CreateMockRead(body, 4, ASYNC), |
| 6596 | CreateMockRead(get_resp2, 6, ASYNC), |
| 6597 | CreateMockRead(body2, 7, ASYNC), |
| 6598 | |
| 6599 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 8), |
| 6600 | }; |
| 6601 | |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 6602 | MockWrite auth_response_writes_discarded_socket[] = { |
| 6603 | MockWrite(ASYNC, 0, |
| 6604 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 6605 | "Host: www.example.org:443\r\n" |
| 6606 | "Proxy-Connection: keep-alive\r\n" |
| 6607 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 6608 | }; |
| 6609 | |
| 6610 | MockRead auth_response_reads_discarded_socket[] = { |
| 6611 | MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n\r\n"), |
| 6612 | }; |
| 6613 | |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6614 | SequencedSocketData auth_challenge1(auth_challenge_reads, |
| 6615 | auth_challenge_writes); |
| 6616 | session_deps_.socket_factory->AddSocketDataProvider(&auth_challenge1); |
| 6617 | |
| 6618 | SequencedSocketData auth_challenge2(auth_challenge_reads, |
| 6619 | auth_challenge_writes); |
| 6620 | session_deps_.socket_factory->AddSocketDataProvider(&auth_challenge2); |
| 6621 | |
| 6622 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
| 6623 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
| 6624 | |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 6625 | SequencedSocketData auth_response_discarded_socket( |
| 6626 | auth_response_reads_discarded_socket, |
| 6627 | auth_response_writes_discarded_socket); |
| 6628 | session_deps_.socket_factory->AddSocketDataProvider( |
| 6629 | &auth_response_discarded_socket); |
| 6630 | |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6631 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 6632 | ssl.next_proto = kProtoHTTP2; |
| 6633 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 6634 | |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 6635 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 6636 | ssl2.next_proto = kProtoHTTP2; |
| 6637 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 6638 | |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6639 | TestCompletionCallback callback; |
| 6640 | std::string response_data; |
| 6641 | |
| 6642 | // Run first request until an auth challenge is observed. |
| 6643 | HttpRequestInfo request1; |
| 6644 | request1.method = "GET"; |
| 6645 | request1.url = GURL(kMyUrl); |
| 6646 | request1.traffic_annotation = |
| 6647 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 6648 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
| 6649 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
| 6650 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6651 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 6652 | ASSERT_TRUE(response); |
| 6653 | ASSERT_TRUE(response->headers); |
| 6654 | EXPECT_EQ(407, response->headers->response_code()); |
| 6655 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6656 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6657 | |
| 6658 | // Run second request until an auth challenge is observed. |
| 6659 | HttpRequestInfo request2; |
| 6660 | request2.method = "GET"; |
| 6661 | request2.url = GURL(kMyUrl); |
| 6662 | request2.traffic_annotation = |
| 6663 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 6664 | HttpNetworkTransaction trans2(LOWEST, session.get()); |
| 6665 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
| 6666 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6667 | response = trans2.GetResponseInfo(); |
| 6668 | ASSERT_TRUE(response); |
| 6669 | ASSERT_TRUE(response->headers); |
| 6670 | EXPECT_EQ(407, response->headers->response_code()); |
| 6671 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6672 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6673 | |
| 6674 | // Now provide credentials for the first request, and wait for it to complete. |
| 6675 | rv = trans1.RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 6676 | rv = callback.GetResult(rv); |
| 6677 | EXPECT_THAT(rv, IsOk()); |
| 6678 | response = trans1.GetResponseInfo(); |
| 6679 | ASSERT_TRUE(response); |
| 6680 | ASSERT_TRUE(response->headers); |
| 6681 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 6682 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 6683 | EXPECT_EQ(kUploadData, response_data); |
| 6684 | |
| 6685 | // Now provide credentials for the second request. It should notice the |
| 6686 | // existing session, and reuse it. |
| 6687 | rv = trans2.RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 6688 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6689 | response = trans2.GetResponseInfo(); |
| 6690 | ASSERT_TRUE(response); |
| 6691 | ASSERT_TRUE(response->headers); |
| 6692 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 6693 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 6694 | EXPECT_EQ(kUploadData, response_data); |
| 6695 | } |
| 6696 | |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6697 | // Test load timing in the case of two HTTPS (non-SPDY) requests through a SPDY |
| 6698 | // HTTPS Proxy to different servers. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6699 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6700 | HttpsProxySpdyConnectHttpsLoadTimingTwoRequestsTwoServers) { |
| 6701 | // Configure against https proxy server "proxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6702 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6703 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6704 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6705 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6706 | std::unique_ptr<HttpNetworkSession> session( |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6707 | SpdySessionDependencies::SpdyCreateSession(&session_deps_)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6708 | |
| 6709 | HttpRequestInfo request1; |
| 6710 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6711 | request1.url = GURL("https://www.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6712 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6713 | request1.traffic_annotation = |
| 6714 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6715 | |
| 6716 | HttpRequestInfo request2; |
| 6717 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6718 | request2.url = GURL("https://mail.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6719 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6720 | request2.traffic_annotation = |
| 6721 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6722 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6723 | // CONNECT to www.example.org:443 via SPDY. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6724 | spdy::SpdySerializedFrame connect1(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6725 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6726 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6727 | spdy::SpdySerializedFrame conn_resp1( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6728 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6729 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6730 | // Fetch https://www.example.org/ via HTTP. |
| 6731 | const char get1[] = |
| 6732 | "GET / HTTP/1.1\r\n" |
| 6733 | "Host: www.example.org\r\n" |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6734 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6735 | spdy::SpdySerializedFrame wrapped_get1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6736 | spdy_util_.ConstructSpdyDataFrame(1, get1, false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6737 | const char resp1[] = "HTTP/1.1 200 OK\r\n" |
| 6738 | "Content-Length: 1\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6739 | spdy::SpdySerializedFrame wrapped_get_resp1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6740 | spdy_util_.ConstructSpdyDataFrame(1, resp1, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6741 | spdy::SpdySerializedFrame wrapped_body1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6742 | spdy_util_.ConstructSpdyDataFrame(1, "1", false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6743 | spdy::SpdySerializedFrame window_update( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6744 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp1.size())); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6745 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6746 | // CONNECT to mail.example.org:443 via SPDY. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6747 | spdy::SpdyHeaderBlock connect2_block; |
| 6748 | connect2_block[spdy::kHttp2MethodHeader] = "CONNECT"; |
| 6749 | connect2_block[spdy::kHttp2AuthorityHeader] = "mail.example.org:443"; |
| 6750 | spdy::SpdySerializedFrame connect2(spdy_util_.ConstructSpdyHeaders( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6751 | 3, std::move(connect2_block), HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6752 | false)); |
[email protected] | 601e03f1 | 2014-04-06 16:26:39 | [diff] [blame] | 6753 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6754 | spdy::SpdySerializedFrame conn_resp2( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6755 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6756 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6757 | // Fetch https://mail.example.org/ via HTTP. |
| 6758 | const char get2[] = |
| 6759 | "GET / HTTP/1.1\r\n" |
| 6760 | "Host: mail.example.org\r\n" |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6761 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6762 | spdy::SpdySerializedFrame wrapped_get2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6763 | spdy_util_.ConstructSpdyDataFrame(3, get2, false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6764 | const char resp2[] = "HTTP/1.1 200 OK\r\n" |
| 6765 | "Content-Length: 2\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6766 | spdy::SpdySerializedFrame wrapped_get_resp2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6767 | spdy_util_.ConstructSpdyDataFrame(3, resp2, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6768 | spdy::SpdySerializedFrame wrapped_body2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6769 | spdy_util_.ConstructSpdyDataFrame(3, "22", false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6770 | |
| 6771 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6772 | CreateMockWrite(connect1, 0), CreateMockWrite(wrapped_get1, 2), |
| 6773 | CreateMockWrite(connect2, 5), CreateMockWrite(wrapped_get2, 7), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6774 | }; |
| 6775 | |
| 6776 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6777 | CreateMockRead(conn_resp1, 1, ASYNC), |
| 6778 | CreateMockRead(wrapped_get_resp1, 3, ASYNC), |
| 6779 | CreateMockRead(wrapped_body1, 4, ASYNC), |
| 6780 | CreateMockRead(conn_resp2, 6, ASYNC), |
| 6781 | CreateMockRead(wrapped_get_resp2, 8, ASYNC), |
| 6782 | CreateMockRead(wrapped_body2, 9, ASYNC), |
| 6783 | MockRead(ASYNC, 0, 10), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6784 | }; |
| 6785 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6786 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6787 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6788 | |
| 6789 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6790 | ssl.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6791 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6792 | SSLSocketDataProvider ssl2(ASYNC, OK); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6793 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6794 | SSLSocketDataProvider ssl3(ASYNC, OK); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6795 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6796 | |
| 6797 | TestCompletionCallback callback; |
| 6798 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6799 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6800 | int rv = trans.Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6801 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6802 | |
| 6803 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6804 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6805 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); |
| 6806 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6807 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6808 | ASSERT_TRUE(response); |
| 6809 | ASSERT_TRUE(response->headers); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6810 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6811 | |
| 6812 | std::string response_data; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 6813 | scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6814 | rv = trans.Read(buf.get(), 256, callback.callback()); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6815 | EXPECT_EQ(1, callback.GetResult(rv)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6816 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6817 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6818 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6819 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6820 | |
| 6821 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6822 | EXPECT_TRUE(trans2.GetLoadTimingInfo(&load_timing_info2)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6823 | // Even though the SPDY connection is reused, a new tunnelled connection has |
| 6824 | // to be created, so the socket's load timing looks like a fresh connection. |
| 6825 | TestLoadTimingNotReused(load_timing_info2, CONNECT_TIMING_HAS_SSL_TIMES); |
| 6826 | |
| 6827 | // The requests should have different IDs, since they each are using their own |
| 6828 | // separate stream. |
| 6829 | EXPECT_NE(load_timing_info.socket_log_id, load_timing_info2.socket_log_id); |
| 6830 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6831 | rv = trans2.Read(buf.get(), 256, callback.callback()); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6832 | EXPECT_EQ(2, callback.GetResult(rv)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6833 | } |
| 6834 | |
| 6835 | // Test load timing in the case of two HTTPS (non-SPDY) requests through a SPDY |
| 6836 | // HTTPS Proxy to the same server. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6837 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6838 | HttpsProxySpdyConnectHttpsLoadTimingTwoRequestsSameServer) { |
| 6839 | // Configure against https proxy server "proxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6840 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6841 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6842 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6843 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6844 | std::unique_ptr<HttpNetworkSession> session( |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6845 | SpdySessionDependencies::SpdyCreateSession(&session_deps_)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6846 | |
| 6847 | HttpRequestInfo request1; |
| 6848 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6849 | request1.url = GURL("https://www.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6850 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6851 | request1.traffic_annotation = |
| 6852 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6853 | |
| 6854 | HttpRequestInfo request2; |
| 6855 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6856 | request2.url = GURL("https://www.example.org/2"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6857 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6858 | request2.traffic_annotation = |
| 6859 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6860 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6861 | // CONNECT to www.example.org:443 via SPDY. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6862 | spdy::SpdySerializedFrame connect1(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6863 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6864 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6865 | spdy::SpdySerializedFrame conn_resp1( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6866 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6867 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6868 | // Fetch https://www.example.org/ via HTTP. |
| 6869 | const char get1[] = |
| 6870 | "GET / HTTP/1.1\r\n" |
| 6871 | "Host: www.example.org\r\n" |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6872 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6873 | spdy::SpdySerializedFrame wrapped_get1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6874 | spdy_util_.ConstructSpdyDataFrame(1, get1, false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6875 | const char resp1[] = "HTTP/1.1 200 OK\r\n" |
| 6876 | "Content-Length: 1\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6877 | spdy::SpdySerializedFrame wrapped_get_resp1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6878 | spdy_util_.ConstructSpdyDataFrame(1, resp1, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6879 | spdy::SpdySerializedFrame wrapped_body1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6880 | spdy_util_.ConstructSpdyDataFrame(1, "1", false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6881 | spdy::SpdySerializedFrame window_update( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6882 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp1.size())); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6883 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6884 | // Fetch https://www.example.org/2 via HTTP. |
| 6885 | const char get2[] = |
| 6886 | "GET /2 HTTP/1.1\r\n" |
| 6887 | "Host: www.example.org\r\n" |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6888 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6889 | spdy::SpdySerializedFrame wrapped_get2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6890 | spdy_util_.ConstructSpdyDataFrame(1, get2, false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6891 | const char resp2[] = "HTTP/1.1 200 OK\r\n" |
| 6892 | "Content-Length: 2\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6893 | spdy::SpdySerializedFrame wrapped_get_resp2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6894 | spdy_util_.ConstructSpdyDataFrame(1, resp2, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6895 | spdy::SpdySerializedFrame wrapped_body2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6896 | spdy_util_.ConstructSpdyDataFrame(1, "22", false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6897 | |
| 6898 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6899 | CreateMockWrite(connect1, 0), CreateMockWrite(wrapped_get1, 2), |
| 6900 | CreateMockWrite(wrapped_get2, 5), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6901 | }; |
| 6902 | |
| 6903 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6904 | CreateMockRead(conn_resp1, 1, ASYNC), |
| 6905 | CreateMockRead(wrapped_get_resp1, 3, ASYNC), |
bnc | eb9aa711 | 2017-01-05 01:03:46 | [diff] [blame] | 6906 | CreateMockRead(wrapped_body1, 4, SYNCHRONOUS), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6907 | CreateMockRead(wrapped_get_resp2, 6, ASYNC), |
bnc | eb9aa711 | 2017-01-05 01:03:46 | [diff] [blame] | 6908 | CreateMockRead(wrapped_body2, 7, SYNCHRONOUS), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6909 | MockRead(ASYNC, 0, 8), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6910 | }; |
| 6911 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6912 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6913 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6914 | |
| 6915 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6916 | ssl.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6917 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6918 | SSLSocketDataProvider ssl2(ASYNC, OK); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6919 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6920 | |
| 6921 | TestCompletionCallback callback; |
| 6922 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 6923 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6924 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6925 | int rv = trans->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6926 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6927 | |
| 6928 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6929 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6930 | |
| 6931 | LoadTimingInfo load_timing_info; |
| 6932 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 6933 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); |
| 6934 | |
| 6935 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6936 | ASSERT_TRUE(response); |
| 6937 | ASSERT_TRUE(response->headers); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6938 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6939 | |
| 6940 | std::string response_data; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 6941 | scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6942 | EXPECT_EQ(1, trans->Read(buf.get(), 256, callback.callback())); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6943 | trans.reset(); |
| 6944 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 6945 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6946 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6947 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6948 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6949 | |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6950 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6951 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6952 | |
| 6953 | LoadTimingInfo load_timing_info2; |
| 6954 | EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); |
| 6955 | TestLoadTimingReused(load_timing_info2); |
| 6956 | |
| 6957 | // The requests should have the same ID. |
| 6958 | EXPECT_EQ(load_timing_info.socket_log_id, load_timing_info2.socket_log_id); |
| 6959 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6960 | EXPECT_EQ(2, trans2->Read(buf.get(), 256, callback.callback())); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6961 | } |
| 6962 | |
| 6963 | // Test load timing in the case of of two HTTP requests through a SPDY HTTPS |
| 6964 | // Proxy to different servers. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6965 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyLoadTimingTwoHttpRequests) { |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6966 | // Configure against https proxy server "proxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 6967 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 6968 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6969 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6970 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6971 | std::unique_ptr<HttpNetworkSession> session( |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6972 | SpdySessionDependencies::SpdyCreateSession(&session_deps_)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6973 | |
| 6974 | HttpRequestInfo request1; |
| 6975 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6976 | request1.url = GURL("http://www.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6977 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6978 | request1.traffic_annotation = |
| 6979 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6980 | |
| 6981 | HttpRequestInfo request2; |
| 6982 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6983 | request2.url = GURL("http://mail.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6984 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 6985 | request2.traffic_annotation = |
| 6986 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6987 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6988 | // http://www.example.org/ |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6989 | spdy::SpdyHeaderBlock headers( |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6990 | spdy_util_.ConstructGetHeaderBlockForProxy("http://www.example.org/")); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6991 | spdy::SpdySerializedFrame get1( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 6992 | spdy_util_.ConstructSpdyHeaders(1, std::move(headers), LOWEST, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6993 | spdy::SpdySerializedFrame get_resp1( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6994 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6995 | spdy::SpdySerializedFrame body1( |
| 6996 | spdy_util_.ConstructSpdyDataFrame(1, "1", true)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 6997 | spdy_util_.UpdateWithStreamDestruction(1); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6998 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6999 | // http://mail.example.org/ |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7000 | spdy::SpdyHeaderBlock headers2( |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7001 | spdy_util_.ConstructGetHeaderBlockForProxy("http://mail.example.org/")); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7002 | spdy::SpdySerializedFrame get2( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 7003 | spdy_util_.ConstructSpdyHeaders(3, std::move(headers2), LOWEST, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7004 | spdy::SpdySerializedFrame get_resp2( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 7005 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7006 | spdy::SpdySerializedFrame body2( |
| 7007 | spdy_util_.ConstructSpdyDataFrame(3, "22", true)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7008 | |
| 7009 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 7010 | CreateMockWrite(get1, 0), CreateMockWrite(get2, 3), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7011 | }; |
| 7012 | |
| 7013 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 7014 | CreateMockRead(get_resp1, 1, ASYNC), |
| 7015 | CreateMockRead(body1, 2, ASYNC), |
| 7016 | CreateMockRead(get_resp2, 4, ASYNC), |
| 7017 | CreateMockRead(body2, 5, ASYNC), |
| 7018 | MockRead(ASYNC, 0, 6), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7019 | }; |
| 7020 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7021 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 7022 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7023 | |
| 7024 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 7025 | ssl.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 7026 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7027 | |
| 7028 | TestCompletionCallback callback; |
| 7029 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 7030 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 7031 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7032 | int rv = trans->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7033 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7034 | |
| 7035 | LoadTimingInfo load_timing_info; |
| 7036 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 7037 | TestLoadTimingNotReused(load_timing_info, |
| 7038 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 7039 | |
| 7040 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7041 | ASSERT_TRUE(response); |
| 7042 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 7043 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7044 | |
| 7045 | std::string response_data; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 7046 | scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 7047 | rv = trans->Read(buf.get(), 256, callback.callback()); |
| 7048 | EXPECT_EQ(1, callback.GetResult(rv)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7049 | // Delete the first request, so the second one can reuse the socket. |
| 7050 | trans.reset(); |
| 7051 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7052 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7053 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7054 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7055 | |
| 7056 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7057 | EXPECT_TRUE(trans2.GetLoadTimingInfo(&load_timing_info2)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7058 | TestLoadTimingReused(load_timing_info2); |
| 7059 | |
| 7060 | // The requests should have the same ID. |
| 7061 | EXPECT_EQ(load_timing_info.socket_log_id, load_timing_info2.socket_log_id); |
| 7062 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7063 | rv = trans2.Read(buf.get(), 256, callback.callback()); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 7064 | EXPECT_EQ(2, callback.GetResult(rv)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7065 | } |
| 7066 | |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7067 | // Test that an HTTP/2 CONNECT through an HTTPS Proxy to a HTTP/2 server and a |
| 7068 | // direct (non-proxied) request to the proxy server are not pooled, as that |
| 7069 | // would break socket pool isolation. |
| 7070 | TEST_F(HttpNetworkTransactionTest, SpdyProxyIsolation1) { |
| 7071 | ProxyConfig proxy_config; |
| 7072 | proxy_config.set_auto_detect(true); |
| 7073 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 7074 | |
| 7075 | CapturingProxyResolver capturing_proxy_resolver; |
| 7076 | session_deps_.proxy_resolution_service = |
| 7077 | std::make_unique<ProxyResolutionService>( |
| 7078 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 7079 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 7080 | std::make_unique<CapturingProxyResolverFactory>( |
| 7081 | &capturing_proxy_resolver), |
| 7082 | nullptr); |
| 7083 | |
| 7084 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 7085 | |
| 7086 | SpdyTestUtil spdy_util1; |
| 7087 | // CONNECT to www.example.org:443 via HTTP/2. |
| 7088 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 7089 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 7090 | HostPortPair("www.example.org", 443))); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7091 | // fetch https://www.example.org/ via HTTP/2. |
| 7092 | const char kMyUrl[] = "https://www.example.org/"; |
| 7093 | spdy::SpdySerializedFrame get(spdy_util1.ConstructSpdyGet(kMyUrl, 1, LOWEST)); |
| 7094 | spdy::SpdySerializedFrame wrapped_get( |
| 7095 | spdy_util_.ConstructWrappedSpdyFrame(get, 1)); |
| 7096 | spdy::SpdySerializedFrame conn_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 7097 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7098 | spdy::SpdySerializedFrame get_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 7099 | spdy_util1.ConstructSpdyGetReply(nullptr, 0, 1)); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7100 | spdy::SpdySerializedFrame wrapped_get_resp( |
| 7101 | spdy_util_.ConstructWrappedSpdyFrame(get_resp, 1)); |
| 7102 | spdy::SpdySerializedFrame body(spdy_util1.ConstructSpdyDataFrame(1, true)); |
| 7103 | spdy::SpdySerializedFrame wrapped_body( |
| 7104 | spdy_util_.ConstructWrappedSpdyFrame(body, 1)); |
| 7105 | spdy::SpdySerializedFrame window_update_get_resp( |
| 7106 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp.size())); |
| 7107 | spdy::SpdySerializedFrame window_update_body( |
| 7108 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_body.size())); |
| 7109 | |
| 7110 | MockWrite spdy_writes1[] = { |
| 7111 | CreateMockWrite(connect, 0), |
| 7112 | CreateMockWrite(wrapped_get, 2), |
| 7113 | CreateMockWrite(window_update_get_resp, 6), |
| 7114 | CreateMockWrite(window_update_body, 7), |
| 7115 | }; |
| 7116 | |
| 7117 | MockRead spdy_reads1[] = { |
| 7118 | CreateMockRead(conn_resp, 1, ASYNC), |
| 7119 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
| 7120 | CreateMockRead(wrapped_get_resp, 4, ASYNC), |
| 7121 | CreateMockRead(wrapped_body, 5, ASYNC), |
| 7122 | MockRead(ASYNC, 0, 8), |
| 7123 | }; |
| 7124 | |
| 7125 | SequencedSocketData spdy_data1(spdy_reads1, spdy_writes1); |
| 7126 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data1); |
| 7127 | |
| 7128 | // Fetch https://proxy:70/ via HTTP/2. Needs a new SpdyTestUtil, since it uses |
| 7129 | // a new pipe. |
| 7130 | SpdyTestUtil spdy_util2; |
| 7131 | spdy::SpdySerializedFrame req( |
| 7132 | spdy_util2.ConstructSpdyGet("https://proxy:70/", 1, LOWEST)); |
| 7133 | MockWrite spdy_writes2[] = {CreateMockWrite(req, 0)}; |
| 7134 | |
| 7135 | spdy::SpdySerializedFrame resp( |
| 7136 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 7137 | spdy::SpdySerializedFrame data(spdy_util2.ConstructSpdyDataFrame(1, true)); |
| 7138 | MockRead spdy_reads2[] = { |
| 7139 | CreateMockRead(resp, 1), |
| 7140 | CreateMockRead(data, 2), |
| 7141 | MockRead(ASYNC, 0, 3), |
| 7142 | }; |
| 7143 | SequencedSocketData spdy_data2(spdy_reads2, spdy_writes2); |
| 7144 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data2); |
| 7145 | |
| 7146 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 7147 | ssl.next_proto = kProtoHTTP2; |
| 7148 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 7149 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 7150 | ssl2.next_proto = kProtoHTTP2; |
| 7151 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 7152 | SSLSocketDataProvider ssl3(ASYNC, OK); |
| 7153 | ssl3.next_proto = kProtoHTTP2; |
| 7154 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
| 7155 | |
| 7156 | TestCompletionCallback callback; |
| 7157 | std::string response_data; |
| 7158 | |
| 7159 | // Make a request using proxy:70 as a HTTP/2 proxy. |
| 7160 | capturing_proxy_resolver.set_proxy_server( |
| 7161 | ProxyServer(ProxyServer::SCHEME_HTTPS, HostPortPair("proxy", 70))); |
| 7162 | HttpRequestInfo request1; |
| 7163 | request1.method = "GET"; |
| 7164 | request1.url = GURL("https://www.example.org/"); |
| 7165 | request1.traffic_annotation = |
| 7166 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7167 | |
| 7168 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
| 7169 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
| 7170 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 7171 | |
| 7172 | // Allow the SpdyProxyClientSocket's write callback to complete. |
| 7173 | base::RunLoop().RunUntilIdle(); |
| 7174 | // Now allow the read of the response to complete. |
| 7175 | spdy_data1.Resume(); |
| 7176 | rv = callback.WaitForResult(); |
| 7177 | EXPECT_THAT(rv, IsOk()); |
| 7178 | |
| 7179 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 7180 | ASSERT_TRUE(response); |
| 7181 | ASSERT_TRUE(response->headers); |
| 7182 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 7183 | |
| 7184 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 7185 | EXPECT_EQ(kUploadData, response_data); |
| 7186 | RunUntilIdle(); |
| 7187 | |
| 7188 | // Make a direct HTTP/2 request to proxy:70. |
| 7189 | capturing_proxy_resolver.set_proxy_server(ProxyServer::Direct()); |
| 7190 | HttpRequestInfo request2; |
| 7191 | request2.method = "GET"; |
| 7192 | request2.url = GURL("https://proxy:70/"); |
| 7193 | request2.traffic_annotation = |
| 7194 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7195 | HttpNetworkTransaction trans2(LOWEST, session.get()); |
| 7196 | EXPECT_THAT(callback.GetResult(trans2.Start(&request2, callback.callback(), |
| 7197 | NetLogWithSource())), |
| 7198 | IsOk()); |
| 7199 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 7200 | } |
| 7201 | |
| 7202 | // Same as above, but reverse request order, since the code to check for an |
| 7203 | // existing session is different for tunnels and direct connections. |
| 7204 | TEST_F(HttpNetworkTransactionTest, SpdyProxyIsolation2) { |
| 7205 | // Configure against https proxy server "myproxy:80". |
| 7206 | ProxyConfig proxy_config; |
| 7207 | proxy_config.set_auto_detect(true); |
| 7208 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 7209 | |
| 7210 | CapturingProxyResolver capturing_proxy_resolver; |
| 7211 | session_deps_.proxy_resolution_service = |
| 7212 | std::make_unique<ProxyResolutionService>( |
| 7213 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 7214 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 7215 | std::make_unique<CapturingProxyResolverFactory>( |
| 7216 | &capturing_proxy_resolver), |
| 7217 | nullptr); |
| 7218 | |
| 7219 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 7220 | // Fetch https://proxy:70/ via HTTP/2. |
| 7221 | SpdyTestUtil spdy_util1; |
| 7222 | spdy::SpdySerializedFrame req( |
| 7223 | spdy_util1.ConstructSpdyGet("https://proxy:70/", 1, LOWEST)); |
| 7224 | MockWrite spdy_writes1[] = {CreateMockWrite(req, 0)}; |
| 7225 | |
| 7226 | spdy::SpdySerializedFrame resp( |
| 7227 | spdy_util1.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 7228 | spdy::SpdySerializedFrame data(spdy_util1.ConstructSpdyDataFrame(1, true)); |
| 7229 | MockRead spdy_reads1[] = { |
| 7230 | CreateMockRead(resp, 1), |
| 7231 | CreateMockRead(data, 2), |
| 7232 | MockRead(ASYNC, 0, 3), |
| 7233 | }; |
| 7234 | SequencedSocketData spdy_data1(spdy_reads1, spdy_writes1); |
| 7235 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data1); |
| 7236 | |
| 7237 | SpdyTestUtil spdy_util2; |
| 7238 | // CONNECT to www.example.org:443 via HTTP/2. |
| 7239 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 7240 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 7241 | HostPortPair("www.example.org", 443))); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7242 | // fetch https://www.example.org/ via HTTP/2. |
| 7243 | const char kMyUrl[] = "https://www.example.org/"; |
| 7244 | spdy::SpdySerializedFrame get(spdy_util2.ConstructSpdyGet(kMyUrl, 1, LOWEST)); |
| 7245 | spdy::SpdySerializedFrame wrapped_get( |
| 7246 | spdy_util_.ConstructWrappedSpdyFrame(get, 1)); |
| 7247 | spdy::SpdySerializedFrame conn_resp( |
| 7248 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 7249 | spdy::SpdySerializedFrame get_resp( |
| 7250 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 7251 | spdy::SpdySerializedFrame wrapped_get_resp( |
| 7252 | spdy_util_.ConstructWrappedSpdyFrame(get_resp, 1)); |
| 7253 | spdy::SpdySerializedFrame body(spdy_util2.ConstructSpdyDataFrame(1, true)); |
| 7254 | spdy::SpdySerializedFrame wrapped_body( |
| 7255 | spdy_util_.ConstructWrappedSpdyFrame(body, 1)); |
| 7256 | spdy::SpdySerializedFrame window_update_get_resp( |
| 7257 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp.size())); |
| 7258 | spdy::SpdySerializedFrame window_update_body( |
| 7259 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_body.size())); |
| 7260 | |
| 7261 | MockWrite spdy_writes2[] = { |
| 7262 | CreateMockWrite(connect, 0), |
| 7263 | CreateMockWrite(wrapped_get, 2), |
| 7264 | CreateMockWrite(window_update_get_resp, 6), |
| 7265 | CreateMockWrite(window_update_body, 7), |
| 7266 | }; |
| 7267 | |
| 7268 | MockRead spdy_reads2[] = { |
| 7269 | CreateMockRead(conn_resp, 1, ASYNC), |
| 7270 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
| 7271 | CreateMockRead(wrapped_get_resp, 4, ASYNC), |
| 7272 | CreateMockRead(wrapped_body, 5, ASYNC), |
| 7273 | MockRead(ASYNC, 0, 8), |
| 7274 | }; |
| 7275 | |
| 7276 | SequencedSocketData spdy_data2(spdy_reads2, spdy_writes2); |
| 7277 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data2); |
| 7278 | |
| 7279 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 7280 | ssl.next_proto = kProtoHTTP2; |
| 7281 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 7282 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 7283 | ssl2.next_proto = kProtoHTTP2; |
| 7284 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 7285 | SSLSocketDataProvider ssl3(ASYNC, OK); |
| 7286 | ssl3.next_proto = kProtoHTTP2; |
| 7287 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
| 7288 | |
| 7289 | TestCompletionCallback callback; |
| 7290 | std::string response_data; |
| 7291 | |
| 7292 | // Make a direct HTTP/2 request to proxy:70. |
| 7293 | capturing_proxy_resolver.set_proxy_server(ProxyServer::Direct()); |
| 7294 | HttpRequestInfo request1; |
| 7295 | request1.method = "GET"; |
| 7296 | request1.url = GURL("https://proxy:70/"); |
| 7297 | request1.traffic_annotation = |
| 7298 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7299 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
| 7300 | EXPECT_THAT(callback.GetResult(trans1.Start(&request1, callback.callback(), |
| 7301 | NetLogWithSource())), |
| 7302 | IsOk()); |
| 7303 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 7304 | RunUntilIdle(); |
| 7305 | |
| 7306 | // Make a request using proxy:70 as a HTTP/2 proxy. |
| 7307 | capturing_proxy_resolver.set_proxy_server( |
| 7308 | ProxyServer(ProxyServer::SCHEME_HTTPS, HostPortPair("proxy", 70))); |
| 7309 | HttpRequestInfo request2; |
| 7310 | request2.method = "GET"; |
| 7311 | request2.url = GURL("https://www.example.org/"); |
| 7312 | request2.traffic_annotation = |
| 7313 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7314 | |
| 7315 | HttpNetworkTransaction trans2(LOWEST, session.get()); |
| 7316 | int rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
| 7317 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 7318 | |
| 7319 | // Allow the SpdyProxyClientSocket's write callback to complete. |
| 7320 | base::RunLoop().RunUntilIdle(); |
| 7321 | // Now allow the read of the response to complete. |
| 7322 | spdy_data2.Resume(); |
| 7323 | rv = callback.WaitForResult(); |
| 7324 | EXPECT_THAT(rv, IsOk()); |
| 7325 | |
| 7326 | const HttpResponseInfo* response2 = trans2.GetResponseInfo(); |
| 7327 | ASSERT_TRUE(response2); |
| 7328 | ASSERT_TRUE(response2->headers); |
| 7329 | EXPECT_EQ("HTTP/1.1 200", response2->headers->GetStatusLine()); |
| 7330 | |
| 7331 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 7332 | EXPECT_EQ(kUploadData, response_data); |
| 7333 | } |
| 7334 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7335 | // Test the challenge-response-retry sequence through an HTTPS Proxy |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7336 | TEST_F(HttpNetworkTransactionTest, HttpsProxyAuthRetry) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7337 | HttpRequestInfo request; |
| 7338 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7339 | request.url = GURL("http://www.example.org/"); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7340 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7341 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 7342 | request.traffic_annotation = |
| 7343 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7344 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 7345 | // Configure against https proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 7346 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 7347 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 7348 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7349 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7350 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7351 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7352 | // Since we have proxy, should use full url |
| 7353 | MockWrite data_writes1[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7354 | MockWrite("GET http://www.example.org/ HTTP/1.1\r\n" |
| 7355 | "Host: www.example.org\r\n" |
| 7356 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7357 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7358 | // After calling trans.RestartWithAuth(), this is the request we should |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7359 | // be issuing -- the final header line contains the credentials. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7360 | MockWrite("GET http://www.example.org/ HTTP/1.1\r\n" |
| 7361 | "Host: www.example.org\r\n" |
| 7362 | "Proxy-Connection: keep-alive\r\n" |
| 7363 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7364 | }; |
| 7365 | |
| 7366 | // The proxy responds to the GET with a 407, using a persistent |
| 7367 | // connection. |
| 7368 | MockRead data_reads1[] = { |
| 7369 | // No credentials. |
| 7370 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 7371 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 7372 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 7373 | MockRead("Content-Length: 0\r\n\r\n"), |
| 7374 | |
| 7375 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7376 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7377 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7378 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7379 | }; |
| 7380 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7381 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7382 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7383 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7384 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7385 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7386 | TestCompletionCallback callback1; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7387 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7388 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7389 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7390 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7391 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7392 | |
| 7393 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7394 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7395 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7396 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7397 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7398 | TestLoadTimingNotReused(load_timing_info, |
| 7399 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 7400 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7401 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7402 | ASSERT_TRUE(response); |
| 7403 | ASSERT_TRUE(response->headers); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7404 | EXPECT_EQ(407, response->headers->response_code()); |
| 7405 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7406 | EXPECT_TRUE(CheckBasicSecureProxyAuth(response->auth_challenge)); |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 7407 | EXPECT_FALSE(response->did_use_http_auth); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7408 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7409 | TestCompletionCallback callback2; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7410 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7411 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7412 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7413 | |
| 7414 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7415 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7416 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7417 | load_timing_info = LoadTimingInfo(); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7418 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7419 | // Retrying with HTTP AUTH is considered to be reusing a socket. |
| 7420 | TestLoadTimingReused(load_timing_info); |
| 7421 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7422 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7423 | ASSERT_TRUE(response); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7424 | |
| 7425 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 7426 | EXPECT_EQ(200, response->headers->response_code()); |
| 7427 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 7428 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 7429 | EXPECT_TRUE(response->did_use_http_auth); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7430 | |
| 7431 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7432 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7433 | } |
| 7434 | |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 7435 | void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus( |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7436 | const MockRead& status, int expected_status) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 7437 | HttpRequestInfo request; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7438 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7439 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 7440 | request.traffic_annotation = |
| 7441 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7442 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7443 | // Configure against proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 7444 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 7445 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7446 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7447 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7448 | // Since we have proxy, should try to establish tunnel. |
| 7449 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 7450 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 7451 | "Host: www.example.org:443\r\n" |
| 7452 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7453 | }; |
| 7454 | |
| 7455 | MockRead data_reads[] = { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 7456 | status, MockRead("Content-Length: 10\r\n\r\n"), |
| 7457 | // No response body because the test stops reading here. |
| 7458 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7459 | }; |
| 7460 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7461 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7462 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7463 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7464 | TestCompletionCallback callback; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7465 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7466 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7467 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7468 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7469 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7470 | |
| 7471 | rv = callback.WaitForResult(); |
| 7472 | EXPECT_EQ(expected_status, rv); |
| 7473 | } |
| 7474 | |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 7475 | void HttpNetworkTransactionTest::ConnectStatusHelper( |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 7476 | const MockRead& status) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7477 | ConnectStatusHelperWithExpectedStatus( |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 7478 | status, ERR_TUNNEL_CONNECTION_FAILED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7479 | } |
| 7480 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7481 | TEST_F(HttpNetworkTransactionTest, ConnectStatus100) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7482 | ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n")); |
| 7483 | } |
| 7484 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7485 | TEST_F(HttpNetworkTransactionTest, ConnectStatus101) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7486 | ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n")); |
| 7487 | } |
| 7488 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7489 | TEST_F(HttpNetworkTransactionTest, ConnectStatus201) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7490 | ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n")); |
| 7491 | } |
| 7492 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7493 | TEST_F(HttpNetworkTransactionTest, ConnectStatus202) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7494 | ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n")); |
| 7495 | } |
| 7496 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7497 | TEST_F(HttpNetworkTransactionTest, ConnectStatus203) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7498 | ConnectStatusHelper( |
| 7499 | MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n")); |
| 7500 | } |
| 7501 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7502 | TEST_F(HttpNetworkTransactionTest, ConnectStatus204) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7503 | ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n")); |
| 7504 | } |
| 7505 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7506 | TEST_F(HttpNetworkTransactionTest, ConnectStatus205) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7507 | ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n")); |
| 7508 | } |
| 7509 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7510 | TEST_F(HttpNetworkTransactionTest, ConnectStatus206) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7511 | ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n")); |
| 7512 | } |
| 7513 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7514 | TEST_F(HttpNetworkTransactionTest, ConnectStatus300) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7515 | ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n")); |
| 7516 | } |
| 7517 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7518 | TEST_F(HttpNetworkTransactionTest, ConnectStatus301) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7519 | ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n")); |
| 7520 | } |
| 7521 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7522 | TEST_F(HttpNetworkTransactionTest, ConnectStatus302) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7523 | ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n")); |
| 7524 | } |
| 7525 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7526 | TEST_F(HttpNetworkTransactionTest, ConnectStatus303) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7527 | ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n")); |
| 7528 | } |
| 7529 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7530 | TEST_F(HttpNetworkTransactionTest, ConnectStatus304) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7531 | ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n")); |
| 7532 | } |
| 7533 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7534 | TEST_F(HttpNetworkTransactionTest, ConnectStatus305) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7535 | ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n")); |
| 7536 | } |
| 7537 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7538 | TEST_F(HttpNetworkTransactionTest, ConnectStatus306) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7539 | ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n")); |
| 7540 | } |
| 7541 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7542 | TEST_F(HttpNetworkTransactionTest, ConnectStatus307) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7543 | ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n")); |
| 7544 | } |
| 7545 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7546 | TEST_F(HttpNetworkTransactionTest, ConnectStatus308) { |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7547 | ConnectStatusHelper(MockRead("HTTP/1.1 308 Permanent Redirect\r\n")); |
| 7548 | } |
| 7549 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7550 | TEST_F(HttpNetworkTransactionTest, ConnectStatus400) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7551 | ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n")); |
| 7552 | } |
| 7553 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7554 | TEST_F(HttpNetworkTransactionTest, ConnectStatus401) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7555 | ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n")); |
| 7556 | } |
| 7557 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7558 | TEST_F(HttpNetworkTransactionTest, ConnectStatus402) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7559 | ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n")); |
| 7560 | } |
| 7561 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7562 | TEST_F(HttpNetworkTransactionTest, ConnectStatus403) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7563 | ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n")); |
| 7564 | } |
| 7565 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7566 | TEST_F(HttpNetworkTransactionTest, ConnectStatus404) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7567 | ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n")); |
| 7568 | } |
| 7569 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7570 | TEST_F(HttpNetworkTransactionTest, ConnectStatus405) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7571 | ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n")); |
| 7572 | } |
| 7573 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7574 | TEST_F(HttpNetworkTransactionTest, ConnectStatus406) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7575 | ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n")); |
| 7576 | } |
| 7577 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7578 | TEST_F(HttpNetworkTransactionTest, ConnectStatus407) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7579 | ConnectStatusHelperWithExpectedStatus( |
| 7580 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 7581 | ERR_PROXY_AUTH_UNSUPPORTED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7582 | } |
| 7583 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7584 | TEST_F(HttpNetworkTransactionTest, ConnectStatus408) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7585 | ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n")); |
| 7586 | } |
| 7587 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7588 | TEST_F(HttpNetworkTransactionTest, ConnectStatus409) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7589 | ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n")); |
| 7590 | } |
| 7591 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7592 | TEST_F(HttpNetworkTransactionTest, ConnectStatus410) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7593 | ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n")); |
| 7594 | } |
| 7595 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7596 | TEST_F(HttpNetworkTransactionTest, ConnectStatus411) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7597 | ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n")); |
| 7598 | } |
| 7599 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7600 | TEST_F(HttpNetworkTransactionTest, ConnectStatus412) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7601 | ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n")); |
| 7602 | } |
| 7603 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7604 | TEST_F(HttpNetworkTransactionTest, ConnectStatus413) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7605 | ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n")); |
| 7606 | } |
| 7607 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7608 | TEST_F(HttpNetworkTransactionTest, ConnectStatus414) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7609 | ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n")); |
| 7610 | } |
| 7611 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7612 | TEST_F(HttpNetworkTransactionTest, ConnectStatus415) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7613 | ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n")); |
| 7614 | } |
| 7615 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7616 | TEST_F(HttpNetworkTransactionTest, ConnectStatus416) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7617 | ConnectStatusHelper( |
| 7618 | MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n")); |
| 7619 | } |
| 7620 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7621 | TEST_F(HttpNetworkTransactionTest, ConnectStatus417) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7622 | ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n")); |
| 7623 | } |
| 7624 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7625 | TEST_F(HttpNetworkTransactionTest, ConnectStatus500) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7626 | ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n")); |
| 7627 | } |
| 7628 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7629 | TEST_F(HttpNetworkTransactionTest, ConnectStatus501) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7630 | ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n")); |
| 7631 | } |
| 7632 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7633 | TEST_F(HttpNetworkTransactionTest, ConnectStatus502) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7634 | ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n")); |
| 7635 | } |
| 7636 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7637 | TEST_F(HttpNetworkTransactionTest, ConnectStatus503) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7638 | ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n")); |
| 7639 | } |
| 7640 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7641 | TEST_F(HttpNetworkTransactionTest, ConnectStatus504) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7642 | ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n")); |
| 7643 | } |
| 7644 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7645 | TEST_F(HttpNetworkTransactionTest, ConnectStatus505) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7646 | ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n")); |
| 7647 | } |
| 7648 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7649 | // Test the flow when both the proxy server AND origin server require |
| 7650 | // authentication. Again, this uses basic auth for both since that is |
| 7651 | // the simplest to mock. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7652 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7653 | HttpRequestInfo request; |
| 7654 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7655 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 7656 | request.traffic_annotation = |
| 7657 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7658 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7659 | // Configure against proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 7660 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 7661 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7662 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3fe8d2f8 | 2013-10-17 08:56:07 | [diff] [blame] | 7663 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7664 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7665 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 7666 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7667 | MockWrite( |
| 7668 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 7669 | "Host: www.example.org\r\n" |
| 7670 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 7671 | }; |
| 7672 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7673 | MockRead data_reads1[] = { |
| 7674 | MockRead("HTTP/1.0 407 Unauthorized\r\n"), |
| 7675 | // Give a couple authenticate options (only the middle one is actually |
| 7676 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 7677 | MockRead("Proxy-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7678 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 7679 | MockRead("Proxy-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 7680 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7681 | // Large content-length -- won't matter, as connection will be reset. |
| 7682 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7683 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7684 | }; |
| 7685 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7686 | // After calling trans.RestartWithAuth() the first time, this is the |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7687 | // request we should be issuing -- the final header line contains the |
| 7688 | // proxy's credentials. |
| 7689 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7690 | MockWrite( |
| 7691 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 7692 | "Host: www.example.org\r\n" |
| 7693 | "Proxy-Connection: keep-alive\r\n" |
| 7694 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7695 | }; |
| 7696 | |
| 7697 | // Now the proxy server lets the request pass through to origin server. |
| 7698 | // The origin server responds with a 401. |
| 7699 | MockRead data_reads2[] = { |
| 7700 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 7701 | // Note: We are using the same realm-name as the proxy server. This is |
| 7702 | // completely valid, as realms are unique across hosts. |
| 7703 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 7704 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7705 | MockRead("Content-Length: 2000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7706 | MockRead(SYNCHRONOUS, ERR_FAILED), // Won't be reached. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7707 | }; |
| 7708 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7709 | // After calling trans.RestartWithAuth() the second time, we should send |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7710 | // the credentials for both the proxy and origin server. |
| 7711 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7712 | MockWrite( |
| 7713 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 7714 | "Host: www.example.org\r\n" |
| 7715 | "Proxy-Connection: keep-alive\r\n" |
| 7716 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 7717 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7718 | }; |
| 7719 | |
| 7720 | // Lastly we get the desired content. |
| 7721 | MockRead data_reads3[] = { |
| 7722 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 7723 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7724 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7725 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7726 | }; |
| 7727 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7728 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 7729 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 7730 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7731 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 7732 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 7733 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7734 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7735 | TestCompletionCallback callback1; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7736 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7737 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7738 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7739 | |
| 7740 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7741 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7742 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7743 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7744 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7745 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7746 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7747 | TestCompletionCallback callback2; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7748 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7749 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7750 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7751 | |
| 7752 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7753 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7754 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7755 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7756 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7757 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7758 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7759 | TestCompletionCallback callback3; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7760 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7761 | rv = trans.RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 7762 | callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7763 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7764 | |
| 7765 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7766 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7767 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7768 | response = trans.GetResponseInfo(); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7769 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7770 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7771 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 7772 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 7773 | // For the NTLM implementation using SSPI, we skip the NTLM tests since we |
| 7774 | // can't hook into its internals to cause it to generate predictable NTLM |
| 7775 | // authorization headers. |
| 7776 | #if defined(NTLM_PORTABLE) |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7777 | // The NTLM authentication unit tests are based on known test data from the |
| 7778 | // [MS-NLMP] Specification [1]. These tests are primarily of the authentication |
| 7779 | // flow rather than the implementation of the NTLM protocol. See net/ntlm |
| 7780 | // for the implementation and testing of the protocol. |
| 7781 | // |
| 7782 | // [1] https://msdn.microsoft.com/en-us/library/cc236621.aspx |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 7783 | |
| 7784 | // Enter the correct password and authenticate successfully. |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7785 | TEST_F(HttpNetworkTransactionTest, NTLMAuthV2) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 7786 | HttpRequestInfo request; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7787 | request.method = "GET"; |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7788 | request.url = GURL("https://server/kids/login.aspx"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 7789 | request.traffic_annotation = |
| 7790 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 7791 | |
| 7792 | // Ensure load is not disrupted by flags which suppress behaviour specific |
| 7793 | // to other auth schemes. |
| 7794 | request.load_flags = LOAD_DO_NOT_USE_EMBEDDED_IDENTITY; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7795 | |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 7796 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter( |
| 7797 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7798 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7799 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7800 | // Generate the NTLM messages based on known test data. |
| 7801 | std::string negotiate_msg; |
| 7802 | std::string challenge_msg; |
| 7803 | std::string authenticate_msg; |
| 7804 | base::Base64Encode( |
| 7805 | base::StringPiece( |
| 7806 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7807 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7808 | &negotiate_msg); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7809 | base::Base64Encode( |
| 7810 | base::StringPiece( |
| 7811 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7812 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7813 | &challenge_msg); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7814 | base::Base64Encode( |
| 7815 | base::StringPiece( |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 7816 | reinterpret_cast<const char*>( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7817 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7818 | base::size( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7819 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7820 | &authenticate_msg); |
| 7821 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7822 | MockWrite data_writes1[] = { |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7823 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 7824 | "Host: server\r\n" |
| 7825 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7826 | }; |
| 7827 | |
| 7828 | MockRead data_reads1[] = { |
| 7829 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 7830 | // Negotiate and NTLM are often requested together. However, we only want |
| 7831 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 7832 | // the header that requests Negotiate for this test. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7833 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 7834 | MockRead("Connection: close\r\n"), |
| 7835 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 7836 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7837 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7838 | }; |
| 7839 | |
| 7840 | MockWrite data_writes2[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7841 | // After restarting with a null identity, this is the |
| 7842 | // request we should be issuing -- the final header line contains a Type |
| 7843 | // 1 message. |
| 7844 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7845 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7846 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7847 | "Authorization: NTLM "), |
| 7848 | MockWrite(negotiate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7849 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7850 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7851 | // (using correct credentials). The second request continues on the |
| 7852 | // same connection. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7853 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7854 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7855 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7856 | "Authorization: NTLM "), |
| 7857 | MockWrite(authenticate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7858 | }; |
| 7859 | |
| 7860 | MockRead data_reads2[] = { |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 7861 | // The origin server responds with a Type 2 message. |
| 7862 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7863 | MockRead("WWW-Authenticate: NTLM "), MockRead(challenge_msg.c_str()), |
| 7864 | MockRead("\r\n"), MockRead("Content-Length: 42\r\n"), |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 7865 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 7866 | MockRead("You are not authorized to view this page\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7867 | |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 7868 | // Lastly we get the desired content. |
| 7869 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7870 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 7871 | MockRead("Content-Length: 14\r\n\r\n"), MockRead("Please Login\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7872 | }; |
| 7873 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7874 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 7875 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7876 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 7877 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7878 | |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 7879 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 7880 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 7881 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 7882 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 7883 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7884 | TestCompletionCallback callback1; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7885 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7886 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7887 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7888 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7889 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7890 | |
| 7891 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7892 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7893 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7894 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 7895 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7896 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7897 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7898 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7899 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7900 | TestCompletionCallback callback2; |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7901 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7902 | rv = trans.RestartWithAuth( |
| 7903 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 7904 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7905 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7906 | |
| 7907 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7908 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7909 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7910 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7911 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7912 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7913 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7914 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7915 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7916 | TestCompletionCallback callback3; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7917 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7918 | rv = trans.RestartWithAuth(AuthCredentials(), callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7919 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7920 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 7921 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7922 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7923 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7924 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7925 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7926 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 7927 | EXPECT_EQ(14, response->headers->GetContentLength()); |
| 7928 | |
| 7929 | std::string response_data; |
| 7930 | rv = ReadTransaction(&trans, &response_data); |
| 7931 | EXPECT_THAT(rv, IsOk()); |
| 7932 | EXPECT_EQ("Please Login\r\n", response_data); |
| 7933 | |
| 7934 | EXPECT_TRUE(data1.AllReadDataConsumed()); |
| 7935 | EXPECT_TRUE(data1.AllWriteDataConsumed()); |
| 7936 | EXPECT_TRUE(data2.AllReadDataConsumed()); |
| 7937 | EXPECT_TRUE(data2.AllWriteDataConsumed()); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7938 | } |
| 7939 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 7940 | // Enter a wrong password, and then the correct one. |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7941 | TEST_F(HttpNetworkTransactionTest, NTLMAuthV2WrongThenRightPassword) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 7942 | HttpRequestInfo request; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 7943 | request.method = "GET"; |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7944 | request.url = GURL("https://server/kids/login.aspx"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 7945 | request.traffic_annotation = |
| 7946 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 7947 | |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 7948 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter( |
| 7949 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7950 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7951 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7952 | // Generate the NTLM messages based on known test data. |
| 7953 | std::string negotiate_msg; |
| 7954 | std::string challenge_msg; |
| 7955 | std::string authenticate_msg; |
| 7956 | base::Base64Encode( |
| 7957 | base::StringPiece( |
| 7958 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7959 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7960 | &negotiate_msg); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7961 | base::Base64Encode( |
| 7962 | base::StringPiece( |
| 7963 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7964 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7965 | &challenge_msg); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7966 | base::Base64Encode( |
| 7967 | base::StringPiece( |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 7968 | reinterpret_cast<const char*>( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7969 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7970 | base::size( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7971 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7972 | &authenticate_msg); |
| 7973 | |
| 7974 | // The authenticate message when |kWrongPassword| is sent. |
| 7975 | std::string wrong_password_authenticate_msg( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7976 | "TlRMTVNTUAADAAAAGAAYAFgAAACKAIoAcAAAAAwADAD6AAAACAAIAAYBAAAQABAADgEAAAAA" |
| 7977 | "AABYAAAAA4IIAAAAAAAAAAAAAPknEYqtJQtusopDRSfYzAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 7978 | "AAAAAOtVz38osnFdRRggUQHUJ3EBAQAAAAAAAIALyP0A1NIBqqqqqqqqqqoAAAAAAgAMAEQA" |
| 7979 | "bwBtAGEAaQBuAAEADABTAGUAcgB2AGUAcgAGAAQAAgAAAAoAEAAAAAAAAAAAAAAAAAAAAAAA" |
| 7980 | "CQAWAEgAVABUAFAALwBzAGUAcgB2AGUAcgAAAAAAAAAAAEQAbwBtAGEAaQBuAFUAcwBlAHIA" |
| 7981 | "QwBPAE0AUABVAFQARQBSAA=="); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7982 | |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7983 | // Sanity check that it's the same length as the correct authenticate message |
| 7984 | // and that it's different. |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7985 | ASSERT_EQ(authenticate_msg.length(), |
| 7986 | wrong_password_authenticate_msg.length()); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7987 | ASSERT_NE(authenticate_msg, wrong_password_authenticate_msg); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7988 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 7989 | MockWrite data_writes1[] = { |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7990 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 7991 | "Host: server\r\n" |
| 7992 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 7993 | }; |
| 7994 | |
| 7995 | MockRead data_reads1[] = { |
| 7996 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 7997 | // Negotiate and NTLM are often requested together. However, we only want |
| 7998 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 7999 | // the header that requests Negotiate for this test. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8000 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 8001 | MockRead("Connection: close\r\n"), |
| 8002 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 8003 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8004 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8005 | }; |
| 8006 | |
| 8007 | MockWrite data_writes2[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8008 | // After restarting with a null identity, this is the |
| 8009 | // request we should be issuing -- the final header line contains a Type |
| 8010 | // 1 message. |
| 8011 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8012 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8013 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8014 | "Authorization: NTLM "), |
| 8015 | MockWrite(negotiate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8016 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8017 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8018 | // (using incorrect credentials). The second request continues on the |
| 8019 | // same connection. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8020 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8021 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8022 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8023 | "Authorization: NTLM "), |
| 8024 | MockWrite(wrong_password_authenticate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8025 | }; |
| 8026 | |
| 8027 | MockRead data_reads2[] = { |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8028 | // The origin server responds with a Type 2 message. |
| 8029 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 8030 | MockRead("WWW-Authenticate: NTLM "), MockRead(challenge_msg.c_str()), |
| 8031 | MockRead("\r\n"), MockRead("Content-Length: 42\r\n"), |
| 8032 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8033 | MockRead("You are not authorized to view this page\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8034 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8035 | // Wrong password. |
| 8036 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 8037 | MockRead("WWW-Authenticate: NTLM\r\n"), MockRead("Connection: close\r\n"), |
| 8038 | MockRead("Content-Length: 42\r\n"), |
| 8039 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8040 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8041 | }; |
| 8042 | |
| 8043 | MockWrite data_writes3[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8044 | // After restarting with a null identity, this is the |
| 8045 | // request we should be issuing -- the final header line contains a Type |
| 8046 | // 1 message. |
| 8047 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8048 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8049 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8050 | "Authorization: NTLM "), |
| 8051 | MockWrite(negotiate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8052 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8053 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
| 8054 | // (the credentials for the origin server). The second request continues |
| 8055 | // on the same connection. |
| 8056 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8057 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8058 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8059 | "Authorization: NTLM "), |
| 8060 | MockWrite(authenticate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8061 | }; |
| 8062 | |
| 8063 | MockRead data_reads3[] = { |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8064 | // The origin server responds with a Type 2 message. |
| 8065 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8066 | MockRead("WWW-Authenticate: NTLM "), MockRead(challenge_msg.c_str()), |
| 8067 | MockRead("\r\n"), MockRead("Content-Length: 42\r\n"), |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8068 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8069 | MockRead("You are not authorized to view this page\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8070 | |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8071 | // Lastly we get the desired content. |
| 8072 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8073 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 8074 | MockRead("Content-Length: 14\r\n\r\n"), MockRead("Please Login\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8075 | }; |
| 8076 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8077 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 8078 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 8079 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8080 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 8081 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 8082 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8083 | |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8084 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 8085 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 8086 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 8087 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 8088 | SSLSocketDataProvider ssl3(ASYNC, OK); |
| 8089 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
| 8090 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8091 | TestCompletionCallback callback1; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8092 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8093 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 8094 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8095 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8096 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8097 | |
| 8098 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8099 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8100 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8101 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8102 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8103 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8104 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8105 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8106 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8107 | TestCompletionCallback callback2; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8108 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8109 | // Enter the wrong password. |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8110 | rv = trans.RestartWithAuth( |
| 8111 | AuthCredentials(ntlm::test::kDomainUserCombined, kWrongPassword), |
| 8112 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8113 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8114 | |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8115 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8116 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8117 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8118 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8119 | TestCompletionCallback callback3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8120 | rv = trans.RestartWithAuth(AuthCredentials(), callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8121 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8122 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8123 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8124 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8125 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8126 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8127 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8128 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8129 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8130 | TestCompletionCallback callback4; |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8131 | |
| 8132 | // Now enter the right password. |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8133 | rv = trans.RestartWithAuth( |
| 8134 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 8135 | callback4.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8136 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8137 | |
| 8138 | rv = callback4.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8139 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8140 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8141 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8142 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8143 | TestCompletionCallback callback5; |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8144 | |
| 8145 | // One more roundtrip |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8146 | rv = trans.RestartWithAuth(AuthCredentials(), callback5.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8147 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8148 | |
| 8149 | rv = callback5.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8150 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8151 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8152 | response = trans.GetResponseInfo(); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8153 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8154 | EXPECT_EQ(14, response->headers->GetContentLength()); |
| 8155 | |
| 8156 | std::string response_data; |
| 8157 | rv = ReadTransaction(&trans, &response_data); |
| 8158 | EXPECT_THAT(rv, IsOk()); |
| 8159 | EXPECT_EQ("Please Login\r\n", response_data); |
| 8160 | |
| 8161 | EXPECT_TRUE(data1.AllReadDataConsumed()); |
| 8162 | EXPECT_TRUE(data1.AllWriteDataConsumed()); |
| 8163 | EXPECT_TRUE(data2.AllReadDataConsumed()); |
| 8164 | EXPECT_TRUE(data2.AllWriteDataConsumed()); |
| 8165 | EXPECT_TRUE(data3.AllReadDataConsumed()); |
| 8166 | EXPECT_TRUE(data3.AllWriteDataConsumed()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8167 | } |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8168 | |
Bence Béky | 3238f2e1 | 2017-09-22 22:44:49 | [diff] [blame] | 8169 | // Server requests NTLM authentication, which is not supported over HTTP/2. |
| 8170 | // Subsequent request with authorization header should be sent over HTTP/1.1. |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8171 | TEST_F(HttpNetworkTransactionTest, NTLMOverHttp2) { |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 8172 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter( |
| 8173 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8174 | |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8175 | const char* kUrl = "https://server/kids/login.aspx"; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8176 | |
| 8177 | HttpRequestInfo request; |
| 8178 | request.method = "GET"; |
| 8179 | request.url = GURL(kUrl); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 8180 | request.traffic_annotation = |
| 8181 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8182 | |
| 8183 | // First request without credentials. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 8184 | spdy::SpdyHeaderBlock request_headers0( |
| 8185 | spdy_util_.ConstructGetHeaderBlock(kUrl)); |
| 8186 | spdy::SpdySerializedFrame request0(spdy_util_.ConstructSpdyHeaders( |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8187 | 1, std::move(request_headers0), LOWEST, true)); |
| 8188 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 8189 | spdy::SpdyHeaderBlock response_headers0; |
| 8190 | response_headers0[spdy::kHttp2StatusHeader] = "401"; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8191 | response_headers0["www-authenticate"] = "NTLM"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 8192 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyResponseHeaders( |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8193 | 1, std::move(response_headers0), true)); |
| 8194 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8195 | // Stream 1 is closed. |
| 8196 | spdy_util_.UpdateWithStreamDestruction(1); |
| 8197 | |
| 8198 | // Generate the NTLM messages based on known test data. |
| 8199 | std::string negotiate_msg; |
| 8200 | std::string challenge_msg; |
| 8201 | std::string authenticate_msg; |
| 8202 | base::Base64Encode( |
| 8203 | base::StringPiece( |
| 8204 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8205 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8206 | &negotiate_msg); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8207 | base::Base64Encode( |
| 8208 | base::StringPiece( |
| 8209 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8210 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8211 | &challenge_msg); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8212 | base::Base64Encode( |
| 8213 | base::StringPiece( |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 8214 | reinterpret_cast<const char*>( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8215 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8216 | base::size( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8217 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8218 | &authenticate_msg); |
| 8219 | |
Bence Béky | 3238f2e1 | 2017-09-22 22:44:49 | [diff] [blame] | 8220 | MockWrite writes0[] = {CreateMockWrite(request0, 0)}; |
Matt Menke | 6082f95 | 2019-08-21 16:23:16 | [diff] [blame] | 8221 | MockRead reads0[] = {CreateMockRead(resp, 1), |
| 8222 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 2)}; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8223 | |
| 8224 | // Retry yet again using HTTP/1.1. |
| 8225 | MockWrite writes1[] = { |
| 8226 | // After restarting with a null identity, this is the |
| 8227 | // request we should be issuing -- the final header line contains a Type |
| 8228 | // 1 message. |
| 8229 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8230 | "Host: server\r\n" |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8231 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8232 | "Authorization: NTLM "), |
| 8233 | MockWrite(negotiate_msg.c_str()), MockWrite("\r\n\r\n"), |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8234 | |
| 8235 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
| 8236 | // (the credentials for the origin server). The second request continues |
| 8237 | // on the same connection. |
| 8238 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8239 | "Host: server\r\n" |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8240 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8241 | "Authorization: NTLM "), |
| 8242 | MockWrite(authenticate_msg.c_str()), MockWrite("\r\n\r\n"), |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8243 | }; |
| 8244 | |
| 8245 | MockRead reads1[] = { |
| 8246 | // The origin server responds with a Type 2 message. |
| 8247 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8248 | MockRead("WWW-Authenticate: NTLM "), MockRead(challenge_msg.c_str()), |
| 8249 | MockRead("\r\n"), MockRead("Content-Length: 42\r\n"), |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8250 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8251 | MockRead("You are not authorized to view this page\r\n"), |
| 8252 | |
| 8253 | // Lastly we get the desired content. |
| 8254 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8255 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8256 | 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] | 8257 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8258 | SequencedSocketData data0(reads0, writes0); |
| 8259 | StaticSocketDataProvider data1(reads1, writes1); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8260 | session_deps_.socket_factory->AddSocketDataProvider(&data0); |
| 8261 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 8262 | |
| 8263 | SSLSocketDataProvider ssl0(ASYNC, OK); |
| 8264 | ssl0.next_proto = kProtoHTTP2; |
Matt Menke | 6082f95 | 2019-08-21 16:23:16 | [diff] [blame] | 8265 | ssl0.next_protos_expected_in_ssl_config = |
| 8266 | NextProtoVector{kProtoHTTP2, kProtoHTTP11}; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8267 | SSLSocketDataProvider ssl1(ASYNC, OK); |
Matt Menke | 6082f95 | 2019-08-21 16:23:16 | [diff] [blame] | 8268 | // When creating the second connection, only HTTP/1.1 should be allowed. |
| 8269 | ssl1.next_protos_expected_in_ssl_config = NextProtoVector{kProtoHTTP11}; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8270 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl0); |
| 8271 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 8272 | |
| 8273 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 8274 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 8275 | |
| 8276 | TestCompletionCallback callback1; |
| 8277 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
| 8278 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8279 | |
| 8280 | rv = callback1.WaitForResult(); |
| 8281 | EXPECT_THAT(rv, IsOk()); |
| 8282 | |
| 8283 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
| 8284 | |
| 8285 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 8286 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8287 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8288 | |
| 8289 | TestCompletionCallback callback2; |
| 8290 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8291 | rv = trans.RestartWithAuth( |
| 8292 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 8293 | callback2.callback()); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8294 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8295 | |
| 8296 | rv = callback2.WaitForResult(); |
| 8297 | EXPECT_THAT(rv, IsOk()); |
| 8298 | |
| 8299 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
| 8300 | |
| 8301 | response = trans.GetResponseInfo(); |
| 8302 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8303 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8304 | |
| 8305 | TestCompletionCallback callback3; |
| 8306 | |
| 8307 | rv = trans.RestartWithAuth(AuthCredentials(), callback3.callback()); |
| 8308 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8309 | |
| 8310 | rv = callback3.WaitForResult(); |
| 8311 | EXPECT_THAT(rv, IsOk()); |
| 8312 | |
| 8313 | response = trans.GetResponseInfo(); |
| 8314 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8315 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8316 | EXPECT_EQ(14, response->headers->GetContentLength()); |
| 8317 | |
| 8318 | std::string response_data; |
| 8319 | rv = ReadTransaction(&trans, &response_data); |
| 8320 | EXPECT_THAT(rv, IsOk()); |
| 8321 | EXPECT_EQ("Please Login\r\n", response_data); |
| 8322 | |
| 8323 | EXPECT_TRUE(data0.AllReadDataConsumed()); |
| 8324 | EXPECT_TRUE(data0.AllWriteDataConsumed()); |
| 8325 | EXPECT_TRUE(data1.AllReadDataConsumed()); |
| 8326 | EXPECT_TRUE(data1.AllWriteDataConsumed()); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8327 | } |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8328 | |
Matt Menke | f2ee07c | 2019-08-29 02:10:36 | [diff] [blame] | 8329 | #if BUILDFLAG(ENABLE_WEBSOCKETS) |
| 8330 | |
| 8331 | // Variant of above test using WebSockets. |
| 8332 | TEST_F(HttpNetworkTransactionTest, NTLMOverHttp2WithWebsockets) { |
| 8333 | const GURL kInitialUrl("https://server/"); |
| 8334 | const GURL kWebSocketUrl("wss://server/"); |
| 8335 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter( |
| 8336 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
| 8337 | |
| 8338 | // Initial request establishes an H2 connection, which will then be reused for |
| 8339 | // WebSockets. This is needed since WebSockets will reuse H2 connections, but |
| 8340 | // it won't create a new one. |
| 8341 | spdy::SpdyHeaderBlock initial_request_headers( |
| 8342 | spdy_util_.ConstructGetHeaderBlock(kInitialUrl.spec())); |
| 8343 | spdy::SpdySerializedFrame initial_request(spdy_util_.ConstructSpdyHeaders( |
| 8344 | 1, std::move(initial_request_headers), DEFAULT_PRIORITY, true)); |
| 8345 | spdy::SpdySerializedFrame settings_ack(spdy_util_.ConstructSpdySettingsAck()); |
| 8346 | |
| 8347 | // Settings frame, indicating WebSockets is supported. |
| 8348 | spdy::SettingsMap settings; |
| 8349 | settings[spdy::SETTINGS_ENABLE_CONNECT_PROTOCOL] = 1; |
| 8350 | spdy::SpdySerializedFrame settings_frame( |
| 8351 | spdy_util_.ConstructSpdySettings(settings)); |
| 8352 | |
| 8353 | // Response headers for first request. Body is never received, but that |
| 8354 | // shouldn't matter for the purposes of this test. |
| 8355 | spdy::SpdySerializedFrame initial_response( |
| 8356 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 8357 | |
| 8358 | // First WebSocket request, which has no credentials. |
| 8359 | spdy::SpdyHeaderBlock websocket_request_headers; |
| 8360 | websocket_request_headers[spdy::kHttp2MethodHeader] = "CONNECT"; |
| 8361 | websocket_request_headers[spdy::kHttp2AuthorityHeader] = "server"; |
| 8362 | websocket_request_headers[spdy::kHttp2SchemeHeader] = "https"; |
| 8363 | websocket_request_headers[spdy::kHttp2PathHeader] = "/"; |
| 8364 | websocket_request_headers[spdy::kHttp2ProtocolHeader] = "websocket"; |
| 8365 | websocket_request_headers["origin"] = "http://server"; |
| 8366 | websocket_request_headers["sec-websocket-version"] = "13"; |
| 8367 | websocket_request_headers["sec-websocket-extensions"] = |
| 8368 | "permessage-deflate; client_max_window_bits"; |
| 8369 | spdy::SpdySerializedFrame websocket_request(spdy_util_.ConstructSpdyHeaders( |
| 8370 | 3, std::move(websocket_request_headers), MEDIUM, false)); |
| 8371 | |
| 8372 | // Auth challenge to WebSocket request. |
| 8373 | spdy::SpdyHeaderBlock auth_challenge_headers; |
| 8374 | auth_challenge_headers[spdy::kHttp2StatusHeader] = "401"; |
| 8375 | auth_challenge_headers["www-authenticate"] = "NTLM"; |
| 8376 | spdy::SpdySerializedFrame websocket_auth_challenge( |
| 8377 | spdy_util_.ConstructSpdyResponseHeaders( |
| 8378 | 3, std::move(auth_challenge_headers), true)); |
| 8379 | |
| 8380 | MockWrite writes0[] = {CreateMockWrite(initial_request, 0), |
| 8381 | CreateMockWrite(settings_ack, 2), |
| 8382 | CreateMockWrite(websocket_request, 4), |
| 8383 | MockWrite(SYNCHRONOUS, ERR_IO_PENDING, 7)}; |
| 8384 | MockRead reads0[] = {CreateMockRead(settings_frame, 1), |
| 8385 | CreateMockRead(initial_response, 3), |
| 8386 | CreateMockRead(websocket_auth_challenge, 5), |
| 8387 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 6)}; |
| 8388 | |
| 8389 | // Generate the NTLM messages based on known test data. |
| 8390 | std::string negotiate_msg; |
| 8391 | std::string challenge_msg; |
| 8392 | std::string authenticate_msg; |
| 8393 | base::Base64Encode( |
| 8394 | base::StringPiece( |
| 8395 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
| 8396 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
| 8397 | &negotiate_msg); |
| 8398 | base::Base64Encode( |
| 8399 | base::StringPiece( |
| 8400 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
| 8401 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
| 8402 | &challenge_msg); |
| 8403 | base::Base64Encode( |
| 8404 | base::StringPiece( |
| 8405 | reinterpret_cast<const char*>( |
| 8406 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
| 8407 | base::size( |
| 8408 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
| 8409 | &authenticate_msg); |
| 8410 | |
| 8411 | // Retry yet again using HTTP/1.1. |
| 8412 | MockWrite writes1[] = { |
| 8413 | // After restarting with a null identity, this is the |
| 8414 | // request we should be issuing -- the final header line contains a Type |
| 8415 | // 1 message. |
| 8416 | MockWrite("GET / HTTP/1.1\r\n" |
| 8417 | "Host: server\r\n" |
| 8418 | "Connection: Upgrade\r\n" |
| 8419 | "Authorization: NTLM "), |
| 8420 | MockWrite(negotiate_msg.c_str()), |
| 8421 | MockWrite("\r\n"), |
| 8422 | MockWrite("Origin: http://server\r\n" |
| 8423 | "Sec-WebSocket-Version: 13\r\n" |
| 8424 | "Upgrade: websocket\r\n" |
| 8425 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 8426 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 8427 | "client_max_window_bits\r\n\r\n"), |
| 8428 | |
| 8429 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
| 8430 | // (the credentials for the origin server). The second request continues |
| 8431 | // on the same connection. |
| 8432 | MockWrite("GET / HTTP/1.1\r\n" |
| 8433 | "Host: server\r\n" |
| 8434 | "Connection: Upgrade\r\n" |
| 8435 | "Authorization: NTLM "), |
| 8436 | MockWrite(authenticate_msg.c_str()), |
| 8437 | MockWrite("\r\n"), |
| 8438 | MockWrite("Origin: http://server\r\n" |
| 8439 | "Sec-WebSocket-Version: 13\r\n" |
| 8440 | "Upgrade: websocket\r\n" |
| 8441 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 8442 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 8443 | "client_max_window_bits\r\n\r\n"), |
| 8444 | }; |
| 8445 | |
| 8446 | MockRead reads1[] = { |
| 8447 | // The origin server responds with a Type 2 message. |
| 8448 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 8449 | MockRead("WWW-Authenticate: NTLM "), |
| 8450 | MockRead(challenge_msg.c_str()), |
| 8451 | MockRead("\r\n"), |
| 8452 | MockRead("Content-Length: 42\r\n"), |
| 8453 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8454 | MockRead("You are not authorized to view this page\r\n"), |
| 8455 | |
| 8456 | // Lastly we get the desired content. |
| 8457 | MockRead("HTTP/1.1 101 Switching Protocols\r\n" |
| 8458 | "Upgrade: websocket\r\n" |
| 8459 | "Connection: Upgrade\r\n" |
| 8460 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n"), |
| 8461 | }; |
| 8462 | SequencedSocketData data0(reads0, writes0); |
| 8463 | session_deps_.socket_factory->AddSocketDataProvider(&data0); |
| 8464 | SSLSocketDataProvider ssl0(ASYNC, OK); |
| 8465 | ssl0.next_proto = kProtoHTTP2; |
| 8466 | ssl0.next_protos_expected_in_ssl_config = |
| 8467 | NextProtoVector{kProtoHTTP2, kProtoHTTP11}; |
| 8468 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl0); |
| 8469 | |
| 8470 | StaticSocketDataProvider data1(reads1, writes1); |
| 8471 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 8472 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 8473 | // When creating the second connection, only HTTP/1.1 should be allowed. |
| 8474 | ssl1.next_protos_expected_in_ssl_config = NextProtoVector{}; |
| 8475 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 8476 | |
| 8477 | session_deps_.enable_websocket_over_http2 = true; |
| 8478 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 8479 | |
| 8480 | HttpRequestInfo initial_request_info; |
| 8481 | initial_request_info.method = "GET"; |
| 8482 | initial_request_info.url = kInitialUrl; |
| 8483 | initial_request_info.traffic_annotation = |
| 8484 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 8485 | HttpNetworkTransaction initial_trans(DEFAULT_PRIORITY, session.get()); |
| 8486 | TestCompletionCallback initial_callback; |
| 8487 | int rv = initial_trans.Start(&initial_request_info, |
| 8488 | initial_callback.callback(), NetLogWithSource()); |
| 8489 | EXPECT_THAT(initial_callback.GetResult(rv), IsOk()); |
| 8490 | |
| 8491 | EXPECT_FALSE(session->http_server_properties()->RequiresHTTP11( |
| 8492 | url::SchemeHostPort(kInitialUrl), NetworkIsolationKey())); |
| 8493 | |
| 8494 | HttpRequestInfo websocket_request_info; |
| 8495 | websocket_request_info.method = "GET"; |
| 8496 | websocket_request_info.url = kWebSocketUrl; |
| 8497 | websocket_request_info.traffic_annotation = |
| 8498 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 8499 | EXPECT_TRUE(HostPortPair::FromURL(initial_request_info.url) |
| 8500 | .Equals(HostPortPair::FromURL(websocket_request_info.url))); |
| 8501 | websocket_request_info.extra_headers.SetHeader("Origin", "http://server"); |
| 8502 | websocket_request_info.extra_headers.SetHeader("Sec-WebSocket-Version", "13"); |
| 8503 | // The following two headers must be removed by WebSocketHttp2HandshakeStream. |
| 8504 | websocket_request_info.extra_headers.SetHeader("Connection", "Upgrade"); |
| 8505 | websocket_request_info.extra_headers.SetHeader("Upgrade", "websocket"); |
| 8506 | |
| 8507 | TestWebSocketHandshakeStreamCreateHelper websocket_stream_create_helper; |
| 8508 | |
| 8509 | HttpNetworkTransaction websocket_trans(MEDIUM, session.get()); |
| 8510 | websocket_trans.SetWebSocketHandshakeStreamCreateHelper( |
| 8511 | &websocket_stream_create_helper); |
| 8512 | |
| 8513 | TestCompletionCallback websocket_callback; |
| 8514 | rv = websocket_trans.Start(&websocket_request_info, |
| 8515 | websocket_callback.callback(), NetLogWithSource()); |
| 8516 | EXPECT_THAT(websocket_callback.GetResult(rv), IsOk()); |
| 8517 | |
| 8518 | EXPECT_FALSE(websocket_trans.IsReadyToRestartForAuth()); |
| 8519 | |
| 8520 | const HttpResponseInfo* response = websocket_trans.GetResponseInfo(); |
| 8521 | ASSERT_TRUE(response); |
| 8522 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
| 8523 | |
| 8524 | rv = websocket_trans.RestartWithAuth( |
| 8525 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 8526 | websocket_callback.callback()); |
| 8527 | EXPECT_THAT(websocket_callback.GetResult(rv), IsOk()); |
| 8528 | |
| 8529 | EXPECT_TRUE(websocket_trans.IsReadyToRestartForAuth()); |
| 8530 | |
| 8531 | response = websocket_trans.GetResponseInfo(); |
| 8532 | ASSERT_TRUE(response); |
| 8533 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 8534 | |
| 8535 | rv = websocket_trans.RestartWithAuth(AuthCredentials(), |
| 8536 | websocket_callback.callback()); |
| 8537 | EXPECT_THAT(websocket_callback.GetResult(rv), IsOk()); |
| 8538 | |
| 8539 | // The server should have been marked as requiring HTTP/1.1. The important |
| 8540 | // part here is that the scheme that requires HTTP/1.1 should be HTTPS, not |
| 8541 | // WSS. |
| 8542 | EXPECT_TRUE(session->http_server_properties()->RequiresHTTP11( |
| 8543 | url::SchemeHostPort(kInitialUrl), NetworkIsolationKey())); |
| 8544 | } |
| 8545 | |
| 8546 | #endif // BUILDFLAG(ENABLE_WEBSOCKETS) |
| 8547 | |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8548 | // Test that, if we have an NTLM proxy and the origin resets the connection, we |
| 8549 | // do no retry forever checking for TLS version interference. This is a |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 8550 | // regression test for https://crbug.com/823387. The version interference probe |
| 8551 | // has since been removed, but retain the regression test so we can update it if |
| 8552 | // we add future TLS retries. |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8553 | TEST_F(HttpNetworkTransactionTest, NTLMProxyTLSHandshakeReset) { |
| 8554 | // The NTLM test data expects the proxy to be named 'server'. The origin is |
| 8555 | // https://origin/. |
| 8556 | session_deps_.proxy_resolution_service = |
| 8557 | ProxyResolutionService::CreateFixedFromPacResult( |
| 8558 | "PROXY server", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 8559 | |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 8560 | SSLContextConfig config; |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8561 | session_deps_.ssl_config_service = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8562 | std::make_unique<TestSSLConfigService>(config); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8563 | |
| 8564 | HttpRequestInfo request; |
| 8565 | request.method = "GET"; |
| 8566 | request.url = GURL("https://origin/"); |
| 8567 | request.traffic_annotation = |
| 8568 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 8569 | |
| 8570 | // Ensure load is not disrupted by flags which suppress behaviour specific |
| 8571 | // to other auth schemes. |
| 8572 | request.load_flags = LOAD_DO_NOT_USE_EMBEDDED_IDENTITY; |
| 8573 | |
| 8574 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter( |
| 8575 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
| 8576 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 8577 | |
| 8578 | // Generate the NTLM messages based on known test data. |
| 8579 | std::string negotiate_msg; |
| 8580 | std::string challenge_msg; |
| 8581 | std::string authenticate_msg; |
| 8582 | base::Base64Encode( |
| 8583 | base::StringPiece( |
| 8584 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8585 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8586 | &negotiate_msg); |
| 8587 | base::Base64Encode( |
| 8588 | base::StringPiece( |
| 8589 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8590 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8591 | &challenge_msg); |
| 8592 | base::Base64Encode( |
| 8593 | base::StringPiece( |
| 8594 | reinterpret_cast<const char*>( |
| 8595 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8596 | base::size( |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8597 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
| 8598 | &authenticate_msg); |
| 8599 | |
| 8600 | MockWrite data_writes[] = { |
| 8601 | // The initial CONNECT request. |
| 8602 | MockWrite("CONNECT origin:443 HTTP/1.1\r\n" |
| 8603 | "Host: origin:443\r\n" |
| 8604 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 8605 | |
| 8606 | // After restarting with an identity. |
| 8607 | MockWrite("CONNECT origin:443 HTTP/1.1\r\n" |
| 8608 | "Host: origin:443\r\n" |
| 8609 | "Proxy-Connection: keep-alive\r\n" |
| 8610 | "Proxy-Authorization: NTLM "), |
| 8611 | MockWrite(negotiate_msg.c_str()), |
| 8612 | // End headers. |
| 8613 | MockWrite("\r\n\r\n"), |
| 8614 | |
| 8615 | // The second restart. |
| 8616 | MockWrite("CONNECT origin:443 HTTP/1.1\r\n" |
| 8617 | "Host: origin:443\r\n" |
| 8618 | "Proxy-Connection: keep-alive\r\n" |
| 8619 | "Proxy-Authorization: NTLM "), |
| 8620 | MockWrite(authenticate_msg.c_str()), |
| 8621 | // End headers. |
| 8622 | MockWrite("\r\n\r\n"), |
| 8623 | }; |
| 8624 | |
| 8625 | MockRead data_reads[] = { |
| 8626 | // The initial NTLM response. |
| 8627 | MockRead("HTTP/1.1 407 Access Denied\r\n" |
| 8628 | "Content-Length: 0\r\n" |
| 8629 | "Proxy-Authenticate: NTLM\r\n\r\n"), |
| 8630 | |
| 8631 | // The NTLM challenge message. |
| 8632 | MockRead("HTTP/1.1 407 Access Denied\r\n" |
| 8633 | "Content-Length: 0\r\n" |
| 8634 | "Proxy-Authenticate: NTLM "), |
| 8635 | MockRead(challenge_msg.c_str()), |
| 8636 | // End headers. |
| 8637 | MockRead("\r\n\r\n"), |
| 8638 | |
| 8639 | // Finally the tunnel is established. |
| 8640 | MockRead("HTTP/1.1 200 Connected\r\n\r\n"), |
| 8641 | }; |
| 8642 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8643 | StaticSocketDataProvider data(data_reads, data_writes); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8644 | SSLSocketDataProvider data_ssl(ASYNC, ERR_CONNECTION_RESET); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8645 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 8646 | session_deps_.socket_factory->AddSSLSocketDataProvider(&data_ssl); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8647 | |
| 8648 | // Start the transaction. The proxy responds with an NTLM authentication |
| 8649 | // request. |
| 8650 | TestCompletionCallback callback; |
| 8651 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 8652 | int rv = callback.GetResult( |
| 8653 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
| 8654 | |
| 8655 | EXPECT_THAT(rv, IsOk()); |
| 8656 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
| 8657 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 8658 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8659 | EXPECT_TRUE(CheckNTLMProxyAuth(response->auth_challenge)); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8660 | |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 8661 | // Configure credentials and restart. The proxy responds with the challenge |
| 8662 | // message. |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8663 | rv = callback.GetResult(trans.RestartWithAuth( |
| 8664 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 8665 | callback.callback())); |
| 8666 | EXPECT_THAT(rv, IsOk()); |
| 8667 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
| 8668 | response = trans.GetResponseInfo(); |
| 8669 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8670 | EXPECT_FALSE(response->auth_challenge.has_value()); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8671 | |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 8672 | // Restart once more. The tunnel will be established and then the SSL |
| 8673 | // handshake will reset. |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8674 | rv = callback.GetResult( |
| 8675 | trans.RestartWithAuth(AuthCredentials(), callback.callback())); |
| 8676 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
| 8677 | } |
| 8678 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 8679 | #endif // NTLM_PORTABLE |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8680 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8681 | // Test reading a server response which has only headers, and no body. |
| 8682 | // After some maximum number of bytes is consumed, the transaction should |
| 8683 | // fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8684 | TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 8685 | HttpRequestInfo request; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8686 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8687 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 8688 | request.traffic_annotation = |
| 8689 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8690 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8691 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8692 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8693 | |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 8694 | // Respond with 300 kb of headers (we should fail after 256 kb). |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 8695 | std::string large_headers_string; |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 8696 | FillLargeHeadersString(&large_headers_string, 300 * 1024); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8697 | |
| 8698 | MockRead data_reads[] = { |
| 8699 | MockRead("HTTP/1.0 200 OK\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8700 | MockRead(ASYNC, large_headers_string.data(), large_headers_string.size()), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8701 | MockRead("\r\nBODY"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8702 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8703 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8704 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8705 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8706 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8707 | TestCompletionCallback callback; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8708 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8709 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8710 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8711 | |
| 8712 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8713 | EXPECT_THAT(rv, IsError(ERR_RESPONSE_HEADERS_TOO_BIG)); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8714 | } |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8715 | |
| 8716 | // Make sure that we don't try to reuse a TCPClientSocket when failing to |
| 8717 | // establish tunnel. |
| 8718 | // http://code.google.com/p/chromium/issues/detail?id=3772 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8719 | TEST_F(HttpNetworkTransactionTest, DontRecycleTransportSocketForSSLTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8720 | HttpRequestInfo request; |
| 8721 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8722 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 8723 | request.traffic_annotation = |
| 8724 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8725 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8726 | // Configure against proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 8727 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 8728 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 8729 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8730 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8731 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 8732 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 8733 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8734 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8735 | // Since we have proxy, should try to establish tunnel. |
| 8736 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 8737 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 8738 | "Host: www.example.org:443\r\n" |
| 8739 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8740 | }; |
| 8741 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 8742 | // The proxy responds to the connect with a 404, using a persistent |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8743 | // connection. Usually a proxy would return 501 (not implemented), |
| 8744 | // or 200 (tunnel established). |
| 8745 | MockRead data_reads1[] = { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 8746 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 8747 | MockRead("Content-Length: 10\r\n\r\n"), |
| 8748 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8749 | }; |
| 8750 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8751 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8752 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8753 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8754 | TestCompletionCallback callback1; |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8755 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8756 | int rv = trans->Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8757 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8758 | |
| 8759 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8760 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8761 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 8762 | // Empty the current queue. This is necessary because idle sockets are |
| 8763 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8764 | base::RunLoop().RunUntilIdle(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 8765 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8766 | // We now check to make sure the TCPClientSocket was not added back to |
| 8767 | // the pool. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8768 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8769 | trans.reset(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8770 | base::RunLoop().RunUntilIdle(); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8771 | // Make sure that the socket didn't get recycled after calling the destructor. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8772 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8773 | } |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 8774 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8775 | // Make sure that we recycle a socket after reading all of the response body. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8776 | TEST_F(HttpNetworkTransactionTest, RecycleSocket) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 8777 | HttpRequestInfo request; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8778 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8779 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 8780 | request.traffic_annotation = |
| 8781 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8782 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8783 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8784 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8785 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8786 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8787 | MockRead data_reads[] = { |
| 8788 | // A part of the response body is received with the response headers. |
| 8789 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 8790 | // The rest of the response body is received in two parts. |
| 8791 | MockRead("lo"), |
| 8792 | MockRead(" world"), |
| 8793 | MockRead("junk"), // Should not be read!! |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8794 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8795 | }; |
| 8796 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8797 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8798 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8799 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8800 | TestCompletionCallback callback; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8801 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8802 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8803 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8804 | |
| 8805 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8806 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8807 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8808 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8809 | ASSERT_TRUE(response); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8810 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8811 | EXPECT_TRUE(response->headers); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8812 | std::string status_line = response->headers->GetStatusLine(); |
| 8813 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 8814 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8815 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8816 | |
| 8817 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8818 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8819 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8820 | EXPECT_EQ("hello world", response_data); |
| 8821 | |
| 8822 | // Empty the current queue. This is necessary because idle sockets are |
| 8823 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8824 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8825 | |
| 8826 | // 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] | 8827 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8828 | } |
| 8829 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8830 | // Make sure that we recycle a SSL socket after reading all of the response |
| 8831 | // body. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8832 | TEST_F(HttpNetworkTransactionTest, RecycleSSLSocket) { |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8833 | HttpRequestInfo request; |
| 8834 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8835 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 8836 | request.traffic_annotation = |
| 8837 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8838 | |
| 8839 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8840 | MockWrite( |
| 8841 | "GET / HTTP/1.1\r\n" |
| 8842 | "Host: www.example.org\r\n" |
| 8843 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8844 | }; |
| 8845 | |
| 8846 | MockRead data_reads[] = { |
| 8847 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8848 | MockRead("Content-Length: 11\r\n\r\n"), |
| 8849 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8850 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8851 | }; |
| 8852 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8853 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8854 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8855 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8856 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8857 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8858 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8859 | TestCompletionCallback callback; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8860 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8861 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8862 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8863 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8864 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8865 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8866 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8867 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8868 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8869 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8870 | ASSERT_TRUE(response); |
| 8871 | ASSERT_TRUE(response->headers); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8872 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8873 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8874 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8875 | |
| 8876 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8877 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8878 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8879 | EXPECT_EQ("hello world", response_data); |
| 8880 | |
| 8881 | // Empty the current queue. This is necessary because idle sockets are |
| 8882 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8883 | base::RunLoop().RunUntilIdle(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8884 | |
| 8885 | // 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] | 8886 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8887 | } |
| 8888 | |
| 8889 | // Grab a SSL socket, use it, and put it back into the pool. Then, reuse it |
| 8890 | // from the pool and make sure that we recover okay. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8891 | TEST_F(HttpNetworkTransactionTest, RecycleDeadSSLSocket) { |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8892 | HttpRequestInfo request; |
| 8893 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8894 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 8895 | request.traffic_annotation = |
| 8896 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8897 | |
| 8898 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8899 | MockWrite( |
| 8900 | "GET / HTTP/1.1\r\n" |
| 8901 | "Host: www.example.org\r\n" |
| 8902 | "Connection: keep-alive\r\n\r\n"), |
| 8903 | MockWrite( |
| 8904 | "GET / HTTP/1.1\r\n" |
| 8905 | "Host: www.example.org\r\n" |
| 8906 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8907 | }; |
| 8908 | |
| 8909 | MockRead data_reads[] = { |
mmenke | 911ee022 | 2015-12-04 03:58:42 | [diff] [blame] | 8910 | MockRead("HTTP/1.1 200 OK\r\n"), MockRead("Content-Length: 11\r\n\r\n"), |
| 8911 | MockRead("hello world"), MockRead(ASYNC, ERR_CONNECTION_CLOSED)}; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8912 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8913 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 8914 | SSLSocketDataProvider ssl2(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8915 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 8916 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8917 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8918 | StaticSocketDataProvider data(data_reads, data_writes); |
| 8919 | StaticSocketDataProvider data2(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8920 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 8921 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8922 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8923 | TestCompletionCallback callback; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8924 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8925 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 8926 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 8927 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8928 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8929 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8930 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8931 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8932 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8933 | |
| 8934 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8935 | ASSERT_TRUE(response); |
| 8936 | ASSERT_TRUE(response->headers); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8937 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8938 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8939 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8940 | |
| 8941 | std::string response_data; |
| 8942 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8943 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8944 | EXPECT_EQ("hello world", response_data); |
| 8945 | |
| 8946 | // Empty the current queue. This is necessary because idle sockets are |
| 8947 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8948 | base::RunLoop().RunUntilIdle(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8949 | |
| 8950 | // 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] | 8951 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8952 | |
| 8953 | // Now start the second transaction, which should reuse the previous socket. |
| 8954 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 8955 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 8956 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8957 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8958 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8959 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8960 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8961 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8962 | |
| 8963 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8964 | ASSERT_TRUE(response); |
| 8965 | ASSERT_TRUE(response->headers); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8966 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8967 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8968 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8969 | |
| 8970 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8971 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8972 | EXPECT_EQ("hello world", response_data); |
| 8973 | |
| 8974 | // Empty the current queue. This is necessary because idle sockets are |
| 8975 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8976 | base::RunLoop().RunUntilIdle(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8977 | |
| 8978 | // 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] | 8979 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8980 | } |
| 8981 | |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 8982 | // Grab a socket, use it, and put it back into the pool. Then, make |
| 8983 | // low memory notification and ensure the socket pool is flushed. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8984 | TEST_F(HttpNetworkTransactionTest, FlushSocketPoolOnLowMemoryNotifications) { |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 8985 | HttpRequestInfo request; |
| 8986 | request.method = "GET"; |
| 8987 | request.url = GURL("http://www.example.org/"); |
| 8988 | request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 8989 | request.traffic_annotation = |
| 8990 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 8991 | |
| 8992 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 8993 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8994 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 8995 | |
| 8996 | MockRead data_reads[] = { |
| 8997 | // A part of the response body is received with the response headers. |
| 8998 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 8999 | // The rest of the response body is received in two parts. |
| 9000 | MockRead("lo"), MockRead(" world"), |
| 9001 | MockRead("junk"), // Should not be read!! |
| 9002 | MockRead(SYNCHRONOUS, OK), |
| 9003 | }; |
| 9004 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9005 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9006 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 9007 | |
| 9008 | TestCompletionCallback callback; |
| 9009 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9010 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9011 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 9012 | |
| 9013 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 9014 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9015 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9016 | ASSERT_TRUE(response); |
| 9017 | EXPECT_TRUE(response->headers); |
| 9018 | std::string status_line = response->headers->GetStatusLine(); |
| 9019 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 9020 | |
| 9021 | // Make memory critical notification and ensure the transaction still has been |
| 9022 | // operating right. |
| 9023 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9024 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9025 | base::RunLoop().RunUntilIdle(); |
| 9026 | |
| 9027 | // Socket should not be flushed as long as it is not idle. |
| 9028 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9029 | |
| 9030 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9031 | rv = ReadTransaction(&trans, &response_data); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9032 | EXPECT_THAT(rv, IsOk()); |
| 9033 | EXPECT_EQ("hello world", response_data); |
| 9034 | |
| 9035 | // Empty the current queue. This is necessary because idle sockets are |
| 9036 | // added to the connection pool asynchronously with a PostTask. |
| 9037 | base::RunLoop().RunUntilIdle(); |
| 9038 | |
| 9039 | // We now check to make sure the socket was added back to the pool. |
| 9040 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9041 | |
| 9042 | // Idle sockets should be flushed now. |
| 9043 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9044 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9045 | base::RunLoop().RunUntilIdle(); |
| 9046 | |
| 9047 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9048 | } |
| 9049 | |
yucliu | 48f235d | 2018-01-11 00:59:55 | [diff] [blame] | 9050 | // Disable idle socket closing on memory pressure. |
| 9051 | // Grab a socket, use it, and put it back into the pool. Then, make |
| 9052 | // low memory notification and ensure the socket pool is NOT flushed. |
| 9053 | TEST_F(HttpNetworkTransactionTest, NoFlushSocketPoolOnLowMemoryNotifications) { |
| 9054 | HttpRequestInfo request; |
| 9055 | request.method = "GET"; |
| 9056 | request.url = GURL("http://www.example.org/"); |
| 9057 | request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9058 | request.traffic_annotation = |
| 9059 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
yucliu | 48f235d | 2018-01-11 00:59:55 | [diff] [blame] | 9060 | |
| 9061 | // Disable idle socket closing on memory pressure. |
| 9062 | session_deps_.disable_idle_sockets_close_on_memory_pressure = true; |
| 9063 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 9064 | |
| 9065 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 9066 | |
| 9067 | MockRead data_reads[] = { |
| 9068 | // A part of the response body is received with the response headers. |
| 9069 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 9070 | // The rest of the response body is received in two parts. |
| 9071 | MockRead("lo"), MockRead(" world"), |
| 9072 | MockRead("junk"), // Should not be read!! |
| 9073 | MockRead(SYNCHRONOUS, OK), |
| 9074 | }; |
| 9075 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9076 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
yucliu | 48f235d | 2018-01-11 00:59:55 | [diff] [blame] | 9077 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 9078 | |
| 9079 | TestCompletionCallback callback; |
| 9080 | |
| 9081 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 9082 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 9083 | |
| 9084 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 9085 | |
| 9086 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 9087 | ASSERT_TRUE(response); |
| 9088 | EXPECT_TRUE(response->headers); |
| 9089 | std::string status_line = response->headers->GetStatusLine(); |
| 9090 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 9091 | |
| 9092 | // Make memory critical notification and ensure the transaction still has been |
| 9093 | // operating right. |
| 9094 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9095 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9096 | base::RunLoop().RunUntilIdle(); |
| 9097 | |
| 9098 | // Socket should not be flushed as long as it is not idle. |
| 9099 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9100 | |
| 9101 | std::string response_data; |
| 9102 | rv = ReadTransaction(&trans, &response_data); |
| 9103 | EXPECT_THAT(rv, IsOk()); |
| 9104 | EXPECT_EQ("hello world", response_data); |
| 9105 | |
| 9106 | // Empty the current queue. This is necessary because idle sockets are |
| 9107 | // added to the connection pool asynchronously with a PostTask. |
| 9108 | base::RunLoop().RunUntilIdle(); |
| 9109 | |
| 9110 | // We now check to make sure the socket was added back to the pool. |
| 9111 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9112 | |
| 9113 | // Idle sockets should NOT be flushed on moderate memory pressure. |
| 9114 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9115 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 9116 | base::RunLoop().RunUntilIdle(); |
| 9117 | |
| 9118 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9119 | |
| 9120 | // Idle sockets should NOT be flushed on critical memory pressure. |
| 9121 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9122 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9123 | base::RunLoop().RunUntilIdle(); |
| 9124 | |
| 9125 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9126 | } |
| 9127 | |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9128 | // Grab an SSL socket, use it, and put it back into the pool. Then, make |
| 9129 | // low memory notification and ensure the socket pool is flushed. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9130 | TEST_F(HttpNetworkTransactionTest, FlushSSLSocketPoolOnLowMemoryNotifications) { |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9131 | HttpRequestInfo request; |
| 9132 | request.method = "GET"; |
| 9133 | request.url = GURL("https://www.example.org/"); |
| 9134 | request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9135 | request.traffic_annotation = |
| 9136 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9137 | |
| 9138 | MockWrite data_writes[] = { |
| 9139 | MockWrite("GET / HTTP/1.1\r\n" |
| 9140 | "Host: www.example.org\r\n" |
| 9141 | "Connection: keep-alive\r\n\r\n"), |
| 9142 | }; |
| 9143 | |
| 9144 | MockRead data_reads[] = { |
| 9145 | MockRead("HTTP/1.1 200 OK\r\n"), MockRead("Content-Length: 11\r\n\r\n"), |
| 9146 | MockRead("hello world"), MockRead(ASYNC, ERR_CONNECTION_CLOSED)}; |
| 9147 | |
| 9148 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 9149 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 9150 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9151 | StaticSocketDataProvider data(data_reads, data_writes); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9152 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 9153 | |
| 9154 | TestCompletionCallback callback; |
| 9155 | |
| 9156 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9157 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9158 | |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 9159 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9160 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9161 | |
| 9162 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 9163 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9164 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9165 | ASSERT_TRUE(response); |
| 9166 | ASSERT_TRUE(response->headers); |
| 9167 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9168 | |
| 9169 | // Make memory critical notification and ensure the transaction still has been |
| 9170 | // operating right. |
| 9171 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9172 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9173 | base::RunLoop().RunUntilIdle(); |
| 9174 | |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 9175 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9176 | |
| 9177 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9178 | rv = ReadTransaction(&trans, &response_data); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9179 | EXPECT_THAT(rv, IsOk()); |
| 9180 | EXPECT_EQ("hello world", response_data); |
| 9181 | |
| 9182 | // Empty the current queue. This is necessary because idle sockets are |
| 9183 | // added to the connection pool asynchronously with a PostTask. |
| 9184 | base::RunLoop().RunUntilIdle(); |
| 9185 | |
| 9186 | // 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] | 9187 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9188 | |
| 9189 | // Make memory notification once again and ensure idle socket is closed. |
| 9190 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9191 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9192 | base::RunLoop().RunUntilIdle(); |
| 9193 | |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 9194 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9195 | } |
| 9196 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9197 | // Make sure that we recycle a socket after a zero-length response. |
| 9198 | // http://crbug.com/9880 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9199 | TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9200 | HttpRequestInfo request; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9201 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9202 | request.url = GURL( |
| 9203 | "http://www.example.org/csi?v=3&s=web&action=&" |
| 9204 | "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" |
| 9205 | "e=17259,18167,19592,19773,19981,20133,20173,20233&" |
| 9206 | "rt=prt.2642,ol.2649,xjs.2951"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9207 | request.traffic_annotation = |
| 9208 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9209 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9210 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9211 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9212 | MockRead data_reads[] = { |
| 9213 | MockRead("HTTP/1.1 204 No Content\r\n" |
| 9214 | "Content-Length: 0\r\n" |
| 9215 | "Content-Type: text/html\r\n\r\n"), |
| 9216 | MockRead("junk"), // Should not be read!! |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9217 | MockRead(SYNCHRONOUS, OK), |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9218 | }; |
| 9219 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9220 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9221 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9222 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 9223 | // Transaction must be created after the MockReads, so it's destroyed before |
| 9224 | // them. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9225 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 9226 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9227 | TestCompletionCallback callback; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9228 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9229 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9230 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9231 | |
| 9232 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9233 | EXPECT_THAT(rv, IsOk()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9234 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9235 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9236 | ASSERT_TRUE(response); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9237 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9238 | EXPECT_TRUE(response->headers); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9239 | std::string status_line = response->headers->GetStatusLine(); |
| 9240 | EXPECT_EQ("HTTP/1.1 204 No Content", status_line); |
| 9241 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 9242 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9243 | |
| 9244 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9245 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9246 | EXPECT_THAT(rv, IsOk()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9247 | EXPECT_EQ("", response_data); |
| 9248 | |
| 9249 | // Empty the current queue. This is necessary because idle sockets are |
| 9250 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9251 | base::RunLoop().RunUntilIdle(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9252 | |
| 9253 | // 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] | 9254 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9255 | } |
| 9256 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9257 | TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9258 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 9259 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 9260 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 9261 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 9262 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9263 | HttpRequestInfo request[2]; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9264 | // Transaction 1: a GET request that succeeds. The socket is recycled |
| 9265 | // after use. |
| 9266 | request[0].method = "GET"; |
| 9267 | request[0].url = GURL("http://www.google.com/"); |
| 9268 | request[0].load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9269 | request[0].traffic_annotation = |
| 9270 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9271 | // Transaction 2: a POST request. Reuses the socket kept alive from |
| 9272 | // transaction 1. The first attempts fails when writing the POST data. |
| 9273 | // This causes the transaction to retry with a new socket. The second |
| 9274 | // attempt succeeds. |
| 9275 | request[1].method = "POST"; |
| 9276 | request[1].url = GURL("http://www.google.com/login.cgi"); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 9277 | request[1].upload_data_stream = &upload_data_stream; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9278 | request[1].load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9279 | request[1].traffic_annotation = |
| 9280 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9281 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9282 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9283 | |
| 9284 | // The first socket is used for transaction 1 and the first attempt of |
| 9285 | // transaction 2. |
| 9286 | |
| 9287 | // The response of transaction 1. |
| 9288 | MockRead data_reads1[] = { |
| 9289 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"), |
| 9290 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9291 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9292 | }; |
| 9293 | // The mock write results of transaction 1 and the first attempt of |
| 9294 | // transaction 2. |
| 9295 | MockWrite data_writes1[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9296 | MockWrite(SYNCHRONOUS, 64), // GET |
| 9297 | MockWrite(SYNCHRONOUS, 93), // POST |
| 9298 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_ABORTED), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9299 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9300 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9301 | |
| 9302 | // The second socket is used for the second attempt of transaction 2. |
| 9303 | |
| 9304 | // The response of transaction 2. |
| 9305 | MockRead data_reads2[] = { |
| 9306 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\n"), |
| 9307 | MockRead("welcome"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9308 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9309 | }; |
| 9310 | // The mock write results of the second attempt of transaction 2. |
| 9311 | MockWrite data_writes2[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9312 | MockWrite(SYNCHRONOUS, 93), // POST |
| 9313 | MockWrite(SYNCHRONOUS, 3), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9314 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9315 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9316 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9317 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9318 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9319 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 9320 | const char* const kExpectedResponseData[] = { |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9321 | "hello world", "welcome" |
| 9322 | }; |
| 9323 | |
| 9324 | for (int i = 0; i < 2; ++i) { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9325 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9326 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9327 | TestCompletionCallback callback; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9328 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9329 | int rv = trans.Start(&request[i], callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9330 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9331 | |
| 9332 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9333 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9334 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9335 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9336 | ASSERT_TRUE(response); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9337 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9338 | EXPECT_TRUE(response->headers); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9339 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9340 | |
| 9341 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9342 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9343 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9344 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 9345 | } |
| 9346 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9347 | |
| 9348 | // Test the request-challenge-retry sequence for basic auth when there is |
| 9349 | // 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] | 9350 | // it fails the identity from the URL is used to answer the challenge. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9351 | TEST_F(HttpNetworkTransactionTest, AuthIdentityInURL) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9352 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9353 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9354 | request.url = GURL("http://foo:b@[email protected]/"); |
[email protected] | 7b08ba6 | 2012-02-10 20:19:41 | [diff] [blame] | 9355 | request.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9356 | request.traffic_annotation = |
| 9357 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 9358 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9359 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9360 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9361 | |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 9362 | // The password contains an escaped character -- for this test to pass it |
| 9363 | // will need to be unescaped by HttpNetworkTransaction. |
| 9364 | EXPECT_EQ("b%40r", request.url.password()); |
| 9365 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9366 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9367 | MockWrite( |
| 9368 | "GET / HTTP/1.1\r\n" |
| 9369 | "Host: www.example.org\r\n" |
| 9370 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9371 | }; |
| 9372 | |
| 9373 | MockRead data_reads1[] = { |
| 9374 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9375 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9376 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9377 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9378 | }; |
| 9379 | |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9380 | // After the challenge above, the transaction will be restarted using the |
| 9381 | // identity from the url (foo, b@r) to answer the challenge. |
| 9382 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9383 | MockWrite( |
| 9384 | "GET / HTTP/1.1\r\n" |
| 9385 | "Host: www.example.org\r\n" |
| 9386 | "Connection: keep-alive\r\n" |
| 9387 | "Authorization: Basic Zm9vOmJAcg==\r\n\r\n"), |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9388 | }; |
| 9389 | |
| 9390 | MockRead data_reads2[] = { |
| 9391 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9392 | MockRead("Content-Length: 100\r\n\r\n"), |
| 9393 | MockRead(SYNCHRONOUS, OK), |
| 9394 | }; |
| 9395 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9396 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9397 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9398 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9399 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9400 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9401 | TestCompletionCallback callback1; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9402 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9403 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9404 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9405 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9406 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9407 | |
| 9408 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9409 | rv = trans.RestartWithAuth(AuthCredentials(), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9410 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9411 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9412 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9413 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 9414 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9415 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9416 | ASSERT_TRUE(response); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9417 | |
| 9418 | // There is no challenge info, since the identity in URL worked. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9419 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9420 | |
| 9421 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9422 | |
| 9423 | // Empty the current queue. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9424 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9425 | } |
| 9426 | |
| 9427 | // Test the request-challenge-retry sequence for basic auth when there is an |
| 9428 | // incorrect identity in the URL. The identity from the URL should be used only |
| 9429 | // once. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9430 | TEST_F(HttpNetworkTransactionTest, WrongAuthIdentityInURL) { |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9431 | HttpRequestInfo request; |
| 9432 | request.method = "GET"; |
| 9433 | // Note: the URL has a username:password in it. The password "baz" is |
| 9434 | // wrong (should be "bar"). |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9435 | request.url = GURL("http://foo:[email protected]/"); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9436 | |
| 9437 | request.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9438 | request.traffic_annotation = |
| 9439 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9440 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9441 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9442 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9443 | |
| 9444 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9445 | MockWrite( |
| 9446 | "GET / HTTP/1.1\r\n" |
| 9447 | "Host: www.example.org\r\n" |
| 9448 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9449 | }; |
| 9450 | |
| 9451 | MockRead data_reads1[] = { |
| 9452 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9453 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9454 | MockRead("Content-Length: 10\r\n\r\n"), |
| 9455 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 9456 | }; |
| 9457 | |
| 9458 | // After the challenge above, the transaction will be restarted using the |
| 9459 | // identity from the url (foo, baz) to answer the challenge. |
| 9460 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9461 | MockWrite( |
| 9462 | "GET / HTTP/1.1\r\n" |
| 9463 | "Host: www.example.org\r\n" |
| 9464 | "Connection: keep-alive\r\n" |
| 9465 | "Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9466 | }; |
| 9467 | |
| 9468 | MockRead data_reads2[] = { |
| 9469 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9470 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9471 | MockRead("Content-Length: 10\r\n\r\n"), |
| 9472 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 9473 | }; |
| 9474 | |
| 9475 | // After the challenge above, the transaction will be restarted using the |
| 9476 | // identity supplied by the user (foo, bar) to answer the challenge. |
| 9477 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9478 | MockWrite( |
| 9479 | "GET / HTTP/1.1\r\n" |
| 9480 | "Host: www.example.org\r\n" |
| 9481 | "Connection: keep-alive\r\n" |
| 9482 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9483 | }; |
| 9484 | |
| 9485 | MockRead data_reads3[] = { |
| 9486 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9487 | MockRead("Content-Length: 100\r\n\r\n"), |
| 9488 | MockRead(SYNCHRONOUS, OK), |
| 9489 | }; |
| 9490 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9491 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9492 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 9493 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9494 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9495 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 9496 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9497 | |
| 9498 | TestCompletionCallback callback1; |
| 9499 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9500 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9501 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9502 | |
| 9503 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9504 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9505 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9506 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9507 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9508 | rv = trans.RestartWithAuth(AuthCredentials(), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9509 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9510 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9511 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9512 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9513 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9514 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9515 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9516 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9517 | |
| 9518 | TestCompletionCallback callback3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9519 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9520 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9521 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9522 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9523 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9524 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9525 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9526 | ASSERT_TRUE(response); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9527 | |
| 9528 | // There is no challenge info, since the identity worked. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9529 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9530 | |
| 9531 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9532 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 9533 | // Empty the current queue. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9534 | base::RunLoop().RunUntilIdle(); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 9535 | } |
| 9536 | |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9537 | |
| 9538 | // Test the request-challenge-retry sequence for basic auth when there is a |
| 9539 | // correct identity in the URL, but its use is being suppressed. The identity |
| 9540 | // from the URL should never be used. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9541 | TEST_F(HttpNetworkTransactionTest, AuthIdentityInURLSuppressed) { |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9542 | HttpRequestInfo request; |
| 9543 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9544 | request.url = GURL("http://foo:[email protected]/"); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9545 | request.load_flags = LOAD_DO_NOT_USE_EMBEDDED_IDENTITY; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9546 | request.traffic_annotation = |
| 9547 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9548 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9549 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9550 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9551 | |
| 9552 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9553 | MockWrite( |
| 9554 | "GET / HTTP/1.1\r\n" |
| 9555 | "Host: www.example.org\r\n" |
| 9556 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9557 | }; |
| 9558 | |
| 9559 | MockRead data_reads1[] = { |
| 9560 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9561 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9562 | MockRead("Content-Length: 10\r\n\r\n"), |
| 9563 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 9564 | }; |
| 9565 | |
| 9566 | // After the challenge above, the transaction will be restarted using the |
| 9567 | // identity supplied by the user, not the one in the URL, to answer the |
| 9568 | // challenge. |
| 9569 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9570 | MockWrite( |
| 9571 | "GET / HTTP/1.1\r\n" |
| 9572 | "Host: www.example.org\r\n" |
| 9573 | "Connection: keep-alive\r\n" |
| 9574 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9575 | }; |
| 9576 | |
| 9577 | MockRead data_reads3[] = { |
| 9578 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9579 | MockRead("Content-Length: 100\r\n\r\n"), |
| 9580 | MockRead(SYNCHRONOUS, OK), |
| 9581 | }; |
| 9582 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9583 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9584 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9585 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9586 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 9587 | |
| 9588 | TestCompletionCallback callback1; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9589 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9590 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9591 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9592 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9593 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9594 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9595 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9596 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9597 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9598 | |
| 9599 | TestCompletionCallback callback3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9600 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9601 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9602 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9603 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9604 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9605 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9606 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9607 | ASSERT_TRUE(response); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9608 | |
| 9609 | // There is no challenge info, since the identity worked. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9610 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9611 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9612 | |
| 9613 | // Empty the current queue. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9614 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9615 | } |
| 9616 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9617 | // Test that previously tried username/passwords for a realm get re-used. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9618 | TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9619 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9620 | |
| 9621 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 9622 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9623 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9624 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9625 | request.url = GURL("http://www.example.org/x/y/z"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9626 | request.traffic_annotation = |
| 9627 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9628 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9629 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9630 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9631 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9632 | MockWrite( |
| 9633 | "GET /x/y/z HTTP/1.1\r\n" |
| 9634 | "Host: www.example.org\r\n" |
| 9635 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9636 | }; |
| 9637 | |
| 9638 | MockRead data_reads1[] = { |
| 9639 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9640 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9641 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9642 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9643 | }; |
| 9644 | |
| 9645 | // Resend with authorization (username=foo, password=bar) |
| 9646 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9647 | MockWrite( |
| 9648 | "GET /x/y/z HTTP/1.1\r\n" |
| 9649 | "Host: www.example.org\r\n" |
| 9650 | "Connection: keep-alive\r\n" |
| 9651 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9652 | }; |
| 9653 | |
| 9654 | // Sever accepts the authorization. |
| 9655 | MockRead data_reads2[] = { |
| 9656 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9657 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9658 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9659 | }; |
| 9660 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9661 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9662 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9663 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9664 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9665 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9666 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9667 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9668 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9669 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9670 | |
| 9671 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9672 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9673 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9674 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9675 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9676 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9677 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9678 | TestCompletionCallback callback2; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9679 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9680 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 9681 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9682 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9683 | |
| 9684 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9685 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9686 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9687 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9688 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9689 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9690 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9691 | } |
| 9692 | |
| 9693 | // ------------------------------------------------------------------------ |
| 9694 | |
| 9695 | // Transaction 2: authenticate (foo2, bar2) on MyRealm2 |
| 9696 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9697 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9698 | request.method = "GET"; |
| 9699 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 9700 | // protection space as MyRealm1. |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9701 | request.url = GURL("http://www.example.org/x/y/a/b"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9702 | request.traffic_annotation = |
| 9703 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9704 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9705 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9706 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9707 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9708 | MockWrite( |
| 9709 | "GET /x/y/a/b HTTP/1.1\r\n" |
| 9710 | "Host: www.example.org\r\n" |
| 9711 | "Connection: keep-alive\r\n" |
| 9712 | // Send preemptive authorization for MyRealm1 |
| 9713 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9714 | }; |
| 9715 | |
| 9716 | // The server didn't like the preemptive authorization, and |
| 9717 | // challenges us for a different realm (MyRealm2). |
| 9718 | MockRead data_reads1[] = { |
| 9719 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9720 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm2\"\r\n"), |
| 9721 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9722 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9723 | }; |
| 9724 | |
| 9725 | // Resend with authorization for MyRealm2 (username=foo2, password=bar2) |
| 9726 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9727 | MockWrite( |
| 9728 | "GET /x/y/a/b HTTP/1.1\r\n" |
| 9729 | "Host: www.example.org\r\n" |
| 9730 | "Connection: keep-alive\r\n" |
| 9731 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9732 | }; |
| 9733 | |
| 9734 | // Sever accepts the authorization. |
| 9735 | MockRead data_reads2[] = { |
| 9736 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9737 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9738 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9739 | }; |
| 9740 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9741 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9742 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9743 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9744 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9745 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9746 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9747 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9748 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9749 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9750 | |
| 9751 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9752 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9753 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9754 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9755 | ASSERT_TRUE(response); |
| 9756 | ASSERT_TRUE(response->auth_challenge); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 9757 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 9758 | EXPECT_EQ("http://www.example.org", |
| 9759 | response->auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 9760 | EXPECT_EQ("MyRealm2", response->auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 9761 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9762 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9763 | TestCompletionCallback callback2; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9764 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9765 | rv = trans.RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 9766 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9767 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9768 | |
| 9769 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9770 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9771 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9772 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9773 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9774 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9775 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9776 | } |
| 9777 | |
| 9778 | // ------------------------------------------------------------------------ |
| 9779 | |
| 9780 | // Transaction 3: Resend a request in MyRealm's protection space -- |
| 9781 | // succeed with preemptive authorization. |
| 9782 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9783 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9784 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9785 | request.url = GURL("http://www.example.org/x/y/z2"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9786 | request.traffic_annotation = |
| 9787 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9788 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9789 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9790 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9791 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9792 | MockWrite( |
| 9793 | "GET /x/y/z2 HTTP/1.1\r\n" |
| 9794 | "Host: www.example.org\r\n" |
| 9795 | "Connection: keep-alive\r\n" |
| 9796 | // The authorization for MyRealm1 gets sent preemptively |
| 9797 | // (since the url is in the same protection space) |
| 9798 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9799 | }; |
| 9800 | |
| 9801 | // Sever accepts the preemptive authorization |
| 9802 | MockRead data_reads1[] = { |
| 9803 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9804 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9805 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9806 | }; |
| 9807 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9808 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9809 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9810 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9811 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9812 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9813 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9814 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9815 | |
| 9816 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9817 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9818 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9819 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9820 | ASSERT_TRUE(response); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9821 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9822 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9823 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9824 | } |
| 9825 | |
| 9826 | // ------------------------------------------------------------------------ |
| 9827 | |
| 9828 | // Transaction 4: request another URL in MyRealm (however the |
| 9829 | // url is not known to belong to the protection space, so no pre-auth). |
| 9830 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9831 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9832 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9833 | request.url = GURL("http://www.example.org/x/1"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9834 | request.traffic_annotation = |
| 9835 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9836 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9837 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9838 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9839 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9840 | MockWrite( |
| 9841 | "GET /x/1 HTTP/1.1\r\n" |
| 9842 | "Host: www.example.org\r\n" |
| 9843 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9844 | }; |
| 9845 | |
| 9846 | MockRead data_reads1[] = { |
| 9847 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9848 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9849 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9850 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9851 | }; |
| 9852 | |
| 9853 | // Resend with authorization from MyRealm's cache. |
| 9854 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9855 | MockWrite( |
| 9856 | "GET /x/1 HTTP/1.1\r\n" |
| 9857 | "Host: www.example.org\r\n" |
| 9858 | "Connection: keep-alive\r\n" |
| 9859 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9860 | }; |
| 9861 | |
| 9862 | // Sever accepts the authorization. |
| 9863 | MockRead data_reads2[] = { |
| 9864 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9865 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9866 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9867 | }; |
| 9868 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9869 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9870 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9871 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9872 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9873 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9874 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9875 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9876 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9877 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9878 | |
| 9879 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9880 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9881 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9882 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9883 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9884 | rv = trans.RestartWithAuth(AuthCredentials(), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9885 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 9886 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9887 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9888 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 9889 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9890 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9891 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9892 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9893 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9894 | } |
| 9895 | |
| 9896 | // ------------------------------------------------------------------------ |
| 9897 | |
| 9898 | // Transaction 5: request a URL in MyRealm, but the server rejects the |
| 9899 | // cached identity. Should invalidate and re-prompt. |
| 9900 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9901 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9902 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9903 | request.url = GURL("http://www.example.org/p/q/t"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 9904 | request.traffic_annotation = |
| 9905 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9906 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9907 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9908 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9909 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9910 | MockWrite( |
| 9911 | "GET /p/q/t HTTP/1.1\r\n" |
| 9912 | "Host: www.example.org\r\n" |
| 9913 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9914 | }; |
| 9915 | |
| 9916 | MockRead data_reads1[] = { |
| 9917 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9918 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9919 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9920 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9921 | }; |
| 9922 | |
| 9923 | // Resend with authorization from cache for MyRealm. |
| 9924 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9925 | MockWrite( |
| 9926 | "GET /p/q/t HTTP/1.1\r\n" |
| 9927 | "Host: www.example.org\r\n" |
| 9928 | "Connection: keep-alive\r\n" |
| 9929 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9930 | }; |
| 9931 | |
| 9932 | // Sever rejects the authorization. |
| 9933 | MockRead data_reads2[] = { |
| 9934 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9935 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9936 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9937 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9938 | }; |
| 9939 | |
| 9940 | // At this point we should prompt for new credentials for MyRealm. |
| 9941 | // Restart with username=foo3, password=foo4. |
| 9942 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9943 | MockWrite( |
| 9944 | "GET /p/q/t HTTP/1.1\r\n" |
| 9945 | "Host: www.example.org\r\n" |
| 9946 | "Connection: keep-alive\r\n" |
| 9947 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9948 | }; |
| 9949 | |
| 9950 | // Sever accepts the authorization. |
| 9951 | MockRead data_reads3[] = { |
| 9952 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9953 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9954 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9955 | }; |
| 9956 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9957 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9958 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 9959 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9960 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9961 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 9962 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9963 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9964 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9965 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9966 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9967 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9968 | |
| 9969 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9970 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9971 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9972 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9973 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9974 | rv = trans.RestartWithAuth(AuthCredentials(), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9975 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 9976 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9977 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9978 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 9979 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9980 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9981 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9982 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9983 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9984 | TestCompletionCallback callback3; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9985 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9986 | rv = trans.RestartWithAuth(AuthCredentials(kFoo3, kBar3), |
| 9987 | callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9988 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9989 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 9990 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9991 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9992 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9993 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9994 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9995 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9996 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9997 | } |
| 9998 | } |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 9999 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10000 | // Tests that nonce count increments when multiple auth attempts |
| 10001 | // are started with the same nonce. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10002 | TEST_F(HttpNetworkTransactionTest, DigestPreAuthNonceCount) { |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 10003 | HttpAuthHandlerDigest::Factory* digest_factory = |
| 10004 | new HttpAuthHandlerDigest::Factory(); |
| 10005 | HttpAuthHandlerDigest::FixedNonceGenerator* nonce_generator = |
| 10006 | new HttpAuthHandlerDigest::FixedNonceGenerator("0123456789abcdef"); |
| 10007 | digest_factory->set_nonce_generator(nonce_generator); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10008 | session_deps_.http_auth_handler_factory.reset(digest_factory); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10009 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10010 | |
| 10011 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 10012 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10013 | HttpRequestInfo request; |
| 10014 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10015 | request.url = GURL("http://www.example.org/x/y/z"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10016 | request.traffic_annotation = |
| 10017 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10018 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10019 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 10020 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10021 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10022 | MockWrite( |
| 10023 | "GET /x/y/z HTTP/1.1\r\n" |
| 10024 | "Host: www.example.org\r\n" |
| 10025 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10026 | }; |
| 10027 | |
| 10028 | MockRead data_reads1[] = { |
| 10029 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 10030 | MockRead("WWW-Authenticate: Digest realm=\"digestive\", nonce=\"OU812\", " |
| 10031 | "algorithm=MD5, qop=\"auth\"\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10032 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10033 | }; |
| 10034 | |
| 10035 | // Resend with authorization (username=foo, password=bar) |
| 10036 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10037 | MockWrite( |
| 10038 | "GET /x/y/z HTTP/1.1\r\n" |
| 10039 | "Host: www.example.org\r\n" |
| 10040 | "Connection: keep-alive\r\n" |
| 10041 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 10042 | "nonce=\"OU812\", uri=\"/x/y/z\", algorithm=MD5, " |
| 10043 | "response=\"03ffbcd30add722589c1de345d7a927f\", qop=auth, " |
| 10044 | "nc=00000001, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10045 | }; |
| 10046 | |
| 10047 | // Sever accepts the authorization. |
| 10048 | MockRead data_reads2[] = { |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 10049 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 10050 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10051 | }; |
| 10052 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10053 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 10054 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10055 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 10056 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10057 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10058 | TestCompletionCallback callback1; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10059 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10060 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10061 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10062 | |
| 10063 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10064 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10065 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10066 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10067 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10068 | EXPECT_TRUE(CheckDigestServerAuth(response->auth_challenge)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10069 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10070 | TestCompletionCallback callback2; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10071 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10072 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 10073 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10074 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10075 | |
| 10076 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10077 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10078 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10079 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10080 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10081 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10082 | } |
| 10083 | |
| 10084 | // ------------------------------------------------------------------------ |
| 10085 | |
| 10086 | // Transaction 2: Request another resource in digestive's protection space. |
| 10087 | // This will preemptively add an Authorization header which should have an |
| 10088 | // "nc" value of 2 (as compared to 1 in the first use. |
| 10089 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10090 | HttpRequestInfo request; |
| 10091 | request.method = "GET"; |
| 10092 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 10093 | // protection space as digest. |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10094 | request.url = GURL("http://www.example.org/x/y/a/b"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10095 | request.traffic_annotation = |
| 10096 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10097 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10098 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 10099 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10100 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10101 | MockWrite( |
| 10102 | "GET /x/y/a/b HTTP/1.1\r\n" |
| 10103 | "Host: www.example.org\r\n" |
| 10104 | "Connection: keep-alive\r\n" |
| 10105 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 10106 | "nonce=\"OU812\", uri=\"/x/y/a/b\", algorithm=MD5, " |
| 10107 | "response=\"d6f9a2c07d1c5df7b89379dca1269b35\", qop=auth, " |
| 10108 | "nc=00000002, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10109 | }; |
| 10110 | |
| 10111 | // Sever accepts the authorization. |
| 10112 | MockRead data_reads1[] = { |
| 10113 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 10114 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10115 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10116 | }; |
| 10117 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10118 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10119 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10120 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10121 | TestCompletionCallback callback1; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10122 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10123 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10124 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10125 | |
| 10126 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10127 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10128 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10129 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10130 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10131 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10132 | } |
| 10133 | } |
| 10134 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10135 | // Test the ResetStateForRestart() private method. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10136 | TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10137 | // Create a transaction (the dependencies aren't important). |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10138 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10139 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10140 | |
| 10141 | // Setup some state (which we expect ResetStateForRestart() will clear). |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 10142 | trans.read_buf_ = base::MakeRefCounted<IOBuffer>(15); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10143 | trans.read_buf_len_ = 15; |
| 10144 | trans.request_headers_.SetHeader("Authorization", "NTLM"); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10145 | |
| 10146 | // Setup state in response_ |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10147 | HttpResponseInfo* response = &trans.response_; |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10148 | response->auth_challenge = base::nullopt; |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 10149 | response->ssl_info.cert_status = static_cast<CertStatus>(-1); // Nonsensical. |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 10150 | response->response_time = base::Time::Now(); |
| 10151 | response->was_cached = true; // (Wouldn't ever actually be true...) |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10152 | |
| 10153 | { // Setup state for response_.vary_data |
| 10154 | HttpRequestInfo request; |
| 10155 | std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 10156 | std::replace(temp.begin(), temp.end(), '\n', '\0'); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 10157 | scoped_refptr<HttpResponseHeaders> headers(new HttpResponseHeaders(temp)); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 10158 | request.extra_headers.SetHeader("Foo", "1"); |
| 10159 | request.extra_headers.SetHeader("bar", "23"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10160 | EXPECT_TRUE(response->vary_data.Init(request, *headers.get())); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10161 | } |
| 10162 | |
| 10163 | // Cause the above state to be reset. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10164 | trans.ResetStateForRestart(); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10165 | |
| 10166 | // Verify that the state that needed to be reset, has been reset. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10167 | EXPECT_FALSE(trans.read_buf_); |
| 10168 | EXPECT_EQ(0, trans.read_buf_len_); |
| 10169 | EXPECT_TRUE(trans.request_headers_.IsEmpty()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10170 | EXPECT_FALSE(response->auth_challenge.has_value()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10171 | EXPECT_FALSE(response->headers); |
[email protected] | 34f4094 | 2010-10-04 00:34:04 | [diff] [blame] | 10172 | EXPECT_FALSE(response->was_cached); |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 10173 | EXPECT_EQ(0U, response->ssl_info.cert_status); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 10174 | EXPECT_FALSE(response->vary_data.is_valid()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10175 | } |
| 10176 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10177 | // Test HTTPS connections to a site with a bad certificate |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10178 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10179 | HttpRequestInfo request; |
| 10180 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10181 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10182 | request.traffic_annotation = |
| 10183 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10184 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10185 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10186 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 10187 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10188 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10189 | MockWrite( |
| 10190 | "GET / HTTP/1.1\r\n" |
| 10191 | "Host: www.example.org\r\n" |
| 10192 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10193 | }; |
| 10194 | |
| 10195 | MockRead data_reads[] = { |
| 10196 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 10197 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 10198 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10199 | MockRead(SYNCHRONOUS, OK), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10200 | }; |
| 10201 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 10202 | StaticSocketDataProvider ssl_bad_certificate; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10203 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10204 | SSLSocketDataProvider ssl_bad(ASYNC, ERR_CERT_AUTHORITY_INVALID); |
| 10205 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10206 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10207 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_bad_certificate); |
| 10208 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10209 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bad); |
| 10210 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10211 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10212 | TestCompletionCallback callback; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10213 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10214 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10215 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10216 | |
| 10217 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10218 | EXPECT_THAT(rv, IsError(ERR_CERT_AUTHORITY_INVALID)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10219 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10220 | rv = trans.RestartIgnoringLastError(callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10221 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10222 | |
| 10223 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10224 | EXPECT_THAT(rv, IsOk()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10225 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10226 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10227 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10228 | ASSERT_TRUE(response); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10229 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 10230 | } |
| 10231 | |
| 10232 | // Test HTTPS connections to a site with a bad certificate, going through a |
| 10233 | // proxy |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10234 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaProxy) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10235 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 10236 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10237 | |
| 10238 | HttpRequestInfo request; |
| 10239 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10240 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10241 | request.traffic_annotation = |
| 10242 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10243 | |
| 10244 | MockWrite proxy_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10245 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10246 | "Host: www.example.org:443\r\n" |
| 10247 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10248 | }; |
| 10249 | |
| 10250 | MockRead proxy_reads[] = { |
| 10251 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10252 | MockRead(SYNCHRONOUS, OK) |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10253 | }; |
| 10254 | |
| 10255 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10256 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10257 | "Host: www.example.org:443\r\n" |
| 10258 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 10259 | MockWrite("GET / HTTP/1.1\r\n" |
| 10260 | "Host: www.example.org\r\n" |
| 10261 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10262 | }; |
| 10263 | |
| 10264 | MockRead data_reads[] = { |
| 10265 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 10266 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 10267 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 10268 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10269 | MockRead(SYNCHRONOUS, OK), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10270 | }; |
| 10271 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10272 | StaticSocketDataProvider ssl_bad_certificate(proxy_reads, proxy_writes); |
| 10273 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10274 | SSLSocketDataProvider ssl_bad(ASYNC, ERR_CERT_AUTHORITY_INVALID); |
| 10275 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10276 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10277 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_bad_certificate); |
| 10278 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10279 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bad); |
| 10280 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10281 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10282 | TestCompletionCallback callback; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10283 | |
| 10284 | for (int i = 0; i < 2; i++) { |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10285 | session_deps_.socket_factory->ResetNextMockIndexes(); |
[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] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10289 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10290 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10291 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10292 | |
| 10293 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10294 | EXPECT_THAT(rv, IsError(ERR_CERT_AUTHORITY_INVALID)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10295 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10296 | rv = trans.RestartIgnoringLastError(callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10297 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10298 | |
| 10299 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10300 | EXPECT_THAT(rv, IsOk()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10301 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10302 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10303 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10304 | ASSERT_TRUE(response); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10305 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 10306 | } |
| 10307 | } |
| 10308 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10309 | |
| 10310 | // Test HTTPS connections to a site, going through an HTTPS proxy |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10311 | TEST_F(HttpNetworkTransactionTest, HTTPSViaHttpsProxy) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10312 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10313 | ProxyResolutionService::CreateFixedFromPacResult( |
| 10314 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 10315 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10316 | session_deps_.net_log = &net_log; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10317 | |
| 10318 | HttpRequestInfo request; |
| 10319 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10320 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10321 | request.traffic_annotation = |
| 10322 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10323 | |
| 10324 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10325 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10326 | "Host: www.example.org:443\r\n" |
| 10327 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 10328 | MockWrite("GET / HTTP/1.1\r\n" |
| 10329 | "Host: www.example.org\r\n" |
| 10330 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10331 | }; |
| 10332 | |
| 10333 | MockRead data_reads[] = { |
| 10334 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 10335 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 10336 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 10337 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10338 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10339 | }; |
| 10340 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10341 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10342 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
| 10343 | SSLSocketDataProvider tunnel_ssl(ASYNC, OK); // SSL through the tunnel |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10344 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10345 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10346 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
| 10347 | session_deps_.socket_factory->AddSSLSocketDataProvider(&tunnel_ssl); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10348 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10349 | TestCompletionCallback callback; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10350 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10351 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10352 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10353 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10354 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10355 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10356 | |
| 10357 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10358 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10359 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10360 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10361 | ASSERT_TRUE(response); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10362 | |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 10363 | EXPECT_TRUE(response->proxy_server.is_https()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10364 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 10365 | EXPECT_EQ(200, response->headers->response_code()); |
| 10366 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 10367 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 10368 | |
| 10369 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10370 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 10371 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 10372 | CONNECT_TIMING_HAS_SSL_TIMES); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10373 | } |
| 10374 | |
Eric Roman | 695a787 | 2019-04-16 21:53:29 | [diff] [blame] | 10375 | // Test that an HTTPS Proxy cannot redirect a CONNECT request for main frames. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10376 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaHttpsProxy) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10377 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10378 | ProxyResolutionService::CreateFixedFromPacResult( |
| 10379 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 10380 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10381 | session_deps_.net_log = &net_log; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10382 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10383 | const base::TimeDelta kTimeIncrement = base::TimeDelta::FromSeconds(4); |
| 10384 | session_deps_.host_resolver->set_ondemand_mode(true); |
| 10385 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10386 | HttpRequestInfo request; |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10387 | request.load_flags = LOAD_MAIN_FRAME_DEPRECATED; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10388 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10389 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10390 | request.traffic_annotation = |
| 10391 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10392 | |
| 10393 | MockWrite data_writes[] = { |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10394 | MockWrite(ASYNC, 0, |
| 10395 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10396 | "Host: www.example.org:443\r\n" |
| 10397 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10398 | }; |
| 10399 | |
| 10400 | MockRead data_reads[] = { |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10401 | // Pause on first read. |
| 10402 | MockRead(ASYNC, ERR_IO_PENDING, 1), |
| 10403 | MockRead(ASYNC, 2, "HTTP/1.1 302 Redirect\r\n"), |
| 10404 | MockRead(ASYNC, 3, "Location: http://login.example.com/\r\n"), |
| 10405 | MockRead(ASYNC, 4, "Content-Length: 0\r\n\r\n"), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10406 | }; |
| 10407 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10408 | SequencedSocketData data(MockConnect(ASYNC, OK), data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10409 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10410 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10411 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10412 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10413 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10414 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10415 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10416 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10417 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10418 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10419 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10420 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10421 | EXPECT_TRUE(session_deps_.host_resolver->has_pending_requests()); |
| 10422 | |
| 10423 | // Host resolution takes |kTimeIncrement|. |
| 10424 | FastForwardBy(kTimeIncrement); |
| 10425 | // Resolving the current request with |ResolveNow| will cause the pending |
| 10426 | // request to instantly complete, and the async connect will start as well. |
| 10427 | session_deps_.host_resolver->ResolveOnlyRequestNow(); |
| 10428 | |
| 10429 | // Connecting takes |kTimeIncrement|. |
| 10430 | FastForwardBy(kTimeIncrement); |
| 10431 | data.RunUntilPaused(); |
| 10432 | |
| 10433 | // The server takes |kTimeIncrement| to respond. |
| 10434 | FastForwardBy(kTimeIncrement); |
| 10435 | data.Resume(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10436 | |
| 10437 | rv = callback.WaitForResult(); |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 10438 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10439 | } |
| 10440 | |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10441 | // Test that an HTTPS Proxy cannot redirect a CONNECT request for subresources. |
| 10442 | TEST_F(HttpNetworkTransactionTest, |
| 10443 | RedirectOfHttpsConnectSubresourceViaHttpsProxy) { |
| 10444 | base::HistogramTester histograms; |
| 10445 | session_deps_.proxy_resolution_service = |
| 10446 | ProxyResolutionService::CreateFixedFromPacResult( |
| 10447 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 10448 | TestNetLog net_log; |
| 10449 | session_deps_.net_log = &net_log; |
| 10450 | |
| 10451 | HttpRequestInfo request; |
| 10452 | request.method = "GET"; |
| 10453 | request.url = GURL("https://www.example.org/"); |
| 10454 | request.traffic_annotation = |
| 10455 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 10456 | |
| 10457 | MockWrite data_writes[] = { |
| 10458 | MockWrite(ASYNC, 0, |
| 10459 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10460 | "Host: www.example.org:443\r\n" |
| 10461 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 10462 | }; |
| 10463 | |
| 10464 | MockRead data_reads[] = { |
| 10465 | MockRead(ASYNC, 1, "HTTP/1.1 302 Redirect\r\n"), |
| 10466 | MockRead(ASYNC, 2, "Location: http://login.example.com/\r\n"), |
| 10467 | MockRead(ASYNC, 3, "Content-Length: 0\r\n\r\n"), |
| 10468 | }; |
| 10469 | |
| 10470 | SequencedSocketData data(MockConnect(ASYNC, OK), data_reads, data_writes); |
| 10471 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
| 10472 | |
| 10473 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10474 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
| 10475 | |
| 10476 | TestCompletionCallback callback; |
| 10477 | |
| 10478 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 10479 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 10480 | |
| 10481 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 10482 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 10483 | |
| 10484 | rv = callback.WaitForResult(); |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 10485 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10486 | } |
| 10487 | |
| 10488 | // Test that an HTTPS Proxy which was auto-detected cannot redirect a CONNECT |
| 10489 | // request for main frames. |
| 10490 | TEST_F(HttpNetworkTransactionTest, |
| 10491 | RedirectOfHttpsConnectViaAutoDetectedHttpsProxy) { |
| 10492 | base::HistogramTester histograms; |
| 10493 | session_deps_.proxy_resolution_service = |
| 10494 | ProxyResolutionService::CreateFixedFromAutoDetectedPacResult( |
| 10495 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 10496 | TestNetLog net_log; |
| 10497 | session_deps_.net_log = &net_log; |
| 10498 | |
| 10499 | HttpRequestInfo request; |
| 10500 | request.load_flags = LOAD_MAIN_FRAME_DEPRECATED; |
| 10501 | request.method = "GET"; |
| 10502 | request.url = GURL("https://www.example.org/"); |
| 10503 | request.traffic_annotation = |
| 10504 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 10505 | |
| 10506 | MockWrite data_writes[] = { |
| 10507 | MockWrite(ASYNC, 0, |
| 10508 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10509 | "Host: www.example.org:443\r\n" |
| 10510 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 10511 | }; |
| 10512 | |
| 10513 | MockRead data_reads[] = { |
| 10514 | MockRead(ASYNC, 1, "HTTP/1.1 302 Redirect\r\n"), |
| 10515 | MockRead(ASYNC, 2, "Location: http://login.example.com/\r\n"), |
| 10516 | MockRead(ASYNC, 3, "Content-Length: 0\r\n\r\n"), |
| 10517 | }; |
| 10518 | |
| 10519 | SequencedSocketData data(MockConnect(ASYNC, OK), data_reads, data_writes); |
| 10520 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
| 10521 | |
| 10522 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10523 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
| 10524 | |
| 10525 | TestCompletionCallback callback; |
| 10526 | |
| 10527 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 10528 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 10529 | |
| 10530 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 10531 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 10532 | |
| 10533 | rv = callback.WaitForResult(); |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 10534 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10535 | } |
| 10536 | |
Eric Roman | 695a787 | 2019-04-16 21:53:29 | [diff] [blame] | 10537 | // 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] | 10538 | // frames. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10539 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaSpdyProxy) { |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10540 | base::HistogramTester histograms; |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10541 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 10542 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10543 | TestNetLog net_log; |
| 10544 | session_deps_.net_log = &net_log; |
| 10545 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10546 | const base::TimeDelta kTimeIncrement = base::TimeDelta::FromSeconds(4); |
| 10547 | session_deps_.host_resolver->set_ondemand_mode(true); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10548 | |
| 10549 | HttpRequestInfo request; |
| 10550 | request.method = "GET"; |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10551 | request.load_flags = LOAD_MAIN_FRAME_DEPRECATED; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10552 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10553 | request.traffic_annotation = |
| 10554 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10555 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10556 | spdy::SpdySerializedFrame conn(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 10557 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 10558 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10559 | spdy::SpdySerializedFrame goaway( |
| 10560 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_CANCEL)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10561 | MockWrite data_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10562 | CreateMockWrite(conn, 0, SYNCHRONOUS), |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10563 | CreateMockWrite(goaway, 3, SYNCHRONOUS), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10564 | }; |
| 10565 | |
| 10566 | static const char* const kExtraHeaders[] = { |
| 10567 | "location", |
| 10568 | "http://login.example.com/", |
| 10569 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10570 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyReplyError( |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 10571 | "302", kExtraHeaders, base::size(kExtraHeaders) / 2, 1)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10572 | MockRead data_reads[] = { |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10573 | // Pause on first read. |
| 10574 | MockRead(ASYNC, ERR_IO_PENDING, 1), CreateMockRead(resp, 2), |
| 10575 | MockRead(ASYNC, 0, 4), // EOF |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10576 | }; |
| 10577 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10578 | SequencedSocketData data(MockConnect(ASYNC, OK), data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10579 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 10580 | proxy_ssl.next_proto = kProtoHTTP2; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10581 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10582 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10583 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10584 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10585 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10586 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10587 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10588 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10589 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10590 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10591 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10592 | EXPECT_TRUE(session_deps_.host_resolver->has_pending_requests()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10593 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10594 | // Host resolution takes |kTimeIncrement|. |
| 10595 | FastForwardBy(kTimeIncrement); |
| 10596 | // Resolving the current request with |ResolveNow| will cause the pending |
| 10597 | // request to instantly complete, and the async connect will start as well. |
| 10598 | session_deps_.host_resolver->ResolveOnlyRequestNow(); |
| 10599 | |
| 10600 | // Connecting takes |kTimeIncrement|. |
| 10601 | FastForwardBy(kTimeIncrement); |
| 10602 | data.RunUntilPaused(); |
| 10603 | |
| 10604 | FastForwardBy(kTimeIncrement); |
| 10605 | data.Resume(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10606 | rv = callback.WaitForResult(); |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 10607 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10608 | } |
| 10609 | |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 10610 | // Test that an HTTPS proxy's response to a CONNECT request is filtered. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10611 | TEST_F(HttpNetworkTransactionTest, ErrorResponseToHttpsConnectViaHttpsProxy) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10612 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 10613 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10614 | |
| 10615 | HttpRequestInfo request; |
| 10616 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10617 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10618 | request.traffic_annotation = |
| 10619 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10620 | |
| 10621 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10622 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10623 | "Host: www.example.org:443\r\n" |
| 10624 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10625 | }; |
| 10626 | |
| 10627 | MockRead data_reads[] = { |
| 10628 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 10629 | MockRead("Content-Length: 23\r\n\r\n"), |
| 10630 | MockRead("The host does not exist"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10631 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10632 | }; |
| 10633 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10634 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10635 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10636 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10637 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10638 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10639 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10640 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10641 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10642 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10643 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10644 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10645 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10646 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10647 | |
| 10648 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10649 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10650 | |
ttuttle | 960fcbf | 2016-04-19 13:26:32 | [diff] [blame] | 10651 | // TODO(juliatuttle): Anything else to check here? |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10652 | } |
| 10653 | |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 10654 | // Test that a SPDY proxy's response to a CONNECT request is filtered. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10655 | TEST_F(HttpNetworkTransactionTest, ErrorResponseToHttpsConnectViaSpdyProxy) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10656 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 10657 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10658 | |
| 10659 | HttpRequestInfo request; |
| 10660 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10661 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10662 | request.traffic_annotation = |
| 10663 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10664 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10665 | spdy::SpdySerializedFrame conn(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 10666 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 10667 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10668 | spdy::SpdySerializedFrame rst( |
| 10669 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_CANCEL)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10670 | MockWrite data_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10671 | CreateMockWrite(conn, 0), CreateMockWrite(rst, 3), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10672 | }; |
| 10673 | |
| 10674 | static const char* const kExtraHeaders[] = { |
| 10675 | "location", |
| 10676 | "http://login.example.com/", |
| 10677 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10678 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyReplyError( |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 10679 | "404", kExtraHeaders, base::size(kExtraHeaders) / 2, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10680 | spdy::SpdySerializedFrame body( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 10681 | spdy_util_.ConstructSpdyDataFrame(1, "The host does not exist", true)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10682 | MockRead data_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10683 | CreateMockRead(resp, 1), CreateMockRead(body, 2), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 10684 | MockRead(ASYNC, 0, 4), // EOF |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10685 | }; |
| 10686 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10687 | SequencedSocketData data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10688 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 10689 | proxy_ssl.next_proto = kProtoHTTP2; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10690 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10691 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10692 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10693 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10694 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10695 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10696 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10697 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10698 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10699 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10700 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10701 | |
| 10702 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10703 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10704 | |
ttuttle | 960fcbf | 2016-04-19 13:26:32 | [diff] [blame] | 10705 | // TODO(juliatuttle): Anything else to check here? |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10706 | } |
| 10707 | |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10708 | // Test the request-challenge-retry sequence for basic auth, through |
| 10709 | // a SPDY proxy over a single SPDY session. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10710 | TEST_F(HttpNetworkTransactionTest, BasicAuthSpdyProxy) { |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10711 | HttpRequestInfo request; |
| 10712 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10713 | request.url = GURL("https://www.example.org/"); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10714 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 10715 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10716 | request.traffic_annotation = |
| 10717 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10718 | |
| 10719 | // Configure against https proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10720 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10721 | ProxyResolutionService::CreateFixedFromPacResult( |
| 10722 | "HTTPS myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 10723 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10724 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10725 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10726 | |
| 10727 | // Since we have proxy, should try to establish tunnel. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10728 | spdy::SpdySerializedFrame req(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 10729 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 10730 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10731 | spdy::SpdySerializedFrame rst( |
| 10732 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_CANCEL)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 10733 | spdy_util_.UpdateWithStreamDestruction(1); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10734 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10735 | // After calling trans.RestartWithAuth(), this is the request we should |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10736 | // be issuing -- the final header line contains the credentials. |
| 10737 | const char* const kAuthCredentials[] = { |
| 10738 | "proxy-authorization", "Basic Zm9vOmJhcg==", |
| 10739 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10740 | spdy::SpdySerializedFrame connect2(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 10741 | kAuthCredentials, base::size(kAuthCredentials) / 2, 3, |
| 10742 | HttpProxyConnectJob::kH2QuicTunnelPriority, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10743 | HostPortPair("www.example.org", 443))); |
| 10744 | // fetch https://www.example.org/ via HTTP |
| 10745 | const char get[] = |
| 10746 | "GET / HTTP/1.1\r\n" |
| 10747 | "Host: www.example.org\r\n" |
| 10748 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10749 | spdy::SpdySerializedFrame wrapped_get( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 10750 | spdy_util_.ConstructSpdyDataFrame(3, get, false)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10751 | |
| 10752 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10753 | CreateMockWrite(req, 0, ASYNC), CreateMockWrite(rst, 2, ASYNC), |
| 10754 | CreateMockWrite(connect2, 3), CreateMockWrite(wrapped_get, 5), |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10755 | }; |
| 10756 | |
| 10757 | // The proxy responds to the connect with a 407, using a persistent |
| 10758 | // connection. |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 10759 | const char kAuthStatus[] = "407"; |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10760 | const char* const kAuthChallenge[] = { |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10761 | "proxy-authenticate", "Basic realm=\"MyRealm1\"", |
| 10762 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10763 | spdy::SpdySerializedFrame conn_auth_resp(spdy_util_.ConstructSpdyReplyError( |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 10764 | kAuthStatus, kAuthChallenge, base::size(kAuthChallenge) / 2, 1)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10765 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10766 | spdy::SpdySerializedFrame conn_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10767 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10768 | const char resp[] = "HTTP/1.1 200 OK\r\n" |
| 10769 | "Content-Length: 5\r\n\r\n"; |
| 10770 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10771 | spdy::SpdySerializedFrame wrapped_get_resp( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 10772 | spdy_util_.ConstructSpdyDataFrame(3, resp, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10773 | spdy::SpdySerializedFrame wrapped_body( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 10774 | spdy_util_.ConstructSpdyDataFrame(3, "hello", false)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10775 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10776 | CreateMockRead(conn_auth_resp, 1, ASYNC), |
| 10777 | CreateMockRead(conn_resp, 4, ASYNC), |
| 10778 | CreateMockRead(wrapped_get_resp, 6, ASYNC), |
| 10779 | CreateMockRead(wrapped_body, 7, ASYNC), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 10780 | MockRead(ASYNC, OK, 8), // EOF. May or may not be read. |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10781 | }; |
| 10782 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10783 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10784 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10785 | // Negotiate SPDY to the proxy |
| 10786 | SSLSocketDataProvider proxy(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 10787 | proxy.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10788 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10789 | // Vanilla SSL to the server |
| 10790 | SSLSocketDataProvider server(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10791 | session_deps_.socket_factory->AddSSLSocketDataProvider(&server); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10792 | |
| 10793 | TestCompletionCallback callback1; |
| 10794 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 10795 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10796 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10797 | |
| 10798 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10799 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10800 | |
| 10801 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10802 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 10803 | auto entries = log.GetEntries(); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10804 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 10805 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 10806 | NetLogEventPhase::NONE); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10807 | ExpectLogContainsSomewhere( |
| 10808 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 10809 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 10810 | NetLogEventPhase::NONE); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10811 | |
| 10812 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10813 | ASSERT_TRUE(response); |
| 10814 | ASSERT_TRUE(response->headers); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10815 | EXPECT_EQ(407, response->headers->response_code()); |
| 10816 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10817 | EXPECT_TRUE(response->auth_challenge.has_value()); |
| 10818 | EXPECT_TRUE(CheckBasicSecureProxyAuth(response->auth_challenge)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10819 | |
| 10820 | TestCompletionCallback callback2; |
| 10821 | |
| 10822 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 10823 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10824 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10825 | |
| 10826 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10827 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10828 | |
| 10829 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10830 | ASSERT_TRUE(response); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10831 | |
| 10832 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 10833 | EXPECT_EQ(200, response->headers->response_code()); |
| 10834 | EXPECT_EQ(5, response->headers->GetContentLength()); |
| 10835 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 10836 | |
| 10837 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10838 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10839 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 10840 | LoadTimingInfo load_timing_info; |
| 10841 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 10842 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 10843 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 10844 | |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10845 | trans.reset(); |
| 10846 | session->CloseAllConnections(); |
| 10847 | } |
| 10848 | |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10849 | // Test that an explicitly trusted SPDY proxy can push a resource from an |
| 10850 | // origin that is different from that of its associated resource. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10851 | TEST_F(HttpNetworkTransactionTest, CrossOriginSPDYProxyPush) { |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 10852 | // Configure the proxy delegate to allow cross-origin SPDY pushes. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10853 | auto proxy_delegate = std::make_unique<TestProxyDelegate>(); |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 10854 | proxy_delegate->set_trusted_spdy_proxy(net::ProxyServer::FromURI( |
| 10855 | "https://myproxy:443", net::ProxyServer::SCHEME_HTTP)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10856 | HttpRequestInfo request; |
| 10857 | HttpRequestInfo push_request; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10858 | request.traffic_annotation = |
| 10859 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10860 | |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10861 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10862 | request.url = GURL("http://www.example.org/"); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10863 | push_request.method = "GET"; |
| 10864 | push_request.url = GURL("http://www.another-origin.com/foo.dat"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10865 | push_request.traffic_annotation = |
| 10866 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10867 | |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 10868 | // Configure against https proxy server "myproxy:443". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10869 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10870 | ProxyResolutionService::CreateFixedFromPacResult( |
| 10871 | "HTTPS myproxy:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 10872 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10873 | session_deps_.net_log = log.bound().net_log(); |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 10874 | |
Eric Roman | 3d8546a | 2018-09-10 17:00:52 | [diff] [blame] | 10875 | session_deps_.proxy_resolution_service->SetProxyDelegate( |
| 10876 | proxy_delegate.get()); |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 10877 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10878 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10879 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10880 | spdy::SpdySerializedFrame stream1_syn( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 10881 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10882 | spdy::SpdySerializedFrame stream2_priority( |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 10883 | spdy_util_.ConstructSpdyPriority(2, 1, IDLE, true)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10884 | |
| 10885 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10886 | CreateMockWrite(stream1_syn, 0, ASYNC), |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 10887 | CreateMockWrite(stream2_priority, 3, ASYNC), |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10888 | }; |
| 10889 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10890 | spdy::SpdySerializedFrame stream2_syn(spdy_util_.ConstructSpdyPush( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10891 | nullptr, 0, 2, 1, "http://www.another-origin.com/foo.dat")); |
Bence Béky | 7bf9436 | 2018-01-10 13:19:36 | [diff] [blame] | 10892 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10893 | spdy::SpdySerializedFrame stream1_reply( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10894 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10895 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10896 | spdy::SpdySerializedFrame stream1_body( |
| 10897 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10898 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10899 | spdy::SpdySerializedFrame stream2_body( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 10900 | spdy_util_.ConstructSpdyDataFrame(2, "pushed", true)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10901 | |
| 10902 | MockRead spdy_reads[] = { |
Bence Béky | 7bf9436 | 2018-01-10 13:19:36 | [diff] [blame] | 10903 | CreateMockRead(stream2_syn, 1, ASYNC), |
| 10904 | CreateMockRead(stream1_reply, 2, ASYNC), |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 10905 | CreateMockRead(stream1_body, 4, ASYNC), |
| 10906 | CreateMockRead(stream2_body, 5, ASYNC), |
| 10907 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 6), // Force a hang |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10908 | }; |
| 10909 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10910 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10911 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10912 | // Negotiate SPDY to the proxy |
| 10913 | SSLSocketDataProvider proxy(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 10914 | proxy.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10915 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10916 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 10917 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10918 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10919 | TestCompletionCallback callback; |
| 10920 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10921 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10922 | |
| 10923 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10924 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10925 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 10926 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 10927 | auto push_trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10928 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10929 | rv = push_trans->Start(&push_request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10930 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10931 | |
| 10932 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10933 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10934 | const HttpResponseInfo* push_response = push_trans->GetResponseInfo(); |
| 10935 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10936 | ASSERT_TRUE(response); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10937 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 10938 | |
| 10939 | EXPECT_EQ(200, response->headers->response_code()); |
| 10940 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 10941 | |
| 10942 | std::string response_data; |
| 10943 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10944 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10945 | EXPECT_EQ("hello!", response_data); |
| 10946 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 10947 | LoadTimingInfo load_timing_info; |
| 10948 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 10949 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 10950 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 10951 | |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10952 | // Verify the pushed stream. |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10953 | EXPECT_TRUE(push_response->headers); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10954 | EXPECT_EQ(200, push_response->headers->response_code()); |
| 10955 | |
| 10956 | rv = ReadTransaction(push_trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10957 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10958 | EXPECT_EQ("pushed", response_data); |
| 10959 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 10960 | LoadTimingInfo push_load_timing_info; |
| 10961 | EXPECT_TRUE(push_trans->GetLoadTimingInfo(&push_load_timing_info)); |
| 10962 | TestLoadTimingReusedWithPac(push_load_timing_info); |
| 10963 | // The transactions should share a socket ID, despite being for different |
| 10964 | // origins. |
| 10965 | EXPECT_EQ(load_timing_info.socket_log_id, |
| 10966 | push_load_timing_info.socket_log_id); |
| 10967 | |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10968 | trans.reset(); |
| 10969 | push_trans.reset(); |
| 10970 | session->CloseAllConnections(); |
| 10971 | } |
| 10972 | |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 10973 | // Test that an explicitly trusted SPDY proxy cannot push HTTPS content. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10974 | TEST_F(HttpNetworkTransactionTest, CrossOriginProxyPushCorrectness) { |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 10975 | // Configure the proxy delegate to allow cross-origin SPDY pushes. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10976 | auto proxy_delegate = std::make_unique<TestProxyDelegate>(); |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 10977 | proxy_delegate->set_trusted_spdy_proxy(net::ProxyServer::FromURI( |
| 10978 | "https://myproxy:443", net::ProxyServer::SCHEME_HTTP)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 10979 | HttpRequestInfo request; |
| 10980 | |
| 10981 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10982 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 10983 | request.traffic_annotation = |
| 10984 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 10985 | |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10986 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 10987 | "https://myproxy:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 10988 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10989 | session_deps_.net_log = log.bound().net_log(); |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 10990 | |
| 10991 | // Enable cross-origin push. |
Eric Roman | 3d8546a | 2018-09-10 17:00:52 | [diff] [blame] | 10992 | session_deps_.proxy_resolution_service->SetProxyDelegate( |
| 10993 | proxy_delegate.get()); |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 10994 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10995 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 10996 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10997 | spdy::SpdySerializedFrame stream1_syn( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 10998 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 10999 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11000 | spdy::SpdySerializedFrame push_rst( |
| 11001 | spdy_util_.ConstructSpdyRstStream(2, spdy::ERROR_CODE_REFUSED_STREAM)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11002 | |
| 11003 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 11004 | CreateMockWrite(stream1_syn, 0, ASYNC), CreateMockWrite(push_rst, 3), |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11005 | }; |
| 11006 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11007 | spdy::SpdySerializedFrame stream1_reply( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 11008 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11009 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11010 | spdy::SpdySerializedFrame stream1_body( |
| 11011 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11012 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11013 | spdy::SpdySerializedFrame stream2_syn(spdy_util_.ConstructSpdyPush( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 11014 | nullptr, 0, 2, 1, "https://www.another-origin.com/foo.dat")); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11015 | |
| 11016 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 11017 | CreateMockRead(stream1_reply, 1, ASYNC), |
| 11018 | CreateMockRead(stream2_syn, 2, ASYNC), |
| 11019 | CreateMockRead(stream1_body, 4, ASYNC), |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 11020 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 5), // Force a hang |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11021 | }; |
| 11022 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11023 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11024 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11025 | // Negotiate SPDY to the proxy |
| 11026 | SSLSocketDataProvider proxy(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 11027 | proxy.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11028 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11029 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 11030 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 11031 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11032 | TestCompletionCallback callback; |
| 11033 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11034 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11035 | |
| 11036 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11037 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11038 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 11039 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11040 | ASSERT_TRUE(response); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11041 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 11042 | |
| 11043 | EXPECT_EQ(200, response->headers->response_code()); |
| 11044 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 11045 | |
| 11046 | std::string response_data; |
| 11047 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11048 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11049 | EXPECT_EQ("hello!", response_data); |
| 11050 | |
| 11051 | trans.reset(); |
| 11052 | session->CloseAllConnections(); |
| 11053 | } |
| 11054 | |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11055 | // Test that an explicitly trusted SPDY proxy can push same-origin HTTPS |
| 11056 | // resources. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11057 | TEST_F(HttpNetworkTransactionTest, SameOriginProxyPushCorrectness) { |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 11058 | // Configure the proxy delegate to allow cross-origin SPDY pushes. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 11059 | auto proxy_delegate = std::make_unique<TestProxyDelegate>(); |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 11060 | proxy_delegate->set_trusted_spdy_proxy( |
| 11061 | net::ProxyServer::FromURI("myproxy:70", net::ProxyServer::SCHEME_HTTP)); |
| 11062 | |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11063 | HttpRequestInfo request; |
| 11064 | |
| 11065 | request.method = "GET"; |
| 11066 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11067 | request.traffic_annotation = |
| 11068 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11069 | |
| 11070 | // Configure against https proxy server "myproxy:70". |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11071 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 11072 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11073 | BoundTestNetLog log; |
| 11074 | session_deps_.net_log = log.bound().net_log(); |
| 11075 | |
| 11076 | // Enable cross-origin push. |
Eric Roman | 3d8546a | 2018-09-10 17:00:52 | [diff] [blame] | 11077 | session_deps_.proxy_resolution_service->SetProxyDelegate( |
| 11078 | proxy_delegate.get()); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11079 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11080 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11081 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11082 | spdy::SpdySerializedFrame stream1_syn( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 11083 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11084 | spdy::SpdySerializedFrame stream2_priority( |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 11085 | spdy_util_.ConstructSpdyPriority(2, 1, IDLE, true)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11086 | |
| 11087 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 11088 | CreateMockWrite(stream1_syn, 0, ASYNC), |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 11089 | CreateMockWrite(stream2_priority, 3, ASYNC), |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11090 | }; |
| 11091 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11092 | spdy::SpdySerializedFrame stream1_reply( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 11093 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11094 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11095 | spdy::SpdySerializedFrame stream2_syn(spdy_util_.ConstructSpdyPush( |
Bence Béky | 096cf05 | 2017-08-08 23:55:33 | [diff] [blame] | 11096 | nullptr, 0, 2, 1, "http://www.example.org/foo.dat")); |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 11097 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11098 | spdy::SpdySerializedFrame stream1_body( |
| 11099 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11100 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11101 | spdy::SpdySerializedFrame stream2_reply( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 11102 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11103 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11104 | spdy::SpdySerializedFrame stream2_body( |
| 11105 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11106 | |
| 11107 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 11108 | CreateMockRead(stream1_reply, 1, ASYNC), |
| 11109 | CreateMockRead(stream2_syn, 2, ASYNC), |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 11110 | CreateMockRead(stream1_body, 4, ASYNC), |
| 11111 | CreateMockRead(stream2_body, 5, ASYNC), |
| 11112 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 6), // Force a hang |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11113 | }; |
| 11114 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11115 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11116 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
| 11117 | // Negotiate SPDY to the proxy |
| 11118 | SSLSocketDataProvider proxy(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 11119 | proxy.next_proto = kProtoHTTP2; |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11120 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); |
| 11121 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 11122 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 11123 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11124 | TestCompletionCallback callback; |
| 11125 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11126 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11127 | |
| 11128 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11129 | EXPECT_THAT(rv, IsOk()); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11130 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 11131 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11132 | ASSERT_TRUE(response); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11133 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 11134 | |
| 11135 | EXPECT_EQ(200, response->headers->response_code()); |
| 11136 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 11137 | |
| 11138 | std::string response_data; |
| 11139 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11140 | EXPECT_THAT(rv, IsOk()); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11141 | EXPECT_EQ("hello!", response_data); |
| 11142 | |
| 11143 | trans.reset(); |
| 11144 | session->CloseAllConnections(); |
| 11145 | } |
| 11146 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11147 | // Test HTTPS connections to a site with a bad certificate, going through an |
| 11148 | // HTTPS proxy |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11149 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaHttpsProxy) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11150 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 11151 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11152 | |
| 11153 | HttpRequestInfo request; |
| 11154 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11155 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11156 | request.traffic_annotation = |
| 11157 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11158 | |
| 11159 | // Attempt to fetch the URL from a server with a bad cert |
| 11160 | MockWrite bad_cert_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 11161 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 11162 | "Host: www.example.org:443\r\n" |
| 11163 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11164 | }; |
| 11165 | |
| 11166 | MockRead bad_cert_reads[] = { |
| 11167 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11168 | MockRead(SYNCHRONOUS, OK) |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11169 | }; |
| 11170 | |
| 11171 | // Attempt to fetch the URL with a good cert |
| 11172 | MockWrite good_data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 11173 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 11174 | "Host: www.example.org:443\r\n" |
| 11175 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 11176 | MockWrite("GET / HTTP/1.1\r\n" |
| 11177 | "Host: www.example.org\r\n" |
| 11178 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11179 | }; |
| 11180 | |
| 11181 | MockRead good_cert_reads[] = { |
| 11182 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 11183 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11184 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11185 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11186 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11187 | }; |
| 11188 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11189 | StaticSocketDataProvider ssl_bad_certificate(bad_cert_reads, bad_cert_writes); |
| 11190 | StaticSocketDataProvider data(good_cert_reads, good_data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11191 | SSLSocketDataProvider ssl_bad(ASYNC, ERR_CERT_AUTHORITY_INVALID); |
| 11192 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11193 | |
| 11194 | // SSL to the proxy, then CONNECT request, then SSL with bad certificate |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11195 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 11196 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_bad_certificate); |
| 11197 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bad); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11198 | |
| 11199 | // SSL to the proxy, then CONNECT request, then valid SSL certificate |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11200 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 11201 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 11202 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11203 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11204 | TestCompletionCallback callback; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11205 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11206 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11207 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11208 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11209 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11210 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11211 | |
| 11212 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11213 | EXPECT_THAT(rv, IsError(ERR_CERT_AUTHORITY_INVALID)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11214 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11215 | rv = trans.RestartIgnoringLastError(callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11216 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11217 | |
| 11218 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11219 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11220 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11221 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11222 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11223 | ASSERT_TRUE(response); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11224 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 11225 | } |
| 11226 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11227 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11228 | HttpRequestInfo request; |
| 11229 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11230 | request.url = GURL("http://www.example.org/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 11231 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 11232 | "Chromium Ultra Awesome X Edition"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11233 | request.traffic_annotation = |
| 11234 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11235 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11236 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11237 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11238 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11239 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11240 | MockWrite( |
| 11241 | "GET / HTTP/1.1\r\n" |
| 11242 | "Host: www.example.org\r\n" |
| 11243 | "Connection: keep-alive\r\n" |
| 11244 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11245 | }; |
| 11246 | |
| 11247 | // Lastly, the server responds with the actual content. |
| 11248 | MockRead data_reads[] = { |
| 11249 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11250 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11251 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11252 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11253 | }; |
| 11254 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11255 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11256 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11257 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11258 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11259 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11260 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11261 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11262 | |
| 11263 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11264 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11265 | } |
| 11266 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11267 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgentOverTunnel) { |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11268 | // Test user agent values, used both for the request header of the original |
| 11269 | // request, and the value returned by the HttpUserAgentSettings. nullptr means |
| 11270 | // no request header / no HttpUserAgentSettings object. |
| 11271 | const char* kTestUserAgents[] = {nullptr, "", "Foopy"}; |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11272 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11273 | for (const char* setting_user_agent : kTestUserAgents) { |
| 11274 | if (!setting_user_agent) { |
| 11275 | session_deps_.http_user_agent_settings.reset(); |
| 11276 | } else { |
| 11277 | session_deps_.http_user_agent_settings = |
| 11278 | std::make_unique<StaticHttpUserAgentSettings>( |
| 11279 | std::string() /* accept-language */, setting_user_agent); |
| 11280 | } |
| 11281 | session_deps_.proxy_resolution_service = |
| 11282 | ProxyResolutionService::CreateFixed("myproxy:70", |
| 11283 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 11284 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 11285 | for (const char* request_user_agent : kTestUserAgents) { |
| 11286 | HttpRequestInfo request; |
| 11287 | request.method = "GET"; |
| 11288 | request.url = GURL("https://www.example.org/"); |
| 11289 | if (request_user_agent) { |
| 11290 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 11291 | request_user_agent); |
| 11292 | } |
| 11293 | request.traffic_annotation = |
| 11294 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11295 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11296 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11297 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11298 | std::string expected_request; |
| 11299 | if (!setting_user_agent || strlen(setting_user_agent) == 0) { |
| 11300 | expected_request = |
| 11301 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 11302 | "Host: www.example.org:443\r\n" |
| 11303 | "Proxy-Connection: keep-alive\r\n\r\n"; |
| 11304 | } else { |
| 11305 | expected_request = base::StringPrintf( |
| 11306 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 11307 | "Host: www.example.org:443\r\n" |
| 11308 | "Proxy-Connection: keep-alive\r\n" |
| 11309 | "User-Agent: %s\r\n\r\n", |
| 11310 | setting_user_agent); |
| 11311 | } |
| 11312 | MockWrite data_writes[] = { |
| 11313 | MockWrite(expected_request.c_str()), |
| 11314 | }; |
| 11315 | MockRead data_reads[] = { |
| 11316 | // Return an error, so the transaction stops here (this test isn't |
| 11317 | // interested in the rest). |
| 11318 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 11319 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 11320 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 11321 | }; |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11322 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11323 | StaticSocketDataProvider data(data_reads, data_writes); |
| 11324 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11325 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11326 | TestCompletionCallback callback; |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11327 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11328 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 11329 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 11330 | |
| 11331 | rv = callback.WaitForResult(); |
| 11332 | EXPECT_THAT(rv, IsOk()); |
| 11333 | } |
| 11334 | } |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11335 | } |
| 11336 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11337 | TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11338 | HttpRequestInfo request; |
| 11339 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11340 | request.url = GURL("http://www.example.org/"); |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 11341 | request.extra_headers.SetHeader(HttpRequestHeaders::kReferer, |
| 11342 | "http://the.previous.site.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11343 | request.traffic_annotation = |
| 11344 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11345 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11346 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11347 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11348 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11349 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11350 | MockWrite( |
| 11351 | "GET / HTTP/1.1\r\n" |
| 11352 | "Host: www.example.org\r\n" |
| 11353 | "Connection: keep-alive\r\n" |
| 11354 | "Referer: http://the.previous.site.com/\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11355 | }; |
| 11356 | |
| 11357 | // Lastly, the server responds with the actual content. |
| 11358 | MockRead data_reads[] = { |
| 11359 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11360 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11361 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11362 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11363 | }; |
| 11364 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11365 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11366 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11367 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11368 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11369 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11370 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11371 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11372 | |
| 11373 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11374 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11375 | } |
| 11376 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11377 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11378 | HttpRequestInfo request; |
| 11379 | request.method = "POST"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11380 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11381 | request.traffic_annotation = |
| 11382 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11383 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11384 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11385 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11386 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11387 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11388 | MockWrite( |
| 11389 | "POST / HTTP/1.1\r\n" |
| 11390 | "Host: www.example.org\r\n" |
| 11391 | "Connection: keep-alive\r\n" |
| 11392 | "Content-Length: 0\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11393 | }; |
| 11394 | |
| 11395 | // Lastly, the server responds with the actual content. |
| 11396 | MockRead data_reads[] = { |
| 11397 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11398 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11399 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11400 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11401 | }; |
| 11402 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11403 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11404 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11405 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11406 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11407 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11408 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11409 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11410 | |
| 11411 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11412 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11413 | } |
| 11414 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11415 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11416 | HttpRequestInfo request; |
| 11417 | request.method = "PUT"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11418 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11419 | request.traffic_annotation = |
| 11420 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11421 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11422 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11423 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11424 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11425 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11426 | MockWrite( |
| 11427 | "PUT / HTTP/1.1\r\n" |
| 11428 | "Host: www.example.org\r\n" |
| 11429 | "Connection: keep-alive\r\n" |
| 11430 | "Content-Length: 0\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11431 | }; |
| 11432 | |
| 11433 | // Lastly, the server responds with the actual content. |
| 11434 | MockRead data_reads[] = { |
| 11435 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11436 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11437 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11438 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11439 | }; |
| 11440 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11441 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11442 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11443 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11444 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11445 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11446 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11447 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11448 | |
| 11449 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11450 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11451 | } |
| 11452 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11453 | TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11454 | HttpRequestInfo request; |
| 11455 | request.method = "HEAD"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11456 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11457 | request.traffic_annotation = |
| 11458 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11459 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11460 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11461 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11462 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11463 | MockWrite data_writes[] = { |
csharrison | f473dd19 | 2015-08-18 13:54:13 | [diff] [blame] | 11464 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 11465 | "Host: www.example.org\r\n" |
| 11466 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11467 | }; |
| 11468 | |
| 11469 | // Lastly, the server responds with the actual content. |
| 11470 | MockRead data_reads[] = { |
| 11471 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11472 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11473 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11474 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11475 | }; |
| 11476 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11477 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11478 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11479 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11480 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11481 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11482 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11483 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11484 | |
| 11485 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11486 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11487 | } |
| 11488 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11489 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11490 | HttpRequestInfo request; |
| 11491 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11492 | request.url = GURL("http://www.example.org/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11493 | request.load_flags = LOAD_BYPASS_CACHE; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11494 | request.traffic_annotation = |
| 11495 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11496 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11497 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11498 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11499 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11500 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11501 | MockWrite( |
| 11502 | "GET / HTTP/1.1\r\n" |
| 11503 | "Host: www.example.org\r\n" |
| 11504 | "Connection: keep-alive\r\n" |
| 11505 | "Pragma: no-cache\r\n" |
| 11506 | "Cache-Control: no-cache\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11507 | }; |
| 11508 | |
| 11509 | // Lastly, the server responds with the actual content. |
| 11510 | MockRead data_reads[] = { |
| 11511 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11512 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11513 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11514 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11515 | }; |
| 11516 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11517 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11518 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11519 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11520 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11521 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11522 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11523 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11524 | |
| 11525 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11526 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11527 | } |
| 11528 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11529 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlValidateCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11530 | HttpRequestInfo request; |
| 11531 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11532 | request.url = GURL("http://www.example.org/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11533 | request.load_flags = LOAD_VALIDATE_CACHE; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11534 | request.traffic_annotation = |
| 11535 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11536 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11537 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11538 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11539 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11540 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11541 | MockWrite( |
| 11542 | "GET / HTTP/1.1\r\n" |
| 11543 | "Host: www.example.org\r\n" |
| 11544 | "Connection: keep-alive\r\n" |
| 11545 | "Cache-Control: max-age=0\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11546 | }; |
| 11547 | |
| 11548 | // Lastly, the server responds with the actual content. |
| 11549 | MockRead data_reads[] = { |
| 11550 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11551 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11552 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11553 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11554 | }; |
| 11555 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11556 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11557 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11558 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11559 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11560 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11561 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11562 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11563 | |
| 11564 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11565 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11566 | } |
| 11567 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11568 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11569 | HttpRequestInfo request; |
| 11570 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11571 | request.url = GURL("http://www.example.org/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 11572 | request.extra_headers.SetHeader("FooHeader", "Bar"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11573 | request.traffic_annotation = |
| 11574 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11575 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11576 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11577 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11578 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11579 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11580 | MockWrite( |
| 11581 | "GET / HTTP/1.1\r\n" |
| 11582 | "Host: www.example.org\r\n" |
| 11583 | "Connection: keep-alive\r\n" |
| 11584 | "FooHeader: Bar\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11585 | }; |
| 11586 | |
| 11587 | // Lastly, the server responds with the actual content. |
| 11588 | MockRead data_reads[] = { |
| 11589 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11590 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11591 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11592 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11593 | }; |
| 11594 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11595 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11596 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11597 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11598 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11599 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11600 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11601 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11602 | |
| 11603 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11604 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11605 | } |
| 11606 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11607 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeadersStripped) { |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11608 | HttpRequestInfo request; |
| 11609 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11610 | request.url = GURL("http://www.example.org/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 11611 | request.extra_headers.SetHeader("referer", "www.foo.com"); |
| 11612 | request.extra_headers.SetHeader("hEllo", "Kitty"); |
| 11613 | request.extra_headers.SetHeader("FoO", "bar"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11614 | request.traffic_annotation = |
| 11615 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11616 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11617 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11618 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11619 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11620 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11621 | MockWrite( |
| 11622 | "GET / HTTP/1.1\r\n" |
| 11623 | "Host: www.example.org\r\n" |
| 11624 | "Connection: keep-alive\r\n" |
| 11625 | "referer: www.foo.com\r\n" |
| 11626 | "hEllo: Kitty\r\n" |
| 11627 | "FoO: bar\r\n\r\n"), |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11628 | }; |
| 11629 | |
| 11630 | // Lastly, the server responds with the actual content. |
| 11631 | MockRead data_reads[] = { |
| 11632 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11633 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11634 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11635 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11636 | }; |
| 11637 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11638 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11639 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11640 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11641 | TestCompletionCallback callback; |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11642 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11643 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11644 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11645 | |
| 11646 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11647 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11648 | } |
| 11649 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11650 | TEST_F(HttpNetworkTransactionTest, SOCKS4_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11651 | HttpRequestInfo request; |
| 11652 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11653 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11654 | request.traffic_annotation = |
| 11655 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11656 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11657 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11658 | ProxyResolutionService::CreateFixedFromPacResult( |
| 11659 | "SOCKS myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 11660 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11661 | session_deps_.net_log = &net_log; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11662 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11663 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11664 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11665 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11666 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 11667 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 11668 | |
| 11669 | MockWrite data_writes[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11670 | MockWrite(ASYNC, write_buffer, base::size(write_buffer)), |
| 11671 | MockWrite("GET / HTTP/1.1\r\n" |
| 11672 | "Host: www.example.org\r\n" |
| 11673 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11674 | |
| 11675 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11676 | MockRead(ASYNC, read_buffer, base::size(read_buffer)), |
| 11677 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11678 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 11679 | MockRead("Payload"), MockRead(SYNCHRONOUS, OK)}; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11680 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11681 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11682 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11683 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11684 | TestCompletionCallback callback; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11685 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11686 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11687 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11688 | |
| 11689 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11690 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11691 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11692 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11693 | ASSERT_TRUE(response); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11694 | |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 11695 | EXPECT_EQ(ProxyServer::SCHEME_SOCKS4, response->proxy_server.scheme()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11696 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11697 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11698 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 11699 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 11700 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11701 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11702 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11703 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11704 | EXPECT_EQ("Payload", response_text); |
| 11705 | } |
| 11706 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11707 | TEST_F(HttpNetworkTransactionTest, SOCKS4_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11708 | HttpRequestInfo request; |
| 11709 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11710 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11711 | request.traffic_annotation = |
| 11712 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11713 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11714 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11715 | ProxyResolutionService::CreateFixedFromPacResult( |
| 11716 | "SOCKS myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 11717 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11718 | session_deps_.net_log = &net_log; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11719 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11720 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11721 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11722 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11723 | unsigned char write_buffer[] = { 0x04, 0x01, 0x01, 0xBB, 127, 0, 0, 1, 0 }; |
| 11724 | unsigned char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 11725 | |
| 11726 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11727 | MockWrite(ASYNC, reinterpret_cast<char*>(write_buffer), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11728 | base::size(write_buffer)), |
| 11729 | MockWrite("GET / HTTP/1.1\r\n" |
| 11730 | "Host: www.example.org\r\n" |
| 11731 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11732 | |
| 11733 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11734 | MockRead(ASYNC, reinterpret_cast<char*>(read_buffer), |
| 11735 | base::size(read_buffer)), |
| 11736 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11737 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 11738 | MockRead("Payload"), MockRead(SYNCHRONOUS, OK)}; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11739 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11740 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11741 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11742 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11743 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11744 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11745 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11746 | TestCompletionCallback callback; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11747 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11748 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11749 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11750 | |
| 11751 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11752 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11753 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11754 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11755 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11756 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 11757 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 11758 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11759 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11760 | ASSERT_TRUE(response); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 11761 | EXPECT_EQ(ProxyServer::SCHEME_SOCKS4, response->proxy_server.scheme()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11762 | |
| 11763 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11764 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11765 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11766 | EXPECT_EQ("Payload", response_text); |
| 11767 | } |
| 11768 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11769 | TEST_F(HttpNetworkTransactionTest, SOCKS4_HTTP_GET_no_PAC) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11770 | HttpRequestInfo request; |
| 11771 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11772 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11773 | request.traffic_annotation = |
| 11774 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11775 | |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11776 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 11777 | "socks4://myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 11778 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11779 | session_deps_.net_log = &net_log; |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11780 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11781 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11782 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11783 | |
| 11784 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 11785 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 11786 | |
| 11787 | MockWrite data_writes[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11788 | MockWrite(ASYNC, write_buffer, base::size(write_buffer)), |
| 11789 | MockWrite("GET / HTTP/1.1\r\n" |
| 11790 | "Host: www.example.org\r\n" |
| 11791 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11792 | |
| 11793 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11794 | MockRead(ASYNC, read_buffer, base::size(read_buffer)), |
| 11795 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11796 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 11797 | MockRead("Payload"), MockRead(SYNCHRONOUS, OK)}; |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11798 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11799 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11800 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11801 | |
| 11802 | TestCompletionCallback callback; |
| 11803 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11804 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11805 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11806 | |
| 11807 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11808 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11809 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11810 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11811 | ASSERT_TRUE(response); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11812 | |
| 11813 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11814 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11815 | TestLoadTimingNotReused(load_timing_info, |
| 11816 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 11817 | |
| 11818 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11819 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11820 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11821 | EXPECT_EQ("Payload", response_text); |
| 11822 | } |
| 11823 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11824 | TEST_F(HttpNetworkTransactionTest, SOCKS5_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11825 | HttpRequestInfo request; |
| 11826 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11827 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11828 | request.traffic_annotation = |
| 11829 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11830 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11831 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11832 | ProxyResolutionService::CreateFixedFromPacResult( |
| 11833 | "SOCKS5 myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 11834 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11835 | session_deps_.net_log = &net_log; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11836 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11837 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11838 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11839 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11840 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 11841 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 11842 | const char kSOCKS5OkRequest[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11843 | 0x05, // Version |
| 11844 | 0x01, // Command (CONNECT) |
| 11845 | 0x00, // Reserved. |
| 11846 | 0x03, // Address type (DOMAINNAME). |
| 11847 | 0x0F, // Length of domain (15) |
| 11848 | 'w', 'w', 'w', '.', 'e', 'x', 'a', 'm', 'p', 'l', 'e', // Domain string |
| 11849 | '.', 'o', 'r', 'g', 0x00, 0x50, // 16-bit port (80) |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 11850 | }; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11851 | const char kSOCKS5OkResponse[] = |
| 11852 | { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 11853 | |
| 11854 | MockWrite data_writes[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11855 | MockWrite(ASYNC, kSOCKS5GreetRequest, base::size(kSOCKS5GreetRequest)), |
| 11856 | MockWrite(ASYNC, kSOCKS5OkRequest, base::size(kSOCKS5OkRequest)), |
| 11857 | MockWrite("GET / HTTP/1.1\r\n" |
| 11858 | "Host: www.example.org\r\n" |
| 11859 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11860 | |
| 11861 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11862 | MockRead(ASYNC, kSOCKS5GreetResponse, base::size(kSOCKS5GreetResponse)), |
| 11863 | MockRead(ASYNC, kSOCKS5OkResponse, base::size(kSOCKS5OkResponse)), |
| 11864 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11865 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 11866 | MockRead("Payload"), |
| 11867 | MockRead(SYNCHRONOUS, OK)}; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11868 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11869 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11870 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11871 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11872 | TestCompletionCallback callback; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11873 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11874 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11875 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11876 | |
| 11877 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11878 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11879 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11880 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11881 | ASSERT_TRUE(response); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 11882 | EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, response->proxy_server.scheme()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11883 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11884 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11885 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11886 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 11887 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 11888 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11889 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11890 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11891 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11892 | EXPECT_EQ("Payload", response_text); |
| 11893 | } |
| 11894 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11895 | TEST_F(HttpNetworkTransactionTest, SOCKS5_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11896 | HttpRequestInfo request; |
| 11897 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11898 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 11899 | request.traffic_annotation = |
| 11900 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11901 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11902 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11903 | ProxyResolutionService::CreateFixedFromPacResult( |
| 11904 | "SOCKS5 myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 11905 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11906 | session_deps_.net_log = &net_log; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11907 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11908 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11909 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11910 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11911 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 11912 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 11913 | const unsigned char kSOCKS5OkRequest[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11914 | 0x05, // Version |
| 11915 | 0x01, // Command (CONNECT) |
| 11916 | 0x00, // Reserved. |
| 11917 | 0x03, // Address type (DOMAINNAME). |
| 11918 | 0x0F, // Length of domain (15) |
| 11919 | 'w', 'w', 'w', '.', 'e', 'x', 'a', 'm', 'p', 'l', 'e', // Domain string |
| 11920 | '.', 'o', 'r', 'g', 0x01, 0xBB, // 16-bit port (443) |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 11921 | }; |
| 11922 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11923 | const char kSOCKS5OkResponse[] = |
| 11924 | { 0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0x00, 0x00 }; |
| 11925 | |
| 11926 | MockWrite data_writes[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11927 | MockWrite(ASYNC, kSOCKS5GreetRequest, base::size(kSOCKS5GreetRequest)), |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11928 | MockWrite(ASYNC, reinterpret_cast<const char*>(kSOCKS5OkRequest), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11929 | base::size(kSOCKS5OkRequest)), |
| 11930 | MockWrite("GET / HTTP/1.1\r\n" |
| 11931 | "Host: www.example.org\r\n" |
| 11932 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11933 | |
| 11934 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11935 | MockRead(ASYNC, kSOCKS5GreetResponse, base::size(kSOCKS5GreetResponse)), |
| 11936 | MockRead(ASYNC, kSOCKS5OkResponse, base::size(kSOCKS5OkResponse)), |
| 11937 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11938 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 11939 | MockRead("Payload"), |
| 11940 | MockRead(SYNCHRONOUS, OK)}; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11941 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11942 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11943 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11944 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11945 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11946 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11947 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11948 | TestCompletionCallback callback; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11949 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11950 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11951 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11952 | |
| 11953 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11954 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11955 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11956 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11957 | ASSERT_TRUE(response); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 11958 | EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, response->proxy_server.scheme()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11959 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11960 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11961 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11962 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 11963 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 11964 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11965 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11966 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11967 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11968 | EXPECT_EQ("Payload", response_text); |
| 11969 | } |
| 11970 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 11971 | namespace { |
| 11972 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 11973 | // Tests that for connection endpoints the group ids are correctly set. |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 11974 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 11975 | struct GroupIdTest { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 11976 | std::string proxy_server; |
| 11977 | std::string url; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 11978 | ClientSocketPool::GroupId expected_group_id; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 11979 | bool ssl; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 11980 | }; |
| 11981 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 11982 | std::unique_ptr<HttpNetworkSession> SetupSessionForGroupIdTests( |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11983 | SpdySessionDependencies* session_deps_) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11984 | std::unique_ptr<HttpNetworkSession> session(CreateSession(session_deps_)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 11985 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 11986 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 11987 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 11988 | AlternativeService alternative_service(kProtoHTTP2, "", 444); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 11989 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 11990 | http_server_properties->SetHttp2AlternativeService( |
bnc | aa60ff40 | 2016-06-22 19:12:42 | [diff] [blame] | 11991 | url::SchemeHostPort("https", "host.with.alternate", 443), |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 11992 | NetworkIsolationKey(), alternative_service, expiration); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 11993 | |
| 11994 | return session; |
| 11995 | } |
| 11996 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 11997 | int GroupIdTransactionHelper(const std::string& url, |
| 11998 | HttpNetworkSession* session) { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 11999 | HttpRequestInfo request; |
| 12000 | request.method = "GET"; |
| 12001 | request.url = GURL(url); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12002 | request.traffic_annotation = |
| 12003 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12004 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12005 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12006 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12007 | TestCompletionCallback callback; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12008 | |
| 12009 | // We do not complete this request, the dtor will clean the transaction up. |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12010 | return trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12011 | } |
| 12012 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 12013 | } // namespace |
| 12014 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12015 | TEST_F(HttpNetworkTransactionTest, GroupIdForDirectConnections) { |
| 12016 | const GroupIdTest tests[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12017 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12018 | "", // unused |
| 12019 | "http://www.example.org/direct", |
| 12020 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 80), |
| 12021 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12022 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12023 | NetworkIsolationKey(), |
| 12024 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12025 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12026 | }, |
| 12027 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12028 | "", // unused |
| 12029 | "http://[2001:1418:13:1::25]/direct", |
| 12030 | ClientSocketPool::GroupId(HostPortPair("2001:1418:13:1::25", 80), |
| 12031 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12032 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12033 | NetworkIsolationKey(), |
| 12034 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12035 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12036 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12037 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12038 | // SSL Tests |
| 12039 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12040 | "", // unused |
| 12041 | "https://www.example.org/direct_ssl", |
| 12042 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 443), |
| 12043 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12044 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12045 | NetworkIsolationKey(), |
| 12046 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12047 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12048 | }, |
| 12049 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12050 | "", // unused |
| 12051 | "https://[2001:1418:13:1::25]/direct", |
| 12052 | ClientSocketPool::GroupId(HostPortPair("2001:1418:13:1::25", 443), |
| 12053 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12054 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12055 | NetworkIsolationKey(), |
| 12056 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12057 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12058 | }, |
| 12059 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12060 | "", // unused |
| 12061 | "https://host.with.alternate/direct", |
| 12062 | ClientSocketPool::GroupId(HostPortPair("host.with.alternate", 443), |
| 12063 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12064 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12065 | NetworkIsolationKey(), |
| 12066 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12067 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12068 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12069 | }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 12070 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 12071 | for (size_t i = 0; i < base::size(tests); ++i) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 12072 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 12073 | ProxyResolutionService::CreateFixed(tests[i].proxy_server, |
| 12074 | TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12075 | std::unique_ptr<HttpNetworkSession> session( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12076 | SetupSessionForGroupIdTests(&session_deps_)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12077 | |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 12078 | HttpNetworkSessionPeer peer(session.get()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12079 | CaptureGroupIdTransportSocketPool* transport_conn_pool = |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 12080 | new CaptureGroupIdTransportSocketPool(&dummy_connect_job_params_); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12081 | auto mock_pool_manager = std::make_unique<MockClientSocketPoolManager>(); |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 12082 | mock_pool_manager->SetSocketPool(ProxyServer::Direct(), |
| 12083 | base::WrapUnique(transport_conn_pool)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 12084 | peer.SetClientSocketPoolManager(std::move(mock_pool_manager)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12085 | |
| 12086 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12087 | GroupIdTransactionHelper(tests[i].url, session.get())); |
| 12088 | EXPECT_EQ(tests[i].expected_group_id, |
| 12089 | transport_conn_pool->last_group_id_received()); |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 12090 | EXPECT_TRUE(transport_conn_pool->socket_requested()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12091 | } |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12092 | } |
| 12093 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12094 | TEST_F(HttpNetworkTransactionTest, GroupIdForHTTPProxyConnections) { |
| 12095 | const GroupIdTest tests[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12096 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12097 | "http_proxy", |
| 12098 | "http://www.example.org/http_proxy_normal", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12099 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 80), |
| 12100 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12101 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12102 | NetworkIsolationKey(), |
| 12103 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12104 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12105 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12106 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12107 | // SSL Tests |
| 12108 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12109 | "http_proxy", |
| 12110 | "https://www.example.org/http_connect_ssl", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12111 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 443), |
| 12112 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12113 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12114 | NetworkIsolationKey(), |
| 12115 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12116 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12117 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 12118 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12119 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12120 | "http_proxy", |
| 12121 | "https://host.with.alternate/direct", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12122 | ClientSocketPool::GroupId(HostPortPair("host.with.alternate", 443), |
| 12123 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12124 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12125 | NetworkIsolationKey(), |
| 12126 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12127 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12128 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12129 | }; |
| 12130 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 12131 | for (size_t i = 0; i < base::size(tests); ++i) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 12132 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 12133 | ProxyResolutionService::CreateFixed(tests[i].proxy_server, |
| 12134 | TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12135 | std::unique_ptr<HttpNetworkSession> session( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12136 | SetupSessionForGroupIdTests(&session_deps_)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12137 | |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 12138 | HttpNetworkSessionPeer peer(session.get()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12139 | |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 12140 | ProxyServer proxy_server(ProxyServer::SCHEME_HTTP, |
| 12141 | HostPortPair("http_proxy", 80)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12142 | CaptureGroupIdTransportSocketPool* http_proxy_pool = |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 12143 | new CaptureGroupIdTransportSocketPool(&dummy_connect_job_params_); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12144 | auto mock_pool_manager = std::make_unique<MockClientSocketPoolManager>(); |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 12145 | mock_pool_manager->SetSocketPool(proxy_server, |
| 12146 | base::WrapUnique(http_proxy_pool)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 12147 | peer.SetClientSocketPoolManager(std::move(mock_pool_manager)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12148 | |
| 12149 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12150 | GroupIdTransactionHelper(tests[i].url, session.get())); |
| 12151 | EXPECT_EQ(tests[i].expected_group_id, |
| 12152 | http_proxy_pool->last_group_id_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12153 | } |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12154 | } |
| 12155 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12156 | TEST_F(HttpNetworkTransactionTest, GroupIdForSOCKSConnections) { |
| 12157 | const GroupIdTest tests[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12158 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12159 | "socks4://socks_proxy:1080", |
| 12160 | "http://www.example.org/socks4_direct", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12161 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 80), |
| 12162 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12163 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12164 | NetworkIsolationKey(), |
| 12165 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12166 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12167 | }, |
| 12168 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12169 | "socks5://socks_proxy:1080", |
| 12170 | "http://www.example.org/socks5_direct", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12171 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 80), |
| 12172 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12173 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12174 | NetworkIsolationKey(), |
| 12175 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12176 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12177 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12178 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12179 | // SSL Tests |
| 12180 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12181 | "socks4://socks_proxy:1080", |
| 12182 | "https://www.example.org/socks4_ssl", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12183 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 443), |
| 12184 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12185 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12186 | NetworkIsolationKey(), |
| 12187 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12188 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12189 | }, |
| 12190 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12191 | "socks5://socks_proxy:1080", |
| 12192 | "https://www.example.org/socks5_ssl", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12193 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 443), |
| 12194 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12195 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12196 | NetworkIsolationKey(), |
| 12197 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12198 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12199 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 12200 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12201 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12202 | "socks4://socks_proxy:1080", |
| 12203 | "https://host.with.alternate/direct", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12204 | ClientSocketPool::GroupId(HostPortPair("host.with.alternate", 443), |
| 12205 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12206 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12207 | NetworkIsolationKey(), |
| 12208 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12209 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12210 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12211 | }; |
| 12212 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 12213 | for (size_t i = 0; i < base::size(tests); ++i) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 12214 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 12215 | ProxyResolutionService::CreateFixed(tests[i].proxy_server, |
| 12216 | TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12217 | std::unique_ptr<HttpNetworkSession> session( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12218 | SetupSessionForGroupIdTests(&session_deps_)); |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 12219 | |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 12220 | HttpNetworkSessionPeer peer(session.get()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12221 | |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 12222 | ProxyServer proxy_server( |
| 12223 | ProxyServer::FromURI(tests[i].proxy_server, ProxyServer::SCHEME_HTTP)); |
| 12224 | ASSERT_TRUE(proxy_server.is_valid()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12225 | CaptureGroupIdTransportSocketPool* socks_conn_pool = |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 12226 | new CaptureGroupIdTransportSocketPool(&dummy_connect_job_params_); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12227 | auto mock_pool_manager = std::make_unique<MockClientSocketPoolManager>(); |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 12228 | mock_pool_manager->SetSocketPool(proxy_server, |
| 12229 | base::WrapUnique(socks_conn_pool)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 12230 | peer.SetClientSocketPoolManager(std::move(mock_pool_manager)); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12231 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12232 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12233 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12234 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12235 | GroupIdTransactionHelper(tests[i].url, session.get())); |
| 12236 | EXPECT_EQ(tests[i].expected_group_id, |
| 12237 | socks_conn_pool->last_group_id_received()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12238 | } |
| 12239 | } |
| 12240 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12241 | TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12242 | HttpRequestInfo request; |
| 12243 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12244 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12245 | request.traffic_annotation = |
| 12246 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12247 | |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 12248 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 12249 | "myproxy:70;foobar:80", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 12250 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 12251 | // This simulates failure resolving all hostnames; that means we will fail |
| 12252 | // connecting to both proxies (myproxy:70 and foobar:80). |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12253 | session_deps_.host_resolver->rules()->AddSimulatedFailure("*"); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 12254 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12255 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12256 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12257 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12258 | TestCompletionCallback callback; |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12259 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12260 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12261 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12262 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12263 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12264 | EXPECT_THAT(rv, IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12265 | } |
| 12266 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12267 | // Make sure we can handle an error when writing the request. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12268 | TEST_F(HttpNetworkTransactionTest, RequestWriteError) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12269 | HttpRequestInfo request; |
| 12270 | request.method = "GET"; |
| 12271 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12272 | request.traffic_annotation = |
| 12273 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12274 | |
| 12275 | MockWrite write_failure[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12276 | MockWrite(ASYNC, ERR_CONNECTION_RESET), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12277 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12278 | StaticSocketDataProvider data(base::span<MockRead>(), write_failure); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12279 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12280 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12281 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12282 | TestCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12283 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12284 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12285 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12286 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12287 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12288 | |
| 12289 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12290 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 12291 | |
| 12292 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12293 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 12294 | EXPECT_LT(0u, endpoint.address().size()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12295 | } |
| 12296 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12297 | // Check that a connection closed after the start of the headers finishes ok. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12298 | TEST_F(HttpNetworkTransactionTest, ConnectionClosedAfterStartOfHeaders) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12299 | HttpRequestInfo request; |
| 12300 | request.method = "GET"; |
| 12301 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12302 | request.traffic_annotation = |
| 12303 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12304 | |
| 12305 | MockRead data_reads[] = { |
| 12306 | MockRead("HTTP/1."), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12307 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12308 | }; |
| 12309 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12310 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12311 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12312 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12313 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12314 | TestCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12315 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12316 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12317 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12318 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12319 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12320 | |
| 12321 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12322 | EXPECT_THAT(rv, IsOk()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12323 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12324 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12325 | ASSERT_TRUE(response); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12326 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12327 | EXPECT_TRUE(response->headers); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12328 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 12329 | |
| 12330 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12331 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12332 | EXPECT_THAT(rv, IsOk()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12333 | EXPECT_EQ("", response_data); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 12334 | |
| 12335 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12336 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 12337 | EXPECT_LT(0u, endpoint.address().size()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12338 | } |
| 12339 | |
| 12340 | // Make sure that a dropped connection while draining the body for auth |
| 12341 | // restart does the right thing. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12342 | TEST_F(HttpNetworkTransactionTest, DrainResetOK) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12343 | HttpRequestInfo request; |
| 12344 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12345 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12346 | request.traffic_annotation = |
| 12347 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12348 | |
| 12349 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12350 | MockWrite( |
| 12351 | "GET / HTTP/1.1\r\n" |
| 12352 | "Host: www.example.org\r\n" |
| 12353 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12354 | }; |
| 12355 | |
| 12356 | MockRead data_reads1[] = { |
| 12357 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 12358 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 12359 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 12360 | MockRead("Content-Length: 14\r\n\r\n"), |
| 12361 | MockRead("Unauth"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12362 | MockRead(ASYNC, ERR_CONNECTION_RESET), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12363 | }; |
| 12364 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12365 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12366 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12367 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12368 | // After calling trans.RestartWithAuth(), this is the request we should |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12369 | // be issuing -- the final header line contains the credentials. |
| 12370 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12371 | MockWrite( |
| 12372 | "GET / HTTP/1.1\r\n" |
| 12373 | "Host: www.example.org\r\n" |
| 12374 | "Connection: keep-alive\r\n" |
| 12375 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12376 | }; |
| 12377 | |
| 12378 | // Lastly, the server responds with the actual content. |
| 12379 | MockRead data_reads2[] = { |
| 12380 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 12381 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 12382 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12383 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12384 | }; |
| 12385 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12386 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12387 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12388 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12389 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12390 | TestCompletionCallback callback1; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12391 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12392 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 12393 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12394 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12395 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12396 | |
| 12397 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12398 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12399 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12400 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12401 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12402 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12403 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12404 | TestCompletionCallback callback2; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12405 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12406 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12407 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12408 | |
| 12409 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12410 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12411 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12412 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12413 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12414 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12415 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 12416 | } |
| 12417 | |
| 12418 | // Test HTTPS connections going through a proxy that sends extra data. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12419 | TEST_F(HttpNetworkTransactionTest, HTTPSViaProxyWithExtraData) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 12420 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 12421 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12422 | |
| 12423 | HttpRequestInfo request; |
| 12424 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12425 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12426 | request.traffic_annotation = |
| 12427 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12428 | |
| 12429 | MockRead proxy_reads[] = { |
| 12430 | MockRead("HTTP/1.0 200 Connected\r\n\r\nExtra data"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12431 | MockRead(SYNCHRONOUS, OK) |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12432 | }; |
| 12433 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12434 | StaticSocketDataProvider data(proxy_reads, base::span<MockWrite>()); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12435 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12436 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12437 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 12438 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12439 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12440 | TestCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12441 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12442 | session_deps_.socket_factory->ResetNextMockIndexes(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12443 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12444 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12445 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12446 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12447 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12448 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12449 | |
| 12450 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12451 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12452 | } |
| 12453 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12454 | TEST_F(HttpNetworkTransactionTest, LargeContentLengthThenClose) { |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12455 | HttpRequestInfo request; |
| 12456 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12457 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12458 | request.traffic_annotation = |
| 12459 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12460 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12461 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12462 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12463 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 12464 | MockRead data_reads[] = { |
| 12465 | 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] | 12466 | MockRead(SYNCHRONOUS, OK), |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 12467 | }; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12468 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12469 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12470 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12471 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12472 | TestCompletionCallback callback; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12473 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12474 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12475 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12476 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12477 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12478 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12479 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12480 | ASSERT_TRUE(response); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12481 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12482 | EXPECT_TRUE(response->headers); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12483 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 12484 | |
| 12485 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12486 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12487 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 12488 | } |
| 12489 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12490 | TEST_F(HttpNetworkTransactionTest, UploadFileSmallerThanLength) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 12491 | base::FilePath temp_file_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 12492 | ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path)); |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 12493 | const uint64_t kFakeSize = 100000; // file is actually blank |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 12494 | UploadFileElementReader::ScopedOverridingContentLengthForTests |
| 12495 | overriding_content_length(kFakeSize); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12496 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12497 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12498 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 12499 | base::ThreadTaskRunnerHandle::Get().get(), temp_file_path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 12500 | std::numeric_limits<uint64_t>::max(), base::Time())); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 12501 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12502 | |
| 12503 | HttpRequestInfo request; |
| 12504 | request.method = "POST"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12505 | request.url = GURL("http://www.example.org/upload"); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12506 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12507 | request.traffic_annotation = |
| 12508 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12509 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12510 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12511 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12512 | |
| 12513 | MockRead data_reads[] = { |
| 12514 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 12515 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12516 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12517 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12518 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12519 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12520 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12521 | TestCompletionCallback callback; |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12522 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12523 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12524 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12525 | |
| 12526 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12527 | EXPECT_THAT(rv, IsError(ERR_UPLOAD_FILE_CHANGED)); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12528 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12529 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12530 | ASSERT_TRUE(response); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12531 | |
maksim.sisov | e869bf5 | 2016-06-23 17:11:52 | [diff] [blame] | 12532 | EXPECT_FALSE(response->headers); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12533 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 12534 | base::DeleteFile(temp_file_path, false); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12535 | } |
| 12536 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12537 | TEST_F(HttpNetworkTransactionTest, UploadUnreadableFile) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 12538 | base::FilePath temp_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 12539 | ASSERT_TRUE(base::CreateTemporaryFile(&temp_file)); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12540 | std::string temp_file_content("Unreadable file."); |
lazyboy | 8df84fc | 2017-04-12 02:12:48 | [diff] [blame] | 12541 | ASSERT_EQ(static_cast<int>(temp_file_content.length()), |
| 12542 | base::WriteFile(temp_file, temp_file_content.c_str(), |
| 12543 | temp_file_content.length())); |
[email protected] | 92be8eb | 2014-08-07 22:57:11 | [diff] [blame] | 12544 | ASSERT_TRUE(base::MakeFileUnreadable(temp_file)); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12545 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12546 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12547 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 12548 | base::ThreadTaskRunnerHandle::Get().get(), temp_file, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 12549 | std::numeric_limits<uint64_t>::max(), base::Time())); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 12550 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12551 | |
| 12552 | HttpRequestInfo request; |
| 12553 | request.method = "POST"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12554 | request.url = GURL("http://www.example.org/upload"); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12555 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12556 | request.traffic_annotation = |
| 12557 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12558 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 12559 | // If we try to upload an unreadable file, the transaction should fail. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12560 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12561 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12562 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12563 | StaticSocketDataProvider data; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12564 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12565 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12566 | TestCompletionCallback callback; |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12567 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12568 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12569 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12570 | |
| 12571 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12572 | EXPECT_THAT(rv, IsError(ERR_ACCESS_DENIED)); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12573 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 12574 | base::DeleteFile(temp_file, false); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12575 | } |
| 12576 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12577 | TEST_F(HttpNetworkTransactionTest, CancelDuringInitRequestBody) { |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12578 | class FakeUploadElementReader : public UploadElementReader { |
| 12579 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 12580 | FakeUploadElementReader() = default; |
| 12581 | ~FakeUploadElementReader() override = default; |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12582 | |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12583 | CompletionOnceCallback TakeCallback() { return std::move(callback_); } |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12584 | |
| 12585 | // UploadElementReader overrides: |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12586 | int Init(CompletionOnceCallback callback) override { |
| 12587 | callback_ = std::move(callback); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12588 | return ERR_IO_PENDING; |
| 12589 | } |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 12590 | uint64_t GetContentLength() const override { return 0; } |
| 12591 | uint64_t BytesRemaining() const override { return 0; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 12592 | int Read(IOBuffer* buf, |
| 12593 | int buf_length, |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12594 | CompletionOnceCallback callback) override { |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12595 | return ERR_FAILED; |
| 12596 | } |
| 12597 | |
| 12598 | private: |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12599 | CompletionOnceCallback callback_; |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12600 | }; |
| 12601 | |
| 12602 | FakeUploadElementReader* fake_reader = new FakeUploadElementReader; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12603 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 12604 | element_readers.push_back(base::WrapUnique(fake_reader)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 12605 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12606 | |
| 12607 | HttpRequestInfo request; |
| 12608 | request.method = "POST"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12609 | request.url = GURL("http://www.example.org/upload"); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12610 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12611 | request.traffic_annotation = |
| 12612 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12613 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12614 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 12615 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12616 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12617 | |
| 12618 | StaticSocketDataProvider data; |
| 12619 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 12620 | |
| 12621 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12622 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12623 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 12624 | base::RunLoop().RunUntilIdle(); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12625 | |
| 12626 | // Transaction is pending on request body initialization. |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12627 | CompletionOnceCallback init_callback = fake_reader->TakeCallback(); |
| 12628 | ASSERT_FALSE(init_callback.is_null()); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12629 | |
| 12630 | // Return Init()'s result after the transaction gets destroyed. |
| 12631 | trans.reset(); |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12632 | std::move(init_callback).Run(OK); // Should not crash. |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12633 | } |
| 12634 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12635 | // Tests that changes to Auth realms are treated like auth rejections. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12636 | TEST_F(HttpNetworkTransactionTest, ChangeAuthRealms) { |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12637 | HttpRequestInfo request; |
| 12638 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12639 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12640 | request.traffic_annotation = |
| 12641 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12642 | |
| 12643 | // First transaction will request a resource and receive a Basic challenge |
| 12644 | // with realm="first_realm". |
| 12645 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12646 | MockWrite( |
| 12647 | "GET / HTTP/1.1\r\n" |
| 12648 | "Host: www.example.org\r\n" |
| 12649 | "Connection: keep-alive\r\n" |
| 12650 | "\r\n"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12651 | }; |
| 12652 | MockRead data_reads1[] = { |
| 12653 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 12654 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 12655 | "\r\n"), |
| 12656 | }; |
| 12657 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12658 | // After calling trans.RestartWithAuth(), provide an Authentication header |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12659 | // for first_realm. The server will reject and provide a challenge with |
| 12660 | // second_realm. |
| 12661 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12662 | MockWrite( |
| 12663 | "GET / HTTP/1.1\r\n" |
| 12664 | "Host: www.example.org\r\n" |
| 12665 | "Connection: keep-alive\r\n" |
| 12666 | "Authorization: Basic Zmlyc3Q6YmF6\r\n" |
| 12667 | "\r\n"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12668 | }; |
| 12669 | MockRead data_reads2[] = { |
| 12670 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 12671 | "WWW-Authenticate: Basic realm=\"second_realm\"\r\n" |
| 12672 | "\r\n"), |
| 12673 | }; |
| 12674 | |
| 12675 | // This again fails, and goes back to first_realm. Make sure that the |
| 12676 | // entry is removed from cache. |
| 12677 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12678 | MockWrite( |
| 12679 | "GET / HTTP/1.1\r\n" |
| 12680 | "Host: www.example.org\r\n" |
| 12681 | "Connection: keep-alive\r\n" |
| 12682 | "Authorization: Basic c2Vjb25kOmZvdQ==\r\n" |
| 12683 | "\r\n"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12684 | }; |
| 12685 | MockRead data_reads3[] = { |
| 12686 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 12687 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 12688 | "\r\n"), |
| 12689 | }; |
| 12690 | |
| 12691 | // Try one last time (with the correct password) and get the resource. |
| 12692 | MockWrite data_writes4[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12693 | MockWrite( |
| 12694 | "GET / HTTP/1.1\r\n" |
| 12695 | "Host: www.example.org\r\n" |
| 12696 | "Connection: keep-alive\r\n" |
| 12697 | "Authorization: Basic Zmlyc3Q6YmFy\r\n" |
| 12698 | "\r\n"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12699 | }; |
| 12700 | MockRead data_reads4[] = { |
| 12701 | MockRead("HTTP/1.1 200 OK\r\n" |
| 12702 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 12703 | "Content-Length: 5\r\n" |
| 12704 | "\r\n" |
| 12705 | "hello"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12706 | }; |
| 12707 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12708 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 12709 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 12710 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
| 12711 | StaticSocketDataProvider data4(data_reads4, data_writes4); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12712 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 12713 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 12714 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 12715 | session_deps_.socket_factory->AddSocketDataProvider(&data4); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12716 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12717 | TestCompletionCallback callback1; |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12718 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12719 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12720 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 12721 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12722 | // Issue the first request with Authorize headers. There should be a |
| 12723 | // password prompt for first_realm waiting to be filled in after the |
| 12724 | // transaction completes. |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12725 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12726 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12727 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12728 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12729 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12730 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12731 | base::Optional<AuthChallengeInfo> challenge = response->auth_challenge; |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12732 | ASSERT_TRUE(challenge); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12733 | EXPECT_FALSE(challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 12734 | EXPECT_EQ("http://www.example.org", challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12735 | EXPECT_EQ("first_realm", challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 12736 | EXPECT_EQ(kBasicAuthScheme, challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12737 | |
| 12738 | // Issue the second request with an incorrect password. There should be a |
| 12739 | // password prompt for second_realm waiting to be filled in after the |
| 12740 | // transaction completes. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12741 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12742 | rv = trans.RestartWithAuth(AuthCredentials(kFirst, kBaz), |
| 12743 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12744 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12745 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12746 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12747 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12748 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12749 | challenge = response->auth_challenge; |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12750 | ASSERT_TRUE(challenge); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12751 | EXPECT_FALSE(challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 12752 | EXPECT_EQ("http://www.example.org", challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12753 | EXPECT_EQ("second_realm", challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 12754 | EXPECT_EQ(kBasicAuthScheme, challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12755 | |
| 12756 | // Issue the third request with another incorrect password. There should be |
| 12757 | // a password prompt for first_realm waiting to be filled in. If the password |
| 12758 | // prompt is not present, it indicates that the HttpAuthCacheEntry for |
| 12759 | // first_realm was not correctly removed. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12760 | TestCompletionCallback callback3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12761 | rv = trans.RestartWithAuth(AuthCredentials(kSecond, kFou), |
| 12762 | callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12763 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12764 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12765 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12766 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12767 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12768 | challenge = response->auth_challenge; |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12769 | ASSERT_TRUE(challenge); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12770 | EXPECT_FALSE(challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 12771 | EXPECT_EQ("http://www.example.org", challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12772 | EXPECT_EQ("first_realm", challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 12773 | EXPECT_EQ(kBasicAuthScheme, challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12774 | |
| 12775 | // Issue the fourth request with the correct password and username. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12776 | TestCompletionCallback callback4; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12777 | rv = trans.RestartWithAuth(AuthCredentials(kFirst, kBar), |
| 12778 | callback4.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12779 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12780 | rv = callback4.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12781 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12782 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12783 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12784 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12785 | } |
| 12786 | |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12787 | // Regression test for https://crbug.com/754395. |
| 12788 | TEST_F(HttpNetworkTransactionTest, IgnoreAltSvcWithInvalidCert) { |
| 12789 | MockRead data_reads[] = { |
| 12790 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 12791 | MockRead(kAlternativeServiceHttpHeader), |
| 12792 | MockRead("\r\n"), |
| 12793 | MockRead("hello world"), |
| 12794 | MockRead(SYNCHRONOUS, OK), |
| 12795 | }; |
| 12796 | |
| 12797 | HttpRequestInfo request; |
| 12798 | request.method = "GET"; |
| 12799 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12800 | request.traffic_annotation = |
| 12801 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12802 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12803 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12804 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 12805 | |
| 12806 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 12807 | ssl.ssl_info.cert = |
| 12808 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 12809 | ASSERT_TRUE(ssl.ssl_info.cert); |
| 12810 | ssl.ssl_info.cert_status = CERT_STATUS_COMMON_NAME_INVALID; |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12811 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 12812 | |
| 12813 | TestCompletionCallback callback; |
| 12814 | |
| 12815 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 12816 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 12817 | |
| 12818 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 12819 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 12820 | |
| 12821 | url::SchemeHostPort test_server(request.url); |
| 12822 | HttpServerProperties* http_server_properties = |
| 12823 | session->http_server_properties(); |
| 12824 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 12825 | http_server_properties |
| 12826 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 12827 | .empty()); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12828 | |
| 12829 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 12830 | |
| 12831 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 12832 | ASSERT_TRUE(response); |
| 12833 | ASSERT_TRUE(response->headers); |
| 12834 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 12835 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 12836 | EXPECT_FALSE(response->was_alpn_negotiated); |
| 12837 | |
| 12838 | std::string response_data; |
| 12839 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
| 12840 | EXPECT_EQ("hello world", response_data); |
| 12841 | |
| 12842 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 12843 | http_server_properties |
| 12844 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 12845 | .empty()); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12846 | } |
| 12847 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12848 | TEST_F(HttpNetworkTransactionTest, HonorAlternativeServiceHeader) { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12849 | MockRead data_reads[] = { |
| 12850 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 12851 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12852 | MockRead("\r\n"), |
| 12853 | MockRead("hello world"), |
| 12854 | MockRead(SYNCHRONOUS, OK), |
| 12855 | }; |
| 12856 | |
| 12857 | HttpRequestInfo request; |
| 12858 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 12859 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 12860 | request.traffic_annotation = |
| 12861 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12862 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12863 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12864 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 12865 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 12866 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 12867 | ssl.ssl_info.cert = |
| 12868 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 12869 | ASSERT_TRUE(ssl.ssl_info.cert); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 12870 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 12871 | |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12872 | TestCompletionCallback callback; |
| 12873 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12874 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12875 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12876 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12877 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12878 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12879 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 12880 | url::SchemeHostPort test_server(request.url); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 12881 | HttpServerProperties* http_server_properties = |
| 12882 | session->http_server_properties(); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 12883 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 12884 | http_server_properties |
| 12885 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 12886 | .empty()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12887 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12888 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12889 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12890 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12891 | ASSERT_TRUE(response); |
| 12892 | ASSERT_TRUE(response->headers); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12893 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 12894 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 12895 | EXPECT_FALSE(response->was_alpn_negotiated); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12896 | |
| 12897 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12898 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12899 | EXPECT_EQ("hello world", response_data); |
| 12900 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 12901 | AlternativeServiceInfoVector alternative_service_info_vector = |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 12902 | http_server_properties->GetAlternativeServiceInfos(test_server, |
| 12903 | NetworkIsolationKey()); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 12904 | ASSERT_EQ(1u, alternative_service_info_vector.size()); |
| 12905 | AlternativeService alternative_service(kProtoHTTP2, "mail.example.org", 443); |
| 12906 | EXPECT_EQ(alternative_service, |
zhongyi | 422ce35 | 2017-06-09 23:28:54 | [diff] [blame] | 12907 | alternative_service_info_vector[0].alternative_service()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12908 | } |
| 12909 | |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 12910 | TEST_F(HttpNetworkTransactionTest, |
| 12911 | HonorAlternativeServiceHeaderWithNetworkIsolationKey) { |
| 12912 | base::test::ScopedFeatureList feature_list; |
| 12913 | feature_list.InitWithFeatures( |
| 12914 | // enabled_features |
| 12915 | {features::kPartitionHttpServerPropertiesByNetworkIsolationKey, |
| 12916 | // Need to partition connections by NetworkIsolationKey for |
| 12917 | // SpdySessionKeys to include NetworkIsolationKeys. |
| 12918 | features::kPartitionConnectionsByNetworkIsolationKey}, |
| 12919 | // disabled_features |
| 12920 | {}); |
| 12921 | // Since HttpServerProperties caches the feature value, have to create a new |
| 12922 | // one. |
| 12923 | session_deps_.http_server_properties = |
| 12924 | std::make_unique<HttpServerProperties>(); |
| 12925 | |
| 12926 | const url::Origin kOrigin1 = url::Origin::Create(GURL("https://foo.test/")); |
| 12927 | const net::NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 12928 | const url::Origin kOrigin2 = url::Origin::Create(GURL("https://bar.test/")); |
| 12929 | const net::NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
| 12930 | |
| 12931 | MockRead data_reads[] = { |
| 12932 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 12933 | MockRead(kAlternativeServiceHttpHeader), |
| 12934 | MockRead("\r\n"), |
| 12935 | MockRead("hello world"), |
| 12936 | MockRead(SYNCHRONOUS, OK), |
| 12937 | }; |
| 12938 | |
| 12939 | HttpRequestInfo request; |
| 12940 | request.method = "GET"; |
| 12941 | request.url = GURL("https://www.example.org/"); |
| 12942 | request.traffic_annotation = |
| 12943 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 12944 | request.network_isolation_key = kNetworkIsolationKey1; |
| 12945 | |
| 12946 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
| 12947 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 12948 | |
| 12949 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 12950 | ssl.ssl_info.cert = |
| 12951 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 12952 | ASSERT_TRUE(ssl.ssl_info.cert); |
| 12953 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 12954 | |
| 12955 | TestCompletionCallback callback; |
| 12956 | |
| 12957 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 12958 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 12959 | |
| 12960 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 12961 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 12962 | |
| 12963 | url::SchemeHostPort test_server(request.url); |
| 12964 | HttpServerProperties* http_server_properties = |
| 12965 | session->http_server_properties(); |
| 12966 | EXPECT_TRUE( |
| 12967 | http_server_properties |
| 12968 | ->GetAlternativeServiceInfos(test_server, kNetworkIsolationKey1) |
| 12969 | .empty()); |
| 12970 | |
| 12971 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 12972 | |
| 12973 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 12974 | ASSERT_TRUE(response); |
| 12975 | ASSERT_TRUE(response->headers); |
| 12976 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 12977 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 12978 | EXPECT_FALSE(response->was_alpn_negotiated); |
| 12979 | |
| 12980 | std::string response_data; |
| 12981 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
| 12982 | EXPECT_EQ("hello world", response_data); |
| 12983 | |
| 12984 | AlternativeServiceInfoVector alternative_service_info_vector = |
| 12985 | http_server_properties->GetAlternativeServiceInfos(test_server, |
| 12986 | kNetworkIsolationKey1); |
| 12987 | ASSERT_EQ(1u, alternative_service_info_vector.size()); |
| 12988 | AlternativeService alternative_service(kProtoHTTP2, "mail.example.org", 443); |
| 12989 | EXPECT_EQ(alternative_service, |
| 12990 | alternative_service_info_vector[0].alternative_service()); |
| 12991 | |
| 12992 | // Make sure the alternative service information is only associated with |
| 12993 | // kNetworkIsolationKey1. |
| 12994 | EXPECT_TRUE( |
| 12995 | http_server_properties |
| 12996 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 12997 | .empty()); |
| 12998 | EXPECT_TRUE( |
| 12999 | http_server_properties |
| 13000 | ->GetAlternativeServiceInfos(test_server, kNetworkIsolationKey2) |
| 13001 | .empty()); |
| 13002 | } |
| 13003 | |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13004 | // Regression test for https://crbug.com/615497. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13005 | TEST_F(HttpNetworkTransactionTest, |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13006 | DoNotParseAlternativeServiceHeaderOnInsecureRequest) { |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13007 | MockRead data_reads[] = { |
| 13008 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 13009 | MockRead(kAlternativeServiceHttpHeader), |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13010 | MockRead("\r\n"), |
| 13011 | MockRead("hello world"), |
| 13012 | MockRead(SYNCHRONOUS, OK), |
| 13013 | }; |
| 13014 | |
| 13015 | HttpRequestInfo request; |
| 13016 | request.method = "GET"; |
| 13017 | request.url = GURL("http://www.example.org/"); |
| 13018 | request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13019 | request.traffic_annotation = |
| 13020 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13021 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13022 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13023 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 13024 | |
| 13025 | TestCompletionCallback callback; |
| 13026 | |
| 13027 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13028 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13029 | |
| 13030 | url::SchemeHostPort test_server(request.url); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13031 | HttpServerProperties* http_server_properties = |
| 13032 | session->http_server_properties(); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13033 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13034 | http_server_properties |
| 13035 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13036 | .empty()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13037 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13038 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13039 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13040 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13041 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13042 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13043 | ASSERT_TRUE(response); |
| 13044 | ASSERT_TRUE(response->headers); |
| 13045 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13046 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13047 | EXPECT_FALSE(response->was_alpn_negotiated); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13048 | |
| 13049 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13050 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13051 | EXPECT_EQ("hello world", response_data); |
| 13052 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13053 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13054 | http_server_properties |
| 13055 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13056 | .empty()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13057 | } |
| 13058 | |
bnc | a86731e | 2017-04-17 12:31:28 | [diff] [blame] | 13059 | // HTTP/2 Alternative Services should be disabled by default. |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13060 | // TODO(bnc): Remove when https://crbug.com/615413 is fixed. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13061 | TEST_F(HttpNetworkTransactionTest, |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13062 | DisableHTTP2AlternativeServicesWithDifferentHost) { |
bnc | a86731e | 2017-04-17 12:31:28 | [diff] [blame] | 13063 | session_deps_.enable_http2_alternative_service = false; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13064 | |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13065 | HttpRequestInfo request; |
| 13066 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13067 | request.url = GURL("https://www.example.org/"); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13068 | request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13069 | request.traffic_annotation = |
| 13070 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13071 | |
| 13072 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
| 13073 | StaticSocketDataProvider first_data; |
| 13074 | first_data.set_connect_data(mock_connect); |
| 13075 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13076 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13077 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13078 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13079 | |
| 13080 | MockRead data_reads[] = { |
| 13081 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 13082 | MockRead(ASYNC, OK), |
| 13083 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13084 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13085 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
| 13086 | |
| 13087 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 13088 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13089 | HttpServerProperties* http_server_properties = |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13090 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13091 | AlternativeService alternative_service(kProtoHTTP2, "different.example.org", |
| 13092 | 444); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13093 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13094 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13095 | url::SchemeHostPort(request.url), NetworkIsolationKey(), |
| 13096 | alternative_service, expiration); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13097 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13098 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13099 | TestCompletionCallback callback; |
| 13100 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13101 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13102 | // Alternative service is not used, request fails. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13103 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_CONNECTION_REFUSED)); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13104 | } |
| 13105 | |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13106 | // Regression test for https://crbug.com/615497: |
| 13107 | // Alternative Services should be disabled for http origin. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13108 | TEST_F(HttpNetworkTransactionTest, |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13109 | DisableAlternativeServicesForInsecureOrigin) { |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13110 | HttpRequestInfo request; |
| 13111 | request.method = "GET"; |
| 13112 | request.url = GURL("http://www.example.org/"); |
| 13113 | request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13114 | request.traffic_annotation = |
| 13115 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13116 | |
| 13117 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
| 13118 | StaticSocketDataProvider first_data; |
| 13119 | first_data.set_connect_data(mock_connect); |
| 13120 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
| 13121 | |
| 13122 | MockRead data_reads[] = { |
| 13123 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 13124 | MockRead(ASYNC, OK), |
| 13125 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13126 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13127 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
| 13128 | |
| 13129 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 13130 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13131 | HttpServerProperties* http_server_properties = |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13132 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13133 | AlternativeService alternative_service(kProtoHTTP2, "", 444); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13134 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13135 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13136 | url::SchemeHostPort(request.url), NetworkIsolationKey(), |
| 13137 | alternative_service, expiration); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13138 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13139 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13140 | TestCompletionCallback callback; |
| 13141 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13142 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13143 | // Alternative service is not used, request fails. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13144 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_CONNECTION_REFUSED)); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13145 | } |
| 13146 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13147 | TEST_F(HttpNetworkTransactionTest, ClearAlternativeServices) { |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13148 | // Set an alternative service for origin. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13149 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13150 | HttpServerProperties* http_server_properties = |
| 13151 | session->http_server_properties(); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13152 | url::SchemeHostPort test_server("https", "www.example.org", 443); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13153 | AlternativeService alternative_service(kProtoQUIC, "", 80); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13154 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13155 | http_server_properties->SetQuicAlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13156 | test_server, NetworkIsolationKey(), alternative_service, expiration, |
Nick Harper | 72ade19 | 2019-07-17 03:30:42 | [diff] [blame] | 13157 | session->params().quic_params.supported_versions); |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13158 | EXPECT_EQ(1u, |
| 13159 | http_server_properties |
| 13160 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13161 | .size()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13162 | |
| 13163 | // Send a clear header. |
| 13164 | MockRead data_reads[] = { |
| 13165 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 13166 | MockRead("Alt-Svc: clear\r\n"), |
| 13167 | MockRead("\r\n"), |
| 13168 | MockRead("hello world"), |
| 13169 | MockRead(SYNCHRONOUS, OK), |
| 13170 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13171 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13172 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 13173 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13174 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 13175 | ssl.ssl_info.cert = |
| 13176 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 13177 | ASSERT_TRUE(ssl.ssl_info.cert); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13178 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13179 | |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13180 | HttpRequestInfo request; |
| 13181 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13182 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13183 | request.traffic_annotation = |
| 13184 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13185 | |
| 13186 | TestCompletionCallback callback; |
| 13187 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13188 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13189 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13190 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13191 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13192 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13193 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13194 | ASSERT_TRUE(response); |
| 13195 | ASSERT_TRUE(response->headers); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13196 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13197 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13198 | EXPECT_FALSE(response->was_alpn_negotiated); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13199 | |
| 13200 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13201 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13202 | EXPECT_EQ("hello world", response_data); |
| 13203 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13204 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13205 | http_server_properties |
| 13206 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13207 | .empty()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13208 | } |
| 13209 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13210 | TEST_F(HttpNetworkTransactionTest, HonorMultipleAlternativeServiceHeaders) { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13211 | MockRead data_reads[] = { |
| 13212 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 13213 | MockRead("Alt-Svc: h2=\"www.example.com:443\","), |
| 13214 | MockRead("h2=\":1234\"\r\n\r\n"), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13215 | MockRead("hello world"), |
| 13216 | MockRead(SYNCHRONOUS, OK), |
| 13217 | }; |
| 13218 | |
| 13219 | HttpRequestInfo request; |
| 13220 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13221 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13222 | request.traffic_annotation = |
| 13223 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13224 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13225 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13226 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 13227 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13228 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 13229 | ssl.ssl_info.cert = |
| 13230 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 13231 | ASSERT_TRUE(ssl.ssl_info.cert); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13232 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13233 | |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13234 | TestCompletionCallback callback; |
| 13235 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13236 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13237 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13238 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13239 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13240 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13241 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13242 | url::SchemeHostPort test_server("https", "www.example.org", 443); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13243 | HttpServerProperties* http_server_properties = |
| 13244 | session->http_server_properties(); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13245 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13246 | http_server_properties |
| 13247 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13248 | .empty()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13249 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13250 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13251 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13252 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13253 | ASSERT_TRUE(response); |
| 13254 | ASSERT_TRUE(response->headers); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13255 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13256 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13257 | EXPECT_FALSE(response->was_alpn_negotiated); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13258 | |
| 13259 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13260 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13261 | EXPECT_EQ("hello world", response_data); |
| 13262 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13263 | AlternativeServiceInfoVector alternative_service_info_vector = |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13264 | http_server_properties->GetAlternativeServiceInfos(test_server, |
| 13265 | NetworkIsolationKey()); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13266 | ASSERT_EQ(2u, alternative_service_info_vector.size()); |
| 13267 | |
| 13268 | AlternativeService alternative_service(kProtoHTTP2, "www.example.com", 443); |
| 13269 | EXPECT_EQ(alternative_service, |
zhongyi | 422ce35 | 2017-06-09 23:28:54 | [diff] [blame] | 13270 | alternative_service_info_vector[0].alternative_service()); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13271 | AlternativeService alternative_service_2(kProtoHTTP2, "www.example.org", |
| 13272 | 1234); |
| 13273 | EXPECT_EQ(alternative_service_2, |
zhongyi | 422ce35 | 2017-06-09 23:28:54 | [diff] [blame] | 13274 | alternative_service_info_vector[1].alternative_service()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13275 | } |
| 13276 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13277 | TEST_F(HttpNetworkTransactionTest, IdentifyQuicBroken) { |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13278 | url::SchemeHostPort server("https", "origin.example.org", 443); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13279 | HostPortPair alternative("alternative.example.org", 443); |
| 13280 | std::string origin_url = "https://origin.example.org:443"; |
| 13281 | std::string alternative_url = "https://alternative.example.org:443"; |
| 13282 | |
| 13283 | // Negotiate HTTP/1.1 with alternative.example.org. |
| 13284 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13285 | ssl.next_proto = kProtoHTTP11; |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13286 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13287 | |
| 13288 | // HTTP/1.1 data for request. |
| 13289 | MockWrite http_writes[] = { |
| 13290 | MockWrite("GET / HTTP/1.1\r\n" |
| 13291 | "Host: alternative.example.org\r\n" |
| 13292 | "Connection: keep-alive\r\n\r\n"), |
| 13293 | }; |
| 13294 | |
| 13295 | MockRead http_reads[] = { |
| 13296 | MockRead("HTTP/1.1 200 OK\r\n" |
| 13297 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 13298 | "Content-Length: 40\r\n\r\n" |
| 13299 | "first HTTP/1.1 response from alternative"), |
| 13300 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13301 | StaticSocketDataProvider http_data(http_reads, http_writes); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13302 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 13303 | |
| 13304 | StaticSocketDataProvider data_refused; |
| 13305 | data_refused.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_REFUSED)); |
| 13306 | session_deps_.socket_factory->AddSocketDataProvider(&data_refused); |
| 13307 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13308 | // Set up a QUIC alternative service for server. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13309 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13310 | HttpServerProperties* http_server_properties = |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13311 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13312 | AlternativeService alternative_service(kProtoQUIC, alternative); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13313 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13314 | http_server_properties->SetQuicAlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13315 | server, NetworkIsolationKey(), alternative_service, expiration, |
Nick Harper | 72ade19 | 2019-07-17 03:30:42 | [diff] [blame] | 13316 | HttpNetworkSession::Params().quic_params.supported_versions); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13317 | // Mark the QUIC alternative service as broken. |
Matt Menke | b32ba512 | 2019-09-10 19:17:05 | [diff] [blame] | 13318 | http_server_properties->MarkAlternativeServiceBroken(alternative_service, |
| 13319 | NetworkIsolationKey()); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13320 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13321 | HttpRequestInfo request; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 13322 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13323 | request.method = "GET"; |
| 13324 | request.url = GURL(origin_url); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13325 | request.traffic_annotation = |
| 13326 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 13327 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13328 | TestCompletionCallback callback; |
| 13329 | NetErrorDetails details; |
| 13330 | EXPECT_FALSE(details.quic_broken); |
| 13331 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13332 | trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13333 | trans.PopulateNetErrorDetails(&details); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13334 | EXPECT_TRUE(details.quic_broken); |
| 13335 | } |
| 13336 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13337 | TEST_F(HttpNetworkTransactionTest, IdentifyQuicNotBroken) { |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13338 | url::SchemeHostPort server("https", "origin.example.org", 443); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13339 | HostPortPair alternative1("alternative1.example.org", 443); |
| 13340 | HostPortPair alternative2("alternative2.example.org", 443); |
| 13341 | std::string origin_url = "https://origin.example.org:443"; |
| 13342 | std::string alternative_url1 = "https://alternative1.example.org:443"; |
| 13343 | std::string alternative_url2 = "https://alternative2.example.org:443"; |
| 13344 | |
| 13345 | // Negotiate HTTP/1.1 with alternative1.example.org. |
| 13346 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13347 | ssl.next_proto = kProtoHTTP11; |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13348 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13349 | |
| 13350 | // HTTP/1.1 data for request. |
| 13351 | MockWrite http_writes[] = { |
| 13352 | MockWrite("GET / HTTP/1.1\r\n" |
| 13353 | "Host: alternative1.example.org\r\n" |
| 13354 | "Connection: keep-alive\r\n\r\n"), |
| 13355 | }; |
| 13356 | |
| 13357 | MockRead http_reads[] = { |
| 13358 | MockRead("HTTP/1.1 200 OK\r\n" |
| 13359 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 13360 | "Content-Length: 40\r\n\r\n" |
| 13361 | "first HTTP/1.1 response from alternative1"), |
| 13362 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13363 | StaticSocketDataProvider http_data(http_reads, http_writes); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13364 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 13365 | |
| 13366 | StaticSocketDataProvider data_refused; |
| 13367 | data_refused.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_REFUSED)); |
| 13368 | session_deps_.socket_factory->AddSocketDataProvider(&data_refused); |
| 13369 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13370 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13371 | HttpServerProperties* http_server_properties = |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13372 | session->http_server_properties(); |
| 13373 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13374 | // Set up two QUIC alternative services for server. |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13375 | AlternativeServiceInfoVector alternative_service_info_vector; |
| 13376 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 13377 | |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13378 | AlternativeService alternative_service1(kProtoQUIC, alternative1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13379 | alternative_service_info_vector.push_back( |
| 13380 | AlternativeServiceInfo::CreateQuicAlternativeServiceInfo( |
| 13381 | alternative_service1, expiration, |
Nick Harper | 72ade19 | 2019-07-17 03:30:42 | [diff] [blame] | 13382 | session->params().quic_params.supported_versions)); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13383 | AlternativeService alternative_service2(kProtoQUIC, alternative2); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13384 | alternative_service_info_vector.push_back( |
| 13385 | AlternativeServiceInfo::CreateQuicAlternativeServiceInfo( |
| 13386 | alternative_service2, expiration, |
Nick Harper | 72ade19 | 2019-07-17 03:30:42 | [diff] [blame] | 13387 | session->params().quic_params.supported_versions)); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13388 | |
| 13389 | http_server_properties->SetAlternativeServices( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13390 | server, NetworkIsolationKey(), alternative_service_info_vector); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13391 | |
| 13392 | // Mark one of the QUIC alternative service as broken. |
Matt Menke | b32ba512 | 2019-09-10 19:17:05 | [diff] [blame] | 13393 | http_server_properties->MarkAlternativeServiceBroken(alternative_service1, |
| 13394 | NetworkIsolationKey()); |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13395 | EXPECT_EQ(2u, http_server_properties |
| 13396 | ->GetAlternativeServiceInfos(server, NetworkIsolationKey()) |
| 13397 | .size()); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13398 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13399 | HttpRequestInfo request; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 13400 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13401 | request.method = "GET"; |
| 13402 | request.url = GURL(origin_url); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13403 | request.traffic_annotation = |
| 13404 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 13405 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13406 | TestCompletionCallback callback; |
| 13407 | NetErrorDetails details; |
| 13408 | EXPECT_FALSE(details.quic_broken); |
| 13409 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13410 | trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13411 | trans.PopulateNetErrorDetails(&details); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13412 | EXPECT_FALSE(details.quic_broken); |
| 13413 | } |
| 13414 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13415 | TEST_F(HttpNetworkTransactionTest, MarkBrokenAlternateProtocolAndFallback) { |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13416 | HttpRequestInfo request; |
| 13417 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13418 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13419 | request.traffic_annotation = |
| 13420 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13421 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13422 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13423 | StaticSocketDataProvider first_data; |
| 13424 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13425 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13426 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13427 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13428 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13429 | |
| 13430 | MockRead data_reads[] = { |
| 13431 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13432 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13433 | MockRead(ASYNC, OK), |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13434 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13435 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13436 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13437 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13438 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13439 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13440 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13441 | session->http_server_properties(); |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13442 | const url::SchemeHostPort server(request.url); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13443 | // Port must be < 1024, or the header will be ignored (since initial port was |
| 13444 | // port 80 (another restricted port). |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13445 | // Port is ignored by MockConnect anyway. |
| 13446 | const AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13447 | 666); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13448 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13449 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13450 | server, NetworkIsolationKey(), alternative_service, expiration); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13451 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13452 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13453 | TestCompletionCallback callback; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13454 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13455 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13456 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13457 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13458 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13459 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13460 | ASSERT_TRUE(response); |
| 13461 | ASSERT_TRUE(response->headers); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13462 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13463 | |
| 13464 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13465 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13466 | EXPECT_EQ("hello world", response_data); |
| 13467 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13468 | const AlternativeServiceInfoVector alternative_service_info_vector = |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13469 | http_server_properties->GetAlternativeServiceInfos(server, |
| 13470 | NetworkIsolationKey()); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13471 | ASSERT_EQ(1u, alternative_service_info_vector.size()); |
| 13472 | EXPECT_EQ(alternative_service, |
zhongyi | 422ce35 | 2017-06-09 23:28:54 | [diff] [blame] | 13473 | alternative_service_info_vector[0].alternative_service()); |
Matt Menke | b32ba512 | 2019-09-10 19:17:05 | [diff] [blame] | 13474 | EXPECT_TRUE(http_server_properties->IsAlternativeServiceBroken( |
| 13475 | alternative_service, NetworkIsolationKey())); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13476 | } |
| 13477 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13478 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
| 13479 | // to an unrestricted (port >= 1024) when the original traffic was on a |
| 13480 | // restricted port (port < 1024). Ensure that we can redirect in all other |
| 13481 | // cases. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13482 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedBlocked) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13483 | HttpRequestInfo restricted_port_request; |
| 13484 | restricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13485 | restricted_port_request.url = GURL("https://www.example.org:1023/"); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13486 | restricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13487 | restricted_port_request.traffic_annotation = |
| 13488 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13489 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13490 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13491 | StaticSocketDataProvider first_data; |
| 13492 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13493 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13494 | |
| 13495 | MockRead data_reads[] = { |
| 13496 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13497 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13498 | MockRead(ASYNC, OK), |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13499 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13500 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13501 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13502 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13503 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13504 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13505 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13506 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13507 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13508 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13509 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13510 | const int kUnrestrictedAlternatePort = 1024; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13511 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13512 | kUnrestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13513 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13514 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13515 | url::SchemeHostPort(restricted_port_request.url), NetworkIsolationKey(), |
| 13516 | alternative_service, expiration); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13517 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13518 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13519 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13520 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13521 | int rv = trans.Start(&restricted_port_request, callback.callback(), |
| 13522 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13523 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13524 | // Invalid change to unrestricted port should fail. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13525 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_REFUSED)); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13526 | } |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13527 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13528 | // Ensure that we are allowed to redirect traffic via an alternate protocol to |
| 13529 | // an unrestricted (port >= 1024) when the original traffic was on a restricted |
| 13530 | // port (port < 1024) if we set |enable_user_alternate_protocol_ports|. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13531 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedPermitted) { |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13532 | session_deps_.enable_user_alternate_protocol_ports = true; |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13533 | |
| 13534 | HttpRequestInfo restricted_port_request; |
| 13535 | restricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13536 | restricted_port_request.url = GURL("https://www.example.org:1023/"); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13537 | restricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13538 | restricted_port_request.traffic_annotation = |
| 13539 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13540 | |
| 13541 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
| 13542 | StaticSocketDataProvider first_data; |
| 13543 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13544 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13545 | |
| 13546 | MockRead data_reads[] = { |
| 13547 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13548 | MockRead("hello world"), |
| 13549 | MockRead(ASYNC, OK), |
| 13550 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13551 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13552 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13553 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13554 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13555 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13556 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13557 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13558 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13559 | HttpServerProperties* http_server_properties = |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13560 | session->http_server_properties(); |
| 13561 | const int kUnrestrictedAlternatePort = 1024; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13562 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13563 | kUnrestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13564 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13565 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13566 | url::SchemeHostPort(restricted_port_request.url), NetworkIsolationKey(), |
| 13567 | alternative_service, expiration); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13568 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13569 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13570 | TestCompletionCallback callback; |
| 13571 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13572 | EXPECT_EQ(ERR_IO_PENDING, |
| 13573 | trans.Start(&restricted_port_request, callback.callback(), |
| 13574 | NetLogWithSource())); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13575 | // Change to unrestricted port should succeed. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13576 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13577 | } |
| 13578 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13579 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
| 13580 | // to an unrestricted (port >= 1024) when the original traffic was on a |
| 13581 | // restricted port (port < 1024). Ensure that we can redirect in all other |
| 13582 | // cases. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13583 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedAllowed) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13584 | HttpRequestInfo restricted_port_request; |
| 13585 | restricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13586 | restricted_port_request.url = GURL("https://www.example.org:1023/"); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13587 | restricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13588 | restricted_port_request.traffic_annotation = |
| 13589 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13590 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13591 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13592 | StaticSocketDataProvider first_data; |
| 13593 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13594 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13595 | |
| 13596 | MockRead data_reads[] = { |
| 13597 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13598 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13599 | MockRead(ASYNC, OK), |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13600 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13601 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13602 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13603 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13604 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 13605 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13606 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13607 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13608 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13609 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13610 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13611 | const int kRestrictedAlternatePort = 80; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13612 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13613 | kRestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13614 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13615 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13616 | url::SchemeHostPort(restricted_port_request.url), NetworkIsolationKey(), |
| 13617 | alternative_service, expiration); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13618 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13619 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13620 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13621 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13622 | int rv = trans.Start(&restricted_port_request, callback.callback(), |
| 13623 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13624 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13625 | // Valid change to restricted port should pass. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13626 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13627 | } |
| 13628 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13629 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
| 13630 | // to an unrestricted (port >= 1024) when the original traffic was on a |
| 13631 | // restricted port (port < 1024). Ensure that we can redirect in all other |
| 13632 | // cases. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13633 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortUnrestrictedAllowed1) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13634 | HttpRequestInfo unrestricted_port_request; |
| 13635 | unrestricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13636 | unrestricted_port_request.url = GURL("https://www.example.org:1024/"); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13637 | unrestricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13638 | unrestricted_port_request.traffic_annotation = |
| 13639 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13640 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13641 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13642 | StaticSocketDataProvider first_data; |
| 13643 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13644 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13645 | |
| 13646 | MockRead data_reads[] = { |
| 13647 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13648 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13649 | MockRead(ASYNC, OK), |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13650 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13651 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13652 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13653 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13654 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13655 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13656 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13657 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13658 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13659 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13660 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13661 | const int kRestrictedAlternatePort = 80; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13662 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13663 | kRestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13664 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13665 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13666 | url::SchemeHostPort(unrestricted_port_request.url), NetworkIsolationKey(), |
| 13667 | alternative_service, expiration); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13668 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13669 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13670 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13671 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13672 | int rv = trans.Start(&unrestricted_port_request, callback.callback(), |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13673 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13674 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13675 | // Valid change to restricted port should pass. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13676 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13677 | } |
| 13678 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13679 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
| 13680 | // to an unrestricted (port >= 1024) when the original traffic was on a |
| 13681 | // restricted port (port < 1024). Ensure that we can redirect in all other |
| 13682 | // cases. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13683 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortUnrestrictedAllowed2) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13684 | HttpRequestInfo unrestricted_port_request; |
| 13685 | unrestricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13686 | unrestricted_port_request.url = GURL("https://www.example.org:1024/"); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13687 | unrestricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13688 | unrestricted_port_request.traffic_annotation = |
| 13689 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13690 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13691 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13692 | StaticSocketDataProvider first_data; |
| 13693 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13694 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13695 | |
| 13696 | MockRead data_reads[] = { |
| 13697 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13698 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13699 | MockRead(ASYNC, OK), |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13700 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13701 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13702 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13703 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13704 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 13705 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13706 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13707 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13708 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13709 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13710 | session->http_server_properties(); |
bnc | 0bbb0262 | 2015-07-23 10:06:22 | [diff] [blame] | 13711 | const int kUnrestrictedAlternatePort = 1025; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13712 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13713 | kUnrestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13714 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13715 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13716 | url::SchemeHostPort(unrestricted_port_request.url), NetworkIsolationKey(), |
| 13717 | alternative_service, expiration); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13718 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13719 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13720 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13721 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13722 | int rv = trans.Start(&unrestricted_port_request, callback.callback(), |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13723 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13724 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13725 | // Valid change to an unrestricted port should pass. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13726 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13727 | } |
| 13728 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13729 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
Xida Chen | 9bfe0b6 | 2018-04-24 19:52:21 | [diff] [blame] | 13730 | // to an unsafe port, and that we resume the second HttpStreamFactory::Job once |
| 13731 | // the alternate protocol request fails. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13732 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolUnsafeBlocked) { |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13733 | HttpRequestInfo request; |
| 13734 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 13735 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13736 | request.traffic_annotation = |
| 13737 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13738 | |
| 13739 | // The alternate protocol request will error out before we attempt to connect, |
| 13740 | // so only the standard HTTP request will try to connect. |
| 13741 | MockRead data_reads[] = { |
| 13742 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13743 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13744 | MockRead(ASYNC, OK), |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13745 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13746 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13747 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13748 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13749 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13750 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13751 | HttpServerProperties* http_server_properties = |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13752 | session->http_server_properties(); |
| 13753 | const int kUnsafePort = 7; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13754 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13755 | kUnsafePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13756 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13757 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13758 | url::SchemeHostPort(request.url), NetworkIsolationKey(), |
| 13759 | alternative_service, expiration); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13760 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13761 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13762 | TestCompletionCallback callback; |
| 13763 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13764 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13765 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13766 | // The HTTP request should succeed. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13767 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13768 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13769 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13770 | ASSERT_TRUE(response); |
| 13771 | ASSERT_TRUE(response->headers); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13772 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13773 | |
| 13774 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13775 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13776 | EXPECT_EQ("hello world", response_data); |
| 13777 | } |
| 13778 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13779 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForNpnSpdy) { |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13780 | HttpRequestInfo request; |
| 13781 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13782 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13783 | request.traffic_annotation = |
| 13784 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13785 | |
| 13786 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13787 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 13788 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13789 | MockRead("\r\n"), |
| 13790 | MockRead("hello world"), |
| 13791 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 13792 | MockRead(ASYNC, OK)}; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13793 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13794 | StaticSocketDataProvider first_transaction(data_reads, |
| 13795 | base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13796 | session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13797 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13798 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13799 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13800 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 13801 | AddSSLSocketData(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13802 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 13803 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13804 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 13805 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13806 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 13807 | spdy::SpdySerializedFrame resp( |
| 13808 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 13809 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13810 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 13811 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13812 | }; |
| 13813 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13814 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13815 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13816 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13817 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13818 | StaticSocketDataProvider hanging_non_alternate_protocol_socket; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13819 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 13820 | never_finishing_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13821 | session_deps_.socket_factory->AddSocketDataProvider( |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13822 | &hanging_non_alternate_protocol_socket); |
| 13823 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13824 | TestCompletionCallback callback; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13825 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13826 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 13827 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 13828 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13829 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13830 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13831 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13832 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13833 | |
| 13834 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13835 | ASSERT_TRUE(response); |
| 13836 | ASSERT_TRUE(response->headers); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13837 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13838 | |
| 13839 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13840 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13841 | EXPECT_EQ("hello world", response_data); |
| 13842 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 13843 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 13844 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13845 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13846 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13847 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13848 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13849 | |
| 13850 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13851 | ASSERT_TRUE(response); |
| 13852 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 13853 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 13854 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13855 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13856 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13857 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13858 | EXPECT_EQ("hello!", response_data); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13859 | } |
| 13860 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13861 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolWithSpdyLateBinding) { |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13862 | HttpRequestInfo request; |
| 13863 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13864 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13865 | request.traffic_annotation = |
| 13866 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13867 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13868 | // First transaction receives Alt-Svc header over HTTP/1.1. |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13869 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13870 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 13871 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13872 | MockRead("\r\n"), |
| 13873 | MockRead("hello world"), |
| 13874 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 13875 | MockRead(ASYNC, OK), |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13876 | }; |
| 13877 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13878 | StaticSocketDataProvider http11_data(data_reads, base::span<MockWrite>()); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13879 | session_deps_.socket_factory->AddSocketDataProvider(&http11_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13880 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13881 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 13882 | ssl_http11.ssl_info.cert = |
| 13883 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 13884 | ASSERT_TRUE(ssl_http11.ssl_info.cert); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13885 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
| 13886 | |
| 13887 | // Second transaction starts an alternative and a non-alternative Job. |
| 13888 | // Both sockets hang. |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13889 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13890 | StaticSocketDataProvider hanging_socket1; |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 13891 | hanging_socket1.set_connect_data(never_finishing_connect); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 13892 | session_deps_.socket_factory->AddSocketDataProvider(&hanging_socket1); |
| 13893 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13894 | StaticSocketDataProvider hanging_socket2; |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 13895 | hanging_socket2.set_connect_data(never_finishing_connect); |
| 13896 | session_deps_.socket_factory->AddSocketDataProvider(&hanging_socket2); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13897 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13898 | // Third transaction starts an alternative and a non-alternative job. |
| 13899 | // The non-alternative job hangs, but the alternative one succeeds. |
| 13900 | // The second transaction, still pending, binds to this socket. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 13901 | spdy::SpdySerializedFrame req1( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13902 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 13903 | spdy::SpdySerializedFrame req2( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13904 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 3, LOWEST)); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13905 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 13906 | CreateMockWrite(req1, 0), CreateMockWrite(req2, 1), |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13907 | }; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 13908 | spdy::SpdySerializedFrame resp1( |
| 13909 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 13910 | spdy::SpdySerializedFrame data1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 13911 | spdy::SpdySerializedFrame resp2( |
| 13912 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 13913 | spdy::SpdySerializedFrame data2(spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13914 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 13915 | CreateMockRead(resp1, 2), CreateMockRead(data1, 3), |
| 13916 | CreateMockRead(resp2, 4), CreateMockRead(data2, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 13917 | MockRead(ASYNC, 0, 6), |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13918 | }; |
| 13919 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13920 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13921 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13922 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 13923 | AddSSLSocketData(); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13924 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13925 | StaticSocketDataProvider hanging_socket3; |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 13926 | hanging_socket3.set_connect_data(never_finishing_connect); |
| 13927 | session_deps_.socket_factory->AddSocketDataProvider(&hanging_socket3); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13928 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13929 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13930 | TestCompletionCallback callback1; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 13931 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13932 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13933 | int rv = trans1.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13934 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13935 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13936 | |
| 13937 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13938 | ASSERT_TRUE(response); |
| 13939 | ASSERT_TRUE(response->headers); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13940 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13941 | |
| 13942 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13943 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13944 | EXPECT_EQ("hello world", response_data); |
| 13945 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13946 | TestCompletionCallback callback2; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 13947 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13948 | rv = trans2.Start(&request, callback2.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13949 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13950 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13951 | TestCompletionCallback callback3; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 13952 | HttpNetworkTransaction trans3(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13953 | rv = trans3.Start(&request, callback3.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13954 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13955 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13956 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 13957 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13958 | |
| 13959 | response = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13960 | ASSERT_TRUE(response); |
| 13961 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 13962 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13963 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13964 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13965 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13966 | EXPECT_EQ("hello!", response_data); |
| 13967 | |
| 13968 | response = trans3.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13969 | ASSERT_TRUE(response); |
| 13970 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 13971 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13972 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13973 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13974 | ASSERT_THAT(ReadTransaction(&trans3, &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13975 | EXPECT_EQ("hello!", response_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13976 | } |
| 13977 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13978 | TEST_F(HttpNetworkTransactionTest, StallAlternativeServiceForNpnSpdy) { |
Bence Béky | bcae3709 | 2018-06-12 04:18:53 | [diff] [blame] | 13979 | session_deps_.host_resolver->set_synchronous_mode(true); |
| 13980 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13981 | HttpRequestInfo request; |
| 13982 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13983 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 13984 | request.traffic_annotation = |
| 13985 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13986 | |
| 13987 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13988 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 13989 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13990 | MockRead("\r\n"), |
| 13991 | MockRead("hello world"), |
| 13992 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 13993 | MockRead(ASYNC, OK), |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13994 | }; |
| 13995 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13996 | StaticSocketDataProvider first_transaction(data_reads, |
| 13997 | base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13998 | session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13999 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 14000 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 14001 | ssl.ssl_info.cert = |
| 14002 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 14003 | ASSERT_TRUE(ssl.ssl_info.cert); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14004 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14005 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 14006 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14007 | StaticSocketDataProvider hanging_alternate_protocol_socket; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14008 | hanging_alternate_protocol_socket.set_connect_data( |
| 14009 | never_finishing_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14010 | session_deps_.socket_factory->AddSocketDataProvider( |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14011 | &hanging_alternate_protocol_socket); |
| 14012 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14013 | // 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] | 14014 | StaticSocketDataProvider second_transaction(data_reads, |
| 14015 | base::span<MockWrite>()); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 14016 | session_deps_.socket_factory->AddSocketDataProvider(&second_transaction); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14017 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14018 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 14019 | TestCompletionCallback callback; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14020 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 14021 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14022 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14023 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14024 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14025 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14026 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14027 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14028 | |
| 14029 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14030 | ASSERT_TRUE(response); |
| 14031 | ASSERT_TRUE(response->headers); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14032 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 14033 | |
| 14034 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14035 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14036 | EXPECT_EQ("hello world", response_data); |
| 14037 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14038 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14039 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14040 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14041 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14042 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14043 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14044 | |
| 14045 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14046 | ASSERT_TRUE(response); |
| 14047 | ASSERT_TRUE(response->headers); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14048 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 14049 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14050 | EXPECT_FALSE(response->was_alpn_negotiated); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14051 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14052 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14053 | EXPECT_EQ("hello world", response_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14054 | } |
| 14055 | |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14056 | // Test that proxy is resolved using the origin url, |
| 14057 | // regardless of the alternative server. |
| 14058 | TEST_F(HttpNetworkTransactionTest, UseOriginNotAlternativeForProxy) { |
| 14059 | // Configure proxy to bypass www.example.org, which is the origin URL. |
| 14060 | ProxyConfig proxy_config; |
| 14061 | proxy_config.proxy_rules().ParseFromString("myproxy:70"); |
| 14062 | proxy_config.proxy_rules().bypass_rules.AddRuleFromString("www.example.org"); |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 14063 | auto proxy_config_service = std::make_unique<ProxyConfigServiceFixed>( |
| 14064 | ProxyConfigWithAnnotation(proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14065 | |
| 14066 | CapturingProxyResolver capturing_proxy_resolver; |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14067 | auto proxy_resolver_factory = std::make_unique<CapturingProxyResolverFactory>( |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14068 | &capturing_proxy_resolver); |
| 14069 | |
| 14070 | TestNetLog net_log; |
| 14071 | |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 14072 | session_deps_.proxy_resolution_service = |
| 14073 | std::make_unique<ProxyResolutionService>( |
| 14074 | std::move(proxy_config_service), std::move(proxy_resolver_factory), |
| 14075 | &net_log); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14076 | |
| 14077 | session_deps_.net_log = &net_log; |
| 14078 | |
| 14079 | // Configure alternative service with a hostname that is not bypassed by the |
| 14080 | // proxy. |
| 14081 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 14082 | HttpServerProperties* http_server_properties = |
| 14083 | session->http_server_properties(); |
| 14084 | url::SchemeHostPort server("https", "www.example.org", 443); |
| 14085 | HostPortPair alternative("www.example.com", 443); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 14086 | AlternativeService alternative_service(kProtoHTTP2, alternative); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14087 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 14088 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 14089 | server, NetworkIsolationKey(), alternative_service, expiration); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14090 | |
| 14091 | // Non-alternative job should hang. |
| 14092 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14093 | StaticSocketDataProvider hanging_alternate_protocol_socket; |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14094 | hanging_alternate_protocol_socket.set_connect_data(never_finishing_connect); |
| 14095 | session_deps_.socket_factory->AddSocketDataProvider( |
| 14096 | &hanging_alternate_protocol_socket); |
| 14097 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 14098 | AddSSLSocketData(); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14099 | |
| 14100 | HttpRequestInfo request; |
| 14101 | request.method = "GET"; |
| 14102 | request.url = GURL("https://www.example.org/"); |
| 14103 | request.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 14104 | request.traffic_annotation = |
| 14105 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14106 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14107 | spdy::SpdySerializedFrame req( |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14108 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
| 14109 | |
| 14110 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
| 14111 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14112 | spdy::SpdySerializedFrame resp( |
| 14113 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 14114 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14115 | MockRead spdy_reads[] = { |
| 14116 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
| 14117 | }; |
| 14118 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14119 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14120 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
| 14121 | |
| 14122 | TestCompletionCallback callback; |
| 14123 | |
| 14124 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 14125 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14126 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14127 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 14128 | |
| 14129 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 14130 | ASSERT_TRUE(response); |
| 14131 | ASSERT_TRUE(response->headers); |
| 14132 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 14133 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14134 | EXPECT_TRUE(response->was_alpn_negotiated); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14135 | |
| 14136 | std::string response_data; |
| 14137 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
| 14138 | EXPECT_EQ("hello!", response_data); |
| 14139 | |
| 14140 | // Origin host bypasses proxy, no resolution should have happened. |
| 14141 | ASSERT_TRUE(capturing_proxy_resolver.resolved().empty()); |
| 14142 | } |
| 14143 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 14144 | TEST_F(HttpNetworkTransactionTest, UseAlternativeServiceForTunneledNpnSpdy) { |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14145 | ProxyConfig proxy_config; |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 14146 | proxy_config.set_auto_detect(true); |
| 14147 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 14148 | |
sammc | 5dd160c | 2015-04-02 02:43:13 | [diff] [blame] | 14149 | CapturingProxyResolver capturing_proxy_resolver; |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 14150 | session_deps_.proxy_resolution_service = |
| 14151 | std::make_unique<ProxyResolutionService>( |
| 14152 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 14153 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 14154 | std::make_unique<CapturingProxyResolverFactory>( |
| 14155 | &capturing_proxy_resolver), |
| 14156 | nullptr); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 14157 | TestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14158 | session_deps_.net_log = &net_log; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14159 | |
| 14160 | HttpRequestInfo request; |
| 14161 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14162 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 14163 | request.traffic_annotation = |
| 14164 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14165 | |
| 14166 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14167 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 14168 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14169 | MockRead("\r\n"), |
| 14170 | MockRead("hello world"), |
| 14171 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 14172 | MockRead(ASYNC, OK), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14173 | }; |
| 14174 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14175 | StaticSocketDataProvider first_transaction(data_reads, |
| 14176 | base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14177 | session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14178 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 14179 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14180 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14181 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 14182 | AddSSLSocketData(); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14183 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14184 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14185 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14186 | MockWrite spdy_writes[] = { |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 14187 | MockWrite(ASYNC, 0, |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 14188 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 14189 | "Host: www.example.org:443\r\n" |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 14190 | "Proxy-Connection: keep-alive\r\n\r\n"), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14191 | CreateMockWrite(req, 2), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14192 | }; |
| 14193 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 14194 | const char kCONNECTResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 14195 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14196 | spdy::SpdySerializedFrame resp( |
| 14197 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14198 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14199 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14200 | MockRead(ASYNC, 1, kCONNECTResponse), CreateMockRead(resp, 3), |
| 14201 | CreateMockRead(data, 4), MockRead(SYNCHRONOUS, ERR_IO_PENDING, 5), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14202 | }; |
| 14203 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14204 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14205 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14206 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 14207 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14208 | StaticSocketDataProvider hanging_non_alternate_protocol_socket; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14209 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 14210 | never_finishing_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14211 | session_deps_.socket_factory->AddSocketDataProvider( |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14212 | &hanging_non_alternate_protocol_socket); |
| 14213 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 14214 | TestCompletionCallback callback; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14215 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 14216 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14217 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14218 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14219 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14220 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 14221 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14222 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 14223 | |
| 14224 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 14225 | ASSERT_TRUE(response); |
| 14226 | ASSERT_TRUE(response->headers); |
| 14227 | EXPECT_EQ("HTTP/0.9 200 OK", response->headers->GetStatusLine()); |
| 14228 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14229 | EXPECT_TRUE(response->was_alpn_negotiated); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 14230 | |
| 14231 | std::string response_data; |
| 14232 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 14233 | EXPECT_EQ("hello world", response_data); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14234 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14235 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14236 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14237 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14238 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14239 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14240 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14241 | |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 14242 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14243 | ASSERT_TRUE(response); |
| 14244 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 14245 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 14246 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14247 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14248 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14249 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14250 | EXPECT_EQ("hello!", response_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14251 | ASSERT_EQ(2u, capturing_proxy_resolver.resolved().size()); |
| 14252 | EXPECT_EQ("https://www.example.org/", |
sammc | 5dd160c | 2015-04-02 02:43:13 | [diff] [blame] | 14253 | capturing_proxy_resolver.resolved()[0].spec()); |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 14254 | EXPECT_EQ("https://www.example.org/", |
sammc | 5dd160c | 2015-04-02 02:43:13 | [diff] [blame] | 14255 | capturing_proxy_resolver.resolved()[1].spec()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14256 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 14257 | LoadTimingInfo load_timing_info; |
| 14258 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 14259 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 14260 | CONNECT_TIMING_HAS_SSL_TIMES); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14261 | } |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14262 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 14263 | TEST_F(HttpNetworkTransactionTest, |
bnc | 1c196c6e | 2016-05-28 13:51:48 | [diff] [blame] | 14264 | UseAlternativeServiceForNpnSpdyWithExistingSpdySession) { |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14265 | HttpRequestInfo request; |
| 14266 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14267 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 14268 | request.traffic_annotation = |
| 14269 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14270 | |
| 14271 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14272 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 14273 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14274 | MockRead("\r\n"), |
| 14275 | MockRead("hello world"), |
| 14276 | MockRead(ASYNC, OK), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14277 | }; |
| 14278 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14279 | StaticSocketDataProvider first_transaction(data_reads, |
| 14280 | base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14281 | session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14282 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 14283 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14284 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14285 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 14286 | AddSSLSocketData(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14287 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14288 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14289 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14290 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14291 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14292 | spdy::SpdySerializedFrame resp( |
| 14293 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14294 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14295 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14296 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14297 | }; |
| 14298 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14299 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14300 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14301 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 14302 | TestCompletionCallback callback; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14303 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 14304 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14305 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14306 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14307 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14308 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14309 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14310 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14311 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14312 | |
| 14313 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14314 | ASSERT_TRUE(response); |
| 14315 | ASSERT_TRUE(response->headers); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14316 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 14317 | |
| 14318 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14319 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14320 | EXPECT_EQ("hello world", response_data); |
| 14321 | |
| 14322 | // Set up an initial SpdySession in the pool to reuse. |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 14323 | HostPortPair host_port_pair("www.example.org", 443); |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 14324 | SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 14325 | PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 14326 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 14327 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 14328 | base::WeakPtr<SpdySession> spdy_session = |
Bence Béky | 0ef1556e | 2017-06-30 19:52:52 | [diff] [blame] | 14329 | CreateSpdySession(session.get(), key, NetLogWithSource()); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 14330 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14331 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14332 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14333 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14334 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14335 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14336 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14337 | |
| 14338 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14339 | ASSERT_TRUE(response); |
| 14340 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 14341 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 14342 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14343 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14344 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14345 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14346 | EXPECT_EQ("hello!", response_data); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 14347 | } |
| 14348 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14349 | // GenerateAuthToken is a mighty big test. |
| 14350 | // It tests all permutation of GenerateAuthToken behavior: |
| 14351 | // - Synchronous and Asynchronous completion. |
| 14352 | // - OK or error on completion. |
| 14353 | // - Direct connection, non-authenticating proxy, and authenticating proxy. |
| 14354 | // - HTTP or HTTPS backend (to include proxy tunneling). |
| 14355 | // - Non-authenticating and authenticating backend. |
| 14356 | // |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 14357 | // In all, there are 44 reasonable permuations (for example, if there are |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14358 | // problems generating an auth token for an authenticating proxy, we don't |
| 14359 | // need to test all permutations of the backend server). |
| 14360 | // |
| 14361 | // The test proceeds by going over each of the configuration cases, and |
| 14362 | // potentially running up to three rounds in each of the tests. The TestConfig |
| 14363 | // specifies both the configuration for the test as well as the expectations |
| 14364 | // for the results. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 14365 | TEST_F(HttpNetworkTransactionTest, GenerateAuthToken) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 14366 | static const char kServer[] = "http://www.example.com"; |
| 14367 | static const char kSecureServer[] = "https://www.example.com"; |
| 14368 | static const char kProxy[] = "myproxy:70"; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14369 | |
| 14370 | enum AuthTiming { |
| 14371 | AUTH_NONE, |
| 14372 | AUTH_SYNC, |
| 14373 | AUTH_ASYNC, |
| 14374 | }; |
| 14375 | |
| 14376 | const MockWrite kGet( |
| 14377 | "GET / HTTP/1.1\r\n" |
| 14378 | "Host: www.example.com\r\n" |
| 14379 | "Connection: keep-alive\r\n\r\n"); |
| 14380 | const MockWrite kGetProxy( |
| 14381 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 14382 | "Host: www.example.com\r\n" |
| 14383 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 14384 | const MockWrite kGetAuth( |
| 14385 | "GET / HTTP/1.1\r\n" |
| 14386 | "Host: www.example.com\r\n" |
| 14387 | "Connection: keep-alive\r\n" |
| 14388 | "Authorization: auth_token\r\n\r\n"); |
| 14389 | const MockWrite kGetProxyAuth( |
| 14390 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 14391 | "Host: www.example.com\r\n" |
| 14392 | "Proxy-Connection: keep-alive\r\n" |
| 14393 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 14394 | const MockWrite kGetAuthThroughProxy( |
| 14395 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 14396 | "Host: www.example.com\r\n" |
| 14397 | "Proxy-Connection: keep-alive\r\n" |
| 14398 | "Authorization: auth_token\r\n\r\n"); |
| 14399 | const MockWrite kGetAuthWithProxyAuth( |
| 14400 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 14401 | "Host: www.example.com\r\n" |
| 14402 | "Proxy-Connection: keep-alive\r\n" |
| 14403 | "Proxy-Authorization: auth_token\r\n" |
| 14404 | "Authorization: auth_token\r\n\r\n"); |
| 14405 | const MockWrite kConnect( |
| 14406 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 14407 | "Host: www.example.com:443\r\n" |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14408 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 14409 | const MockWrite kConnectProxyAuth( |
| 14410 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 14411 | "Host: www.example.com:443\r\n" |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14412 | "Proxy-Connection: keep-alive\r\n" |
| 14413 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 14414 | |
| 14415 | const MockRead kSuccess( |
| 14416 | "HTTP/1.1 200 OK\r\n" |
| 14417 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 14418 | "Content-Length: 3\r\n\r\n" |
| 14419 | "Yes"); |
| 14420 | const MockRead kFailure( |
| 14421 | "Should not be called."); |
| 14422 | const MockRead kServerChallenge( |
| 14423 | "HTTP/1.1 401 Unauthorized\r\n" |
| 14424 | "WWW-Authenticate: Mock realm=server\r\n" |
| 14425 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 14426 | "Content-Length: 14\r\n\r\n" |
| 14427 | "Unauthorized\r\n"); |
| 14428 | const MockRead kProxyChallenge( |
| 14429 | "HTTP/1.1 407 Unauthorized\r\n" |
| 14430 | "Proxy-Authenticate: Mock realm=proxy\r\n" |
| 14431 | "Proxy-Connection: close\r\n" |
| 14432 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 14433 | "Content-Length: 14\r\n\r\n" |
| 14434 | "Unauthorized\r\n"); |
| 14435 | const MockRead kProxyConnected( |
| 14436 | "HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 14437 | |
| 14438 | // NOTE(cbentzel): I wanted TestReadWriteRound to be a simple struct with |
| 14439 | // no constructors, but the C++ compiler on Windows warns about |
| 14440 | // unspecified data in compound literals. So, moved to using constructors, |
| 14441 | // and TestRound's created with the default constructor should not be used. |
| 14442 | struct TestRound { |
| 14443 | TestRound() |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14444 | : expected_rv(ERR_UNEXPECTED), |
| 14445 | extra_write(nullptr), |
| 14446 | extra_read(nullptr) {} |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 14447 | TestRound(const MockWrite& write_arg, |
| 14448 | const MockRead& read_arg, |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14449 | int expected_rv_arg) |
| 14450 | : write(write_arg), |
| 14451 | read(read_arg), |
| 14452 | expected_rv(expected_rv_arg), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14453 | extra_write(nullptr), |
| 14454 | extra_read(nullptr) {} |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14455 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 14456 | int expected_rv_arg, const MockWrite* extra_write_arg, |
[email protected] | f871ee15 | 2012-07-27 19:02:01 | [diff] [blame] | 14457 | const MockRead* extra_read_arg) |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14458 | : write(write_arg), |
| 14459 | read(read_arg), |
| 14460 | expected_rv(expected_rv_arg), |
| 14461 | extra_write(extra_write_arg), |
| 14462 | extra_read(extra_read_arg) { |
| 14463 | } |
| 14464 | MockWrite write; |
| 14465 | MockRead read; |
| 14466 | int expected_rv; |
| 14467 | const MockWrite* extra_write; |
| 14468 | const MockRead* extra_read; |
| 14469 | }; |
| 14470 | |
| 14471 | static const int kNoSSL = 500; |
| 14472 | |
| 14473 | struct TestConfig { |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14474 | int line_number; |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 14475 | const char* const proxy_url; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14476 | AuthTiming proxy_auth_timing; |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14477 | int first_generate_proxy_token_rv; |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 14478 | const char* const server_url; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14479 | AuthTiming server_auth_timing; |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14480 | int first_generate_server_token_rv; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14481 | int num_auth_rounds; |
| 14482 | int first_ssl_round; |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14483 | TestRound rounds[4]; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14484 | } test_configs[] = { |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14485 | // Non-authenticating HTTP server with a direct connection. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14486 | {__LINE__, |
| 14487 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14488 | AUTH_NONE, |
| 14489 | OK, |
| 14490 | kServer, |
| 14491 | AUTH_NONE, |
| 14492 | OK, |
| 14493 | 1, |
| 14494 | kNoSSL, |
| 14495 | {TestRound(kGet, kSuccess, OK)}}, |
| 14496 | // Authenticating HTTP server with a direct connection. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14497 | {__LINE__, |
| 14498 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14499 | AUTH_NONE, |
| 14500 | OK, |
| 14501 | kServer, |
| 14502 | AUTH_SYNC, |
| 14503 | OK, |
| 14504 | 2, |
| 14505 | kNoSSL, |
| 14506 | {TestRound(kGet, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14507 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14508 | {__LINE__, |
| 14509 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14510 | AUTH_NONE, |
| 14511 | OK, |
| 14512 | kServer, |
| 14513 | AUTH_SYNC, |
| 14514 | ERR_INVALID_AUTH_CREDENTIALS, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14515 | 3, |
| 14516 | kNoSSL, |
| 14517 | {TestRound(kGet, kServerChallenge, OK), |
| 14518 | TestRound(kGet, kServerChallenge, OK), |
| 14519 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14520 | {__LINE__, |
| 14521 | nullptr, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14522 | AUTH_NONE, |
| 14523 | OK, |
| 14524 | kServer, |
| 14525 | AUTH_SYNC, |
| 14526 | ERR_UNSUPPORTED_AUTH_SCHEME, |
| 14527 | 2, |
| 14528 | kNoSSL, |
| 14529 | {TestRound(kGet, kServerChallenge, OK), TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14530 | {__LINE__, |
| 14531 | nullptr, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14532 | AUTH_NONE, |
| 14533 | OK, |
| 14534 | kServer, |
| 14535 | AUTH_SYNC, |
| 14536 | ERR_UNDOCUMENTED_SECURITY_LIBRARY_STATUS, |
| 14537 | 2, |
| 14538 | kNoSSL, |
| 14539 | {TestRound(kGet, kServerChallenge, OK), TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14540 | {__LINE__, |
| 14541 | kProxy, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14542 | AUTH_SYNC, |
| 14543 | ERR_FAILED, |
| 14544 | kServer, |
| 14545 | AUTH_NONE, |
| 14546 | OK, |
| 14547 | 2, |
| 14548 | kNoSSL, |
| 14549 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14550 | TestRound(kGetProxy, kFailure, ERR_FAILED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14551 | {__LINE__, |
| 14552 | kProxy, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14553 | AUTH_ASYNC, |
| 14554 | ERR_FAILED, |
| 14555 | kServer, |
| 14556 | AUTH_NONE, |
| 14557 | OK, |
| 14558 | 2, |
| 14559 | kNoSSL, |
| 14560 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14561 | TestRound(kGetProxy, kFailure, ERR_FAILED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14562 | {__LINE__, |
| 14563 | nullptr, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14564 | AUTH_NONE, |
| 14565 | OK, |
| 14566 | kServer, |
| 14567 | AUTH_SYNC, |
| 14568 | ERR_FAILED, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14569 | 2, |
| 14570 | kNoSSL, |
| 14571 | {TestRound(kGet, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14572 | TestRound(kGet, kFailure, ERR_FAILED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14573 | {__LINE__, |
| 14574 | nullptr, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14575 | AUTH_NONE, |
| 14576 | OK, |
| 14577 | kServer, |
| 14578 | AUTH_ASYNC, |
| 14579 | ERR_FAILED, |
| 14580 | 2, |
| 14581 | kNoSSL, |
| 14582 | {TestRound(kGet, kServerChallenge, OK), |
| 14583 | TestRound(kGet, kFailure, ERR_FAILED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14584 | {__LINE__, |
| 14585 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14586 | AUTH_NONE, |
| 14587 | OK, |
| 14588 | kServer, |
| 14589 | AUTH_ASYNC, |
| 14590 | OK, |
| 14591 | 2, |
| 14592 | kNoSSL, |
| 14593 | {TestRound(kGet, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14594 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14595 | {__LINE__, |
| 14596 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14597 | AUTH_NONE, |
| 14598 | OK, |
| 14599 | kServer, |
| 14600 | AUTH_ASYNC, |
| 14601 | ERR_INVALID_AUTH_CREDENTIALS, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14602 | 3, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14603 | kNoSSL, |
| 14604 | {TestRound(kGet, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14605 | // The second round uses a HttpAuthHandlerMock that always succeeds. |
| 14606 | TestRound(kGet, kServerChallenge, OK), |
| 14607 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14608 | // Non-authenticating HTTP server through a non-authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14609 | {__LINE__, |
| 14610 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14611 | AUTH_NONE, |
| 14612 | OK, |
| 14613 | kServer, |
| 14614 | AUTH_NONE, |
| 14615 | OK, |
| 14616 | 1, |
| 14617 | kNoSSL, |
| 14618 | {TestRound(kGetProxy, kSuccess, OK)}}, |
| 14619 | // Authenticating HTTP server through a non-authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14620 | {__LINE__, |
| 14621 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14622 | AUTH_NONE, |
| 14623 | OK, |
| 14624 | kServer, |
| 14625 | AUTH_SYNC, |
| 14626 | OK, |
| 14627 | 2, |
| 14628 | kNoSSL, |
| 14629 | {TestRound(kGetProxy, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14630 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14631 | {__LINE__, |
| 14632 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14633 | AUTH_NONE, |
| 14634 | OK, |
| 14635 | kServer, |
| 14636 | AUTH_SYNC, |
| 14637 | ERR_INVALID_AUTH_CREDENTIALS, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14638 | 3, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14639 | kNoSSL, |
| 14640 | {TestRound(kGetProxy, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14641 | TestRound(kGetProxy, kServerChallenge, OK), |
| 14642 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14643 | {__LINE__, |
| 14644 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14645 | AUTH_NONE, |
| 14646 | OK, |
| 14647 | kServer, |
| 14648 | AUTH_ASYNC, |
| 14649 | OK, |
| 14650 | 2, |
| 14651 | kNoSSL, |
| 14652 | {TestRound(kGetProxy, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14653 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14654 | {__LINE__, |
| 14655 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14656 | AUTH_NONE, |
| 14657 | OK, |
| 14658 | kServer, |
| 14659 | AUTH_ASYNC, |
| 14660 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14661 | 2, |
| 14662 | kNoSSL, |
| 14663 | {TestRound(kGetProxy, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14664 | TestRound(kGetProxy, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14665 | // Non-authenticating HTTP server through an authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14666 | {__LINE__, |
| 14667 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14668 | AUTH_SYNC, |
| 14669 | OK, |
| 14670 | kServer, |
| 14671 | AUTH_NONE, |
| 14672 | OK, |
| 14673 | 2, |
| 14674 | kNoSSL, |
| 14675 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14676 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14677 | {__LINE__, |
| 14678 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14679 | AUTH_SYNC, |
| 14680 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14681 | kServer, |
| 14682 | AUTH_NONE, |
| 14683 | OK, |
| 14684 | 2, |
| 14685 | kNoSSL, |
| 14686 | {TestRound(kGetProxy, kProxyChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14687 | TestRound(kGetProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14688 | {__LINE__, |
| 14689 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14690 | AUTH_ASYNC, |
| 14691 | OK, |
| 14692 | kServer, |
| 14693 | AUTH_NONE, |
| 14694 | OK, |
| 14695 | 2, |
| 14696 | kNoSSL, |
| 14697 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14698 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14699 | {__LINE__, |
| 14700 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14701 | AUTH_ASYNC, |
| 14702 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14703 | kServer, |
| 14704 | AUTH_NONE, |
| 14705 | OK, |
| 14706 | 2, |
| 14707 | kNoSSL, |
| 14708 | {TestRound(kGetProxy, kProxyChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14709 | TestRound(kGetProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14710 | {__LINE__, |
| 14711 | kProxy, |
| 14712 | AUTH_ASYNC, |
| 14713 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14714 | kServer, |
| 14715 | AUTH_NONE, |
| 14716 | OK, |
| 14717 | 3, |
| 14718 | kNoSSL, |
| 14719 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14720 | TestRound(kGetProxy, kProxyChallenge, OK), |
| 14721 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14722 | // Authenticating HTTP server through an authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14723 | {__LINE__, |
| 14724 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14725 | AUTH_SYNC, |
| 14726 | OK, |
| 14727 | kServer, |
| 14728 | AUTH_SYNC, |
| 14729 | OK, |
| 14730 | 3, |
| 14731 | kNoSSL, |
| 14732 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14733 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14734 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14735 | {__LINE__, |
| 14736 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14737 | AUTH_SYNC, |
| 14738 | OK, |
| 14739 | kServer, |
| 14740 | AUTH_SYNC, |
| 14741 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14742 | 3, |
| 14743 | kNoSSL, |
| 14744 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14745 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14746 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14747 | {__LINE__, |
| 14748 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14749 | AUTH_ASYNC, |
| 14750 | OK, |
| 14751 | kServer, |
| 14752 | AUTH_SYNC, |
| 14753 | OK, |
| 14754 | 3, |
| 14755 | kNoSSL, |
| 14756 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14757 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14758 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14759 | {__LINE__, |
| 14760 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14761 | AUTH_ASYNC, |
| 14762 | OK, |
| 14763 | kServer, |
| 14764 | AUTH_SYNC, |
| 14765 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14766 | 3, |
| 14767 | kNoSSL, |
| 14768 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14769 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14770 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14771 | {__LINE__, |
| 14772 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14773 | AUTH_SYNC, |
| 14774 | OK, |
| 14775 | kServer, |
| 14776 | AUTH_ASYNC, |
| 14777 | OK, |
| 14778 | 3, |
| 14779 | kNoSSL, |
| 14780 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14781 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14782 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14783 | {__LINE__, |
| 14784 | kProxy, |
| 14785 | AUTH_SYNC, |
| 14786 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14787 | kServer, |
| 14788 | AUTH_ASYNC, |
| 14789 | OK, |
| 14790 | 4, |
| 14791 | kNoSSL, |
| 14792 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14793 | TestRound(kGetProxy, kProxyChallenge, OK), |
| 14794 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14795 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 14796 | {__LINE__, |
| 14797 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14798 | AUTH_SYNC, |
| 14799 | OK, |
| 14800 | kServer, |
| 14801 | AUTH_ASYNC, |
| 14802 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14803 | 3, |
| 14804 | kNoSSL, |
| 14805 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14806 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14807 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14808 | {__LINE__, |
| 14809 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14810 | AUTH_ASYNC, |
| 14811 | OK, |
| 14812 | kServer, |
| 14813 | AUTH_ASYNC, |
| 14814 | OK, |
| 14815 | 3, |
| 14816 | kNoSSL, |
| 14817 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14818 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14819 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14820 | {__LINE__, |
| 14821 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14822 | AUTH_ASYNC, |
| 14823 | OK, |
| 14824 | kServer, |
| 14825 | AUTH_ASYNC, |
| 14826 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14827 | 3, |
| 14828 | kNoSSL, |
| 14829 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14830 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14831 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14832 | {__LINE__, |
| 14833 | kProxy, |
| 14834 | AUTH_ASYNC, |
| 14835 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14836 | kServer, |
| 14837 | AUTH_ASYNC, |
| 14838 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14839 | 4, |
| 14840 | kNoSSL, |
| 14841 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14842 | TestRound(kGetProxy, kProxyChallenge, OK), |
| 14843 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14844 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14845 | // Non-authenticating HTTPS server with a direct connection. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14846 | {__LINE__, |
| 14847 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14848 | AUTH_NONE, |
| 14849 | OK, |
| 14850 | kSecureServer, |
| 14851 | AUTH_NONE, |
| 14852 | OK, |
| 14853 | 1, |
| 14854 | 0, |
| 14855 | {TestRound(kGet, kSuccess, OK)}}, |
| 14856 | // Authenticating HTTPS server with a direct connection. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14857 | {__LINE__, |
| 14858 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14859 | AUTH_NONE, |
| 14860 | OK, |
| 14861 | kSecureServer, |
| 14862 | AUTH_SYNC, |
| 14863 | OK, |
| 14864 | 2, |
| 14865 | 0, |
| 14866 | {TestRound(kGet, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14867 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14868 | {__LINE__, |
| 14869 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14870 | AUTH_NONE, |
| 14871 | OK, |
| 14872 | kSecureServer, |
| 14873 | AUTH_SYNC, |
| 14874 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14875 | 2, |
| 14876 | 0, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14877 | {TestRound(kGet, kServerChallenge, OK), TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14878 | {__LINE__, |
| 14879 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14880 | AUTH_NONE, |
| 14881 | OK, |
| 14882 | kSecureServer, |
| 14883 | AUTH_ASYNC, |
| 14884 | OK, |
| 14885 | 2, |
| 14886 | 0, |
| 14887 | {TestRound(kGet, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14888 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14889 | {__LINE__, |
| 14890 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14891 | AUTH_NONE, |
| 14892 | OK, |
| 14893 | kSecureServer, |
| 14894 | AUTH_ASYNC, |
| 14895 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14896 | 2, |
| 14897 | 0, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14898 | {TestRound(kGet, kServerChallenge, OK), TestRound(kGet, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14899 | // Non-authenticating HTTPS server with a non-authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14900 | {__LINE__, |
| 14901 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14902 | AUTH_NONE, |
| 14903 | OK, |
| 14904 | kSecureServer, |
| 14905 | AUTH_NONE, |
| 14906 | OK, |
| 14907 | 1, |
| 14908 | 0, |
| 14909 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 14910 | // Authenticating HTTPS server through a non-authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14911 | {__LINE__, |
| 14912 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14913 | AUTH_NONE, |
| 14914 | OK, |
| 14915 | kSecureServer, |
| 14916 | AUTH_SYNC, |
| 14917 | OK, |
| 14918 | 2, |
| 14919 | 0, |
| 14920 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14921 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14922 | {__LINE__, |
| 14923 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14924 | AUTH_NONE, |
| 14925 | OK, |
| 14926 | kSecureServer, |
| 14927 | AUTH_SYNC, |
| 14928 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14929 | 2, |
| 14930 | 0, |
| 14931 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14932 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14933 | {__LINE__, |
| 14934 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14935 | AUTH_NONE, |
| 14936 | OK, |
| 14937 | kSecureServer, |
| 14938 | AUTH_ASYNC, |
| 14939 | OK, |
| 14940 | 2, |
| 14941 | 0, |
| 14942 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14943 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14944 | {__LINE__, |
| 14945 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14946 | AUTH_NONE, |
| 14947 | OK, |
| 14948 | kSecureServer, |
| 14949 | AUTH_ASYNC, |
| 14950 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14951 | 2, |
| 14952 | 0, |
| 14953 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14954 | TestRound(kGet, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14955 | // Non-Authenticating HTTPS server through an authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14956 | {__LINE__, |
| 14957 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14958 | AUTH_SYNC, |
| 14959 | OK, |
| 14960 | kSecureServer, |
| 14961 | AUTH_NONE, |
| 14962 | OK, |
| 14963 | 2, |
| 14964 | 1, |
| 14965 | {TestRound(kConnect, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14966 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14967 | {__LINE__, |
| 14968 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14969 | AUTH_SYNC, |
| 14970 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14971 | kSecureServer, |
| 14972 | AUTH_NONE, |
| 14973 | OK, |
| 14974 | 2, |
| 14975 | kNoSSL, |
| 14976 | {TestRound(kConnect, kProxyChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14977 | TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14978 | {__LINE__, |
| 14979 | kProxy, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14980 | AUTH_SYNC, |
| 14981 | ERR_UNSUPPORTED_AUTH_SCHEME, |
| 14982 | kSecureServer, |
| 14983 | AUTH_NONE, |
| 14984 | OK, |
| 14985 | 2, |
| 14986 | kNoSSL, |
| 14987 | {TestRound(kConnect, kProxyChallenge, OK), |
| 14988 | TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14989 | {__LINE__, |
| 14990 | kProxy, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14991 | AUTH_SYNC, |
| 14992 | ERR_UNEXPECTED, |
| 14993 | kSecureServer, |
| 14994 | AUTH_NONE, |
| 14995 | OK, |
| 14996 | 2, |
| 14997 | kNoSSL, |
| 14998 | {TestRound(kConnect, kProxyChallenge, OK), |
| 14999 | TestRound(kConnect, kProxyConnected, ERR_UNEXPECTED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15000 | {__LINE__, |
| 15001 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15002 | AUTH_ASYNC, |
| 15003 | OK, |
| 15004 | kSecureServer, |
| 15005 | AUTH_NONE, |
| 15006 | OK, |
| 15007 | 2, |
| 15008 | 1, |
| 15009 | {TestRound(kConnect, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15010 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15011 | {__LINE__, |
| 15012 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15013 | AUTH_ASYNC, |
| 15014 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15015 | kSecureServer, |
| 15016 | AUTH_NONE, |
| 15017 | OK, |
| 15018 | 2, |
| 15019 | kNoSSL, |
| 15020 | {TestRound(kConnect, kProxyChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15021 | TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15022 | // Authenticating HTTPS server through an 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_SYNC, |
| 15026 | OK, |
| 15027 | kSecureServer, |
| 15028 | AUTH_SYNC, |
| 15029 | OK, |
| 15030 | 3, |
| 15031 | 1, |
| 15032 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15033 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15034 | &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15035 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15036 | {__LINE__, |
| 15037 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15038 | AUTH_SYNC, |
| 15039 | OK, |
| 15040 | kSecureServer, |
| 15041 | AUTH_SYNC, |
| 15042 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15043 | 3, |
| 15044 | 1, |
| 15045 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15046 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15047 | &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15048 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15049 | {__LINE__, |
| 15050 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15051 | AUTH_ASYNC, |
| 15052 | OK, |
| 15053 | kSecureServer, |
| 15054 | AUTH_SYNC, |
| 15055 | OK, |
| 15056 | 3, |
| 15057 | 1, |
| 15058 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15059 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15060 | &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15061 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15062 | {__LINE__, |
| 15063 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15064 | AUTH_ASYNC, |
| 15065 | OK, |
| 15066 | kSecureServer, |
| 15067 | AUTH_SYNC, |
| 15068 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15069 | 3, |
| 15070 | 1, |
| 15071 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15072 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15073 | &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15074 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15075 | {__LINE__, |
| 15076 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15077 | AUTH_SYNC, |
| 15078 | OK, |
| 15079 | kSecureServer, |
| 15080 | AUTH_ASYNC, |
| 15081 | OK, |
| 15082 | 3, |
| 15083 | 1, |
| 15084 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15085 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15086 | &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15087 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15088 | {__LINE__, |
| 15089 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15090 | AUTH_SYNC, |
| 15091 | OK, |
| 15092 | kSecureServer, |
| 15093 | AUTH_ASYNC, |
| 15094 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15095 | 3, |
| 15096 | 1, |
| 15097 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15098 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15099 | &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15100 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15101 | {__LINE__, |
| 15102 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15103 | AUTH_ASYNC, |
| 15104 | OK, |
| 15105 | kSecureServer, |
| 15106 | AUTH_ASYNC, |
| 15107 | OK, |
| 15108 | 3, |
| 15109 | 1, |
| 15110 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15111 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15112 | &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15113 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15114 | {__LINE__, |
| 15115 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15116 | AUTH_ASYNC, |
| 15117 | OK, |
| 15118 | kSecureServer, |
| 15119 | AUTH_ASYNC, |
| 15120 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15121 | 3, |
| 15122 | 1, |
| 15123 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15124 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15125 | &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15126 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15127 | {__LINE__, |
| 15128 | kProxy, |
| 15129 | AUTH_ASYNC, |
| 15130 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15131 | kSecureServer, |
| 15132 | AUTH_ASYNC, |
| 15133 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15134 | 4, |
| 15135 | 2, |
| 15136 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15137 | TestRound(kConnect, kProxyChallenge, OK), |
| 15138 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15139 | &kServerChallenge), |
| 15140 | TestRound(kGet, kSuccess, OK)}}, |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15141 | }; |
| 15142 | |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15143 | for (const auto& test_config : test_configs) { |
| 15144 | SCOPED_TRACE(::testing::Message() << "Test config at " |
| 15145 | << test_config.line_number); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15146 | HttpAuthHandlerMock::Factory* auth_factory( |
| 15147 | new HttpAuthHandlerMock::Factory()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15148 | session_deps_.http_auth_handler_factory.reset(auth_factory); |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 15149 | SSLInfo empty_ssl_info; |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 15150 | |
| 15151 | // Set up authentication handlers as necessary. |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15152 | if (test_config.proxy_auth_timing != AUTH_NONE) { |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15153 | for (int n = 0; n < 3; n++) { |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15154 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 15155 | std::string auth_challenge = "Mock realm=proxy"; |
| 15156 | GURL origin(test_config.proxy_url); |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 15157 | HttpAuthChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 15158 | auth_challenge.end()); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15159 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_PROXY, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15160 | empty_ssl_info, origin, |
| 15161 | NetLogWithSource()); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15162 | auth_handler->SetGenerateExpectation( |
| 15163 | test_config.proxy_auth_timing == AUTH_ASYNC, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15164 | n == 0 ? test_config.first_generate_proxy_token_rv : OK); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15165 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
| 15166 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15167 | } |
| 15168 | if (test_config.server_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 15169 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15170 | std::string auth_challenge = "Mock realm=server"; |
| 15171 | GURL origin(test_config.server_url); |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 15172 | HttpAuthChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 15173 | auth_challenge.end()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15174 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15175 | empty_ssl_info, origin, |
| 15176 | NetLogWithSource()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15177 | auth_handler->SetGenerateExpectation( |
| 15178 | test_config.server_auth_timing == AUTH_ASYNC, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15179 | test_config.first_generate_server_token_rv); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15180 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15181 | |
| 15182 | // The second handler always succeeds. It should only be used where there |
| 15183 | // are multiple auth sessions for server auth in the same network |
| 15184 | // transaction using the same auth scheme. |
| 15185 | std::unique_ptr<HttpAuthHandlerMock> second_handler = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 15186 | std::make_unique<HttpAuthHandlerMock>(); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15187 | second_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 15188 | empty_ssl_info, origin, |
| 15189 | NetLogWithSource()); |
| 15190 | second_handler->SetGenerateExpectation(true, OK); |
| 15191 | auth_factory->AddMockHandler(second_handler.release(), |
| 15192 | HttpAuth::AUTH_SERVER); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15193 | } |
| 15194 | if (test_config.proxy_url) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 15195 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 15196 | ProxyResolutionService::CreateFixed(test_config.proxy_url, |
| 15197 | TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15198 | } else { |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 15199 | session_deps_.proxy_resolution_service = |
| 15200 | ProxyResolutionService::CreateDirect(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15201 | } |
| 15202 | |
| 15203 | HttpRequestInfo request; |
| 15204 | request.method = "GET"; |
| 15205 | request.url = GURL(test_config.server_url); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15206 | request.traffic_annotation = |
| 15207 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15208 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15209 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15210 | |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15211 | SSLSocketDataProvider ssl_socket_data_provider(SYNCHRONOUS, OK); |
| 15212 | |
| 15213 | std::vector<std::vector<MockRead>> mock_reads(1); |
| 15214 | std::vector<std::vector<MockWrite>> mock_writes(1); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15215 | for (int round = 0; round < test_config.num_auth_rounds; ++round) { |
davidben | 8c7089a | 2017-04-17 20:38:22 | [diff] [blame] | 15216 | SCOPED_TRACE(round); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15217 | const TestRound& read_write_round = test_config.rounds[round]; |
| 15218 | |
| 15219 | // Set up expected reads and writes. |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15220 | mock_reads.back().push_back(read_write_round.read); |
| 15221 | mock_writes.back().push_back(read_write_round.write); |
| 15222 | |
| 15223 | // kProxyChallenge uses Proxy-Connection: close which means that the |
| 15224 | // socket is closed and a new one will be created for the next request. |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 15225 | if (read_write_round.read.data == kProxyChallenge.data) { |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15226 | mock_reads.push_back(std::vector<MockRead>()); |
| 15227 | mock_writes.push_back(std::vector<MockWrite>()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15228 | } |
| 15229 | |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15230 | if (read_write_round.extra_read) { |
| 15231 | mock_reads.back().push_back(*read_write_round.extra_read); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15232 | } |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15233 | if (read_write_round.extra_write) { |
| 15234 | mock_writes.back().push_back(*read_write_round.extra_write); |
| 15235 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15236 | |
| 15237 | // Add an SSL sequence if necessary. |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15238 | if (round >= test_config.first_ssl_round) |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15239 | session_deps_.socket_factory->AddSSLSocketDataProvider( |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15240 | &ssl_socket_data_provider); |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15241 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15242 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15243 | std::vector<std::unique_ptr<StaticSocketDataProvider>> data_providers; |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15244 | for (size_t i = 0; i < mock_reads.size(); ++i) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 15245 | data_providers.push_back(std::make_unique<StaticSocketDataProvider>( |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15246 | mock_reads[i], mock_writes[i])); |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15247 | session_deps_.socket_factory->AddSocketDataProvider( |
olli.raula | 525048c | 2015-12-10 07:38:32 | [diff] [blame] | 15248 | data_providers.back().get()); |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15249 | } |
| 15250 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 15251 | // Transaction must be created after DataProviders, so it's destroyed before |
| 15252 | // they are as well. |
| 15253 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 15254 | |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15255 | for (int round = 0; round < test_config.num_auth_rounds; ++round) { |
davidben | 8c7089a | 2017-04-17 20:38:22 | [diff] [blame] | 15256 | SCOPED_TRACE(round); |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15257 | const TestRound& read_write_round = test_config.rounds[round]; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15258 | // Start or restart the transaction. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15259 | TestCompletionCallback callback; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15260 | int rv; |
| 15261 | if (round == 0) { |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15262 | rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15263 | } else { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15264 | rv = trans.RestartWithAuth( |
| 15265 | AuthCredentials(kFoo, kBar), callback.callback()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15266 | } |
| 15267 | if (rv == ERR_IO_PENDING) |
| 15268 | rv = callback.WaitForResult(); |
| 15269 | |
| 15270 | // Compare results with expected data. |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15271 | EXPECT_THAT(rv, IsError(read_write_round.expected_rv)); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 15272 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 15273 | if (read_write_round.expected_rv != OK) { |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15274 | EXPECT_EQ(round + 1, test_config.num_auth_rounds); |
| 15275 | continue; |
| 15276 | } |
| 15277 | if (round + 1 < test_config.num_auth_rounds) { |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15278 | EXPECT_TRUE(response->auth_challenge.has_value()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15279 | } else { |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15280 | EXPECT_FALSE(response->auth_challenge.has_value()); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15281 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15282 | } |
| 15283 | } |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 15284 | } |
| 15285 | } |
| 15286 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15287 | TEST_F(HttpNetworkTransactionTest, MultiRoundAuth) { |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15288 | // Do multi-round authentication and make sure it works correctly. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15289 | HttpAuthHandlerMock::Factory* auth_factory( |
| 15290 | new HttpAuthHandlerMock::Factory()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15291 | session_deps_.http_auth_handler_factory.reset(auth_factory); |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 15292 | session_deps_.proxy_resolution_service = |
| 15293 | ProxyResolutionService::CreateDirect(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15294 | session_deps_.host_resolver->rules()->AddRule("www.example.com", "10.0.0.1"); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15295 | |
| 15296 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 15297 | auth_handler->set_connection_based(true); |
| 15298 | std::string auth_challenge = "Mock realm=server"; |
| 15299 | GURL origin("http://www.example.com"); |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 15300 | HttpAuthChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 15301 | auth_challenge.end()); |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 15302 | SSLInfo empty_ssl_info; |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15303 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15304 | empty_ssl_info, origin, NetLogWithSource()); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15305 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15306 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15307 | int rv = OK; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 15308 | const HttpResponseInfo* response = nullptr; |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15309 | HttpRequestInfo request; |
| 15310 | request.method = "GET"; |
| 15311 | request.url = origin; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15312 | request.traffic_annotation = |
| 15313 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 15314 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15315 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15316 | |
| 15317 | // Use a TCP Socket Pool with only one connection per group. This is used |
| 15318 | // to validate that the TCP socket is not released to the pool between |
| 15319 | // each round of multi-round authentication. |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 15320 | HttpNetworkSessionPeer session_peer(session.get()); |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 15321 | CommonConnectJobParams common_connect_job_params( |
| 15322 | session->CreateCommonConnectJobParams()); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 15323 | TransportClientSocketPool* transport_pool = new TransportClientSocketPool( |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 15324 | 50, // Max sockets for pool |
| 15325 | 1, // Max sockets per group |
| 15326 | base::TimeDelta::FromSeconds(10), // unused_idle_socket_timeout |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 15327 | ProxyServer::Direct(), false, // is_for_websockets |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 15328 | &common_connect_job_params); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 15329 | auto mock_pool_manager = std::make_unique<MockClientSocketPoolManager>(); |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 15330 | mock_pool_manager->SetSocketPool(ProxyServer::Direct(), |
| 15331 | base::WrapUnique(transport_pool)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 15332 | session_peer.SetClientSocketPoolManager(std::move(mock_pool_manager)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15333 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15334 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15335 | TestCompletionCallback callback; |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15336 | |
| 15337 | const MockWrite kGet( |
| 15338 | "GET / HTTP/1.1\r\n" |
| 15339 | "Host: www.example.com\r\n" |
| 15340 | "Connection: keep-alive\r\n\r\n"); |
| 15341 | const MockWrite kGetAuth( |
| 15342 | "GET / HTTP/1.1\r\n" |
| 15343 | "Host: www.example.com\r\n" |
| 15344 | "Connection: keep-alive\r\n" |
| 15345 | "Authorization: auth_token\r\n\r\n"); |
| 15346 | |
| 15347 | const MockRead kServerChallenge( |
| 15348 | "HTTP/1.1 401 Unauthorized\r\n" |
| 15349 | "WWW-Authenticate: Mock realm=server\r\n" |
| 15350 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 15351 | "Content-Length: 14\r\n\r\n" |
| 15352 | "Unauthorized\r\n"); |
| 15353 | const MockRead kSuccess( |
| 15354 | "HTTP/1.1 200 OK\r\n" |
| 15355 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 15356 | "Content-Length: 3\r\n\r\n" |
| 15357 | "Yes"); |
| 15358 | |
| 15359 | MockWrite writes[] = { |
| 15360 | // First round |
| 15361 | kGet, |
| 15362 | // Second round |
| 15363 | kGetAuth, |
| 15364 | // Third round |
| 15365 | kGetAuth, |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15366 | // Fourth round |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15367 | kGetAuth, |
| 15368 | // Competing request |
| 15369 | kGet, |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15370 | }; |
| 15371 | MockRead reads[] = { |
| 15372 | // First round |
| 15373 | kServerChallenge, |
| 15374 | // Second round |
| 15375 | kServerChallenge, |
| 15376 | // Third round |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15377 | kServerChallenge, |
| 15378 | // Fourth round |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15379 | kSuccess, |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15380 | // Competing response |
| 15381 | kSuccess, |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15382 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15383 | StaticSocketDataProvider data_provider(reads, writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15384 | session_deps_.socket_factory->AddSocketDataProvider(&data_provider); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15385 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 15386 | const ClientSocketPool::GroupId kSocketGroup( |
| 15387 | HostPortPair("www.example.com", 80), ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 15388 | PrivacyMode::PRIVACY_MODE_DISABLED, NetworkIsolationKey(), |
| 15389 | false /* disable_secure_dns */); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15390 | |
| 15391 | // First round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15392 | auth_handler->SetGenerateExpectation(false, OK); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15393 | rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15394 | if (rv == ERR_IO_PENDING) |
| 15395 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15396 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15397 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15398 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15399 | EXPECT_TRUE(response->auth_challenge.has_value()); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15400 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15401 | EXPECT_EQ(HttpAuthHandlerMock::State::WAIT_FOR_GENERATE_AUTH_TOKEN, |
| 15402 | auth_handler->state()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15403 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15404 | // In between rounds, another request comes in for the same domain. |
| 15405 | // It should not be able to grab the TCP socket that trans has already |
| 15406 | // claimed. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15407 | HttpNetworkTransaction trans_compete(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15408 | TestCompletionCallback callback_compete; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15409 | rv = trans_compete.Start(&request, callback_compete.callback(), |
| 15410 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15411 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15412 | // callback_compete.WaitForResult at this point would stall forever, |
| 15413 | // since the HttpNetworkTransaction does not release the request back to |
| 15414 | // the pool until after authentication completes. |
| 15415 | |
| 15416 | // Second round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15417 | auth_handler->SetGenerateExpectation(false, OK); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15418 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15419 | if (rv == ERR_IO_PENDING) |
| 15420 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15421 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15422 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15423 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15424 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15425 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15426 | EXPECT_EQ(HttpAuthHandlerMock::State::WAIT_FOR_GENERATE_AUTH_TOKEN, |
| 15427 | auth_handler->state()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15428 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15429 | // Third round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15430 | auth_handler->SetGenerateExpectation(false, OK); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15431 | rv = trans.RestartWithAuth(AuthCredentials(), callback.callback()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15432 | if (rv == ERR_IO_PENDING) |
| 15433 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15434 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15435 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15436 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15437 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15438 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15439 | EXPECT_EQ(HttpAuthHandlerMock::State::WAIT_FOR_GENERATE_AUTH_TOKEN, |
| 15440 | auth_handler->state()); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15441 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15442 | // Fourth round of authentication, which completes successfully. |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15443 | auth_handler->SetGenerateExpectation(false, OK); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15444 | rv = trans.RestartWithAuth(AuthCredentials(), callback.callback()); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15445 | if (rv == ERR_IO_PENDING) |
| 15446 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15447 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15448 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15449 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15450 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15451 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15452 | |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15453 | // In WAIT_FOR_CHALLENGE, although in reality the auth handler is done. A real |
| 15454 | // auth handler should transition to a DONE state in concert with the remote |
| 15455 | // server. But that's not something we can test here with a mock handler. |
| 15456 | EXPECT_EQ(HttpAuthHandlerMock::State::WAIT_FOR_CHALLENGE, |
| 15457 | auth_handler->state()); |
| 15458 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15459 | // Read the body since the fourth round was successful. This will also |
| 15460 | // release the socket back to the pool. |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 15461 | scoped_refptr<IOBufferWithSize> io_buf = |
| 15462 | base::MakeRefCounted<IOBufferWithSize>(50); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15463 | rv = trans.Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15464 | if (rv == ERR_IO_PENDING) |
| 15465 | rv = callback.WaitForResult(); |
| 15466 | EXPECT_EQ(3, rv); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15467 | rv = trans.Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15468 | EXPECT_EQ(0, rv); |
| 15469 | // There are still 0 idle sockets, since the trans_compete transaction |
| 15470 | // will be handed it immediately after trans releases it to the group. |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15471 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15472 | |
| 15473 | // The competing request can now finish. Wait for the headers and then |
| 15474 | // read the body. |
| 15475 | rv = callback_compete.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15476 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15477 | rv = trans_compete.Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15478 | if (rv == ERR_IO_PENDING) |
| 15479 | rv = callback.WaitForResult(); |
| 15480 | EXPECT_EQ(3, rv); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15481 | rv = trans_compete.Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15482 | EXPECT_EQ(0, rv); |
| 15483 | |
| 15484 | // Finally, the socket is released to the group. |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15485 | EXPECT_EQ(1u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15486 | } |
| 15487 | |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15488 | // This tests the case that a request is issued via http instead of spdy after |
| 15489 | // npn is negotiated. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15490 | TEST_F(HttpNetworkTransactionTest, NpnWithHttpOverSSL) { |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15491 | HttpRequestInfo request; |
| 15492 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15493 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15494 | request.traffic_annotation = |
| 15495 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15496 | |
| 15497 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15498 | MockWrite( |
| 15499 | "GET / HTTP/1.1\r\n" |
| 15500 | "Host: www.example.org\r\n" |
| 15501 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15502 | }; |
| 15503 | |
| 15504 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 15505 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 15506 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 15507 | MockRead("\r\n"), |
| 15508 | MockRead("hello world"), |
| 15509 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15510 | }; |
| 15511 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15512 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 15513 | ssl.next_proto = kProtoHTTP11; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15514 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15515 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15516 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15517 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15518 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15519 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15520 | TestCompletionCallback callback; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15521 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15522 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15523 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15524 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15525 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15526 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15527 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 15528 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15529 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15530 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15531 | ASSERT_TRUE(response); |
| 15532 | ASSERT_TRUE(response->headers); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15533 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 15534 | |
| 15535 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15536 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15537 | EXPECT_EQ("hello world", response_data); |
| 15538 | |
| 15539 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 15540 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15541 | } |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15542 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 15543 | // Simulate the SSL handshake completing with an NPN negotiation followed by an |
| 15544 | // immediate server closing of the socket. |
| 15545 | // Regression test for https://crbug.com/46369. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15546 | TEST_F(HttpNetworkTransactionTest, SpdyPostNPNServerHangup) { |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15547 | HttpRequestInfo request; |
| 15548 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15549 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15550 | request.traffic_annotation = |
| 15551 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15552 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15553 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 15554 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15555 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15556 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 15557 | spdy::SpdySerializedFrame req( |
| 15558 | spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 15559 | MockWrite spdy_writes[] = {CreateMockWrite(req, 1)}; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15560 | |
| 15561 | MockRead spdy_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15562 | MockRead(SYNCHRONOUS, 0, 0) // Not async - return 0 immediately. |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15563 | }; |
| 15564 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15565 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15566 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15567 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15568 | TestCompletionCallback callback; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15569 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15570 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15571 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15572 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15573 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15574 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 15575 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_CLOSED)); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15576 | } |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 15577 | |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 15578 | // A subclass of HttpAuthHandlerMock that records the request URL when |
| 15579 | // it gets it. This is needed since the auth handler may get destroyed |
| 15580 | // before we get a chance to query it. |
| 15581 | class UrlRecordingHttpAuthHandlerMock : public HttpAuthHandlerMock { |
| 15582 | public: |
| 15583 | explicit UrlRecordingHttpAuthHandlerMock(GURL* url) : url_(url) {} |
| 15584 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 15585 | ~UrlRecordingHttpAuthHandlerMock() override = default; |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 15586 | |
| 15587 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 15588 | int GenerateAuthTokenImpl(const AuthCredentials* credentials, |
| 15589 | const HttpRequestInfo* request, |
Bence Béky | 7236fb7 | 2018-08-01 14:35:09 | [diff] [blame] | 15590 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 15591 | std::string* auth_token) override { |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 15592 | *url_ = request->url; |
| 15593 | return HttpAuthHandlerMock::GenerateAuthTokenImpl( |
Bence Béky | 7236fb7 | 2018-08-01 14:35:09 | [diff] [blame] | 15594 | credentials, request, std::move(callback), auth_token); |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 15595 | } |
| 15596 | |
| 15597 | private: |
| 15598 | GURL* url_; |
| 15599 | }; |
| 15600 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15601 | // Test that if we cancel the transaction as the connection is completing, that |
| 15602 | // everything tears down correctly. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15603 | TEST_F(HttpNetworkTransactionTest, SimpleCancel) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15604 | // Setup everything about the connection to complete synchronously, so that |
| 15605 | // after calling HttpNetworkTransaction::Start, the only thing we're waiting |
| 15606 | // for is the callback from the HttpStreamRequest. |
| 15607 | // Then cancel the transaction. |
| 15608 | // Verify that we don't crash. |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 15609 | MockConnect mock_connect(SYNCHRONOUS, OK); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15610 | MockRead data_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15611 | MockRead(SYNCHRONOUS, "HTTP/1.0 200 OK\r\n\r\n"), |
| 15612 | MockRead(SYNCHRONOUS, "hello world"), |
| 15613 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15614 | }; |
| 15615 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15616 | HttpRequestInfo request; |
| 15617 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15618 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15619 | request.traffic_annotation = |
| 15620 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15621 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15622 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 15623 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 15624 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 15625 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15626 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15627 | data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15628 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15629 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15630 | TestCompletionCallback callback; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15631 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 15632 | BoundTestNetLog log; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15633 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15634 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15635 | trans.reset(); // Cancel the transaction here. |
| 15636 | |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 15637 | base::RunLoop().RunUntilIdle(); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 15638 | } |
| 15639 | |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15640 | // Test that if a transaction is cancelled after receiving the headers, the |
| 15641 | // stream is drained properly and added back to the socket pool. The main |
| 15642 | // purpose of this test is to make sure that an HttpStreamParser can be read |
| 15643 | // from after the HttpNetworkTransaction and the objects it owns have been |
| 15644 | // deleted. |
| 15645 | // See http://crbug.com/368418 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15646 | TEST_F(HttpNetworkTransactionTest, CancelAfterHeaders) { |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15647 | MockRead data_reads[] = { |
| 15648 | MockRead(ASYNC, "HTTP/1.1 200 OK\r\n"), |
| 15649 | MockRead(ASYNC, "Content-Length: 2\r\n"), |
| 15650 | MockRead(ASYNC, "Connection: Keep-Alive\r\n\r\n"), |
| 15651 | MockRead(ASYNC, "1"), |
| 15652 | // 2 async reads are necessary to trigger a ReadResponseBody call after the |
| 15653 | // HttpNetworkTransaction has been deleted. |
| 15654 | MockRead(ASYNC, "2"), |
| 15655 | MockRead(SYNCHRONOUS, ERR_IO_PENDING), // Should never read this. |
| 15656 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15657 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15658 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 15659 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15660 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15661 | |
| 15662 | { |
| 15663 | HttpRequestInfo request; |
| 15664 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15665 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15666 | request.traffic_annotation = |
| 15667 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15668 | |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 15669 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15670 | TestCompletionCallback callback; |
| 15671 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15672 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15673 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15674 | callback.WaitForResult(); |
| 15675 | |
| 15676 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15677 | ASSERT_TRUE(response); |
| 15678 | EXPECT_TRUE(response->headers); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15679 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 15680 | |
| 15681 | // The transaction and HttpRequestInfo are deleted. |
| 15682 | } |
| 15683 | |
| 15684 | // Let the HttpResponseBodyDrainer drain the socket. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 15685 | base::RunLoop().RunUntilIdle(); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15686 | |
| 15687 | // Socket should now be idle, waiting to be reused. |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 15688 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15689 | } |
| 15690 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15691 | // Test a basic GET request through a proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15692 | TEST_F(HttpNetworkTransactionTest, ProxyGet) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 15693 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 15694 | ProxyResolutionService::CreateFixedFromPacResult( |
| 15695 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 15696 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15697 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15698 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15699 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15700 | HttpRequestInfo request; |
| 15701 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15702 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15703 | request.traffic_annotation = |
| 15704 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15705 | |
| 15706 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15707 | MockWrite( |
| 15708 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 15709 | "Host: www.example.org\r\n" |
| 15710 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15711 | }; |
| 15712 | |
| 15713 | MockRead data_reads1[] = { |
| 15714 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 15715 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 15716 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15717 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15718 | }; |
| 15719 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15720 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15721 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15722 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15723 | TestCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15724 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15725 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 15726 | BeforeHeadersSentHandler headers_handler; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15727 | trans.SetBeforeHeadersSentCallback( |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 15728 | base::Bind(&BeforeHeadersSentHandler::OnBeforeHeadersSent, |
| 15729 | base::Unretained(&headers_handler))); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 15730 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15731 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15732 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15733 | |
| 15734 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15735 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15736 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15737 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15738 | ASSERT_TRUE(response); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15739 | |
| 15740 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 15741 | EXPECT_EQ(200, response->headers->response_code()); |
| 15742 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 15743 | EXPECT_TRUE(response->was_fetched_via_proxy); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 15744 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 15745 | HostPortPair::FromString("myproxy:70")), |
| 15746 | response->proxy_server); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 15747 | EXPECT_TRUE(headers_handler.observed_before_headers_sent()); |
| 15748 | EXPECT_TRUE(headers_handler.observed_before_headers_sent_with_proxy()); |
| 15749 | EXPECT_EQ("myproxy:70", headers_handler.observed_proxy_server_uri()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15750 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 15751 | |
| 15752 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15753 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 15754 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 15755 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15756 | } |
| 15757 | |
| 15758 | // Test a basic HTTPS GET request through a proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15759 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGet) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 15760 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 15761 | ProxyResolutionService::CreateFixedFromPacResult( |
| 15762 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 15763 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15764 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15765 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15766 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15767 | HttpRequestInfo request; |
| 15768 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15769 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15770 | request.traffic_annotation = |
| 15771 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15772 | |
| 15773 | // Since we have proxy, should try to establish tunnel. |
| 15774 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15775 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 15776 | "Host: www.example.org:443\r\n" |
| 15777 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15778 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15779 | MockWrite("GET / HTTP/1.1\r\n" |
| 15780 | "Host: www.example.org\r\n" |
| 15781 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15782 | }; |
| 15783 | |
| 15784 | MockRead data_reads1[] = { |
| 15785 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 15786 | |
| 15787 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 15788 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 15789 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15790 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15791 | }; |
| 15792 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15793 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15794 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15795 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15796 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15797 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15798 | TestCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15799 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15800 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 15801 | BeforeHeadersSentHandler headers_handler; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15802 | trans.SetBeforeHeadersSentCallback( |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 15803 | base::Bind(&BeforeHeadersSentHandler::OnBeforeHeadersSent, |
| 15804 | base::Unretained(&headers_handler))); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 15805 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15806 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15807 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15808 | |
| 15809 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15810 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 15811 | auto entries = log.GetEntries(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15812 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15813 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 15814 | NetLogEventPhase::NONE); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15815 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 15816 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15817 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 15818 | NetLogEventPhase::NONE); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15819 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15820 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15821 | ASSERT_TRUE(response); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15822 | |
| 15823 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 15824 | EXPECT_EQ(200, response->headers->response_code()); |
| 15825 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 15826 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 15827 | EXPECT_TRUE(response->was_fetched_via_proxy); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 15828 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 15829 | HostPortPair::FromString("myproxy:70")), |
| 15830 | response->proxy_server); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 15831 | EXPECT_TRUE(headers_handler.observed_before_headers_sent()); |
| 15832 | EXPECT_TRUE(headers_handler.observed_before_headers_sent_with_proxy()); |
| 15833 | EXPECT_EQ("myproxy:70", headers_handler.observed_proxy_server_uri()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 15834 | |
| 15835 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15836 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 15837 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 15838 | CONNECT_TIMING_HAS_SSL_TIMES); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15839 | } |
| 15840 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15841 | // Test a basic HTTPS GET request through a proxy, connecting to an IPv6 |
| 15842 | // literal host. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15843 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGetIPv6) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 15844 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 15845 | ProxyResolutionService::CreateFixedFromPacResult( |
| 15846 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15847 | BoundTestNetLog log; |
| 15848 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15849 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15850 | |
| 15851 | HttpRequestInfo request; |
| 15852 | request.method = "GET"; |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 15853 | request.url = GURL("https://[::2]:443/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15854 | request.traffic_annotation = |
| 15855 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15856 | |
| 15857 | // Since we have proxy, should try to establish tunnel. |
| 15858 | MockWrite data_writes1[] = { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 15859 | MockWrite("CONNECT [::2]:443 HTTP/1.1\r\n" |
| 15860 | "Host: [::2]:443\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15861 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 15862 | |
| 15863 | MockWrite("GET / HTTP/1.1\r\n" |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 15864 | "Host: [::2]\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15865 | "Connection: keep-alive\r\n\r\n"), |
| 15866 | }; |
| 15867 | |
| 15868 | MockRead data_reads1[] = { |
| 15869 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 15870 | |
| 15871 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 15872 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 15873 | MockRead("Content-Length: 100\r\n\r\n"), |
| 15874 | MockRead(SYNCHRONOUS, OK), |
| 15875 | }; |
| 15876 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15877 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15878 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 15879 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 15880 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 15881 | |
| 15882 | TestCompletionCallback callback1; |
| 15883 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15884 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15885 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15886 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15887 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15888 | |
| 15889 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15890 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 15891 | auto entries = log.GetEntries(); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15892 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15893 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 15894 | NetLogEventPhase::NONE); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15895 | ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15896 | entries, pos, |
| 15897 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 15898 | NetLogEventPhase::NONE); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15899 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15900 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15901 | ASSERT_TRUE(response); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15902 | |
| 15903 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 15904 | EXPECT_EQ(200, response->headers->response_code()); |
| 15905 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 15906 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 15907 | EXPECT_TRUE(response->was_fetched_via_proxy); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 15908 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 15909 | HostPortPair::FromString("myproxy:70")), |
| 15910 | response->proxy_server); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15911 | |
| 15912 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15913 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15914 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 15915 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 15916 | } |
| 15917 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15918 | // Test a basic HTTPS GET request through a proxy, but the server hangs up |
| 15919 | // while establishing the tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15920 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGetHangup) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 15921 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 15922 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 15923 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15924 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15925 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15926 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15927 | HttpRequestInfo request; |
| 15928 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15929 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 15930 | request.traffic_annotation = |
| 15931 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15932 | |
| 15933 | // Since we have proxy, should try to establish tunnel. |
| 15934 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15935 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 15936 | "Host: www.example.org:443\r\n" |
| 15937 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15938 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15939 | MockWrite("GET / HTTP/1.1\r\n" |
| 15940 | "Host: www.example.org\r\n" |
| 15941 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15942 | }; |
| 15943 | |
| 15944 | MockRead data_reads1[] = { |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15945 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15946 | MockRead(ASYNC, 0, 0), // EOF |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15947 | }; |
| 15948 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15949 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15950 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15951 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15952 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15953 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15954 | TestCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15955 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15956 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 15957 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15958 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15959 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15960 | |
| 15961 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15962 | EXPECT_THAT(rv, IsError(ERR_EMPTY_RESPONSE)); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 15963 | auto entries = log.GetEntries(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15964 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15965 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 15966 | NetLogEventPhase::NONE); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15967 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 15968 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15969 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 15970 | NetLogEventPhase::NONE); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15971 | } |
| 15972 | |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 15973 | // Test for crbug.com/55424. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15974 | TEST_F(HttpNetworkTransactionTest, PreconnectWithExistingSpdySession) { |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 15975 | spdy::SpdySerializedFrame req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 15976 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 15977 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 15978 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 15979 | spdy::SpdySerializedFrame resp( |
| 15980 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 15981 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 15982 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 15983 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 15984 | }; |
| 15985 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15986 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15987 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 15988 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15989 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 15990 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15991 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 15992 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15993 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 15994 | |
| 15995 | // Set up an initial SpdySession in the pool to reuse. |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15996 | HostPortPair host_port_pair("www.example.org", 443); |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 15997 | SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 15998 | PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 15999 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 16000 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 16001 | base::WeakPtr<SpdySession> spdy_session = |
Bence Béky | 0ef1556e | 2017-06-30 19:52:52 | [diff] [blame] | 16002 | CreateSpdySession(session.get(), key, NetLogWithSource()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16003 | |
| 16004 | HttpRequestInfo request; |
| 16005 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 16006 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16007 | request.traffic_annotation = |
| 16008 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16009 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16010 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16011 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 16012 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16013 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16014 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16015 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16016 | } |
| 16017 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16018 | // Given a net error, cause that error to be returned from the first Write() |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16019 | // call and verify that the HttpNetworkTransaction fails with that error. |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 16020 | void HttpNetworkTransactionTest::CheckErrorIsPassedBack( |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16021 | int error, IoMode mode) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16022 | HttpRequestInfo request_info; |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16023 | request_info.url = GURL("https://www.example.com/"); |
| 16024 | request_info.method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16025 | request_info.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16026 | request_info.traffic_annotation = |
| 16027 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16028 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 16029 | SSLSocketDataProvider ssl_data(mode, OK); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16030 | MockWrite data_writes[] = { |
| 16031 | MockWrite(mode, error), |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16032 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16033 | StaticSocketDataProvider data(base::span<MockRead>(), data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16034 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 16035 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data); |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16036 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16037 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16038 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16039 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 16040 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16041 | int rv = trans.Start(&request_info, callback.callback(), NetLogWithSource()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16042 | if (rv == ERR_IO_PENDING) |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16043 | rv = callback.WaitForResult(); |
| 16044 | ASSERT_EQ(error, rv); |
| 16045 | } |
| 16046 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16047 | TEST_F(HttpNetworkTransactionTest, SSLWriteCertError) { |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16048 | // Just check a grab bag of cert errors. |
| 16049 | static const int kErrors[] = { |
| 16050 | ERR_CERT_COMMON_NAME_INVALID, |
| 16051 | ERR_CERT_AUTHORITY_INVALID, |
| 16052 | ERR_CERT_DATE_INVALID, |
| 16053 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 16054 | for (size_t i = 0; i < base::size(kErrors); i++) { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 16055 | CheckErrorIsPassedBack(kErrors[i], ASYNC); |
| 16056 | CheckErrorIsPassedBack(kErrors[i], SYNCHRONOUS); |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16057 | } |
| 16058 | } |
| 16059 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16060 | // Ensure that a client certificate is removed from the SSL client auth |
| 16061 | // cache when: |
| 16062 | // 1) No proxy is involved. |
| 16063 | // 2) TLS False Start is disabled. |
| 16064 | // 3) The initial TLS handshake requests a client certificate. |
| 16065 | // 4) The client supplies an invalid/unacceptable certificate. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16066 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_NoFalseStart) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16067 | HttpRequestInfo request_info; |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16068 | request_info.url = GURL("https://www.example.com/"); |
| 16069 | request_info.method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16070 | request_info.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16071 | request_info.traffic_annotation = |
| 16072 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16073 | |
David Benjamin | 1c4b6d01 | 2019-07-08 17:12:57 | [diff] [blame] | 16074 | auto cert_request = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 16075 | cert_request->host_and_port = HostPortPair("www.example.com", 443); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16076 | |
| 16077 | // [ssl_]data1 contains the data for the first SSL handshake. When a |
| 16078 | // CertificateRequest is received for the first time, the handshake will |
| 16079 | // be aborted to allow the caller to provide a certificate. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16080 | SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16081 | ssl_data1.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16082 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16083 | StaticSocketDataProvider data1; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16084 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16085 | |
| 16086 | // [ssl_]data2 contains the data for the second SSL handshake. When TLS |
| 16087 | // False Start is not being used, the result of the SSL handshake will be |
| 16088 | // returned as part of the SSLClientSocket::Connect() call. This test |
| 16089 | // matches the result of a server sending a handshake_failure alert, |
| 16090 | // rather than a Finished message, because it requires a client |
| 16091 | // certificate and none was supplied. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16092 | SSLSocketDataProvider ssl_data2(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16093 | ssl_data2.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16094 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16095 | StaticSocketDataProvider data2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16096 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16097 | |
| 16098 | // [ssl_]data3 contains the data for the third SSL handshake. When a |
| 16099 | // connection to a server fails during an SSL handshake, |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 16100 | // HttpNetworkTransaction will attempt to fallback to TLSv1.2 if the previous |
| 16101 | // connection was attempted with TLSv1.3. This is transparent to the caller |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16102 | // of the HttpNetworkTransaction. Because this test failure is due to |
| 16103 | // requiring a client certificate, this fallback handshake should also |
| 16104 | // fail. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16105 | SSLSocketDataProvider ssl_data3(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 16106 | ssl_data3.expected_ssl_version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16107 | ssl_data3.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16108 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data3); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16109 | StaticSocketDataProvider data3; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16110 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16111 | |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16112 | // [ssl_]data4 contains the data for the fourth SSL handshake. When a |
| 16113 | // connection to a server fails during an SSL handshake, |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 16114 | // HttpNetworkTransaction will attempt to fallback to TLSv1 if the previous |
| 16115 | // connection was attempted with TLSv1.1. This is transparent to the caller |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16116 | // of the HttpNetworkTransaction. Because this test failure is due to |
| 16117 | // requiring a client certificate, this fallback handshake should also |
| 16118 | // fail. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16119 | SSLSocketDataProvider ssl_data4(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16120 | ssl_data4.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16121 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data4); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16122 | StaticSocketDataProvider data4; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16123 | session_deps_.socket_factory->AddSocketDataProvider(&data4); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16124 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16125 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16126 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16127 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16128 | // Begin the SSL handshake with the peer. This consumes ssl_data1. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 16129 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16130 | int rv = trans.Start(&request_info, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16131 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16132 | |
| 16133 | // Complete the SSL handshake, which should abort due to requiring a |
| 16134 | // client certificate. |
| 16135 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16136 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16137 | |
| 16138 | // Indicate that no certificate should be supplied. From the perspective |
| 16139 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 16140 | // certificate, so this is the same as supply a |
| 16141 | // legitimate-but-unacceptable certificate. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16142 | rv = trans.RestartWithCertificate(nullptr, nullptr, callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16143 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16144 | |
| 16145 | // Ensure the certificate was added to the client auth cache before |
| 16146 | // allowing the connection to continue restarting. |
| 16147 | scoped_refptr<X509Certificate> client_cert; |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16148 | scoped_refptr<SSLPrivateKey> client_private_key; |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16149 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16150 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16151 | ASSERT_FALSE(client_cert); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16152 | |
| 16153 | // Restart the handshake. This will consume ssl_data2, which fails, and |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16154 | // then consume ssl_data3 and ssl_data4, both of which should also fail. |
| 16155 | // The result code is checked against what ssl_data4 should return. |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16156 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16157 | ASSERT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16158 | |
| 16159 | // Ensure that the client certificate is removed from the cache on a |
| 16160 | // handshake failure. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16161 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16162 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16163 | } |
| 16164 | |
| 16165 | // Ensure that a client certificate is removed from the SSL client auth |
| 16166 | // cache when: |
| 16167 | // 1) No proxy is involved. |
| 16168 | // 2) TLS False Start is enabled. |
| 16169 | // 3) The initial TLS handshake requests a client certificate. |
| 16170 | // 4) The client supplies an invalid/unacceptable certificate. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16171 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_FalseStart) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16172 | HttpRequestInfo request_info; |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16173 | request_info.url = GURL("https://www.example.com/"); |
| 16174 | request_info.method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16175 | request_info.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16176 | request_info.traffic_annotation = |
| 16177 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16178 | |
David Benjamin | 1c4b6d01 | 2019-07-08 17:12:57 | [diff] [blame] | 16179 | auto cert_request = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 16180 | cert_request->host_and_port = HostPortPair("www.example.com", 443); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16181 | |
| 16182 | // When TLS False Start is used, SSLClientSocket::Connect() calls will |
| 16183 | // return successfully after reading up to the peer's Certificate message. |
| 16184 | // This is to allow the caller to call SSLClientSocket::Write(), which can |
| 16185 | // enqueue application data to be sent in the same packet as the |
| 16186 | // ChangeCipherSpec and Finished messages. |
| 16187 | // The actual handshake will be finished when SSLClientSocket::Read() is |
| 16188 | // called, which expects to process the peer's ChangeCipherSpec and |
| 16189 | // Finished messages. If there was an error negotiating with the peer, |
| 16190 | // such as due to the peer requiring a client certificate when none was |
| 16191 | // supplied, the alert sent by the peer won't be processed until Read() is |
| 16192 | // called. |
| 16193 | |
| 16194 | // Like the non-False Start case, when a client certificate is requested by |
| 16195 | // the peer, the handshake is aborted during the Connect() call. |
| 16196 | // [ssl_]data1 represents the initial SSL handshake with the peer. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16197 | SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16198 | ssl_data1.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16199 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16200 | StaticSocketDataProvider data1; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16201 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16202 | |
| 16203 | // When a client certificate is supplied, Connect() will not be aborted |
| 16204 | // when the peer requests the certificate. Instead, the handshake will |
| 16205 | // artificially succeed, allowing the caller to write the HTTP request to |
| 16206 | // the socket. The handshake messages are not processed until Read() is |
| 16207 | // called, which then detects that the handshake was aborted, due to the |
| 16208 | // peer sending a handshake_failure because it requires a client |
| 16209 | // certificate. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16210 | SSLSocketDataProvider ssl_data2(ASYNC, OK); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16211 | ssl_data2.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16212 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16213 | MockRead data2_reads[] = { |
| 16214 | MockRead(ASYNC /* async */, ERR_SSL_PROTOCOL_ERROR), |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16215 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16216 | StaticSocketDataProvider data2(data2_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16217 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16218 | |
| 16219 | // As described in ClientAuthCertCache_Direct_NoFalseStart, [ssl_]data3 is |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16220 | // the data for the SSL handshake once the TLSv1.1 connection falls back to |
| 16221 | // TLSv1. It has the same behaviour as [ssl_]data2. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16222 | SSLSocketDataProvider ssl_data3(ASYNC, OK); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16223 | ssl_data3.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16224 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data3); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16225 | StaticSocketDataProvider data3(data2_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16226 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16227 | |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16228 | // [ssl_]data4 is the data for the SSL handshake once the TLSv1 connection |
| 16229 | // falls back to SSLv3. It has the same behaviour as [ssl_]data2. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16230 | SSLSocketDataProvider ssl_data4(ASYNC, OK); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16231 | ssl_data4.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16232 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data4); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16233 | StaticSocketDataProvider data4(data2_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16234 | session_deps_.socket_factory->AddSocketDataProvider(&data4); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16235 | |
[email protected] | 7799de1 | 2013-05-30 05:52:51 | [diff] [blame] | 16236 | // Need one more if TLSv1.2 is enabled. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16237 | SSLSocketDataProvider ssl_data5(ASYNC, OK); |
[email protected] | 7799de1 | 2013-05-30 05:52:51 | [diff] [blame] | 16238 | ssl_data5.cert_request_info = cert_request.get(); |
| 16239 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data5); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16240 | StaticSocketDataProvider data5(data2_reads, base::span<MockWrite>()); |
[email protected] | 7799de1 | 2013-05-30 05:52:51 | [diff] [blame] | 16241 | session_deps_.socket_factory->AddSocketDataProvider(&data5); |
| 16242 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16243 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16244 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16245 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16246 | // Begin the initial SSL handshake. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 16247 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16248 | int rv = trans.Start(&request_info, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16249 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16250 | |
| 16251 | // Complete the SSL handshake, which should abort due to requiring a |
| 16252 | // client certificate. |
| 16253 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16254 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16255 | |
| 16256 | // Indicate that no certificate should be supplied. From the perspective |
| 16257 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 16258 | // certificate, so this is the same as supply a |
| 16259 | // legitimate-but-unacceptable certificate. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16260 | rv = trans.RestartWithCertificate(nullptr, nullptr, callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16261 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16262 | |
| 16263 | // Ensure the certificate was added to the client auth cache before |
| 16264 | // allowing the connection to continue restarting. |
| 16265 | scoped_refptr<X509Certificate> client_cert; |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16266 | scoped_refptr<SSLPrivateKey> client_private_key; |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16267 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16268 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16269 | ASSERT_FALSE(client_cert); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16270 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16271 | // Restart the handshake. This will consume ssl_data2, which fails, and |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16272 | // then consume ssl_data3 and ssl_data4, both of which should also fail. |
| 16273 | // The result code is checked against what ssl_data4 should return. |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16274 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16275 | ASSERT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16276 | |
| 16277 | // Ensure that the client certificate is removed from the cache on a |
| 16278 | // handshake failure. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16279 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16280 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16281 | } |
| 16282 | |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16283 | // Ensure that a client certificate is removed from the SSL client auth |
| 16284 | // cache when: |
| 16285 | // 1) An HTTPS proxy is involved. |
| 16286 | // 3) The HTTPS proxy requests a client certificate. |
| 16287 | // 4) The client supplies an invalid/unacceptable certificate for the |
| 16288 | // proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16289 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Proxy_Fail) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 16290 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 16291 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 16292 | BoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16293 | session_deps_.net_log = log.bound().net_log(); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16294 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16295 | auto cert_request = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 16296 | cert_request->host_and_port = HostPortPair("proxy", 70); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16297 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16298 | // Repeat the test for connecting to an HTTPS endpoint, then for connecting to |
| 16299 | // an HTTP endpoint. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16300 | HttpRequestInfo requests[2]; |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16301 | requests[0].url = GURL("https://www.example.com/"); |
| 16302 | requests[0].method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16303 | requests[0].load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16304 | requests[0].traffic_annotation = |
| 16305 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16306 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16307 | // HTTPS requests are tunneled. |
| 16308 | MockWrite https_writes[] = { |
| 16309 | MockWrite("CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 16310 | "Host: www.example.com:443\r\n" |
| 16311 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 16312 | }; |
| 16313 | |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16314 | requests[1].url = GURL("http://www.example.com/"); |
| 16315 | requests[1].method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16316 | requests[1].load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16317 | requests[1].traffic_annotation = |
| 16318 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16319 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16320 | // HTTP requests are not. |
| 16321 | MockWrite http_writes[] = { |
| 16322 | MockWrite("GET http://www.example.com/ HTTP/1.1\r\n" |
| 16323 | "Host: www.example.com\r\n" |
| 16324 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 16325 | }; |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16326 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16327 | // When the server rejects the client certificate, it will close the |
| 16328 | // connection. In TLS 1.2, this is signaled out of Connect(). In TLS 1.3 (or |
| 16329 | // TLS 1.2 with False Start), the error is returned out of the first Read(). |
| 16330 | for (bool reject_in_connect : {true, false}) { |
| 16331 | SCOPED_TRACE(reject_in_connect); |
| 16332 | // Client certificate errors are typically signaled with |
| 16333 | // ERR_BAD_SSL_CLIENT_AUTH_CERT, but sometimes the server gives an arbitrary |
| 16334 | // protocol error. |
| 16335 | for (Error reject_error : |
| 16336 | {ERR_SSL_PROTOCOL_ERROR, ERR_BAD_SSL_CLIENT_AUTH_CERT}) { |
| 16337 | SCOPED_TRACE(reject_error); |
| 16338 | // Tunneled and non-tunneled requests are handled differently. Test both. |
| 16339 | for (const HttpRequestInfo& request : requests) { |
| 16340 | SCOPED_TRACE(request.url); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16341 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16342 | session_deps_.socket_factory = |
| 16343 | std::make_unique<MockClientSocketFactory>(); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16344 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16345 | // See ClientAuthCertCache_Direct_NoFalseStart for the explanation of |
| 16346 | // [ssl_]data[1-2]. [ssl_]data3 is not needed because we do not retry |
| 16347 | // for proxies. Rather than represending the endpoint |
| 16348 | // (www.example.com:443), they represent failures with the HTTPS proxy |
| 16349 | // (proxy:70). |
| 16350 | SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 16351 | ssl_data1.cert_request_info = cert_request.get(); |
| 16352 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 16353 | StaticSocketDataProvider data1; |
| 16354 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16355 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16356 | base::Optional<SSLSocketDataProvider> ssl_data2; |
| 16357 | base::Optional<StaticSocketDataProvider> data2; |
| 16358 | MockRead error_in_read[] = {MockRead(ASYNC, reject_error)}; |
| 16359 | if (reject_in_connect) { |
| 16360 | ssl_data2.emplace(ASYNC, reject_error); |
| 16361 | // There are no reads or writes. |
| 16362 | data2.emplace(); |
| 16363 | } else { |
| 16364 | ssl_data2.emplace(ASYNC, OK); |
| 16365 | // We will get one Write() in before observing the error in Read(). |
| 16366 | if (request.url.SchemeIsCryptographic()) { |
| 16367 | data2.emplace(error_in_read, https_writes); |
| 16368 | } else { |
| 16369 | data2.emplace(error_in_read, http_writes); |
| 16370 | } |
| 16371 | } |
| 16372 | ssl_data2->cert_request_info = cert_request.get(); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16373 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16374 | session_deps_.socket_factory->AddSSLSocketDataProvider( |
| 16375 | &ssl_data2.value()); |
| 16376 | session_deps_.socket_factory->AddSocketDataProvider(&data2.value()); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16377 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16378 | std::unique_ptr<HttpNetworkSession> session = |
| 16379 | CreateSession(&session_deps_); |
| 16380 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 16381 | |
| 16382 | // Begin the SSL handshake with the proxy. |
| 16383 | TestCompletionCallback callback; |
| 16384 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 16385 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16386 | |
| 16387 | // Complete the SSL handshake, which should abort due to requiring a |
| 16388 | // client certificate. |
| 16389 | rv = callback.WaitForResult(); |
| 16390 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 16391 | |
| 16392 | // Indicate that no certificate should be supplied. From the |
| 16393 | // perspective of SSLClientCertCache, NULL is just as meaningful as a |
| 16394 | // real certificate, so this is the same as supply a |
| 16395 | // legitimate-but-unacceptable certificate. |
| 16396 | rv = |
| 16397 | trans.RestartWithCertificate(nullptr, nullptr, callback.callback()); |
| 16398 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16399 | |
| 16400 | // Ensure the certificate was added to the client auth cache before |
| 16401 | // allowing the connection to continue restarting. |
| 16402 | scoped_refptr<X509Certificate> client_cert; |
| 16403 | scoped_refptr<SSLPrivateKey> client_private_key; |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16404 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16405 | HostPortPair("proxy", 70), &client_cert, &client_private_key)); |
| 16406 | ASSERT_FALSE(client_cert); |
| 16407 | // Ensure the certificate was NOT cached for the endpoint. This only |
| 16408 | // applies to HTTPS requests, but is fine to check for HTTP requests. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16409 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16410 | HostPortPair("www.example.com", 443), &client_cert, |
| 16411 | &client_private_key)); |
| 16412 | |
| 16413 | // Restart the handshake. This will consume ssl_data2. The result code |
| 16414 | // is checked against what ssl_data2 should return. |
| 16415 | rv = callback.WaitForResult(); |
| 16416 | ASSERT_THAT(rv, AnyOf(IsError(ERR_PROXY_CONNECTION_FAILED), |
| 16417 | IsError(reject_error))); |
| 16418 | |
| 16419 | // Now that the new handshake has failed, ensure that the client |
| 16420 | // certificate was removed from the client auth cache. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16421 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16422 | HostPortPair("proxy", 70), &client_cert, &client_private_key)); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16423 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16424 | HostPortPair("www.example.com", 443), &client_cert, |
| 16425 | &client_private_key)); |
| 16426 | } |
| 16427 | } |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16428 | } |
| 16429 | } |
| 16430 | |
David Benjamin | 1a056608 | 2019-04-30 07:36:19 | [diff] [blame] | 16431 | // Test that HttpNetworkTransaction correctly handles (mocked) certificate |
| 16432 | // requests during a TLS renegotiation. |
| 16433 | TEST_F(HttpNetworkTransactionTest, CertificateRequestInRenego) { |
| 16434 | HttpRequestInfo request_info; |
| 16435 | request_info.url = GURL("https://www.example.com/"); |
| 16436 | request_info.method = "GET"; |
| 16437 | request_info.load_flags = LOAD_NORMAL; |
| 16438 | request_info.traffic_annotation = |
| 16439 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 16440 | |
| 16441 | auto cert_request = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 16442 | cert_request->host_and_port = HostPortPair("www.example.com", 443); |
| 16443 | |
| 16444 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 16445 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 16446 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 16447 | ASSERT_TRUE(identity); |
| 16448 | |
| 16449 | // The first connection's handshake succeeds, but we get |
| 16450 | // ERR_SSL_CLIENT_AUTH_CERT_NEEDED instead of an HTTP response. |
| 16451 | SSLSocketDataProvider ssl_data1(ASYNC, OK); |
| 16452 | ssl_data1.cert_request_info = cert_request.get(); |
| 16453 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 16454 | MockWrite data1_writes[] = { |
| 16455 | MockWrite("GET / HTTP/1.1\r\n" |
| 16456 | "Host: www.example.com\r\n" |
| 16457 | "Connection: keep-alive\r\n\r\n"), |
| 16458 | }; |
| 16459 | MockRead data1_reads[] = { |
| 16460 | MockRead(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED), |
| 16461 | }; |
| 16462 | StaticSocketDataProvider data1(data1_reads, data1_writes); |
| 16463 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 16464 | |
| 16465 | // After supplying with certificate, we restart the request from the top, |
| 16466 | // which succeeds this time. |
| 16467 | SSLSocketDataProvider ssl_data2(ASYNC, OK); |
| 16468 | ssl_data2.expected_send_client_cert = true; |
| 16469 | ssl_data2.expected_client_cert = identity->certificate(); |
| 16470 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
| 16471 | MockWrite data2_writes[] = { |
| 16472 | MockWrite("GET / HTTP/1.1\r\n" |
| 16473 | "Host: www.example.com\r\n" |
| 16474 | "Connection: keep-alive\r\n\r\n"), |
| 16475 | }; |
| 16476 | MockRead data2_reads[] = { |
| 16477 | MockRead("HTTP/1.1 200 OK\r\n" |
| 16478 | "Content-Length: 0\r\n\r\n"), |
| 16479 | }; |
| 16480 | StaticSocketDataProvider data2(data2_reads, data2_writes); |
| 16481 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 16482 | |
| 16483 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 16484 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 16485 | |
| 16486 | TestCompletionCallback callback; |
| 16487 | int rv = callback.GetResult( |
| 16488 | trans.Start(&request_info, callback.callback(), NetLogWithSource())); |
| 16489 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 16490 | |
| 16491 | rv = trans.RestartWithCertificate(identity->certificate(), |
| 16492 | identity->ssl_private_key(), |
| 16493 | callback.callback()); |
| 16494 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16495 | |
| 16496 | // Ensure the certificate was added to the client auth cache |
| 16497 | // allowing the connection to continue restarting. |
| 16498 | scoped_refptr<X509Certificate> client_cert; |
| 16499 | scoped_refptr<SSLPrivateKey> client_private_key; |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16500 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 1a056608 | 2019-04-30 07:36:19 | [diff] [blame] | 16501 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
| 16502 | EXPECT_TRUE(client_cert->EqualsIncludingChain(identity->certificate())); |
| 16503 | |
| 16504 | // Complete the handshake. The request now succeeds. |
| 16505 | rv = callback.WaitForResult(); |
| 16506 | ASSERT_THAT(rv, IsError(OK)); |
| 16507 | EXPECT_EQ(200, trans.GetResponseInfo()->headers->response_code()); |
| 16508 | |
| 16509 | // The client certificate remains in the cache. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16510 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 1a056608 | 2019-04-30 07:36:19 | [diff] [blame] | 16511 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
| 16512 | EXPECT_TRUE(client_cert->EqualsIncludingChain(identity->certificate())); |
| 16513 | } |
| 16514 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16515 | TEST_F(HttpNetworkTransactionTest, UseIPConnectionPooling) { |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16516 | // Set up a special HttpNetworkSession with a MockCachingHostResolver. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 16517 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16518 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16519 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 16520 | AddSSLSocketData(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16521 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16522 | spdy::SpdySerializedFrame host1_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 16523 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 16524 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16525 | spdy::SpdySerializedFrame host2_req( |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16526 | spdy_util_.ConstructSpdyGet("https://mail.example.com", 3, LOWEST)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16527 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16528 | CreateMockWrite(host1_req, 0), CreateMockWrite(host2_req, 3), |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16529 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16530 | spdy::SpdySerializedFrame host1_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16531 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16532 | spdy::SpdySerializedFrame host1_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16533 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16534 | spdy::SpdySerializedFrame host2_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16535 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16536 | spdy::SpdySerializedFrame host2_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16537 | spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16538 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16539 | CreateMockRead(host1_resp, 1), CreateMockRead(host1_resp_body, 2), |
| 16540 | CreateMockRead(host2_resp, 4), CreateMockRead(host2_resp_body, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 16541 | MockRead(ASYNC, 0, 6), |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16542 | }; |
| 16543 | |
eroman | 36d84e5443 | 2016-03-17 03:23:02 | [diff] [blame] | 16544 | IPEndPoint peer_addr(IPAddress::IPv4Localhost(), 443); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16545 | MockConnect connect(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16546 | SequencedSocketData spdy_data(connect, spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16547 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16548 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 16549 | TestCompletionCallback callback; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16550 | HttpRequestInfo request1; |
| 16551 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 16552 | request1.url = GURL("https://www.example.org/"); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16553 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16554 | request1.traffic_annotation = |
| 16555 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16556 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16557 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16558 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16559 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16560 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16561 | |
| 16562 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16563 | ASSERT_TRUE(response); |
| 16564 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 16565 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16566 | |
| 16567 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16568 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16569 | EXPECT_EQ("hello!", response_data); |
| 16570 | |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16571 | // Preload mail.example.com into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16572 | rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame^] | 16573 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 16574 | base::nullopt); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16575 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16576 | |
| 16577 | HttpRequestInfo request2; |
| 16578 | request2.method = "GET"; |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16579 | request2.url = GURL("https://mail.example.com/"); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16580 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16581 | request2.traffic_annotation = |
| 16582 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16583 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16584 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16585 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16586 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16587 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16588 | |
| 16589 | response = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16590 | ASSERT_TRUE(response); |
| 16591 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 16592 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16593 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 16594 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16595 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16596 | EXPECT_EQ("hello!", response_data); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16597 | } |
| 16598 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16599 | TEST_F(HttpNetworkTransactionTest, UseIPConnectionPoolingAfterResolution) { |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16600 | // Set up a special HttpNetworkSession with a MockCachingHostResolver. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 16601 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16602 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16603 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 16604 | AddSSLSocketData(); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16605 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16606 | spdy::SpdySerializedFrame host1_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 16607 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 16608 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16609 | spdy::SpdySerializedFrame host2_req( |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16610 | spdy_util_.ConstructSpdyGet("https://mail.example.com", 3, LOWEST)); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16611 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16612 | CreateMockWrite(host1_req, 0), CreateMockWrite(host2_req, 3), |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16613 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16614 | spdy::SpdySerializedFrame host1_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16615 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16616 | spdy::SpdySerializedFrame host1_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16617 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16618 | spdy::SpdySerializedFrame host2_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16619 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16620 | spdy::SpdySerializedFrame host2_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16621 | spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16622 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16623 | CreateMockRead(host1_resp, 1), CreateMockRead(host1_resp_body, 2), |
| 16624 | CreateMockRead(host2_resp, 4), CreateMockRead(host2_resp_body, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 16625 | MockRead(ASYNC, 0, 6), |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16626 | }; |
| 16627 | |
eroman | 36d84e5443 | 2016-03-17 03:23:02 | [diff] [blame] | 16628 | IPEndPoint peer_addr(IPAddress::IPv4Localhost(), 443); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16629 | MockConnect connect(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16630 | SequencedSocketData spdy_data(connect, spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16631 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16632 | |
| 16633 | TestCompletionCallback callback; |
| 16634 | HttpRequestInfo request1; |
| 16635 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 16636 | request1.url = GURL("https://www.example.org/"); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16637 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16638 | request1.traffic_annotation = |
| 16639 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16640 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16641 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16642 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16643 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16644 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16645 | |
| 16646 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16647 | ASSERT_TRUE(response); |
| 16648 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 16649 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16650 | |
| 16651 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16652 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16653 | EXPECT_EQ("hello!", response_data); |
| 16654 | |
| 16655 | HttpRequestInfo request2; |
| 16656 | request2.method = "GET"; |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16657 | request2.url = GURL("https://mail.example.com/"); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16658 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16659 | request2.traffic_annotation = |
| 16660 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16661 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16662 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16663 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16664 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16665 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16666 | |
| 16667 | response = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16668 | ASSERT_TRUE(response); |
| 16669 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 16670 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16671 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 16672 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16673 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16674 | EXPECT_EQ("hello!", response_data); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16675 | } |
| 16676 | |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16677 | // Regression test for https://crbug.com/546991. |
| 16678 | // The server might not be able to serve an IP pooled request, and might send a |
| 16679 | // 421 Misdirected Request response status to indicate this. |
| 16680 | // HttpNetworkTransaction should reset the request and retry without IP pooling. |
| 16681 | TEST_F(HttpNetworkTransactionTest, RetryWithoutConnectionPooling) { |
| 16682 | // Two hosts resolve to the same IP address. |
| 16683 | const std::string ip_addr = "1.2.3.4"; |
| 16684 | IPAddress ip; |
| 16685 | ASSERT_TRUE(ip.AssignFromIPLiteral(ip_addr)); |
| 16686 | IPEndPoint peer_addr = IPEndPoint(ip, 443); |
| 16687 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 16688 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16689 | session_deps_.host_resolver->rules()->AddRule("www.example.org", ip_addr); |
| 16690 | session_deps_.host_resolver->rules()->AddRule("mail.example.org", ip_addr); |
| 16691 | |
| 16692 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 16693 | |
| 16694 | // Two requests on the first connection. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16695 | spdy::SpdySerializedFrame req1( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16696 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
| 16697 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16698 | spdy::SpdySerializedFrame req2( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16699 | spdy_util_.ConstructSpdyGet("https://mail.example.org", 3, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16700 | spdy::SpdySerializedFrame rst( |
| 16701 | spdy_util_.ConstructSpdyRstStream(3, spdy::ERROR_CODE_CANCEL)); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16702 | MockWrite writes1[] = { |
| 16703 | CreateMockWrite(req1, 0), CreateMockWrite(req2, 3), |
| 16704 | CreateMockWrite(rst, 6), |
| 16705 | }; |
| 16706 | |
| 16707 | // The first one succeeds, the second gets error 421 Misdirected Request. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16708 | spdy::SpdySerializedFrame resp1( |
| 16709 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 16710 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
| 16711 | spdy::SpdyHeaderBlock response_headers; |
| 16712 | response_headers[spdy::kHttp2StatusHeader] = "421"; |
| 16713 | spdy::SpdySerializedFrame resp2( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16714 | spdy_util_.ConstructSpdyReply(3, std::move(response_headers))); |
| 16715 | MockRead reads1[] = {CreateMockRead(resp1, 1), CreateMockRead(body1, 2), |
| 16716 | CreateMockRead(resp2, 4), MockRead(ASYNC, 0, 5)}; |
| 16717 | |
| 16718 | MockConnect connect1(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16719 | SequencedSocketData data1(connect1, reads1, writes1); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16720 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 16721 | |
| 16722 | AddSSLSocketData(); |
| 16723 | |
| 16724 | // Retry the second request on a second connection. |
| 16725 | SpdyTestUtil spdy_util2; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16726 | spdy::SpdySerializedFrame req3( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16727 | spdy_util2.ConstructSpdyGet("https://mail.example.org", 1, LOWEST)); |
| 16728 | MockWrite writes2[] = { |
| 16729 | CreateMockWrite(req3, 0), |
| 16730 | }; |
| 16731 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16732 | spdy::SpdySerializedFrame resp3( |
| 16733 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 16734 | spdy::SpdySerializedFrame body3(spdy_util2.ConstructSpdyDataFrame(1, true)); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16735 | MockRead reads2[] = {CreateMockRead(resp3, 1), CreateMockRead(body3, 2), |
| 16736 | MockRead(ASYNC, 0, 3)}; |
| 16737 | |
| 16738 | MockConnect connect2(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16739 | SequencedSocketData data2(connect2, reads2, writes2); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16740 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 16741 | |
| 16742 | AddSSLSocketData(); |
| 16743 | |
| 16744 | // Preload mail.example.org into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16745 | int rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame^] | 16746 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 16747 | base::nullopt); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16748 | EXPECT_THAT(rv, IsOk()); |
| 16749 | |
| 16750 | HttpRequestInfo request1; |
| 16751 | request1.method = "GET"; |
| 16752 | request1.url = GURL("https://www.example.org/"); |
| 16753 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16754 | request1.traffic_annotation = |
| 16755 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16756 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
| 16757 | |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16758 | TestCompletionCallback callback; |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16759 | rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
| 16760 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16761 | rv = callback.WaitForResult(); |
| 16762 | EXPECT_THAT(rv, IsOk()); |
| 16763 | |
| 16764 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 16765 | ASSERT_TRUE(response); |
| 16766 | ASSERT_TRUE(response->headers); |
| 16767 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 16768 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 16769 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 16770 | std::string response_data; |
| 16771 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 16772 | EXPECT_EQ("hello!", response_data); |
| 16773 | |
| 16774 | HttpRequestInfo request2; |
| 16775 | request2.method = "GET"; |
| 16776 | request2.url = GURL("https://mail.example.org/"); |
| 16777 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16778 | request2.traffic_annotation = |
| 16779 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16780 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
| 16781 | |
| 16782 | BoundTestNetLog log; |
| 16783 | rv = trans2.Start(&request2, callback.callback(), log.bound()); |
| 16784 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16785 | rv = callback.WaitForResult(); |
| 16786 | EXPECT_THAT(rv, IsOk()); |
| 16787 | |
| 16788 | response = trans2.GetResponseInfo(); |
| 16789 | ASSERT_TRUE(response); |
| 16790 | ASSERT_TRUE(response->headers); |
| 16791 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 16792 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 16793 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 16794 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 16795 | EXPECT_EQ("hello!", response_data); |
| 16796 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 16797 | auto entries = log.GetEntries(); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16798 | ExpectLogContainsSomewhere( |
| 16799 | entries, 0, NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST, |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16800 | NetLogEventPhase::NONE); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16801 | } |
| 16802 | |
| 16803 | // Test that HTTP 421 responses are properly returned to the caller if received |
| 16804 | // on the retry as well. HttpNetworkTransaction should not infinite loop or lose |
| 16805 | // portions of the response. |
| 16806 | TEST_F(HttpNetworkTransactionTest, ReturnHTTP421OnRetry) { |
| 16807 | // Two hosts resolve to the same IP address. |
| 16808 | const std::string ip_addr = "1.2.3.4"; |
| 16809 | IPAddress ip; |
| 16810 | ASSERT_TRUE(ip.AssignFromIPLiteral(ip_addr)); |
| 16811 | IPEndPoint peer_addr = IPEndPoint(ip, 443); |
| 16812 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 16813 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16814 | session_deps_.host_resolver->rules()->AddRule("www.example.org", ip_addr); |
| 16815 | session_deps_.host_resolver->rules()->AddRule("mail.example.org", ip_addr); |
| 16816 | |
| 16817 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 16818 | |
| 16819 | // Two requests on the first connection. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16820 | spdy::SpdySerializedFrame req1( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16821 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
| 16822 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16823 | spdy::SpdySerializedFrame req2( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16824 | spdy_util_.ConstructSpdyGet("https://mail.example.org", 3, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16825 | spdy::SpdySerializedFrame rst( |
| 16826 | spdy_util_.ConstructSpdyRstStream(3, spdy::ERROR_CODE_CANCEL)); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16827 | MockWrite writes1[] = { |
| 16828 | CreateMockWrite(req1, 0), CreateMockWrite(req2, 3), |
| 16829 | CreateMockWrite(rst, 6), |
| 16830 | }; |
| 16831 | |
| 16832 | // The first one succeeds, the second gets error 421 Misdirected Request. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16833 | spdy::SpdySerializedFrame resp1( |
| 16834 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 16835 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
| 16836 | spdy::SpdyHeaderBlock response_headers; |
| 16837 | response_headers[spdy::kHttp2StatusHeader] = "421"; |
| 16838 | spdy::SpdySerializedFrame resp2( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16839 | spdy_util_.ConstructSpdyReply(3, response_headers.Clone())); |
| 16840 | MockRead reads1[] = {CreateMockRead(resp1, 1), CreateMockRead(body1, 2), |
| 16841 | CreateMockRead(resp2, 4), MockRead(ASYNC, 0, 5)}; |
| 16842 | |
| 16843 | MockConnect connect1(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16844 | SequencedSocketData data1(connect1, reads1, writes1); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16845 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 16846 | |
| 16847 | AddSSLSocketData(); |
| 16848 | |
| 16849 | // Retry the second request on a second connection. It returns 421 Misdirected |
| 16850 | // Retry again. |
| 16851 | SpdyTestUtil spdy_util2; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16852 | spdy::SpdySerializedFrame req3( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16853 | spdy_util2.ConstructSpdyGet("https://mail.example.org", 1, LOWEST)); |
| 16854 | MockWrite writes2[] = { |
| 16855 | CreateMockWrite(req3, 0), |
| 16856 | }; |
| 16857 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16858 | spdy::SpdySerializedFrame resp3( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16859 | spdy_util2.ConstructSpdyReply(1, std::move(response_headers))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16860 | spdy::SpdySerializedFrame body3(spdy_util2.ConstructSpdyDataFrame(1, true)); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16861 | MockRead reads2[] = {CreateMockRead(resp3, 1), CreateMockRead(body3, 2), |
| 16862 | MockRead(ASYNC, 0, 3)}; |
| 16863 | |
| 16864 | MockConnect connect2(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16865 | SequencedSocketData data2(connect2, reads2, writes2); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16866 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 16867 | |
| 16868 | AddSSLSocketData(); |
| 16869 | |
| 16870 | // Preload mail.example.org into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16871 | int rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame^] | 16872 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 16873 | base::nullopt); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16874 | EXPECT_THAT(rv, IsOk()); |
| 16875 | |
| 16876 | HttpRequestInfo request1; |
| 16877 | request1.method = "GET"; |
| 16878 | request1.url = GURL("https://www.example.org/"); |
| 16879 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16880 | request1.traffic_annotation = |
| 16881 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16882 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
| 16883 | |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16884 | TestCompletionCallback callback; |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16885 | rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
| 16886 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16887 | rv = callback.WaitForResult(); |
| 16888 | EXPECT_THAT(rv, IsOk()); |
| 16889 | |
| 16890 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 16891 | ASSERT_TRUE(response); |
| 16892 | ASSERT_TRUE(response->headers); |
| 16893 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 16894 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 16895 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 16896 | std::string response_data; |
| 16897 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 16898 | EXPECT_EQ("hello!", response_data); |
| 16899 | |
| 16900 | HttpRequestInfo request2; |
| 16901 | request2.method = "GET"; |
| 16902 | request2.url = GURL("https://mail.example.org/"); |
| 16903 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16904 | request2.traffic_annotation = |
| 16905 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16906 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
| 16907 | |
| 16908 | BoundTestNetLog log; |
| 16909 | rv = trans2.Start(&request2, callback.callback(), log.bound()); |
| 16910 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16911 | rv = callback.WaitForResult(); |
| 16912 | EXPECT_THAT(rv, IsOk()); |
| 16913 | |
| 16914 | // After a retry, the 421 Misdirected Request is reported back up to the |
| 16915 | // caller. |
| 16916 | response = trans2.GetResponseInfo(); |
| 16917 | ASSERT_TRUE(response); |
| 16918 | ASSERT_TRUE(response->headers); |
| 16919 | EXPECT_EQ("HTTP/1.1 421", response->headers->GetStatusLine()); |
| 16920 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 16921 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 16922 | EXPECT_TRUE(response->ssl_info.cert); |
| 16923 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 16924 | EXPECT_EQ("hello!", response_data); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16925 | } |
| 16926 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16927 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 5c64213 | 2015-06-02 16:05:13 | [diff] [blame] | 16928 | UseIPConnectionPoolingWithHostCacheExpiration) { |
Eric Orth | 1da75de | 2019-02-20 21:10:34 | [diff] [blame] | 16929 | // Set up HostResolver to invalidate cached entries after 1 cached resolve. |
| 16930 | session_deps_.host_resolver = |
| 16931 | std::make_unique<MockCachingHostResolver>(1 /* cache_invalidation_num */); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16932 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16933 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 16934 | AddSSLSocketData(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16935 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16936 | spdy::SpdySerializedFrame host1_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 16937 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 16938 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16939 | spdy::SpdySerializedFrame host2_req( |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16940 | spdy_util_.ConstructSpdyGet("https://mail.example.com", 3, LOWEST)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16941 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16942 | CreateMockWrite(host1_req, 0), CreateMockWrite(host2_req, 3), |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16943 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16944 | spdy::SpdySerializedFrame host1_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16945 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16946 | spdy::SpdySerializedFrame host1_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16947 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16948 | spdy::SpdySerializedFrame host2_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16949 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16950 | spdy::SpdySerializedFrame host2_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16951 | spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16952 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16953 | CreateMockRead(host1_resp, 1), CreateMockRead(host1_resp_body, 2), |
| 16954 | CreateMockRead(host2_resp, 4), CreateMockRead(host2_resp_body, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 16955 | MockRead(ASYNC, 0, 6), |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16956 | }; |
| 16957 | |
eroman | 36d84e5443 | 2016-03-17 03:23:02 | [diff] [blame] | 16958 | IPEndPoint peer_addr(IPAddress::IPv4Localhost(), 443); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16959 | MockConnect connect(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16960 | SequencedSocketData spdy_data(connect, spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16961 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16962 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 16963 | TestCompletionCallback callback; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16964 | HttpRequestInfo request1; |
| 16965 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 16966 | request1.url = GURL("https://www.example.org/"); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16967 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16968 | request1.traffic_annotation = |
| 16969 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16970 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16971 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16972 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16973 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16974 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16975 | |
| 16976 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16977 | ASSERT_TRUE(response); |
| 16978 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 16979 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16980 | |
| 16981 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16982 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16983 | EXPECT_EQ("hello!", response_data); |
| 16984 | |
| 16985 | // Preload cache entries into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16986 | rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame^] | 16987 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 16988 | base::nullopt); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16989 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16990 | |
| 16991 | HttpRequestInfo request2; |
| 16992 | request2.method = "GET"; |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16993 | request2.url = GURL("https://mail.example.com/"); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16994 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 16995 | request2.traffic_annotation = |
| 16996 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16997 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16998 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16999 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17000 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17001 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17002 | |
| 17003 | response = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17004 | ASSERT_TRUE(response); |
| 17005 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 17006 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17007 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17008 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17009 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17010 | EXPECT_EQ("hello!", response_data); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17011 | } |
| 17012 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17013 | TEST_F(HttpNetworkTransactionTest, DoNotUseSpdySessionForHttp) { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17014 | const std::string https_url = "https://www.example.org:8080/"; |
| 17015 | const std::string http_url = "http://www.example.org:8080/"; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17016 | |
| 17017 | // SPDY GET for HTTPS URL |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17018 | spdy::SpdySerializedFrame req1( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17019 | spdy_util_.ConstructSpdyGet(https_url.c_str(), 1, LOWEST)); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17020 | |
| 17021 | MockWrite writes1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17022 | CreateMockWrite(req1, 0), |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17023 | }; |
| 17024 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17025 | spdy::SpdySerializedFrame resp1( |
| 17026 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17027 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17028 | MockRead reads1[] = {CreateMockRead(resp1, 1), CreateMockRead(body1, 2), |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 17029 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 3)}; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17030 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17031 | SequencedSocketData data1(reads1, writes1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17032 | MockConnect connect_data1(ASYNC, OK); |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 17033 | data1.set_connect_data(connect_data1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17034 | |
| 17035 | // HTTP GET for the HTTP URL |
| 17036 | MockWrite writes2[] = { |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 17037 | MockWrite(ASYNC, 0, |
lgarron | a91df87f | 2014-12-05 00:51:34 | [diff] [blame] | 17038 | "GET / HTTP/1.1\r\n" |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17039 | "Host: www.example.org:8080\r\n" |
lgarron | a91df87f | 2014-12-05 00:51:34 | [diff] [blame] | 17040 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17041 | }; |
| 17042 | |
| 17043 | MockRead reads2[] = { |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 17044 | MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 17045 | MockRead(ASYNC, 2, "hello"), |
| 17046 | MockRead(ASYNC, OK, 3), |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17047 | }; |
| 17048 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17049 | SequencedSocketData data2(reads2, writes2); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17050 | |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17051 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17052 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 17053 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 17054 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 17055 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17056 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17057 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17058 | |
| 17059 | // Start the first transaction to set up the SpdySession |
| 17060 | HttpRequestInfo request1; |
| 17061 | request1.method = "GET"; |
| 17062 | request1.url = GURL(https_url); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17063 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17064 | request1.traffic_annotation = |
| 17065 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17066 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17067 | TestCompletionCallback callback1; |
| 17068 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17069 | trans1.Start(&request1, callback1.callback(), NetLogWithSource())); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 17070 | base::RunLoop().RunUntilIdle(); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17071 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17072 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17073 | EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 17074 | |
| 17075 | // Now, start the HTTP request |
| 17076 | HttpRequestInfo request2; |
| 17077 | request2.method = "GET"; |
| 17078 | request2.url = GURL(http_url); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17079 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17080 | request2.traffic_annotation = |
| 17081 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17082 | HttpNetworkTransaction trans2(MEDIUM, session.get()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17083 | TestCompletionCallback callback2; |
| 17084 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17085 | trans2.Start(&request2, callback2.callback(), NetLogWithSource())); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 17086 | base::RunLoop().RunUntilIdle(); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17087 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17088 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17089 | EXPECT_FALSE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 17090 | } |
| 17091 | |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17092 | // Alternative service requires HTTP/2 (or SPDY), but HTTP/1.1 is negotiated |
| 17093 | // with the alternative server. That connection should not be used. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17094 | TEST_F(HttpNetworkTransactionTest, AlternativeServiceNotOnHttp11) { |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17095 | url::SchemeHostPort server("https", "www.example.org", 443); |
| 17096 | HostPortPair alternative("www.example.org", 444); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17097 | |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17098 | // Negotiate HTTP/1.1 with alternative. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17099 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17100 | ssl.next_proto = kProtoHTTP11; |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17101 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 17102 | |
| 17103 | // No data should be read from the alternative, because HTTP/1.1 is |
| 17104 | // negotiated. |
| 17105 | StaticSocketDataProvider data; |
| 17106 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 17107 | |
| 17108 | // 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] | 17109 | // negotiated. In order to test this, a failed connection to the server is |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17110 | // mocked. This way the request relies on the alternate Job. |
| 17111 | StaticSocketDataProvider data_refused; |
| 17112 | data_refused.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_REFUSED)); |
| 17113 | session_deps_.socket_factory->AddSocketDataProvider(&data_refused); |
| 17114 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17115 | // Set up alternative service for server. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17116 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 17117 | HttpServerProperties* http_server_properties = |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17118 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 17119 | AlternativeService alternative_service(kProtoHTTP2, alternative); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 17120 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 17121 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 17122 | server, NetworkIsolationKey(), alternative_service, expiration); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17123 | |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17124 | HttpRequestInfo request; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 17125 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17126 | request.method = "GET"; |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17127 | request.url = GURL("https://www.example.org:443"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17128 | request.traffic_annotation = |
| 17129 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17130 | TestCompletionCallback callback; |
| 17131 | |
| 17132 | // HTTP/2 (or SPDY) is required for alternative service, if HTTP/1.1 is |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17133 | // negotiated, the alternate Job should fail with ERR_ALPN_NEGOTIATION_FAILED. |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17134 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17135 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_ALPN_NEGOTIATION_FAILED)); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17136 | } |
| 17137 | |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17138 | // 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] | 17139 | // server, and an alternate one to the alternative server. If the former |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17140 | // succeeds, the request should succeed, even if the latter fails because |
| 17141 | // HTTP/1.1 is negotiated which is insufficient for alternative service. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17142 | TEST_F(HttpNetworkTransactionTest, FailedAlternativeServiceIsNotUserVisible) { |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17143 | url::SchemeHostPort server("https", "www.example.org", 443); |
| 17144 | HostPortPair alternative("www.example.org", 444); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17145 | |
| 17146 | // Negotiate HTTP/1.1 with alternative. |
| 17147 | SSLSocketDataProvider alternative_ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17148 | alternative_ssl.next_proto = kProtoHTTP11; |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17149 | session_deps_.socket_factory->AddSSLSocketDataProvider(&alternative_ssl); |
| 17150 | |
| 17151 | // No data should be read from the alternative, because HTTP/1.1 is |
| 17152 | // negotiated. |
| 17153 | StaticSocketDataProvider data; |
| 17154 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 17155 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17156 | // Negotiate HTTP/1.1 with server. |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17157 | SSLSocketDataProvider origin_ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17158 | origin_ssl.next_proto = kProtoHTTP11; |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17159 | session_deps_.socket_factory->AddSSLSocketDataProvider(&origin_ssl); |
| 17160 | |
| 17161 | MockWrite http_writes[] = { |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17162 | MockWrite("GET / HTTP/1.1\r\n" |
| 17163 | "Host: www.example.org\r\n" |
| 17164 | "Connection: keep-alive\r\n\r\n"), |
| 17165 | MockWrite("GET /second HTTP/1.1\r\n" |
| 17166 | "Host: www.example.org\r\n" |
| 17167 | "Connection: keep-alive\r\n\r\n"), |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17168 | }; |
| 17169 | |
| 17170 | MockRead http_reads[] = { |
| 17171 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 17172 | MockRead("Content-Type: text/html\r\n"), |
| 17173 | MockRead("Content-Length: 6\r\n\r\n"), |
| 17174 | MockRead("foobar"), |
| 17175 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 17176 | MockRead("Content-Type: text/html\r\n"), |
| 17177 | MockRead("Content-Length: 7\r\n\r\n"), |
| 17178 | MockRead("another"), |
| 17179 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17180 | StaticSocketDataProvider http_data(http_reads, http_writes); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17181 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 17182 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17183 | // Set up alternative service for server. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17184 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 17185 | HttpServerProperties* http_server_properties = |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17186 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 17187 | AlternativeService alternative_service(kProtoHTTP2, alternative); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 17188 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 17189 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 17190 | server, NetworkIsolationKey(), alternative_service, expiration); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17191 | |
| 17192 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
| 17193 | HttpRequestInfo request1; |
| 17194 | request1.method = "GET"; |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17195 | request1.url = GURL("https://www.example.org:443"); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17196 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17197 | request1.traffic_annotation = |
| 17198 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17199 | TestCompletionCallback callback1; |
| 17200 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17201 | int rv = trans1.Start(&request1, callback1.callback(), NetLogWithSource()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17202 | rv = callback1.GetResult(rv); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17203 | EXPECT_THAT(rv, IsOk()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17204 | |
| 17205 | const HttpResponseInfo* response1 = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17206 | ASSERT_TRUE(response1); |
| 17207 | ASSERT_TRUE(response1->headers); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17208 | EXPECT_EQ("HTTP/1.1 200 OK", response1->headers->GetStatusLine()); |
| 17209 | |
| 17210 | std::string response_data1; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17211 | ASSERT_THAT(ReadTransaction(&trans1, &response_data1), IsOk()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17212 | EXPECT_EQ("foobar", response_data1); |
| 17213 | |
| 17214 | // Alternative should be marked as broken, because HTTP/1.1 is not sufficient |
| 17215 | // for alternative service. |
Matt Menke | b32ba512 | 2019-09-10 19:17:05 | [diff] [blame] | 17216 | EXPECT_TRUE(http_server_properties->IsAlternativeServiceBroken( |
| 17217 | alternative_service, NetworkIsolationKey())); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17218 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17219 | // Since |alternative_service| is broken, a second transaction to server |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17220 | // should not start an alternate Job. It should pool to existing connection |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17221 | // to server. |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17222 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
| 17223 | HttpRequestInfo request2; |
| 17224 | request2.method = "GET"; |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17225 | request2.url = GURL("https://www.example.org:443/second"); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17226 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17227 | request2.traffic_annotation = |
| 17228 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17229 | TestCompletionCallback callback2; |
| 17230 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17231 | rv = trans2.Start(&request2, callback2.callback(), NetLogWithSource()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17232 | rv = callback2.GetResult(rv); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17233 | EXPECT_THAT(rv, IsOk()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17234 | |
| 17235 | const HttpResponseInfo* response2 = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17236 | ASSERT_TRUE(response2); |
| 17237 | ASSERT_TRUE(response2->headers); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17238 | EXPECT_EQ("HTTP/1.1 200 OK", response2->headers->GetStatusLine()); |
| 17239 | |
| 17240 | std::string response_data2; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17241 | ASSERT_THAT(ReadTransaction(&trans2, &response_data2), IsOk()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17242 | EXPECT_EQ("another", response_data2); |
| 17243 | } |
| 17244 | |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17245 | // Alternative service requires HTTP/2 (or SPDY), but there is already a |
| 17246 | // HTTP/1.1 socket open to the alternative server. That socket should not be |
| 17247 | // used. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17248 | TEST_F(HttpNetworkTransactionTest, AlternativeServiceShouldNotPoolToHttp11) { |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17249 | url::SchemeHostPort server("https", "origin.example.org", 443); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17250 | HostPortPair alternative("alternative.example.org", 443); |
| 17251 | std::string origin_url = "https://origin.example.org:443"; |
| 17252 | std::string alternative_url = "https://alternative.example.org:443"; |
| 17253 | |
| 17254 | // Negotiate HTTP/1.1 with alternative.example.org. |
| 17255 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17256 | ssl.next_proto = kProtoHTTP11; |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17257 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 17258 | |
| 17259 | // HTTP/1.1 data for |request1| and |request2|. |
| 17260 | MockWrite http_writes[] = { |
| 17261 | MockWrite( |
| 17262 | "GET / HTTP/1.1\r\n" |
| 17263 | "Host: alternative.example.org\r\n" |
| 17264 | "Connection: keep-alive\r\n\r\n"), |
| 17265 | MockWrite( |
| 17266 | "GET / HTTP/1.1\r\n" |
| 17267 | "Host: alternative.example.org\r\n" |
| 17268 | "Connection: keep-alive\r\n\r\n"), |
| 17269 | }; |
| 17270 | |
| 17271 | MockRead http_reads[] = { |
| 17272 | MockRead( |
| 17273 | "HTTP/1.1 200 OK\r\n" |
| 17274 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 17275 | "Content-Length: 40\r\n\r\n" |
| 17276 | "first HTTP/1.1 response from alternative"), |
| 17277 | MockRead( |
| 17278 | "HTTP/1.1 200 OK\r\n" |
| 17279 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 17280 | "Content-Length: 41\r\n\r\n" |
| 17281 | "second HTTP/1.1 response from alternative"), |
| 17282 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17283 | StaticSocketDataProvider http_data(http_reads, http_writes); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17284 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 17285 | |
| 17286 | // This test documents that an alternate Job should not pool to an already |
| 17287 | // existing HTTP/1.1 connection. In order to test this, a failed connection |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17288 | // to the server is mocked. This way |request2| relies on the alternate Job. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17289 | StaticSocketDataProvider data_refused; |
| 17290 | data_refused.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_REFUSED)); |
| 17291 | session_deps_.socket_factory->AddSocketDataProvider(&data_refused); |
| 17292 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17293 | // Set up alternative service for server. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17294 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 17295 | HttpServerProperties* http_server_properties = |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17296 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 17297 | AlternativeService alternative_service(kProtoHTTP2, alternative); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 17298 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 17299 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 17300 | server, NetworkIsolationKey(), alternative_service, expiration); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17301 | |
| 17302 | // First transaction to alternative to open an HTTP/1.1 socket. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17303 | HttpRequestInfo request1; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 17304 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17305 | request1.method = "GET"; |
| 17306 | request1.url = GURL(alternative_url); |
| 17307 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17308 | request1.traffic_annotation = |
| 17309 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17310 | TestCompletionCallback callback1; |
| 17311 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17312 | int rv = trans1.Start(&request1, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17313 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17314 | const HttpResponseInfo* response1 = trans1.GetResponseInfo(); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17315 | ASSERT_TRUE(response1); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17316 | ASSERT_TRUE(response1->headers); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17317 | EXPECT_EQ("HTTP/1.1 200 OK", response1->headers->GetStatusLine()); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17318 | EXPECT_TRUE(response1->was_alpn_negotiated); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17319 | EXPECT_FALSE(response1->was_fetched_via_spdy); |
| 17320 | std::string response_data1; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17321 | ASSERT_THAT(ReadTransaction(&trans1, &response_data1), IsOk()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17322 | EXPECT_EQ("first HTTP/1.1 response from alternative", response_data1); |
| 17323 | |
| 17324 | // Request for origin.example.org, which has an alternative service. This |
| 17325 | // will start two Jobs: the alternative looks for connections to pool to, |
| 17326 | // 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] | 17327 | // open other connections to alternative server. The Job to server fails, so |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17328 | // this request fails. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17329 | HttpRequestInfo request2; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 17330 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17331 | request2.method = "GET"; |
| 17332 | request2.url = GURL(origin_url); |
| 17333 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17334 | request2.traffic_annotation = |
| 17335 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17336 | TestCompletionCallback callback2; |
| 17337 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17338 | rv = trans2.Start(&request2, callback2.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17339 | EXPECT_THAT(callback2.GetResult(rv), IsError(ERR_CONNECTION_REFUSED)); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17340 | |
| 17341 | // Another transaction to alternative. This is to test that the HTTP/1.1 |
| 17342 | // socket is still open and in the pool. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17343 | HttpRequestInfo request3; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 17344 | HttpNetworkTransaction trans3(DEFAULT_PRIORITY, session.get()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17345 | request3.method = "GET"; |
| 17346 | request3.url = GURL(alternative_url); |
| 17347 | request3.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17348 | request3.traffic_annotation = |
| 17349 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17350 | TestCompletionCallback callback3; |
| 17351 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17352 | rv = trans3.Start(&request3, callback3.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17353 | EXPECT_THAT(callback3.GetResult(rv), IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17354 | const HttpResponseInfo* response3 = trans3.GetResponseInfo(); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17355 | ASSERT_TRUE(response3); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17356 | ASSERT_TRUE(response3->headers); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17357 | EXPECT_EQ("HTTP/1.1 200 OK", response3->headers->GetStatusLine()); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17358 | EXPECT_TRUE(response3->was_alpn_negotiated); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17359 | EXPECT_FALSE(response3->was_fetched_via_spdy); |
| 17360 | std::string response_data3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17361 | ASSERT_THAT(ReadTransaction(&trans3, &response_data3), IsOk()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17362 | EXPECT_EQ("second HTTP/1.1 response from alternative", response_data3); |
| 17363 | } |
| 17364 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17365 | TEST_F(HttpNetworkTransactionTest, DoNotUseSpdySessionForHttpOverTunnel) { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17366 | const std::string https_url = "https://www.example.org:8080/"; |
| 17367 | const std::string http_url = "http://www.example.org:8080/"; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17368 | |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17369 | // Separate SPDY util instance for naked and wrapped requests. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17370 | SpdyTestUtil spdy_util_wrapped; |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17371 | |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17372 | // SPDY GET for HTTPS URL (through CONNECT tunnel) |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17373 | const HostPortPair host_port_pair("www.example.org", 8080); |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 17374 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
| 17375 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 17376 | host_port_pair)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17377 | spdy::SpdySerializedFrame req1( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17378 | spdy_util_wrapped.ConstructSpdyGet(https_url.c_str(), 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17379 | spdy::SpdySerializedFrame wrapped_req1( |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 17380 | spdy_util_.ConstructWrappedSpdyFrame(req1, 1)); |
[email protected] | 601e03f1 | 2014-04-06 16:26:39 | [diff] [blame] | 17381 | |
| 17382 | // SPDY GET for HTTP URL (through the proxy, but not the tunnel). |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17383 | spdy::SpdyHeaderBlock req2_block; |
| 17384 | req2_block[spdy::kHttp2MethodHeader] = "GET"; |
| 17385 | req2_block[spdy::kHttp2AuthorityHeader] = "www.example.org:8080"; |
| 17386 | req2_block[spdy::kHttp2SchemeHeader] = "http"; |
| 17387 | req2_block[spdy::kHttp2PathHeader] = "/"; |
| 17388 | spdy::SpdySerializedFrame req2( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 17389 | spdy_util_.ConstructSpdyHeaders(3, std::move(req2_block), MEDIUM, true)); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17390 | |
| 17391 | MockWrite writes1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17392 | CreateMockWrite(connect, 0), CreateMockWrite(wrapped_req1, 2), |
| 17393 | CreateMockWrite(req2, 6), |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17394 | }; |
| 17395 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17396 | spdy::SpdySerializedFrame conn_resp( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 17397 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17398 | spdy::SpdySerializedFrame resp1( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 17399 | spdy_util_wrapped.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17400 | spdy::SpdySerializedFrame body1( |
| 17401 | spdy_util_wrapped.ConstructSpdyDataFrame(1, true)); |
| 17402 | spdy::SpdySerializedFrame wrapped_resp1( |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17403 | spdy_util_wrapped.ConstructWrappedSpdyFrame(resp1, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17404 | spdy::SpdySerializedFrame wrapped_body1( |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17405 | spdy_util_wrapped.ConstructWrappedSpdyFrame(body1, 1)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17406 | spdy::SpdySerializedFrame resp2( |
| 17407 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17408 | spdy::SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, true)); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17409 | MockRead reads1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17410 | CreateMockRead(conn_resp, 1), |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17411 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17412 | CreateMockRead(wrapped_resp1, 4), |
| 17413 | CreateMockRead(wrapped_body1, 5), |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17414 | MockRead(ASYNC, ERR_IO_PENDING, 7), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17415 | CreateMockRead(resp2, 8), |
| 17416 | CreateMockRead(body2, 9), |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17417 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 10), |
| 17418 | }; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17419 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17420 | SequencedSocketData data1(reads1, writes1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17421 | MockConnect connect_data1(ASYNC, OK); |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 17422 | data1.set_connect_data(connect_data1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17423 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 17424 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 17425 | ProxyResolutionService::CreateFixedFromPacResult( |
| 17426 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 17427 | TestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 17428 | session_deps_.net_log = &log; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17429 | SSLSocketDataProvider ssl1(ASYNC, OK); // to the proxy |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17430 | ssl1.next_proto = kProtoHTTP2; |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17431 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17432 | SSLSocketDataProvider ssl2(ASYNC, OK); // to the server |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17433 | ssl2.next_proto = kProtoHTTP2; |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17434 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 17435 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17436 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17437 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17438 | |
| 17439 | // Start the first transaction to set up the SpdySession |
| 17440 | HttpRequestInfo request1; |
| 17441 | request1.method = "GET"; |
| 17442 | request1.url = GURL(https_url); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17443 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17444 | request1.traffic_annotation = |
| 17445 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17446 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17447 | TestCompletionCallback callback1; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17448 | int rv = trans1.Start(&request1, callback1.callback(), NetLogWithSource()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17449 | |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17450 | // This pause is a hack to avoid running into https://crbug.com/497228. |
| 17451 | data1.RunUntilPaused(); |
| 17452 | base::RunLoop().RunUntilIdle(); |
| 17453 | data1.Resume(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17454 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17455 | EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 17456 | |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 17457 | LoadTimingInfo load_timing_info1; |
| 17458 | EXPECT_TRUE(trans1.GetLoadTimingInfo(&load_timing_info1)); |
| 17459 | TestLoadTimingNotReusedWithPac(load_timing_info1, |
| 17460 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 17461 | |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17462 | // Now, start the HTTP request. |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17463 | HttpRequestInfo request2; |
| 17464 | request2.method = "GET"; |
| 17465 | request2.url = GURL(http_url); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17466 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17467 | request2.traffic_annotation = |
| 17468 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17469 | HttpNetworkTransaction trans2(MEDIUM, session.get()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17470 | TestCompletionCallback callback2; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17471 | rv = trans2.Start(&request2, callback2.callback(), NetLogWithSource()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17472 | |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17473 | // This pause is a hack to avoid running into https://crbug.com/497228. |
| 17474 | data1.RunUntilPaused(); |
| 17475 | base::RunLoop().RunUntilIdle(); |
| 17476 | data1.Resume(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17477 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17478 | |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17479 | EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 17480 | |
| 17481 | LoadTimingInfo load_timing_info2; |
| 17482 | EXPECT_TRUE(trans2.GetLoadTimingInfo(&load_timing_info2)); |
| 17483 | // The established SPDY sessions is considered reused by the HTTP request. |
| 17484 | TestLoadTimingReusedWithPac(load_timing_info2); |
| 17485 | // HTTP requests over a SPDY session should have a different connection |
| 17486 | // socket_log_id than requests over a tunnel. |
| 17487 | 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] | 17488 | } |
| 17489 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17490 | // Test that in the case where we have a SPDY session to a SPDY proxy |
| 17491 | // that we do not pool other origins that resolve to the same IP when |
| 17492 | // the certificate does not match the new origin. |
| 17493 | // http://crbug.com/134690 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17494 | TEST_F(HttpNetworkTransactionTest, DoNotUseSpdySessionIfCertDoesNotMatch) { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17495 | const std::string url1 = "http://www.example.org/"; |
| 17496 | const std::string url2 = "https://news.example.org/"; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17497 | const std::string ip_addr = "1.2.3.4"; |
| 17498 | |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17499 | // Second SpdyTestUtil instance for the second socket. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17500 | SpdyTestUtil spdy_util_secure; |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17501 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17502 | // SPDY GET for HTTP URL (through SPDY proxy) |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17503 | spdy::SpdyHeaderBlock headers( |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17504 | spdy_util_.ConstructGetHeaderBlockForProxy("http://www.example.org/")); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17505 | spdy::SpdySerializedFrame req1( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 17506 | spdy_util_.ConstructSpdyHeaders(1, std::move(headers), LOWEST, true)); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17507 | |
| 17508 | MockWrite writes1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17509 | CreateMockWrite(req1, 0), |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17510 | }; |
| 17511 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17512 | spdy::SpdySerializedFrame resp1( |
| 17513 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17514 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17515 | MockRead reads1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17516 | MockRead(ASYNC, ERR_IO_PENDING, 1), CreateMockRead(resp1, 2), |
| 17517 | CreateMockRead(body1, 3), MockRead(ASYNC, OK, 4), // EOF |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17518 | }; |
| 17519 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17520 | SequencedSocketData data1(reads1, writes1); |
martijn | fe9636e | 2016-02-06 14:33:32 | [diff] [blame] | 17521 | IPAddress ip; |
martijn | 654c8c4 | 2016-02-10 22:10:59 | [diff] [blame] | 17522 | ASSERT_TRUE(ip.AssignFromIPLiteral(ip_addr)); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17523 | IPEndPoint peer_addr = IPEndPoint(ip, 443); |
| 17524 | MockConnect connect_data1(ASYNC, OK, peer_addr); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17525 | data1.set_connect_data(connect_data1); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17526 | |
| 17527 | // SPDY GET for HTTPS URL (direct) |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17528 | spdy::SpdySerializedFrame req2( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17529 | spdy_util_secure.ConstructSpdyGet(url2.c_str(), 1, MEDIUM)); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17530 | |
| 17531 | MockWrite writes2[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17532 | CreateMockWrite(req2, 0), |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17533 | }; |
| 17534 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17535 | spdy::SpdySerializedFrame resp2( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17536 | spdy_util_secure.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17537 | spdy::SpdySerializedFrame body2( |
| 17538 | spdy_util_secure.ConstructSpdyDataFrame(1, true)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17539 | MockRead reads2[] = {CreateMockRead(resp2, 1), CreateMockRead(body2, 2), |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17540 | MockRead(ASYNC, OK, 3)}; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17541 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17542 | SequencedSocketData data2(reads2, writes2); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17543 | MockConnect connect_data2(ASYNC, OK); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17544 | data2.set_connect_data(connect_data2); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17545 | |
| 17546 | // Set up a proxy config that sends HTTP requests to a proxy, and |
| 17547 | // all others direct. |
| 17548 | ProxyConfig proxy_config; |
| 17549 | proxy_config.proxy_rules().ParseFromString("http=https://proxy:443"); |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 17550 | session_deps_.proxy_resolution_service = |
| 17551 | std::make_unique<ProxyResolutionService>( |
| 17552 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 17553 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 17554 | nullptr, nullptr); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17555 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17556 | SSLSocketDataProvider ssl1(ASYNC, OK); // to the proxy |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17557 | ssl1.next_proto = kProtoHTTP2; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17558 | // Load a valid cert. Note, that this does not need to |
| 17559 | // be valid for proxy because the MockSSLClientSocket does |
| 17560 | // not actually verify it. But SpdySession will use this |
| 17561 | // to see if it is valid for the new origin |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 17562 | ssl1.ssl_info.cert = |
| 17563 | ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"); |
| 17564 | ASSERT_TRUE(ssl1.ssl_info.cert); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17565 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 17566 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17567 | |
| 17568 | SSLSocketDataProvider ssl2(ASYNC, OK); // to the server |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17569 | ssl2.next_proto = kProtoHTTP2; |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17570 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 17571 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17572 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 17573 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17574 | session_deps_.host_resolver->rules()->AddRule("news.example.org", ip_addr); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 17575 | session_deps_.host_resolver->rules()->AddRule("proxy", ip_addr); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17576 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17577 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17578 | |
| 17579 | // Start the first transaction to set up the SpdySession |
| 17580 | HttpRequestInfo request1; |
| 17581 | request1.method = "GET"; |
| 17582 | request1.url = GURL(url1); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17583 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17584 | request1.traffic_annotation = |
| 17585 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17586 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17587 | TestCompletionCallback callback1; |
| 17588 | ASSERT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17589 | trans1.Start(&request1, callback1.callback(), NetLogWithSource())); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17590 | // This pause is a hack to avoid running into https://crbug.com/497228. |
| 17591 | data1.RunUntilPaused(); |
| 17592 | base::RunLoop().RunUntilIdle(); |
| 17593 | data1.Resume(); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17594 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17595 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17596 | EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 17597 | |
| 17598 | // Now, start the HTTP request |
| 17599 | HttpRequestInfo request2; |
| 17600 | request2.method = "GET"; |
| 17601 | request2.url = GURL(url2); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17602 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17603 | request2.traffic_annotation = |
| 17604 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17605 | HttpNetworkTransaction trans2(MEDIUM, session.get()); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17606 | TestCompletionCallback callback2; |
| 17607 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17608 | trans2.Start(&request2, callback2.callback(), NetLogWithSource())); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 17609 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17610 | |
| 17611 | ASSERT_TRUE(callback2.have_result()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17612 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17613 | EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 17614 | } |
| 17615 | |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17616 | // Test to verify that a failed socket read (due to an ERR_CONNECTION_CLOSED |
| 17617 | // error) in SPDY session, removes the socket from pool and closes the SPDY |
| 17618 | // session. Verify that new url's from the same HttpNetworkSession (and a new |
| 17619 | // SpdySession) do work. http://crbug.com/224701 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17620 | TEST_F(HttpNetworkTransactionTest, ErrorSocketNotConnected) { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17621 | const std::string https_url = "https://www.example.org/"; |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17622 | |
| 17623 | MockRead reads1[] = { |
| 17624 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED, 0) |
| 17625 | }; |
| 17626 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17627 | SequencedSocketData data1(reads1, base::span<MockWrite>()); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17628 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17629 | spdy::SpdySerializedFrame req2( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17630 | spdy_util_.ConstructSpdyGet(https_url.c_str(), 1, MEDIUM)); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17631 | MockWrite writes2[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17632 | CreateMockWrite(req2, 0), |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17633 | }; |
| 17634 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17635 | spdy::SpdySerializedFrame resp2( |
| 17636 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17637 | spdy::SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17638 | MockRead reads2[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17639 | CreateMockRead(resp2, 1), CreateMockRead(body2, 2), |
| 17640 | MockRead(ASYNC, OK, 3) // EOF |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17641 | }; |
| 17642 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17643 | SequencedSocketData data2(reads2, writes2); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17644 | |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17645 | SSLSocketDataProvider ssl1(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17646 | ssl1.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 17647 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 17648 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17649 | |
| 17650 | SSLSocketDataProvider ssl2(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17651 | ssl2.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 17652 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 17653 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17654 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17655 | std::unique_ptr<HttpNetworkSession> session( |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 17656 | SpdySessionDependencies::SpdyCreateSession(&session_deps_)); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17657 | |
| 17658 | // Start the first transaction to set up the SpdySession and verify that |
| 17659 | // connection was closed. |
| 17660 | HttpRequestInfo request1; |
| 17661 | request1.method = "GET"; |
| 17662 | request1.url = GURL(https_url); |
| 17663 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17664 | request1.traffic_annotation = |
| 17665 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17666 | HttpNetworkTransaction trans1(MEDIUM, session.get()); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17667 | TestCompletionCallback callback1; |
| 17668 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17669 | trans1.Start(&request1, callback1.callback(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17670 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_CLOSED)); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17671 | |
| 17672 | // Now, start the second request and make sure it succeeds. |
| 17673 | HttpRequestInfo request2; |
| 17674 | request2.method = "GET"; |
| 17675 | request2.url = GURL(https_url); |
| 17676 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17677 | request2.traffic_annotation = |
| 17678 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17679 | HttpNetworkTransaction trans2(MEDIUM, session.get()); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17680 | TestCompletionCallback callback2; |
| 17681 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17682 | trans2.Start(&request2, callback2.callback(), NetLogWithSource())); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17683 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17684 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17685 | EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 17686 | } |
| 17687 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17688 | TEST_F(HttpNetworkTransactionTest, CloseIdleSpdySessionToOpenNewOne) { |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17689 | ClientSocketPoolManager::set_max_sockets_per_group( |
| 17690 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 17691 | ClientSocketPoolManager::set_max_sockets_per_pool( |
| 17692 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 17693 | |
| 17694 | // Use two different hosts with different IPs so they don't get pooled. |
| 17695 | session_deps_.host_resolver->rules()->AddRule("www.a.com", "10.0.0.1"); |
| 17696 | session_deps_.host_resolver->rules()->AddRule("www.b.com", "10.0.0.2"); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17697 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17698 | |
| 17699 | SSLSocketDataProvider ssl1(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17700 | ssl1.next_proto = kProtoHTTP2; |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17701 | SSLSocketDataProvider ssl2(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17702 | ssl2.next_proto = kProtoHTTP2; |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17703 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 17704 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 17705 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17706 | spdy::SpdySerializedFrame host1_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17707 | spdy_util_.ConstructSpdyGet("https://www.a.com", 1, DEFAULT_PRIORITY)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17708 | MockWrite spdy1_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17709 | CreateMockWrite(host1_req, 0), |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17710 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17711 | spdy::SpdySerializedFrame host1_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17712 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17713 | spdy::SpdySerializedFrame host1_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17714 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17715 | MockRead spdy1_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17716 | CreateMockRead(host1_resp, 1), CreateMockRead(host1_resp_body, 2), |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 17717 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 3), |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17718 | }; |
| 17719 | |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17720 | // Use a separate test instance for the separate SpdySession that will be |
| 17721 | // created. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17722 | SpdyTestUtil spdy_util_2; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17723 | SequencedSocketData spdy1_data(spdy1_reads, spdy1_writes); |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 17724 | session_deps_.socket_factory->AddSocketDataProvider(&spdy1_data); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17725 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17726 | spdy::SpdySerializedFrame host2_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17727 | spdy_util_2.ConstructSpdyGet("https://www.b.com", 1, DEFAULT_PRIORITY)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17728 | MockWrite spdy2_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17729 | CreateMockWrite(host2_req, 0), |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17730 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17731 | spdy::SpdySerializedFrame host2_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17732 | spdy_util_2.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17733 | spdy::SpdySerializedFrame host2_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17734 | spdy_util_2.ConstructSpdyDataFrame(1, true)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17735 | MockRead spdy2_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17736 | CreateMockRead(host2_resp, 1), CreateMockRead(host2_resp_body, 2), |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 17737 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 3), |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17738 | }; |
| 17739 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17740 | SequencedSocketData spdy2_data(spdy2_reads, spdy2_writes); |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 17741 | session_deps_.socket_factory->AddSocketDataProvider(&spdy2_data); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17742 | |
| 17743 | MockWrite http_write[] = { |
| 17744 | MockWrite("GET / HTTP/1.1\r\n" |
| 17745 | "Host: www.a.com\r\n" |
| 17746 | "Connection: keep-alive\r\n\r\n"), |
| 17747 | }; |
| 17748 | |
| 17749 | MockRead http_read[] = { |
| 17750 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 17751 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 17752 | MockRead("Content-Length: 6\r\n\r\n"), |
| 17753 | MockRead("hello!"), |
| 17754 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17755 | StaticSocketDataProvider http_data(http_read, http_write); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17756 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 17757 | |
| 17758 | HostPortPair host_port_pair_a("www.a.com", 443); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 17759 | SpdySessionKey spdy_session_key_a( |
| 17760 | host_port_pair_a, ProxyServer::Direct(), PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 17761 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 17762 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17763 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17764 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17765 | |
| 17766 | TestCompletionCallback callback; |
| 17767 | HttpRequestInfo request1; |
| 17768 | request1.method = "GET"; |
| 17769 | request1.url = GURL("https://www.a.com/"); |
| 17770 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17771 | request1.traffic_annotation = |
| 17772 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 17773 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 17774 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17775 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17776 | int rv = trans->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17777 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17778 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17779 | |
| 17780 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17781 | ASSERT_TRUE(response); |
| 17782 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 17783 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17784 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17785 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17786 | |
| 17787 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17788 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17789 | EXPECT_EQ("hello!", response_data); |
| 17790 | trans.reset(); |
| 17791 | EXPECT_TRUE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17792 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17793 | |
| 17794 | HostPortPair host_port_pair_b("www.b.com", 443); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 17795 | SpdySessionKey spdy_session_key_b( |
| 17796 | host_port_pair_b, ProxyServer::Direct(), PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 17797 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 17798 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17799 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17800 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_b)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17801 | HttpRequestInfo request2; |
| 17802 | request2.method = "GET"; |
| 17803 | request2.url = GURL("https://www.b.com/"); |
| 17804 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17805 | request2.traffic_annotation = |
| 17806 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 17807 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 17808 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17809 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17810 | rv = trans->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17811 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17812 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17813 | |
| 17814 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17815 | ASSERT_TRUE(response); |
| 17816 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 17817 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17818 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17819 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17820 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17821 | EXPECT_EQ("hello!", response_data); |
| 17822 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17823 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17824 | EXPECT_TRUE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17825 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_b)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17826 | |
| 17827 | HostPortPair host_port_pair_a1("www.a.com", 80); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 17828 | SpdySessionKey spdy_session_key_a1( |
| 17829 | host_port_pair_a1, ProxyServer::Direct(), PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 17830 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 17831 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17832 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17833 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a1)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17834 | HttpRequestInfo request3; |
| 17835 | request3.method = "GET"; |
| 17836 | request3.url = GURL("http://www.a.com/"); |
| 17837 | request3.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17838 | request3.traffic_annotation = |
| 17839 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 17840 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 17841 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17842 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17843 | rv = trans->Start(&request3, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17844 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17845 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17846 | |
| 17847 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17848 | ASSERT_TRUE(response); |
| 17849 | ASSERT_TRUE(response->headers); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17850 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 17851 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17852 | EXPECT_FALSE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17853 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17854 | EXPECT_EQ("hello!", response_data); |
| 17855 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17856 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17857 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17858 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_b)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17859 | } |
| 17860 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17861 | TEST_F(HttpNetworkTransactionTest, HttpSyncConnectError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17862 | HttpRequestInfo request; |
| 17863 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17864 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17865 | request.traffic_annotation = |
| 17866 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17867 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17868 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17869 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17870 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 17871 | MockConnect mock_connect(SYNCHRONOUS, ERR_NAME_NOT_RESOLVED); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17872 | StaticSocketDataProvider data; |
| 17873 | data.set_connect_data(mock_connect); |
| 17874 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 17875 | |
| 17876 | TestCompletionCallback callback; |
| 17877 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17878 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17879 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17880 | |
| 17881 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17882 | EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17883 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 17884 | ConnectionAttempts attempts; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17885 | trans.GetConnectionAttempts(&attempts); |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 17886 | ASSERT_EQ(1u, attempts.size()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17887 | EXPECT_THAT(attempts[0].result, IsError(ERR_NAME_NOT_RESOLVED)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 17888 | |
| 17889 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17890 | EXPECT_FALSE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 17891 | EXPECT_TRUE(endpoint.address().empty()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17892 | } |
| 17893 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17894 | TEST_F(HttpNetworkTransactionTest, HttpAsyncConnectError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17895 | HttpRequestInfo request; |
| 17896 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17897 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17898 | request.traffic_annotation = |
| 17899 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17900 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17901 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17902 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17903 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 17904 | MockConnect mock_connect(ASYNC, ERR_NAME_NOT_RESOLVED); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17905 | StaticSocketDataProvider data; |
| 17906 | data.set_connect_data(mock_connect); |
| 17907 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 17908 | |
| 17909 | TestCompletionCallback callback; |
| 17910 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17911 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17912 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17913 | |
| 17914 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17915 | EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17916 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 17917 | ConnectionAttempts attempts; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17918 | trans.GetConnectionAttempts(&attempts); |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 17919 | ASSERT_EQ(1u, attempts.size()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17920 | EXPECT_THAT(attempts[0].result, IsError(ERR_NAME_NOT_RESOLVED)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 17921 | |
| 17922 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17923 | EXPECT_FALSE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 17924 | EXPECT_TRUE(endpoint.address().empty()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17925 | } |
| 17926 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17927 | TEST_F(HttpNetworkTransactionTest, HttpSyncWriteError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17928 | HttpRequestInfo request; |
| 17929 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17930 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17931 | request.traffic_annotation = |
| 17932 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17933 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17934 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17935 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17936 | |
| 17937 | MockWrite data_writes[] = { |
| 17938 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 17939 | }; |
| 17940 | MockRead data_reads[] = { |
| 17941 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. |
| 17942 | }; |
| 17943 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17944 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17945 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 17946 | |
| 17947 | TestCompletionCallback callback; |
| 17948 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17949 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17950 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17951 | |
| 17952 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17953 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17954 | } |
| 17955 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17956 | TEST_F(HttpNetworkTransactionTest, HttpAsyncWriteError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17957 | HttpRequestInfo request; |
| 17958 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17959 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17960 | request.traffic_annotation = |
| 17961 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17962 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17963 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17964 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17965 | |
| 17966 | MockWrite data_writes[] = { |
| 17967 | MockWrite(ASYNC, ERR_CONNECTION_RESET), |
| 17968 | }; |
| 17969 | MockRead data_reads[] = { |
| 17970 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. |
| 17971 | }; |
| 17972 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17973 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17974 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 17975 | |
| 17976 | TestCompletionCallback callback; |
| 17977 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17978 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17979 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17980 | |
| 17981 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17982 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17983 | } |
| 17984 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17985 | TEST_F(HttpNetworkTransactionTest, HttpSyncReadError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17986 | HttpRequestInfo request; |
| 17987 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17988 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 17989 | request.traffic_annotation = |
| 17990 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17991 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17992 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17993 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17994 | |
| 17995 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17996 | MockWrite( |
| 17997 | "GET / HTTP/1.1\r\n" |
| 17998 | "Host: www.example.org\r\n" |
| 17999 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18000 | }; |
| 18001 | MockRead data_reads[] = { |
| 18002 | MockRead(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18003 | }; |
| 18004 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18005 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18006 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18007 | |
| 18008 | TestCompletionCallback callback; |
| 18009 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18010 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18011 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18012 | |
| 18013 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18014 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18015 | } |
| 18016 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18017 | TEST_F(HttpNetworkTransactionTest, HttpAsyncReadError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18018 | HttpRequestInfo request; |
| 18019 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18020 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18021 | request.traffic_annotation = |
| 18022 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18023 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18024 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18025 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18026 | |
| 18027 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18028 | MockWrite( |
| 18029 | "GET / HTTP/1.1\r\n" |
| 18030 | "Host: www.example.org\r\n" |
| 18031 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18032 | }; |
| 18033 | MockRead data_reads[] = { |
| 18034 | MockRead(ASYNC, ERR_CONNECTION_RESET), |
| 18035 | }; |
| 18036 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18037 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18038 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18039 | |
| 18040 | TestCompletionCallback callback; |
| 18041 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18042 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18043 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18044 | |
| 18045 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18046 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18047 | } |
| 18048 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18049 | // Tests that when a used socket is returned to the SSL socket pool, it's closed |
| 18050 | // if the transport socket pool is stalled on the global socket limit. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18051 | TEST_F(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18052 | ClientSocketPoolManager::set_max_sockets_per_group( |
| 18053 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 18054 | ClientSocketPoolManager::set_max_sockets_per_pool( |
| 18055 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 18056 | |
| 18057 | // Set up SSL request. |
| 18058 | |
| 18059 | HttpRequestInfo ssl_request; |
| 18060 | ssl_request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18061 | ssl_request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18062 | ssl_request.traffic_annotation = |
| 18063 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18064 | |
| 18065 | MockWrite ssl_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18066 | MockWrite( |
| 18067 | "GET / HTTP/1.1\r\n" |
| 18068 | "Host: www.example.org\r\n" |
| 18069 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18070 | }; |
| 18071 | MockRead ssl_reads[] = { |
| 18072 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 18073 | MockRead("Content-Length: 11\r\n\r\n"), |
| 18074 | MockRead("hello world"), |
| 18075 | MockRead(SYNCHRONOUS, OK), |
| 18076 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18077 | StaticSocketDataProvider ssl_data(ssl_reads, ssl_writes); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18078 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_data); |
| 18079 | |
| 18080 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 18081 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 18082 | |
| 18083 | // Set up HTTP request. |
| 18084 | |
| 18085 | HttpRequestInfo http_request; |
| 18086 | http_request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18087 | http_request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18088 | http_request.traffic_annotation = |
| 18089 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18090 | |
| 18091 | MockWrite http_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18092 | MockWrite( |
| 18093 | "GET / HTTP/1.1\r\n" |
| 18094 | "Host: www.example.org\r\n" |
| 18095 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18096 | }; |
| 18097 | MockRead http_reads[] = { |
| 18098 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 18099 | MockRead("Content-Length: 7\r\n\r\n"), |
| 18100 | MockRead("falafel"), |
| 18101 | MockRead(SYNCHRONOUS, OK), |
| 18102 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18103 | StaticSocketDataProvider http_data(http_reads, http_writes); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18104 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 18105 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18106 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18107 | |
| 18108 | // Start the SSL request. |
| 18109 | TestCompletionCallback ssl_callback; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18110 | HttpNetworkTransaction ssl_trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18111 | ASSERT_EQ(ERR_IO_PENDING, |
| 18112 | ssl_trans.Start(&ssl_request, ssl_callback.callback(), |
| 18113 | NetLogWithSource())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18114 | |
| 18115 | // Start the HTTP request. Pool should stall. |
| 18116 | TestCompletionCallback http_callback; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18117 | HttpNetworkTransaction http_trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18118 | ASSERT_EQ(ERR_IO_PENDING, |
| 18119 | http_trans.Start(&http_request, http_callback.callback(), |
| 18120 | NetLogWithSource())); |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18121 | EXPECT_TRUE(IsTransportSocketPoolStalled(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18122 | |
| 18123 | // Wait for response from SSL request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18124 | ASSERT_THAT(ssl_callback.WaitForResult(), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18125 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18126 | ASSERT_THAT(ReadTransaction(&ssl_trans, &response_data), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18127 | EXPECT_EQ("hello world", response_data); |
| 18128 | |
| 18129 | // The SSL socket should automatically be closed, so the HTTP request can |
| 18130 | // start. |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 18131 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18132 | ASSERT_FALSE(IsTransportSocketPoolStalled(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18133 | |
| 18134 | // The HTTP request can now complete. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18135 | ASSERT_THAT(http_callback.WaitForResult(), IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18136 | ASSERT_THAT(ReadTransaction(&http_trans, &response_data), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18137 | EXPECT_EQ("falafel", response_data); |
| 18138 | |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18139 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18140 | } |
| 18141 | |
| 18142 | // Tests that when a SSL connection is established but there's no corresponding |
| 18143 | // request that needs it, the new socket is closed if the transport socket pool |
| 18144 | // is stalled on the global socket limit. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18145 | TEST_F(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest2) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18146 | ClientSocketPoolManager::set_max_sockets_per_group( |
| 18147 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 18148 | ClientSocketPoolManager::set_max_sockets_per_pool( |
| 18149 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 18150 | |
| 18151 | // Set up an ssl request. |
| 18152 | |
| 18153 | HttpRequestInfo ssl_request; |
| 18154 | ssl_request.method = "GET"; |
| 18155 | ssl_request.url = GURL("https://www.foopy.com/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18156 | ssl_request.traffic_annotation = |
| 18157 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18158 | |
| 18159 | // No data will be sent on the SSL socket. |
| 18160 | StaticSocketDataProvider ssl_data; |
| 18161 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_data); |
| 18162 | |
| 18163 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 18164 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 18165 | |
| 18166 | // Set up HTTP request. |
| 18167 | |
| 18168 | HttpRequestInfo http_request; |
| 18169 | http_request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18170 | http_request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18171 | http_request.traffic_annotation = |
| 18172 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18173 | |
| 18174 | MockWrite http_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18175 | MockWrite( |
| 18176 | "GET / HTTP/1.1\r\n" |
| 18177 | "Host: www.example.org\r\n" |
| 18178 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18179 | }; |
| 18180 | MockRead http_reads[] = { |
| 18181 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 18182 | MockRead("Content-Length: 7\r\n\r\n"), |
| 18183 | MockRead("falafel"), |
| 18184 | MockRead(SYNCHRONOUS, OK), |
| 18185 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18186 | StaticSocketDataProvider http_data(http_reads, http_writes); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18187 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 18188 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18189 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18190 | |
| 18191 | // Preconnect an SSL socket. A preconnect is needed because connect jobs are |
| 18192 | // cancelled when a normal transaction is cancelled. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 18193 | HttpStreamFactory* http_stream_factory = session->http_stream_factory(); |
nharper | 8cdb0fb | 2016-04-22 21:34:59 | [diff] [blame] | 18194 | http_stream_factory->PreconnectStreams(1, ssl_request); |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 18195 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18196 | |
| 18197 | // Start the HTTP request. Pool should stall. |
| 18198 | TestCompletionCallback http_callback; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18199 | HttpNetworkTransaction http_trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18200 | ASSERT_EQ(ERR_IO_PENDING, |
| 18201 | http_trans.Start(&http_request, http_callback.callback(), |
| 18202 | NetLogWithSource())); |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18203 | EXPECT_TRUE(IsTransportSocketPoolStalled(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18204 | |
| 18205 | // The SSL connection will automatically be closed once the connection is |
| 18206 | // established, to let the HTTP request start. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18207 | ASSERT_THAT(http_callback.WaitForResult(), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18208 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18209 | ASSERT_THAT(ReadTransaction(&http_trans, &response_data), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18210 | EXPECT_EQ("falafel", response_data); |
| 18211 | |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18212 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18213 | } |
| 18214 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18215 | TEST_F(HttpNetworkTransactionTest, PostReadsErrorResponseAfterReset) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18216 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18217 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18218 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18219 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18220 | |
| 18221 | HttpRequestInfo request; |
| 18222 | request.method = "POST"; |
| 18223 | request.url = GURL("http://www.foo.com/"); |
| 18224 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18225 | request.traffic_annotation = |
| 18226 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18227 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18228 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18229 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18230 | // Send headers successfully, but get an error while sending the body. |
| 18231 | MockWrite data_writes[] = { |
| 18232 | MockWrite("POST / HTTP/1.1\r\n" |
| 18233 | "Host: www.foo.com\r\n" |
| 18234 | "Connection: keep-alive\r\n" |
| 18235 | "Content-Length: 3\r\n\r\n"), |
| 18236 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18237 | }; |
| 18238 | |
| 18239 | MockRead data_reads[] = { |
| 18240 | MockRead("HTTP/1.0 400 Not OK\r\n\r\n"), |
| 18241 | MockRead("hello world"), |
| 18242 | MockRead(SYNCHRONOUS, OK), |
| 18243 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18244 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18245 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18246 | |
| 18247 | TestCompletionCallback callback; |
| 18248 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18249 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18250 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18251 | |
| 18252 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18253 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18254 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18255 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18256 | ASSERT_TRUE(response); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18257 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18258 | EXPECT_TRUE(response->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18259 | EXPECT_EQ("HTTP/1.0 400 Not OK", response->headers->GetStatusLine()); |
| 18260 | |
| 18261 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18262 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18263 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18264 | EXPECT_EQ("hello world", response_data); |
| 18265 | } |
| 18266 | |
| 18267 | // This test makes sure the retry logic doesn't trigger when reading an error |
| 18268 | // response from a server that rejected a POST with a CONNECTION_RESET. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18269 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18270 | PostReadsErrorResponseAfterResetOnReusedSocket) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18271 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18272 | MockWrite data_writes[] = { |
| 18273 | MockWrite("GET / HTTP/1.1\r\n" |
| 18274 | "Host: www.foo.com\r\n" |
| 18275 | "Connection: keep-alive\r\n\r\n"), |
| 18276 | MockWrite("POST / HTTP/1.1\r\n" |
| 18277 | "Host: www.foo.com\r\n" |
| 18278 | "Connection: keep-alive\r\n" |
| 18279 | "Content-Length: 3\r\n\r\n"), |
| 18280 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18281 | }; |
| 18282 | |
| 18283 | MockRead data_reads[] = { |
| 18284 | MockRead("HTTP/1.1 200 Peachy\r\n" |
| 18285 | "Content-Length: 14\r\n\r\n"), |
| 18286 | MockRead("first response"), |
| 18287 | MockRead("HTTP/1.1 400 Not OK\r\n" |
| 18288 | "Content-Length: 15\r\n\r\n"), |
| 18289 | MockRead("second response"), |
| 18290 | MockRead(SYNCHRONOUS, OK), |
| 18291 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18292 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18293 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18294 | |
| 18295 | TestCompletionCallback callback; |
| 18296 | HttpRequestInfo request1; |
| 18297 | request1.method = "GET"; |
| 18298 | request1.url = GURL("http://www.foo.com/"); |
| 18299 | request1.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18300 | request1.traffic_annotation = |
| 18301 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18302 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 18303 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18304 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18305 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18306 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18307 | |
| 18308 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18309 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18310 | |
| 18311 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18312 | ASSERT_TRUE(response1); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18313 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18314 | EXPECT_TRUE(response1->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18315 | EXPECT_EQ("HTTP/1.1 200 Peachy", response1->headers->GetStatusLine()); |
| 18316 | |
| 18317 | std::string response_data1; |
| 18318 | rv = ReadTransaction(trans1.get(), &response_data1); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18319 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18320 | EXPECT_EQ("first response", response_data1); |
| 18321 | // Delete the transaction to release the socket back into the socket pool. |
| 18322 | trans1.reset(); |
| 18323 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18324 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18325 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18326 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18327 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18328 | |
| 18329 | HttpRequestInfo request2; |
| 18330 | request2.method = "POST"; |
| 18331 | request2.url = GURL("http://www.foo.com/"); |
| 18332 | request2.upload_data_stream = &upload_data_stream; |
| 18333 | request2.load_flags = 0; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18334 | request2.traffic_annotation = |
| 18335 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18336 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18337 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18338 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18339 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18340 | |
| 18341 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18342 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18343 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18344 | const HttpResponseInfo* response2 = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18345 | ASSERT_TRUE(response2); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18346 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18347 | EXPECT_TRUE(response2->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18348 | EXPECT_EQ("HTTP/1.1 400 Not OK", response2->headers->GetStatusLine()); |
| 18349 | |
| 18350 | std::string response_data2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18351 | rv = ReadTransaction(&trans2, &response_data2); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18352 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18353 | EXPECT_EQ("second response", response_data2); |
| 18354 | } |
| 18355 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18356 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18357 | PostReadsErrorResponseAfterResetPartialBodySent) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18358 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18359 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18360 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18361 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18362 | |
| 18363 | HttpRequestInfo request; |
| 18364 | request.method = "POST"; |
| 18365 | request.url = GURL("http://www.foo.com/"); |
| 18366 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18367 | request.traffic_annotation = |
| 18368 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18369 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18370 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18371 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18372 | // Send headers successfully, but get an error while sending the body. |
| 18373 | MockWrite data_writes[] = { |
| 18374 | MockWrite("POST / HTTP/1.1\r\n" |
| 18375 | "Host: www.foo.com\r\n" |
| 18376 | "Connection: keep-alive\r\n" |
| 18377 | "Content-Length: 3\r\n\r\n" |
| 18378 | "fo"), |
| 18379 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18380 | }; |
| 18381 | |
| 18382 | MockRead data_reads[] = { |
| 18383 | MockRead("HTTP/1.0 400 Not OK\r\n\r\n"), |
| 18384 | MockRead("hello world"), |
| 18385 | MockRead(SYNCHRONOUS, OK), |
| 18386 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18387 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18388 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18389 | |
| 18390 | TestCompletionCallback callback; |
| 18391 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18392 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18393 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18394 | |
| 18395 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18396 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18397 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18398 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18399 | ASSERT_TRUE(response); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18400 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18401 | EXPECT_TRUE(response->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18402 | EXPECT_EQ("HTTP/1.0 400 Not OK", response->headers->GetStatusLine()); |
| 18403 | |
| 18404 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18405 | rv = ReadTransaction(&trans, &response_data); |
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 | EXPECT_EQ("hello world", response_data); |
| 18408 | } |
| 18409 | |
| 18410 | // This tests the more common case than the previous test, where headers and |
| 18411 | // body are not merged into a single request. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18412 | TEST_F(HttpNetworkTransactionTest, ChunkedPostReadsErrorResponseAfterReset) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 18413 | ChunkedUploadDataStream upload_data_stream(0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18414 | |
| 18415 | HttpRequestInfo request; |
| 18416 | request.method = "POST"; |
| 18417 | request.url = GURL("http://www.foo.com/"); |
| 18418 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18419 | request.traffic_annotation = |
| 18420 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18421 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18422 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18423 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18424 | // Send headers successfully, but get an error while sending the body. |
| 18425 | MockWrite data_writes[] = { |
| 18426 | MockWrite("POST / HTTP/1.1\r\n" |
| 18427 | "Host: www.foo.com\r\n" |
| 18428 | "Connection: keep-alive\r\n" |
| 18429 | "Transfer-Encoding: chunked\r\n\r\n"), |
| 18430 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18431 | }; |
| 18432 | |
| 18433 | MockRead data_reads[] = { |
| 18434 | MockRead("HTTP/1.0 400 Not OK\r\n\r\n"), |
| 18435 | MockRead("hello world"), |
| 18436 | MockRead(SYNCHRONOUS, OK), |
| 18437 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18438 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18439 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18440 | |
| 18441 | TestCompletionCallback callback; |
| 18442 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18443 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18444 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18445 | // Make sure the headers are sent before adding a chunk. This ensures that |
| 18446 | // they can't be merged with the body in a single send. Not currently |
| 18447 | // necessary since a chunked body is never merged with headers, but this makes |
| 18448 | // the test more future proof. |
| 18449 | base::RunLoop().RunUntilIdle(); |
| 18450 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 18451 | upload_data_stream.AppendData("last chunk", 10, true); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18452 | |
| 18453 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18454 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18455 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18456 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18457 | ASSERT_TRUE(response); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18458 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18459 | EXPECT_TRUE(response->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18460 | EXPECT_EQ("HTTP/1.0 400 Not OK", response->headers->GetStatusLine()); |
| 18461 | |
| 18462 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18463 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18464 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18465 | EXPECT_EQ("hello world", response_data); |
| 18466 | } |
| 18467 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18468 | TEST_F(HttpNetworkTransactionTest, PostReadsErrorResponseAfterResetAnd100) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18469 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18470 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18471 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18472 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18473 | |
| 18474 | HttpRequestInfo request; |
| 18475 | request.method = "POST"; |
| 18476 | request.url = GURL("http://www.foo.com/"); |
| 18477 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18478 | request.traffic_annotation = |
| 18479 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18480 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18481 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18482 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18483 | |
| 18484 | MockWrite data_writes[] = { |
| 18485 | MockWrite("POST / HTTP/1.1\r\n" |
| 18486 | "Host: www.foo.com\r\n" |
| 18487 | "Connection: keep-alive\r\n" |
| 18488 | "Content-Length: 3\r\n\r\n"), |
| 18489 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18490 | }; |
| 18491 | |
| 18492 | MockRead data_reads[] = { |
| 18493 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 18494 | MockRead("HTTP/1.0 400 Not OK\r\n\r\n"), |
| 18495 | MockRead("hello world"), |
| 18496 | MockRead(SYNCHRONOUS, OK), |
| 18497 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18498 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18499 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18500 | |
| 18501 | TestCompletionCallback callback; |
| 18502 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18503 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18504 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18505 | |
| 18506 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18507 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18508 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18509 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18510 | ASSERT_TRUE(response); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18511 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18512 | EXPECT_TRUE(response->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18513 | EXPECT_EQ("HTTP/1.0 400 Not OK", response->headers->GetStatusLine()); |
| 18514 | |
| 18515 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18516 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18517 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18518 | EXPECT_EQ("hello world", response_data); |
| 18519 | } |
| 18520 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18521 | TEST_F(HttpNetworkTransactionTest, PostIgnoresNonErrorResponseAfterReset) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18522 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18523 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18524 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18525 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18526 | |
| 18527 | HttpRequestInfo request; |
| 18528 | request.method = "POST"; |
| 18529 | request.url = GURL("http://www.foo.com/"); |
| 18530 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18531 | request.traffic_annotation = |
| 18532 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18533 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18534 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18535 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18536 | // Send headers successfully, but get an error while sending the body. |
| 18537 | MockWrite data_writes[] = { |
| 18538 | MockWrite("POST / HTTP/1.1\r\n" |
| 18539 | "Host: www.foo.com\r\n" |
| 18540 | "Connection: keep-alive\r\n" |
| 18541 | "Content-Length: 3\r\n\r\n"), |
| 18542 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18543 | }; |
| 18544 | |
| 18545 | MockRead data_reads[] = { |
| 18546 | MockRead("HTTP/1.0 200 Just Dandy\r\n\r\n"), |
| 18547 | MockRead("hello world"), |
| 18548 | MockRead(SYNCHRONOUS, OK), |
| 18549 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18550 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18551 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18552 | |
| 18553 | TestCompletionCallback callback; |
| 18554 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18555 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18556 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18557 | |
| 18558 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18559 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18560 | } |
| 18561 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18562 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18563 | PostIgnoresNonErrorResponseAfterResetAnd100) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18564 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18565 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18566 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18567 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18568 | |
| 18569 | HttpRequestInfo request; |
| 18570 | request.method = "POST"; |
| 18571 | request.url = GURL("http://www.foo.com/"); |
| 18572 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18573 | request.traffic_annotation = |
| 18574 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18575 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18576 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18577 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18578 | // Send headers successfully, but get an error while sending the body. |
| 18579 | MockWrite data_writes[] = { |
| 18580 | MockWrite("POST / HTTP/1.1\r\n" |
| 18581 | "Host: www.foo.com\r\n" |
| 18582 | "Connection: keep-alive\r\n" |
| 18583 | "Content-Length: 3\r\n\r\n"), |
| 18584 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18585 | }; |
| 18586 | |
| 18587 | MockRead data_reads[] = { |
| 18588 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 18589 | MockRead("HTTP/1.0 302 Redirect\r\n"), |
| 18590 | MockRead("Location: http://somewhere-else.com/\r\n"), |
| 18591 | MockRead("Content-Length: 0\r\n\r\n"), |
| 18592 | MockRead(SYNCHRONOUS, OK), |
| 18593 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18594 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18595 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18596 | |
| 18597 | TestCompletionCallback callback; |
| 18598 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18599 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18600 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18601 | |
| 18602 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18603 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18604 | } |
| 18605 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18606 | TEST_F(HttpNetworkTransactionTest, PostIgnoresHttp09ResponseAfterReset) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18607 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18608 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18609 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18610 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18611 | |
| 18612 | HttpRequestInfo request; |
| 18613 | request.method = "POST"; |
| 18614 | request.url = GURL("http://www.foo.com/"); |
| 18615 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18616 | request.traffic_annotation = |
| 18617 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18618 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18619 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18620 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18621 | // Send headers successfully, but get an error while sending the body. |
| 18622 | MockWrite data_writes[] = { |
| 18623 | MockWrite("POST / HTTP/1.1\r\n" |
| 18624 | "Host: www.foo.com\r\n" |
| 18625 | "Connection: keep-alive\r\n" |
| 18626 | "Content-Length: 3\r\n\r\n"), |
| 18627 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18628 | }; |
| 18629 | |
| 18630 | MockRead data_reads[] = { |
| 18631 | MockRead("HTTP 0.9 rocks!"), |
| 18632 | MockRead(SYNCHRONOUS, OK), |
| 18633 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18634 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18635 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18636 | |
| 18637 | TestCompletionCallback callback; |
| 18638 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18639 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18640 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18641 | |
| 18642 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18643 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18644 | } |
| 18645 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18646 | TEST_F(HttpNetworkTransactionTest, PostIgnoresPartial400HeadersAfterReset) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18647 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18648 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18649 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18650 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18651 | |
| 18652 | HttpRequestInfo request; |
| 18653 | request.method = "POST"; |
| 18654 | request.url = GURL("http://www.foo.com/"); |
| 18655 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18656 | request.traffic_annotation = |
| 18657 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18658 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18659 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18660 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18661 | // Send headers successfully, but get an error while sending the body. |
| 18662 | MockWrite data_writes[] = { |
| 18663 | MockWrite("POST / HTTP/1.1\r\n" |
| 18664 | "Host: www.foo.com\r\n" |
| 18665 | "Connection: keep-alive\r\n" |
| 18666 | "Content-Length: 3\r\n\r\n"), |
| 18667 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18668 | }; |
| 18669 | |
| 18670 | MockRead data_reads[] = { |
| 18671 | MockRead("HTTP/1.0 400 Not a Full Response\r\n"), |
| 18672 | MockRead(SYNCHRONOUS, OK), |
| 18673 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18674 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18675 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18676 | |
| 18677 | TestCompletionCallback callback; |
| 18678 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18679 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18680 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18681 | |
| 18682 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18683 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18684 | } |
| 18685 | |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18686 | #if BUILDFLAG(ENABLE_WEBSOCKETS) |
| 18687 | |
| 18688 | namespace { |
| 18689 | |
| 18690 | void AddWebSocketHeaders(HttpRequestHeaders* headers) { |
| 18691 | headers->SetHeader("Connection", "Upgrade"); |
| 18692 | headers->SetHeader("Upgrade", "websocket"); |
| 18693 | headers->SetHeader("Origin", "http://www.example.org"); |
| 18694 | headers->SetHeader("Sec-WebSocket-Version", "13"); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18695 | } |
| 18696 | |
| 18697 | } // namespace |
| 18698 | |
| 18699 | TEST_F(HttpNetworkTransactionTest, CreateWebSocketHandshakeStream) { |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18700 | for (bool secure : {true, false}) { |
| 18701 | MockWrite data_writes[] = { |
| 18702 | MockWrite("GET / HTTP/1.1\r\n" |
| 18703 | "Host: www.example.org\r\n" |
| 18704 | "Connection: Upgrade\r\n" |
| 18705 | "Upgrade: websocket\r\n" |
| 18706 | "Origin: http://www.example.org\r\n" |
| 18707 | "Sec-WebSocket-Version: 13\r\n" |
| 18708 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 18709 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 18710 | "client_max_window_bits\r\n\r\n")}; |
| 18711 | |
| 18712 | MockRead data_reads[] = { |
| 18713 | MockRead("HTTP/1.1 101 Switching Protocols\r\n" |
| 18714 | "Upgrade: websocket\r\n" |
| 18715 | "Connection: Upgrade\r\n" |
| 18716 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")}; |
| 18717 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18718 | StaticSocketDataProvider data(data_reads, data_writes); |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18719 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18720 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 18721 | if (secure) |
| 18722 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18723 | |
| 18724 | HttpRequestInfo request; |
| 18725 | request.method = "GET"; |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18726 | request.url = |
| 18727 | GURL(secure ? "ws://www.example.org/" : "wss://www.example.org/"); |
| 18728 | AddWebSocketHeaders(&request.extra_headers); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18729 | request.traffic_annotation = |
| 18730 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18731 | |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18732 | TestWebSocketHandshakeStreamCreateHelper |
| 18733 | websocket_handshake_stream_create_helper; |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18734 | |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18735 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18736 | HttpNetworkTransaction trans(LOW, session.get()); |
| 18737 | trans.SetWebSocketHandshakeStreamCreateHelper( |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18738 | &websocket_handshake_stream_create_helper); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18739 | |
| 18740 | TestCompletionCallback callback; |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18741 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 18742 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18743 | |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18744 | const HttpStreamRequest* stream_request = trans.stream_request_.get(); |
| 18745 | ASSERT_TRUE(stream_request); |
| 18746 | EXPECT_EQ(&websocket_handshake_stream_create_helper, |
| 18747 | stream_request->websocket_handshake_stream_create_helper()); |
| 18748 | |
| 18749 | rv = callback.WaitForResult(); |
| 18750 | EXPECT_THAT(rv, IsOk()); |
| 18751 | |
| 18752 | EXPECT_TRUE(data.AllReadDataConsumed()); |
| 18753 | EXPECT_TRUE(data.AllWriteDataConsumed()); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18754 | } |
| 18755 | } |
| 18756 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18757 | // Verify that proxy headers are not sent to the destination server when |
| 18758 | // establishing a tunnel for a secure WebSocket connection. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18759 | TEST_F(HttpNetworkTransactionTest, ProxyHeadersNotSentOverWssTunnel) { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18760 | HttpRequestInfo request; |
| 18761 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18762 | request.url = GURL("wss://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18763 | request.traffic_annotation = |
| 18764 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18765 | AddWebSocketHeaders(&request.extra_headers); |
| 18766 | |
| 18767 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 18768 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 18769 | ProxyResolutionService::CreateFixedFromPacResult( |
| 18770 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18771 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18772 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18773 | |
| 18774 | // Since a proxy is configured, try to establish a tunnel. |
| 18775 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 18776 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 18777 | "Host: www.example.org:443\r\n" |
| 18778 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18779 | |
| 18780 | // After calling trans->RestartWithAuth(), this is the request we should |
| 18781 | // be issuing -- the final header line contains the credentials. |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 18782 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 18783 | "Host: www.example.org:443\r\n" |
| 18784 | "Proxy-Connection: keep-alive\r\n" |
| 18785 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18786 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 18787 | MockWrite("GET / HTTP/1.1\r\n" |
| 18788 | "Host: www.example.org\r\n" |
| 18789 | "Connection: Upgrade\r\n" |
| 18790 | "Upgrade: websocket\r\n" |
| 18791 | "Origin: http://www.example.org\r\n" |
| 18792 | "Sec-WebSocket-Version: 13\r\n" |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18793 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 18794 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 18795 | "client_max_window_bits\r\n\r\n")}; |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18796 | |
| 18797 | // The proxy responds to the connect with a 407, using a persistent |
| 18798 | // connection. |
| 18799 | MockRead data_reads[] = { |
| 18800 | // No credentials. |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18801 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 18802 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 18803 | "Content-Length: 0\r\n" |
| 18804 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18805 | |
| 18806 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 18807 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18808 | MockRead("HTTP/1.1 101 Switching Protocols\r\n" |
| 18809 | "Upgrade: websocket\r\n" |
| 18810 | "Connection: Upgrade\r\n" |
| 18811 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")}; |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18812 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18813 | StaticSocketDataProvider data(data_reads, data_writes); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18814 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18815 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 18816 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 18817 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18818 | TestWebSocketHandshakeStreamCreateHelper websocket_stream_create_helper; |
| 18819 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 18820 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18821 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18822 | trans->SetWebSocketHandshakeStreamCreateHelper( |
| 18823 | &websocket_stream_create_helper); |
| 18824 | |
| 18825 | { |
| 18826 | TestCompletionCallback callback; |
| 18827 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18828 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18829 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18830 | |
| 18831 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18832 | EXPECT_THAT(rv, IsOk()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18833 | } |
| 18834 | |
| 18835 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 18836 | ASSERT_TRUE(response); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18837 | ASSERT_TRUE(response->headers); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18838 | EXPECT_EQ(407, response->headers->response_code()); |
| 18839 | |
| 18840 | { |
| 18841 | TestCompletionCallback callback; |
| 18842 | |
| 18843 | int rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 18844 | callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18845 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18846 | |
| 18847 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18848 | EXPECT_THAT(rv, IsOk()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18849 | } |
| 18850 | |
| 18851 | response = trans->GetResponseInfo(); |
| 18852 | ASSERT_TRUE(response); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18853 | ASSERT_TRUE(response->headers); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18854 | |
| 18855 | EXPECT_EQ(101, response->headers->response_code()); |
| 18856 | |
| 18857 | trans.reset(); |
| 18858 | session->CloseAllConnections(); |
| 18859 | } |
| 18860 | |
| 18861 | // Verify that proxy headers are not sent to the destination server when |
| 18862 | // establishing a tunnel for an insecure WebSocket connection. |
| 18863 | // This requires the authentication info to be injected into the auth cache |
| 18864 | // due to crbug.com/395064 |
| 18865 | // TODO(ricea): Change to use a 407 response once issue 395064 is fixed. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18866 | TEST_F(HttpNetworkTransactionTest, ProxyHeadersNotSentOverWsTunnel) { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18867 | HttpRequestInfo request; |
| 18868 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18869 | request.url = GURL("ws://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18870 | request.traffic_annotation = |
| 18871 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18872 | AddWebSocketHeaders(&request.extra_headers); |
| 18873 | |
| 18874 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 18875 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 18876 | ProxyResolutionService::CreateFixedFromPacResult( |
| 18877 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18878 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18879 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18880 | |
| 18881 | MockWrite data_writes[] = { |
| 18882 | // Try to establish a tunnel for the WebSocket connection, with |
| 18883 | // credentials. Because WebSockets have a separate set of socket pools, |
| 18884 | // they cannot and will not use the same TCP/IP connection as the |
| 18885 | // preflight HTTP request. |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18886 | MockWrite("CONNECT www.example.org:80 HTTP/1.1\r\n" |
| 18887 | "Host: www.example.org:80\r\n" |
| 18888 | "Proxy-Connection: keep-alive\r\n" |
| 18889 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18890 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18891 | MockWrite("GET / HTTP/1.1\r\n" |
| 18892 | "Host: www.example.org\r\n" |
| 18893 | "Connection: Upgrade\r\n" |
| 18894 | "Upgrade: websocket\r\n" |
| 18895 | "Origin: http://www.example.org\r\n" |
| 18896 | "Sec-WebSocket-Version: 13\r\n" |
| 18897 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 18898 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 18899 | "client_max_window_bits\r\n\r\n")}; |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18900 | |
| 18901 | MockRead data_reads[] = { |
| 18902 | // HTTP CONNECT with credentials. |
| 18903 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 18904 | |
| 18905 | // WebSocket connection established inside tunnel. |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18906 | MockRead("HTTP/1.1 101 Switching Protocols\r\n" |
| 18907 | "Upgrade: websocket\r\n" |
| 18908 | "Connection: Upgrade\r\n" |
| 18909 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")}; |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18910 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18911 | StaticSocketDataProvider data(data_reads, data_writes); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18912 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18913 | |
| 18914 | session->http_auth_cache()->Add( |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 18915 | GURL("http://myproxy:70/"), HttpAuth::AUTH_PROXY, "MyRealm1", |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 18916 | HttpAuth::AUTH_SCHEME_BASIC, NetworkIsolationKey(), |
| 18917 | "Basic realm=MyRealm1", AuthCredentials(kFoo, kBar), "/"); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18918 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18919 | TestWebSocketHandshakeStreamCreateHelper websocket_stream_create_helper; |
| 18920 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 18921 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18922 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18923 | trans->SetWebSocketHandshakeStreamCreateHelper( |
| 18924 | &websocket_stream_create_helper); |
| 18925 | |
| 18926 | TestCompletionCallback callback; |
| 18927 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18928 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18929 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18930 | |
| 18931 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18932 | EXPECT_THAT(rv, IsOk()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18933 | |
| 18934 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 18935 | ASSERT_TRUE(response); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18936 | ASSERT_TRUE(response->headers); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18937 | |
| 18938 | EXPECT_EQ(101, response->headers->response_code()); |
| 18939 | |
| 18940 | trans.reset(); |
| 18941 | session->CloseAllConnections(); |
| 18942 | } |
| 18943 | |
Matt Menke | 1d6093e3 | 2019-03-22 17:33:43 | [diff] [blame] | 18944 | // WebSockets over QUIC is not supported, including over QUIC proxies. |
| 18945 | TEST_F(HttpNetworkTransactionTest, WebSocketNotSentOverQuicProxy) { |
| 18946 | for (bool secure : {true, false}) { |
| 18947 | SCOPED_TRACE(secure); |
| 18948 | session_deps_.proxy_resolution_service = |
| 18949 | ProxyResolutionService::CreateFixedFromPacResult( |
| 18950 | "QUIC myproxy.org:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 18951 | session_deps_.enable_quic = true; |
| 18952 | |
| 18953 | HttpRequestInfo request; |
| 18954 | request.url = |
| 18955 | GURL(secure ? "ws://www.example.org/" : "wss://www.example.org/"); |
| 18956 | AddWebSocketHeaders(&request.extra_headers); |
| 18957 | request.traffic_annotation = |
| 18958 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 18959 | |
| 18960 | TestWebSocketHandshakeStreamCreateHelper |
| 18961 | websocket_handshake_stream_create_helper; |
| 18962 | |
| 18963 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 18964 | HttpNetworkTransaction trans(LOW, session.get()); |
| 18965 | trans.SetWebSocketHandshakeStreamCreateHelper( |
| 18966 | &websocket_handshake_stream_create_helper); |
| 18967 | |
| 18968 | TestCompletionCallback callback; |
| 18969 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 18970 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 18971 | |
| 18972 | rv = callback.WaitForResult(); |
| 18973 | EXPECT_THAT(rv, IsError(ERR_NO_SUPPORTED_PROXIES)); |
| 18974 | } |
| 18975 | } |
| 18976 | |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18977 | #endif // BUILDFLAG(ENABLE_WEBSOCKETS) |
| 18978 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18979 | TEST_F(HttpNetworkTransactionTest, TotalNetworkBytesPost) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18980 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18981 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18982 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18983 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 18984 | |
| 18985 | HttpRequestInfo request; |
| 18986 | request.method = "POST"; |
| 18987 | request.url = GURL("http://www.foo.com/"); |
| 18988 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 18989 | request.traffic_annotation = |
| 18990 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 18991 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18992 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18993 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 18994 | MockWrite data_writes[] = { |
| 18995 | MockWrite("POST / HTTP/1.1\r\n" |
| 18996 | "Host: www.foo.com\r\n" |
| 18997 | "Connection: keep-alive\r\n" |
| 18998 | "Content-Length: 3\r\n\r\n"), |
| 18999 | MockWrite("foo"), |
| 19000 | }; |
| 19001 | |
| 19002 | MockRead data_reads[] = { |
| 19003 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 19004 | MockRead(SYNCHRONOUS, OK), |
| 19005 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19006 | StaticSocketDataProvider data(data_reads, data_writes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19007 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19008 | |
| 19009 | TestCompletionCallback callback; |
| 19010 | |
| 19011 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 19012 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 19013 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19014 | |
| 19015 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19016 | EXPECT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19017 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19018 | EXPECT_EQ(CountWriteBytes(data_writes), trans.GetTotalSentBytes()); |
| 19019 | EXPECT_EQ(CountReadBytes(data_reads), trans.GetTotalReceivedBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19020 | } |
| 19021 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 19022 | TEST_F(HttpNetworkTransactionTest, TotalNetworkBytesPost100Continue) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 19023 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 19024 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 19025 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 19026 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19027 | |
| 19028 | HttpRequestInfo request; |
| 19029 | request.method = "POST"; |
| 19030 | request.url = GURL("http://www.foo.com/"); |
| 19031 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 19032 | request.traffic_annotation = |
| 19033 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19034 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 19035 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19036 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19037 | MockWrite data_writes[] = { |
| 19038 | MockWrite("POST / HTTP/1.1\r\n" |
| 19039 | "Host: www.foo.com\r\n" |
| 19040 | "Connection: keep-alive\r\n" |
| 19041 | "Content-Length: 3\r\n\r\n"), |
| 19042 | MockWrite("foo"), |
| 19043 | }; |
| 19044 | |
| 19045 | MockRead data_reads[] = { |
| 19046 | MockRead("HTTP/1.1 100 Continue\r\n\r\n"), |
| 19047 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 19048 | MockRead(SYNCHRONOUS, OK), |
| 19049 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19050 | StaticSocketDataProvider data(data_reads, data_writes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19051 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19052 | |
| 19053 | TestCompletionCallback callback; |
| 19054 | |
| 19055 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 19056 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 19057 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19058 | |
| 19059 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19060 | EXPECT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19061 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19062 | EXPECT_EQ(CountWriteBytes(data_writes), trans.GetTotalSentBytes()); |
| 19063 | EXPECT_EQ(CountReadBytes(data_reads), trans.GetTotalReceivedBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19064 | } |
| 19065 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 19066 | TEST_F(HttpNetworkTransactionTest, TotalNetworkBytesChunkedPost) { |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19067 | ChunkedUploadDataStream upload_data_stream(0); |
| 19068 | |
| 19069 | HttpRequestInfo request; |
| 19070 | request.method = "POST"; |
| 19071 | request.url = GURL("http://www.foo.com/"); |
| 19072 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 19073 | request.traffic_annotation = |
| 19074 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19075 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 19076 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19077 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19078 | // Send headers successfully, but get an error while sending the body. |
| 19079 | MockWrite data_writes[] = { |
| 19080 | MockWrite("POST / HTTP/1.1\r\n" |
| 19081 | "Host: www.foo.com\r\n" |
| 19082 | "Connection: keep-alive\r\n" |
| 19083 | "Transfer-Encoding: chunked\r\n\r\n"), |
| 19084 | MockWrite("1\r\nf\r\n"), MockWrite("2\r\noo\r\n"), MockWrite("0\r\n\r\n"), |
| 19085 | }; |
| 19086 | |
| 19087 | MockRead data_reads[] = { |
| 19088 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 19089 | MockRead(SYNCHRONOUS, OK), |
| 19090 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19091 | StaticSocketDataProvider data(data_reads, data_writes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19092 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19093 | |
| 19094 | TestCompletionCallback callback; |
| 19095 | |
| 19096 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 19097 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19098 | |
| 19099 | base::RunLoop().RunUntilIdle(); |
| 19100 | upload_data_stream.AppendData("f", 1, false); |
| 19101 | |
| 19102 | base::RunLoop().RunUntilIdle(); |
| 19103 | upload_data_stream.AppendData("oo", 2, true); |
| 19104 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 19105 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19106 | |
| 19107 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19108 | EXPECT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19109 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19110 | EXPECT_EQ(CountWriteBytes(data_writes), trans.GetTotalSentBytes()); |
| 19111 | EXPECT_EQ(CountReadBytes(data_reads), trans.GetTotalReceivedBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19112 | } |
| 19113 | |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19114 | void CheckContentEncodingMatching(SpdySessionDependencies* session_deps, |
| 19115 | const std::string& accept_encoding, |
| 19116 | const std::string& content_encoding, |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19117 | const std::string& location, |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19118 | bool should_match) { |
| 19119 | HttpRequestInfo request; |
| 19120 | request.method = "GET"; |
| 19121 | request.url = GURL("http://www.foo.com/"); |
| 19122 | request.extra_headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, |
| 19123 | accept_encoding); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 19124 | request.traffic_annotation = |
| 19125 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19126 | |
| 19127 | std::unique_ptr<HttpNetworkSession> session(CreateSession(session_deps)); |
| 19128 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19129 | // Send headers successfully, but get an error while sending the body. |
| 19130 | MockWrite data_writes[] = { |
| 19131 | MockWrite("GET / HTTP/1.1\r\n" |
| 19132 | "Host: www.foo.com\r\n" |
| 19133 | "Connection: keep-alive\r\n" |
| 19134 | "Accept-Encoding: "), |
| 19135 | MockWrite(accept_encoding.data()), MockWrite("\r\n\r\n"), |
| 19136 | }; |
| 19137 | |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19138 | std::string response_code = "200 OK"; |
| 19139 | std::string extra; |
| 19140 | if (!location.empty()) { |
| 19141 | response_code = "301 Redirect\r\nLocation: "; |
| 19142 | response_code.append(location); |
| 19143 | } |
| 19144 | |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19145 | MockRead data_reads[] = { |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19146 | MockRead("HTTP/1.0 "), |
| 19147 | MockRead(response_code.data()), |
| 19148 | MockRead("\r\nContent-Encoding: "), |
| 19149 | MockRead(content_encoding.data()), |
| 19150 | MockRead("\r\n\r\n"), |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19151 | MockRead(SYNCHRONOUS, OK), |
| 19152 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19153 | StaticSocketDataProvider data(data_reads, data_writes); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19154 | session_deps->socket_factory->AddSocketDataProvider(&data); |
| 19155 | |
| 19156 | TestCompletionCallback callback; |
| 19157 | |
| 19158 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 19159 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19160 | |
| 19161 | rv = callback.WaitForResult(); |
| 19162 | if (should_match) { |
| 19163 | EXPECT_THAT(rv, IsOk()); |
| 19164 | } else { |
| 19165 | EXPECT_THAT(rv, IsError(ERR_CONTENT_DECODING_FAILED)); |
| 19166 | } |
| 19167 | } |
| 19168 | |
| 19169 | TEST_F(HttpNetworkTransactionTest, MatchContentEncoding1) { |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19170 | CheckContentEncodingMatching(&session_deps_, "gzip,sdch", "br", "", false); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19171 | } |
| 19172 | |
| 19173 | TEST_F(HttpNetworkTransactionTest, MatchContentEncoding2) { |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19174 | CheckContentEncodingMatching(&session_deps_, "identity;q=1, *;q=0", "", "", |
| 19175 | true); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19176 | } |
| 19177 | |
| 19178 | TEST_F(HttpNetworkTransactionTest, MatchContentEncoding3) { |
| 19179 | CheckContentEncodingMatching(&session_deps_, "identity;q=1, *;q=0", "gzip", |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19180 | "", false); |
| 19181 | } |
| 19182 | |
| 19183 | TEST_F(HttpNetworkTransactionTest, MatchContentEncoding4) { |
| 19184 | CheckContentEncodingMatching(&session_deps_, "identity;q=1, *;q=0", "gzip", |
| 19185 | "www.foo.com/other", true); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19186 | } |
| 19187 | |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19188 | TEST_F(HttpNetworkTransactionTest, ProxyResolutionFailsSync) { |
| 19189 | ProxyConfig proxy_config; |
| 19190 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 19191 | proxy_config.set_pac_mandatory(true); |
| 19192 | MockAsyncProxyResolver resolver; |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 19193 | session_deps_.proxy_resolution_service.reset(new ProxyResolutionService( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 19194 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 19195 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 19196 | std::make_unique<FailingProxyResolverFactory>(), nullptr)); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19197 | |
| 19198 | HttpRequestInfo request; |
| 19199 | request.method = "GET"; |
| 19200 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 19201 | request.traffic_annotation = |
| 19202 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19203 | |
| 19204 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19205 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19206 | |
| 19207 | TestCompletionCallback callback; |
| 19208 | |
| 19209 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 19210 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19211 | EXPECT_THAT(callback.WaitForResult(), |
| 19212 | IsError(ERR_MANDATORY_PROXY_CONFIGURATION_FAILED)); |
| 19213 | } |
| 19214 | |
| 19215 | TEST_F(HttpNetworkTransactionTest, ProxyResolutionFailsAsync) { |
| 19216 | ProxyConfig proxy_config; |
| 19217 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 19218 | proxy_config.set_pac_mandatory(true); |
| 19219 | MockAsyncProxyResolverFactory* proxy_resolver_factory = |
| 19220 | new MockAsyncProxyResolverFactory(false); |
| 19221 | MockAsyncProxyResolver resolver; |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 19222 | session_deps_.proxy_resolution_service.reset(new ProxyResolutionService( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 19223 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 19224 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 19225 | base::WrapUnique(proxy_resolver_factory), nullptr)); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19226 | HttpRequestInfo request; |
| 19227 | request.method = "GET"; |
| 19228 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 19229 | request.traffic_annotation = |
| 19230 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19231 | |
| 19232 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19233 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19234 | |
| 19235 | TestCompletionCallback callback; |
| 19236 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 19237 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19238 | |
| 19239 | proxy_resolver_factory->pending_requests()[0]->CompleteNowWithForwarder( |
| 19240 | ERR_FAILED, &resolver); |
| 19241 | EXPECT_THAT(callback.WaitForResult(), |
| 19242 | IsError(ERR_MANDATORY_PROXY_CONFIGURATION_FAILED)); |
| 19243 | } |
| 19244 | |
| 19245 | TEST_F(HttpNetworkTransactionTest, NoSupportedProxies) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 19246 | session_deps_.proxy_resolution_service = |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 19247 | ProxyResolutionService::CreateFixedFromPacResult( |
| 19248 | "QUIC myproxy.org:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19249 | session_deps_.enable_quic = false; |
| 19250 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19251 | |
| 19252 | HttpRequestInfo request; |
| 19253 | request.method = "GET"; |
| 19254 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e6 | 2018-02-07 07:41:10 | [diff] [blame] | 19255 | request.traffic_annotation = |
| 19256 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19257 | |
| 19258 | TestCompletionCallback callback; |
| 19259 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19260 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 19261 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19262 | |
| 19263 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NO_SUPPORTED_PROXIES)); |
| 19264 | } |
| 19265 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19266 | //----------------------------------------------------------------------------- |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19267 | // Reporting tests |
| 19268 | |
| 19269 | #if BUILDFLAG(ENABLE_REPORTING) |
| 19270 | class HttpNetworkTransactionReportingTest : public HttpNetworkTransactionTest { |
| 19271 | protected: |
| 19272 | void SetUp() override { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19273 | HttpNetworkTransactionTest::SetUp(); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19274 | auto test_reporting_context = std::make_unique<TestReportingContext>( |
| 19275 | &clock_, &tick_clock_, ReportingPolicy()); |
| 19276 | test_reporting_context_ = test_reporting_context.get(); |
| 19277 | session_deps_.reporting_service = |
| 19278 | ReportingService::CreateForTesting(std::move(test_reporting_context)); |
| 19279 | } |
| 19280 | |
| 19281 | TestReportingContext* reporting_context() const { |
| 19282 | return test_reporting_context_; |
| 19283 | } |
| 19284 | |
| 19285 | void clear_reporting_service() { |
| 19286 | session_deps_.reporting_service.reset(); |
| 19287 | test_reporting_context_ = nullptr; |
| 19288 | } |
| 19289 | |
| 19290 | // Makes an HTTPS request that should install a valid Reporting policy. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19291 | void RequestPolicy(CertStatus cert_status = 0) { |
| 19292 | HttpRequestInfo request; |
| 19293 | request.method = "GET"; |
| 19294 | request.url = GURL(url_); |
| 19295 | request.traffic_annotation = |
| 19296 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 19297 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 19298 | MockWrite data_writes[] = { |
| 19299 | MockWrite("GET / HTTP/1.1\r\n" |
| 19300 | "Host: www.example.org\r\n" |
| 19301 | "Connection: keep-alive\r\n\r\n"), |
| 19302 | }; |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19303 | MockRead data_reads[] = { |
| 19304 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 19305 | MockRead("Report-To: {\"group\": \"nel\", \"max_age\": 86400, " |
| 19306 | "\"endpoints\": [{\"url\": " |
| 19307 | "\"https://www.example.org/upload/\"}]}\r\n"), |
| 19308 | MockRead("\r\n"), |
| 19309 | MockRead("hello world"), |
| 19310 | MockRead(SYNCHRONOUS, OK), |
| 19311 | }; |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19312 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19313 | StaticSocketDataProvider reads(data_reads, data_writes); |
| 19314 | session_deps_.socket_factory->AddSocketDataProvider(&reads); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19315 | |
| 19316 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 19317 | if (request.url.SchemeIsCryptographic()) { |
| 19318 | ssl.ssl_info.cert = |
| 19319 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 19320 | ASSERT_TRUE(ssl.ssl_info.cert); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19321 | ssl.ssl_info.cert_status = cert_status; |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19322 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19323 | } |
| 19324 | |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19325 | TestCompletionCallback callback; |
| 19326 | auto session = CreateSession(&session_deps_); |
| 19327 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19328 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19329 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19330 | } |
| 19331 | |
| 19332 | protected: |
| 19333 | std::string url_ = "https://www.example.org/"; |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19334 | |
| 19335 | private: |
| 19336 | TestReportingContext* test_reporting_context_; |
| 19337 | }; |
| 19338 | |
| 19339 | TEST_F(HttpNetworkTransactionReportingTest, |
| 19340 | DontProcessReportToHeaderNoService) { |
| 19341 | base::HistogramTester histograms; |
| 19342 | clear_reporting_service(); |
| 19343 | RequestPolicy(); |
| 19344 | histograms.ExpectBucketCount( |
| 19345 | ReportingHeaderParser::kHeaderOutcomeHistogram, |
| 19346 | ReportingHeaderParser::HeaderOutcome::DISCARDED_NO_REPORTING_SERVICE, 1); |
| 19347 | } |
| 19348 | |
| 19349 | TEST_F(HttpNetworkTransactionReportingTest, DontProcessReportToHeaderHttp) { |
| 19350 | base::HistogramTester histograms; |
| 19351 | url_ = "http://www.example.org/"; |
| 19352 | RequestPolicy(); |
| 19353 | histograms.ExpectBucketCount( |
| 19354 | ReportingHeaderParser::kHeaderOutcomeHistogram, |
| 19355 | ReportingHeaderParser::HeaderOutcome::DISCARDED_INVALID_SSL_INFO, 1); |
| 19356 | } |
| 19357 | |
| 19358 | TEST_F(HttpNetworkTransactionReportingTest, ProcessReportToHeaderHttps) { |
| 19359 | RequestPolicy(); |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 19360 | ASSERT_EQ(1u, reporting_context()->cache()->GetEndpointCount()); |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 19361 | const ReportingEndpoint endpoint = |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 19362 | reporting_context()->cache()->GetEndpointForTesting( |
| 19363 | url::Origin::Create(GURL("https://www.example.org/")), "nel", |
| 19364 | GURL("https://www.example.org/upload/")); |
| 19365 | EXPECT_TRUE(endpoint); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19366 | } |
| 19367 | |
| 19368 | TEST_F(HttpNetworkTransactionReportingTest, |
| 19369 | DontProcessReportToHeaderInvalidHttps) { |
| 19370 | base::HistogramTester histograms; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19371 | CertStatus cert_status = CERT_STATUS_COMMON_NAME_INVALID; |
| 19372 | RequestPolicy(cert_status); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19373 | histograms.ExpectBucketCount( |
| 19374 | ReportingHeaderParser::kHeaderOutcomeHistogram, |
| 19375 | ReportingHeaderParser::HeaderOutcome::DISCARDED_CERT_STATUS_ERROR, 1); |
| 19376 | } |
| 19377 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 19378 | |
| 19379 | //----------------------------------------------------------------------------- |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19380 | // Network Error Logging tests |
| 19381 | |
| 19382 | #if BUILDFLAG(ENABLE_REPORTING) |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19383 | namespace { |
| 19384 | |
| 19385 | const char kUserAgent[] = "Mozilla/1.0"; |
| 19386 | const char kReferrer[] = "https://www.referrer.org/"; |
| 19387 | |
| 19388 | } // namespace |
| 19389 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19390 | class HttpNetworkTransactionNetworkErrorLoggingTest |
| 19391 | : public HttpNetworkTransactionTest { |
| 19392 | protected: |
| 19393 | void SetUp() override { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19394 | HttpNetworkTransactionTest::SetUp(); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19395 | auto network_error_logging_service = |
| 19396 | std::make_unique<TestNetworkErrorLoggingService>(); |
| 19397 | test_network_error_logging_service_ = network_error_logging_service.get(); |
| 19398 | session_deps_.network_error_logging_service = |
| 19399 | std::move(network_error_logging_service); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19400 | |
| 19401 | extra_headers_.SetHeader("User-Agent", kUserAgent); |
| 19402 | extra_headers_.SetHeader("Referer", kReferrer); |
| 19403 | |
| 19404 | request_.method = "GET"; |
| 19405 | request_.url = GURL(url_); |
| 19406 | request_.extra_headers = extra_headers_; |
| 19407 | request_.reporting_upload_depth = reporting_upload_depth_; |
| 19408 | request_.traffic_annotation = |
| 19409 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19410 | } |
| 19411 | |
| 19412 | TestNetworkErrorLoggingService* network_error_logging_service() const { |
| 19413 | return test_network_error_logging_service_; |
| 19414 | } |
| 19415 | |
| 19416 | void clear_network_error_logging_service() { |
| 19417 | session_deps_.network_error_logging_service.reset(); |
| 19418 | test_network_error_logging_service_ = nullptr; |
| 19419 | } |
| 19420 | |
| 19421 | // Makes an HTTPS request that should install a valid NEL policy. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19422 | void RequestPolicy(CertStatus cert_status = 0) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19423 | std::string extra_header_string = extra_headers_.ToString(); |
Lily Chen | 29c0403 | 2019-02-25 21:50:53 | [diff] [blame] | 19424 | MockWrite data_writes[] = { |
| 19425 | MockWrite("GET / HTTP/1.1\r\n" |
| 19426 | "Host: www.example.org\r\n" |
| 19427 | "Connection: keep-alive\r\n"), |
| 19428 | MockWrite(ASYNC, extra_header_string.data(), |
| 19429 | extra_header_string.size()), |
| 19430 | }; |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 19431 | MockRead data_reads[] = { |
| 19432 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 19433 | MockRead("NEL: {\"report_to\": \"nel\", \"max_age\": 86400}\r\n"), |
| 19434 | MockRead("\r\n"), |
| 19435 | MockRead("hello world"), |
| 19436 | MockRead(SYNCHRONOUS, OK), |
| 19437 | }; |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19438 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19439 | StaticSocketDataProvider reads(data_reads, data_writes); |
| 19440 | session_deps_.socket_factory->AddSocketDataProvider(&reads); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19441 | |
| 19442 | SSLSocketDataProvider ssl(ASYNC, OK); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19443 | if (request_.url.SchemeIsCryptographic()) { |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19444 | ssl.ssl_info.cert = |
| 19445 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 19446 | ASSERT_TRUE(ssl.ssl_info.cert); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19447 | ssl.ssl_info.cert_status = cert_status; |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19448 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19449 | } |
| 19450 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19451 | TestCompletionCallback callback; |
| 19452 | auto session = CreateSession(&session_deps_); |
| 19453 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19454 | int rv = trans.Start(&request_, callback.callback(), NetLogWithSource()); |
| 19455 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 19456 | |
| 19457 | std::string response_data; |
| 19458 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
| 19459 | EXPECT_EQ("hello world", response_data); |
| 19460 | } |
| 19461 | |
| 19462 | void CheckReport(size_t index, |
| 19463 | int status_code, |
| 19464 | int error_type, |
| 19465 | IPAddress server_ip = IPAddress::IPv4Localhost()) { |
| 19466 | ASSERT_LT(index, network_error_logging_service()->errors().size()); |
| 19467 | |
| 19468 | const NetworkErrorLoggingService::RequestDetails& error = |
| 19469 | network_error_logging_service()->errors()[index]; |
| 19470 | EXPECT_EQ(url_, error.uri); |
| 19471 | EXPECT_EQ(kReferrer, error.referrer); |
| 19472 | EXPECT_EQ(kUserAgent, error.user_agent); |
| 19473 | EXPECT_EQ(server_ip, error.server_ip); |
| 19474 | EXPECT_EQ("http/1.1", error.protocol); |
| 19475 | EXPECT_EQ("GET", error.method); |
| 19476 | EXPECT_EQ(status_code, error.status_code); |
| 19477 | EXPECT_EQ(error_type, error.type); |
| 19478 | EXPECT_EQ(0, error.reporting_upload_depth); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19479 | } |
| 19480 | |
| 19481 | protected: |
| 19482 | std::string url_ = "https://www.example.org/"; |
| 19483 | CertStatus cert_status_ = 0; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19484 | HttpRequestInfo request_; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19485 | HttpRequestHeaders extra_headers_; |
| 19486 | int reporting_upload_depth_ = 0; |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19487 | |
| 19488 | private: |
| 19489 | TestNetworkErrorLoggingService* test_network_error_logging_service_; |
| 19490 | }; |
| 19491 | |
| 19492 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19493 | DontProcessNelHeaderNoService) { |
| 19494 | base::HistogramTester histograms; |
| 19495 | clear_network_error_logging_service(); |
| 19496 | RequestPolicy(); |
| 19497 | histograms.ExpectBucketCount( |
| 19498 | NetworkErrorLoggingService::kHeaderOutcomeHistogram, |
| 19499 | NetworkErrorLoggingService::HeaderOutcome:: |
| 19500 | DISCARDED_NO_NETWORK_ERROR_LOGGING_SERVICE, |
| 19501 | 1); |
| 19502 | } |
| 19503 | |
| 19504 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19505 | DontProcessNelHeaderHttp) { |
| 19506 | base::HistogramTester histograms; |
| 19507 | url_ = "http://www.example.org/"; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19508 | request_.url = GURL(url_); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19509 | RequestPolicy(); |
| 19510 | histograms.ExpectBucketCount( |
| 19511 | NetworkErrorLoggingService::kHeaderOutcomeHistogram, |
| 19512 | NetworkErrorLoggingService::HeaderOutcome::DISCARDED_INVALID_SSL_INFO, 1); |
| 19513 | } |
| 19514 | |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 19515 | // Don't set NEL policies received on a proxied connection. |
| 19516 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19517 | DontProcessNelHeaderProxy) { |
| 19518 | session_deps_.proxy_resolution_service = |
| 19519 | ProxyResolutionService::CreateFixedFromPacResult( |
| 19520 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 19521 | BoundTestNetLog log; |
| 19522 | session_deps_.net_log = log.bound().net_log(); |
| 19523 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19524 | |
| 19525 | HttpRequestInfo request; |
| 19526 | request.method = "GET"; |
| 19527 | request.url = GURL("https://www.example.org/"); |
| 19528 | request.traffic_annotation = |
| 19529 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 19530 | |
| 19531 | // Since we have proxy, should try to establish tunnel. |
| 19532 | MockWrite data_writes1[] = { |
| 19533 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 19534 | "Host: www.example.org:443\r\n" |
| 19535 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 19536 | |
| 19537 | MockWrite("GET / HTTP/1.1\r\n" |
| 19538 | "Host: www.example.org\r\n" |
| 19539 | "Connection: keep-alive\r\n\r\n"), |
| 19540 | }; |
| 19541 | |
| 19542 | MockRead data_reads1[] = { |
| 19543 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 19544 | |
| 19545 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 19546 | MockRead("NEL: {\"report_to\": \"nel\", \"max_age\": 86400}\r\n"), |
| 19547 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 19548 | MockRead("Content-Length: 100\r\n\r\n"), |
| 19549 | MockRead(SYNCHRONOUS, OK), |
| 19550 | }; |
| 19551 | |
| 19552 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 19553 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 19554 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 19555 | ssl.ssl_info.cert = |
| 19556 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 19557 | ASSERT_TRUE(ssl.ssl_info.cert); |
| 19558 | ssl.ssl_info.cert_status = 0; |
| 19559 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19560 | |
| 19561 | TestCompletionCallback callback1; |
| 19562 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19563 | |
| 19564 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
| 19565 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19566 | |
| 19567 | rv = callback1.WaitForResult(); |
| 19568 | EXPECT_THAT(rv, IsOk()); |
| 19569 | |
| 19570 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 19571 | ASSERT_TRUE(response); |
| 19572 | EXPECT_EQ(200, response->headers->response_code()); |
| 19573 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 19574 | |
| 19575 | // No NEL header was set. |
| 19576 | EXPECT_EQ(0u, network_error_logging_service()->headers().size()); |
| 19577 | } |
| 19578 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19579 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, ProcessNelHeaderHttps) { |
| 19580 | RequestPolicy(); |
| 19581 | ASSERT_EQ(1u, network_error_logging_service()->headers().size()); |
| 19582 | const auto& header = network_error_logging_service()->headers()[0]; |
| 19583 | EXPECT_EQ(url::Origin::Create(GURL("https://www.example.org/")), |
| 19584 | header.origin); |
| 19585 | EXPECT_EQ(IPAddress::IPv4Localhost(), header.received_ip_address); |
| 19586 | EXPECT_EQ("{\"report_to\": \"nel\", \"max_age\": 86400}", header.value); |
| 19587 | } |
| 19588 | |
| 19589 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19590 | DontProcessNelHeaderInvalidHttps) { |
| 19591 | base::HistogramTester histograms; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19592 | CertStatus cert_status = CERT_STATUS_COMMON_NAME_INVALID; |
| 19593 | RequestPolicy(cert_status); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19594 | histograms.ExpectBucketCount( |
| 19595 | NetworkErrorLoggingService::kHeaderOutcomeHistogram, |
| 19596 | NetworkErrorLoggingService::HeaderOutcome::DISCARDED_CERT_STATUS_ERROR, |
| 19597 | 1); |
| 19598 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19599 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19600 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, CreateReportSuccess) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19601 | RequestPolicy(); |
| 19602 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19603 | CheckReport(0 /* index */, 200 /* status_code */, OK); |
| 19604 | } |
| 19605 | |
| 19606 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19607 | CreateReportErrorAfterStart) { |
| 19608 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19609 | auto trans = |
| 19610 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19611 | |
| 19612 | MockConnect mock_connect(SYNCHRONOUS, ERR_NAME_NOT_RESOLVED); |
| 19613 | StaticSocketDataProvider data; |
| 19614 | data.set_connect_data(mock_connect); |
| 19615 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19616 | |
| 19617 | TestCompletionCallback callback; |
| 19618 | |
| 19619 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 19620 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_NAME_NOT_RESOLVED)); |
| 19621 | |
| 19622 | trans.reset(); |
| 19623 | |
| 19624 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19625 | CheckReport(0 /* index */, 0 /* status_code */, ERR_NAME_NOT_RESOLVED, |
| 19626 | IPAddress() /* server_ip */); |
| 19627 | } |
| 19628 | |
| 19629 | // Same as above except the error is ASYNC |
| 19630 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19631 | CreateReportErrorAfterStartAsync) { |
| 19632 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19633 | auto trans = |
| 19634 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19635 | |
| 19636 | MockConnect mock_connect(ASYNC, ERR_NAME_NOT_RESOLVED); |
| 19637 | StaticSocketDataProvider data; |
| 19638 | data.set_connect_data(mock_connect); |
| 19639 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19640 | |
| 19641 | TestCompletionCallback callback; |
| 19642 | |
| 19643 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 19644 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_NAME_NOT_RESOLVED)); |
| 19645 | |
| 19646 | trans.reset(); |
| 19647 | |
| 19648 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19649 | CheckReport(0 /* index */, 0 /* status_code */, ERR_NAME_NOT_RESOLVED, |
| 19650 | IPAddress() /* server_ip */); |
| 19651 | } |
| 19652 | |
| 19653 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19654 | CreateReportReadBodyError) { |
| 19655 | std::string extra_header_string = extra_headers_.ToString(); |
Lily Chen | 29c0403 | 2019-02-25 21:50:53 | [diff] [blame] | 19656 | MockWrite data_writes[] = { |
| 19657 | MockWrite("GET / HTTP/1.1\r\n" |
| 19658 | "Host: www.example.org\r\n" |
| 19659 | "Connection: keep-alive\r\n"), |
| 19660 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19661 | }; |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 19662 | MockRead data_reads[] = { |
| 19663 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 19664 | MockRead("Content-Length: 100\r\n\r\n"), // wrong content length |
| 19665 | MockRead("hello world"), |
| 19666 | MockRead(SYNCHRONOUS, OK), |
| 19667 | }; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19668 | |
| 19669 | StaticSocketDataProvider reads(data_reads, data_writes); |
| 19670 | session_deps_.socket_factory->AddSocketDataProvider(&reads); |
| 19671 | |
| 19672 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 19673 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19674 | |
| 19675 | // Log start time |
| 19676 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 19677 | |
| 19678 | TestCompletionCallback callback; |
| 19679 | auto session = CreateSession(&session_deps_); |
| 19680 | auto trans = |
| 19681 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19682 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 19683 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 19684 | |
| 19685 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 19686 | ASSERT_TRUE(response); |
| 19687 | |
| 19688 | EXPECT_TRUE(response->headers); |
| 19689 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 19690 | |
| 19691 | std::string response_data; |
| 19692 | rv = ReadTransaction(trans.get(), &response_data); |
| 19693 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
| 19694 | |
| 19695 | trans.reset(); |
| 19696 | |
| 19697 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19698 | |
| 19699 | CheckReport(0 /* index */, 200 /* status_code */, |
| 19700 | ERR_CONTENT_LENGTH_MISMATCH); |
| 19701 | const NetworkErrorLoggingService::RequestDetails& error = |
| 19702 | network_error_logging_service()->errors()[0]; |
| 19703 | EXPECT_LE(error.elapsed_time, base::TimeTicks::Now() - start_time); |
| 19704 | } |
| 19705 | |
| 19706 | // Same as above except the final read is ASYNC. |
| 19707 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19708 | CreateReportReadBodyErrorAsync) { |
| 19709 | std::string extra_header_string = extra_headers_.ToString(); |
Lily Chen | 29c0403 | 2019-02-25 21:50:53 | [diff] [blame] | 19710 | MockWrite data_writes[] = { |
| 19711 | MockWrite("GET / HTTP/1.1\r\n" |
| 19712 | "Host: www.example.org\r\n" |
| 19713 | "Connection: keep-alive\r\n"), |
| 19714 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19715 | }; |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 19716 | MockRead data_reads[] = { |
| 19717 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 19718 | MockRead("Content-Length: 100\r\n\r\n"), // wrong content length |
| 19719 | MockRead("hello world"), |
| 19720 | MockRead(ASYNC, OK), |
| 19721 | }; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19722 | |
| 19723 | StaticSocketDataProvider reads(data_reads, data_writes); |
| 19724 | session_deps_.socket_factory->AddSocketDataProvider(&reads); |
| 19725 | |
| 19726 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 19727 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19728 | |
| 19729 | // Log start time |
| 19730 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 19731 | |
| 19732 | TestCompletionCallback callback; |
| 19733 | auto session = CreateSession(&session_deps_); |
| 19734 | auto trans = |
| 19735 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19736 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 19737 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 19738 | |
| 19739 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 19740 | ASSERT_TRUE(response); |
| 19741 | |
| 19742 | EXPECT_TRUE(response->headers); |
| 19743 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 19744 | |
| 19745 | std::string response_data; |
| 19746 | rv = ReadTransaction(trans.get(), &response_data); |
| 19747 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
| 19748 | |
| 19749 | trans.reset(); |
| 19750 | |
| 19751 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19752 | |
| 19753 | CheckReport(0 /* index */, 200 /* status_code */, |
| 19754 | ERR_CONTENT_LENGTH_MISMATCH); |
| 19755 | const NetworkErrorLoggingService::RequestDetails& error = |
| 19756 | network_error_logging_service()->errors()[0]; |
| 19757 | EXPECT_LE(error.elapsed_time, base::TimeTicks::Now() - start_time); |
| 19758 | } |
| 19759 | |
| 19760 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19761 | CreateReportRestartWithAuth) { |
| 19762 | std::string extra_header_string = extra_headers_.ToString(); |
| 19763 | static const base::TimeDelta kSleepDuration = |
| 19764 | base::TimeDelta::FromMilliseconds(10); |
| 19765 | |
| 19766 | MockWrite data_writes1[] = { |
| 19767 | MockWrite("GET / HTTP/1.1\r\n" |
| 19768 | "Host: www.example.org\r\n" |
| 19769 | "Connection: keep-alive\r\n"), |
| 19770 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19771 | }; |
| 19772 | |
| 19773 | MockRead data_reads1[] = { |
| 19774 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 19775 | // Give a couple authenticate options (only the middle one is actually |
| 19776 | // supported). |
| 19777 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
| 19778 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 19779 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 19780 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 19781 | // Large content-length -- won't matter, as connection will be reset. |
| 19782 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 19783 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 19784 | }; |
| 19785 | |
| 19786 | // After calling trans->RestartWithAuth(), this is the request we should |
| 19787 | // be issuing -- the final header line contains the credentials. |
| 19788 | MockWrite data_writes2[] = { |
| 19789 | MockWrite("GET / HTTP/1.1\r\n" |
| 19790 | "Host: www.example.org\r\n" |
| 19791 | "Connection: keep-alive\r\n" |
| 19792 | "Authorization: Basic Zm9vOmJhcg==\r\n"), |
| 19793 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19794 | }; |
| 19795 | |
| 19796 | // Lastly, the server responds with the actual content. |
| 19797 | MockRead data_reads2[] = { |
| 19798 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 19799 | MockRead("hello world"), |
| 19800 | MockRead(SYNCHRONOUS, OK), |
| 19801 | }; |
| 19802 | |
| 19803 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 19804 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 19805 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 19806 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 19807 | |
| 19808 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 19809 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 19810 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 19811 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 19812 | |
| 19813 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 19814 | base::TimeTicks restart_time; |
| 19815 | |
| 19816 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19817 | auto trans = |
| 19818 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19819 | |
| 19820 | TestCompletionCallback callback1; |
| 19821 | |
| 19822 | int rv = trans->Start(&request_, callback1.callback(), NetLogWithSource()); |
| 19823 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 19824 | |
| 19825 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19826 | |
| 19827 | TestCompletionCallback callback2; |
| 19828 | |
| 19829 | // Wait 10 ms then restart with auth |
| 19830 | FastForwardBy(kSleepDuration); |
| 19831 | restart_time = base::TimeTicks::Now(); |
| 19832 | rv = |
| 19833 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
| 19834 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 19835 | |
| 19836 | std::string response_data; |
| 19837 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 19838 | EXPECT_EQ("hello world", response_data); |
| 19839 | |
| 19840 | trans.reset(); |
| 19841 | |
| 19842 | // One 401 report for the auth challenge, then a 200 report for the successful |
| 19843 | // retry. Note that we don't report the error draining the body, as the first |
| 19844 | // request already generated a report for the auth challenge. |
| 19845 | ASSERT_EQ(2u, network_error_logging_service()->errors().size()); |
| 19846 | |
| 19847 | // Check error report contents |
| 19848 | CheckReport(0 /* index */, 401 /* status_code */, OK); |
| 19849 | CheckReport(1 /* index */, 200 /* status_code */, OK); |
| 19850 | |
| 19851 | const NetworkErrorLoggingService::RequestDetails& error1 = |
| 19852 | network_error_logging_service()->errors()[0]; |
| 19853 | const NetworkErrorLoggingService::RequestDetails& error2 = |
| 19854 | network_error_logging_service()->errors()[1]; |
| 19855 | |
| 19856 | // Sanity-check elapsed time values |
| 19857 | EXPECT_EQ(error1.elapsed_time, restart_time - start_time - kSleepDuration); |
| 19858 | // Check that the start time is refreshed when restarting with auth. |
| 19859 | EXPECT_EQ(error2.elapsed_time, base::TimeTicks::Now() - restart_time); |
| 19860 | } |
| 19861 | |
| 19862 | // Same as above, except draining the body before restarting fails |
| 19863 | // asynchronously. |
| 19864 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19865 | CreateReportRestartWithAuthAsync) { |
| 19866 | std::string extra_header_string = extra_headers_.ToString(); |
| 19867 | static const base::TimeDelta kSleepDuration = |
| 19868 | base::TimeDelta::FromMilliseconds(10); |
| 19869 | |
| 19870 | MockWrite data_writes1[] = { |
| 19871 | MockWrite("GET / HTTP/1.1\r\n" |
| 19872 | "Host: www.example.org\r\n" |
| 19873 | "Connection: keep-alive\r\n"), |
| 19874 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19875 | }; |
| 19876 | |
| 19877 | MockRead data_reads1[] = { |
| 19878 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 19879 | // Give a couple authenticate options (only the middle one is actually |
| 19880 | // supported). |
| 19881 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
| 19882 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 19883 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 19884 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 19885 | // Large content-length -- won't matter, as connection will be reset. |
| 19886 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 19887 | MockRead(ASYNC, ERR_FAILED), |
| 19888 | }; |
| 19889 | |
| 19890 | // After calling trans->RestartWithAuth(), this is the request we should |
| 19891 | // be issuing -- the final header line contains the credentials. |
| 19892 | MockWrite data_writes2[] = { |
| 19893 | MockWrite("GET / HTTP/1.1\r\n" |
| 19894 | "Host: www.example.org\r\n" |
| 19895 | "Connection: keep-alive\r\n" |
| 19896 | "Authorization: Basic Zm9vOmJhcg==\r\n"), |
| 19897 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19898 | }; |
| 19899 | |
| 19900 | // Lastly, the server responds with the actual content. |
| 19901 | MockRead data_reads2[] = { |
| 19902 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 19903 | MockRead("hello world"), |
| 19904 | MockRead(SYNCHRONOUS, OK), |
| 19905 | }; |
| 19906 | |
| 19907 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 19908 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 19909 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 19910 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 19911 | |
| 19912 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 19913 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 19914 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 19915 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 19916 | |
| 19917 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 19918 | base::TimeTicks restart_time; |
| 19919 | |
| 19920 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19921 | auto trans = |
| 19922 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19923 | |
| 19924 | TestCompletionCallback callback1; |
| 19925 | |
| 19926 | int rv = trans->Start(&request_, callback1.callback(), NetLogWithSource()); |
| 19927 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 19928 | |
| 19929 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19930 | |
| 19931 | TestCompletionCallback callback2; |
| 19932 | |
| 19933 | // Wait 10 ms then restart with auth |
| 19934 | FastForwardBy(kSleepDuration); |
| 19935 | restart_time = base::TimeTicks::Now(); |
| 19936 | rv = |
| 19937 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
| 19938 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 19939 | |
| 19940 | std::string response_data; |
| 19941 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 19942 | EXPECT_EQ("hello world", response_data); |
| 19943 | |
| 19944 | trans.reset(); |
| 19945 | |
| 19946 | // One 401 report for the auth challenge, then a 200 report for the successful |
| 19947 | // retry. Note that we don't report the error draining the body, as the first |
| 19948 | // request already generated a report for the auth challenge. |
| 19949 | ASSERT_EQ(2u, network_error_logging_service()->errors().size()); |
| 19950 | |
| 19951 | // Check error report contents |
| 19952 | CheckReport(0 /* index */, 401 /* status_code */, OK); |
| 19953 | CheckReport(1 /* index */, 200 /* status_code */, OK); |
| 19954 | |
| 19955 | const NetworkErrorLoggingService::RequestDetails& error1 = |
| 19956 | network_error_logging_service()->errors()[0]; |
| 19957 | const NetworkErrorLoggingService::RequestDetails& error2 = |
| 19958 | network_error_logging_service()->errors()[1]; |
| 19959 | |
| 19960 | // Sanity-check elapsed time values |
| 19961 | EXPECT_EQ(error1.elapsed_time, restart_time - start_time - kSleepDuration); |
| 19962 | // Check that the start time is refreshed when restarting with auth. |
| 19963 | EXPECT_EQ(error2.elapsed_time, base::TimeTicks::Now() - restart_time); |
| 19964 | } |
| 19965 | |
| 19966 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19967 | CreateReportRetryKeepAliveConnectionReset) { |
| 19968 | std::string extra_header_string = extra_headers_.ToString(); |
| 19969 | MockWrite data_writes1[] = { |
| 19970 | MockWrite("GET / HTTP/1.1\r\n" |
| 19971 | "Host: www.example.org\r\n" |
| 19972 | "Connection: keep-alive\r\n"), |
| 19973 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 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.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 19982 | MockRead("hello"), |
| 19983 | // Connection is reset |
| 19984 | MockRead(ASYNC, ERR_CONNECTION_RESET), |
| 19985 | }; |
| 19986 | |
| 19987 | // Successful retry |
| 19988 | MockRead data_reads2[] = { |
| 19989 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 19990 | MockRead("world"), |
| 19991 | MockRead(ASYNC, OK), |
| 19992 | }; |
| 19993 | |
| 19994 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 19995 | StaticSocketDataProvider data2(data_reads2, base::span<MockWrite>()); |
| 19996 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 19997 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 19998 | |
| 19999 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 20000 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 20001 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 20002 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 20003 | |
| 20004 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20005 | auto trans1 = |
| 20006 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20007 | |
| 20008 | TestCompletionCallback callback1; |
| 20009 | |
| 20010 | int rv = trans1->Start(&request_, callback1.callback(), NetLogWithSource()); |
| 20011 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 20012 | |
| 20013 | std::string response_data; |
| 20014 | ASSERT_THAT(ReadTransaction(trans1.get(), &response_data), IsOk()); |
| 20015 | EXPECT_EQ("hello", response_data); |
| 20016 | |
| 20017 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20018 | |
| 20019 | auto trans2 = |
| 20020 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20021 | |
| 20022 | TestCompletionCallback callback2; |
| 20023 | |
| 20024 | rv = trans2->Start(&request_, callback2.callback(), NetLogWithSource()); |
| 20025 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 20026 | |
| 20027 | ASSERT_THAT(ReadTransaction(trans2.get(), &response_data), IsOk()); |
| 20028 | EXPECT_EQ("world", response_data); |
| 20029 | |
| 20030 | trans1.reset(); |
| 20031 | trans2.reset(); |
| 20032 | |
| 20033 | // One OK report from first request, then a ERR_CONNECTION_RESET report from |
| 20034 | // the second request, then an OK report from the successful retry. |
| 20035 | ASSERT_EQ(3u, network_error_logging_service()->errors().size()); |
| 20036 | |
| 20037 | // Check error report contents |
| 20038 | CheckReport(0 /* index */, 200 /* status_code */, OK); |
| 20039 | CheckReport(1 /* index */, 0 /* status_code */, ERR_CONNECTION_RESET); |
| 20040 | CheckReport(2 /* index */, 200 /* status_code */, OK); |
| 20041 | } |
| 20042 | |
| 20043 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20044 | CreateReportRetryKeepAlive408) { |
| 20045 | std::string extra_header_string = extra_headers_.ToString(); |
| 20046 | MockWrite data_writes1[] = { |
| 20047 | MockWrite("GET / HTTP/1.1\r\n" |
| 20048 | "Host: www.example.org\r\n" |
| 20049 | "Connection: keep-alive\r\n"), |
| 20050 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20051 | MockWrite("GET / HTTP/1.1\r\n" |
| 20052 | "Host: www.example.org\r\n" |
| 20053 | "Connection: keep-alive\r\n"), |
| 20054 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20055 | }; |
| 20056 | |
| 20057 | MockRead data_reads1[] = { |
| 20058 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 20059 | MockRead("hello"), |
| 20060 | // 408 Request Timeout |
| 20061 | MockRead(SYNCHRONOUS, |
| 20062 | "HTTP/1.1 408 Request Timeout\r\n" |
| 20063 | "Connection: Keep-Alive\r\n" |
| 20064 | "Content-Length: 6\r\n\r\n" |
| 20065 | "Pickle"), |
| 20066 | }; |
| 20067 | |
| 20068 | // Successful retry |
| 20069 | MockRead data_reads2[] = { |
| 20070 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 20071 | MockRead("world"), |
| 20072 | MockRead(ASYNC, OK), |
| 20073 | }; |
| 20074 | |
| 20075 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 20076 | StaticSocketDataProvider data2(data_reads2, base::span<MockWrite>()); |
| 20077 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 20078 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 20079 | |
| 20080 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 20081 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 20082 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 20083 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 20084 | |
| 20085 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20086 | auto trans1 = |
| 20087 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20088 | |
| 20089 | TestCompletionCallback callback1; |
| 20090 | |
| 20091 | int rv = trans1->Start(&request_, callback1.callback(), NetLogWithSource()); |
| 20092 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 20093 | |
| 20094 | std::string response_data; |
| 20095 | ASSERT_THAT(ReadTransaction(trans1.get(), &response_data), IsOk()); |
| 20096 | EXPECT_EQ("hello", response_data); |
| 20097 | |
| 20098 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20099 | |
| 20100 | auto trans2 = |
| 20101 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20102 | |
| 20103 | TestCompletionCallback callback2; |
| 20104 | |
| 20105 | rv = trans2->Start(&request_, callback2.callback(), NetLogWithSource()); |
| 20106 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 20107 | |
| 20108 | ASSERT_THAT(ReadTransaction(trans2.get(), &response_data), IsOk()); |
| 20109 | EXPECT_EQ("world", response_data); |
| 20110 | |
| 20111 | trans1.reset(); |
| 20112 | trans2.reset(); |
| 20113 | |
| 20114 | // One 200 report from first request, then a 408 report from |
| 20115 | // the second request, then a 200 report from the successful retry. |
| 20116 | ASSERT_EQ(3u, network_error_logging_service()->errors().size()); |
| 20117 | |
| 20118 | // Check error report contents |
| 20119 | CheckReport(0 /* index */, 200 /* status_code */, OK); |
| 20120 | CheckReport(1 /* index */, 408 /* status_code */, OK); |
| 20121 | CheckReport(2 /* index */, 200 /* status_code */, OK); |
| 20122 | } |
| 20123 | |
| 20124 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20125 | CreateReportRetry421WithoutConnectionPooling) { |
| 20126 | // Two hosts resolve to the same IP address. |
| 20127 | const std::string ip_addr = "1.2.3.4"; |
| 20128 | IPAddress ip; |
| 20129 | ASSERT_TRUE(ip.AssignFromIPLiteral(ip_addr)); |
| 20130 | IPEndPoint peer_addr = IPEndPoint(ip, 443); |
| 20131 | |
| 20132 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
| 20133 | session_deps_.host_resolver->rules()->AddRule("www.example.org", ip_addr); |
| 20134 | session_deps_.host_resolver->rules()->AddRule("mail.example.org", ip_addr); |
| 20135 | |
| 20136 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20137 | |
| 20138 | // Two requests on the first connection. |
| 20139 | spdy::SpdySerializedFrame req1( |
| 20140 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
| 20141 | spdy_util_.UpdateWithStreamDestruction(1); |
| 20142 | spdy::SpdySerializedFrame req2( |
| 20143 | spdy_util_.ConstructSpdyGet("https://mail.example.org", 3, LOWEST)); |
| 20144 | spdy::SpdySerializedFrame rst( |
| 20145 | spdy_util_.ConstructSpdyRstStream(3, spdy::ERROR_CODE_CANCEL)); |
| 20146 | MockWrite writes1[] = { |
| 20147 | CreateMockWrite(req1, 0), |
| 20148 | CreateMockWrite(req2, 3), |
| 20149 | CreateMockWrite(rst, 6), |
| 20150 | }; |
| 20151 | |
| 20152 | // The first one succeeds, the second gets error 421 Misdirected Request. |
| 20153 | spdy::SpdySerializedFrame resp1( |
| 20154 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 20155 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
| 20156 | spdy::SpdyHeaderBlock response_headers; |
| 20157 | response_headers[spdy::kHttp2StatusHeader] = "421"; |
| 20158 | spdy::SpdySerializedFrame resp2( |
| 20159 | spdy_util_.ConstructSpdyReply(3, std::move(response_headers))); |
| 20160 | MockRead reads1[] = {CreateMockRead(resp1, 1), CreateMockRead(body1, 2), |
| 20161 | CreateMockRead(resp2, 4), MockRead(ASYNC, 0, 5)}; |
| 20162 | |
| 20163 | MockConnect connect1(ASYNC, OK, peer_addr); |
| 20164 | SequencedSocketData data1(connect1, reads1, writes1); |
| 20165 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 20166 | |
| 20167 | AddSSLSocketData(); |
| 20168 | |
| 20169 | // Retry the second request on a second connection. |
| 20170 | SpdyTestUtil spdy_util2; |
| 20171 | spdy::SpdySerializedFrame req3( |
| 20172 | spdy_util2.ConstructSpdyGet("https://mail.example.org", 1, LOWEST)); |
| 20173 | MockWrite writes2[] = { |
| 20174 | CreateMockWrite(req3, 0), |
| 20175 | }; |
| 20176 | |
| 20177 | spdy::SpdySerializedFrame resp3( |
| 20178 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 20179 | spdy::SpdySerializedFrame body3(spdy_util2.ConstructSpdyDataFrame(1, true)); |
| 20180 | MockRead reads2[] = {CreateMockRead(resp3, 1), CreateMockRead(body3, 2), |
| 20181 | MockRead(ASYNC, 0, 3)}; |
| 20182 | |
| 20183 | MockConnect connect2(ASYNC, OK, peer_addr); |
| 20184 | SequencedSocketData data2(connect2, reads2, writes2); |
| 20185 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 20186 | |
| 20187 | AddSSLSocketData(); |
| 20188 | |
| 20189 | // Preload mail.example.org into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 20190 | int rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame^] | 20191 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 20192 | base::nullopt); |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 20193 | EXPECT_THAT(rv, IsOk()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20194 | |
| 20195 | HttpRequestInfo request1; |
| 20196 | request1.method = "GET"; |
| 20197 | request1.url = GURL("https://www.example.org/"); |
| 20198 | request1.load_flags = 0; |
| 20199 | request1.traffic_annotation = |
| 20200 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20201 | auto trans1 = |
| 20202 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20203 | |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 20204 | TestCompletionCallback callback; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20205 | rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 20206 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20207 | |
| 20208 | const HttpResponseInfo* response = trans1->GetResponseInfo(); |
| 20209 | ASSERT_TRUE(response); |
| 20210 | ASSERT_TRUE(response->headers); |
| 20211 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 20212 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 20213 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 20214 | std::string response_data; |
| 20215 | ASSERT_THAT(ReadTransaction(trans1.get(), &response_data), IsOk()); |
| 20216 | EXPECT_EQ("hello!", response_data); |
| 20217 | |
| 20218 | trans1.reset(); |
| 20219 | |
| 20220 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20221 | |
| 20222 | HttpRequestInfo request2; |
| 20223 | request2.method = "GET"; |
| 20224 | request2.url = GURL("https://mail.example.org/"); |
| 20225 | request2.load_flags = 0; |
| 20226 | request2.traffic_annotation = |
| 20227 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20228 | auto trans2 = |
| 20229 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20230 | |
| 20231 | BoundTestNetLog log; |
| 20232 | rv = trans2->Start(&request2, callback.callback(), log.bound()); |
| 20233 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20234 | |
| 20235 | response = trans2->GetResponseInfo(); |
| 20236 | ASSERT_TRUE(response); |
| 20237 | ASSERT_TRUE(response->headers); |
| 20238 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 20239 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 20240 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 20241 | ASSERT_THAT(ReadTransaction(trans2.get(), &response_data), IsOk()); |
| 20242 | EXPECT_EQ("hello!", response_data); |
| 20243 | |
| 20244 | trans2.reset(); |
| 20245 | |
| 20246 | // One 200 report from the first request, then a 421 report from the |
| 20247 | // second request, then a 200 report from the successful retry. |
| 20248 | ASSERT_EQ(3u, network_error_logging_service()->errors().size()); |
| 20249 | |
| 20250 | // Check error report contents |
| 20251 | const NetworkErrorLoggingService::RequestDetails& error1 = |
| 20252 | network_error_logging_service()->errors()[0]; |
| 20253 | EXPECT_EQ(GURL("https://www.example.org/"), error1.uri); |
| 20254 | EXPECT_TRUE(error1.referrer.is_empty()); |
| 20255 | EXPECT_EQ("", error1.user_agent); |
| 20256 | EXPECT_EQ(ip, error1.server_ip); |
| 20257 | EXPECT_EQ("h2", error1.protocol); |
| 20258 | EXPECT_EQ("GET", error1.method); |
| 20259 | EXPECT_EQ(200, error1.status_code); |
| 20260 | EXPECT_EQ(OK, error1.type); |
| 20261 | EXPECT_EQ(0, error1.reporting_upload_depth); |
| 20262 | |
| 20263 | const NetworkErrorLoggingService::RequestDetails& error2 = |
| 20264 | network_error_logging_service()->errors()[1]; |
| 20265 | EXPECT_EQ(GURL("https://mail.example.org/"), error2.uri); |
| 20266 | EXPECT_TRUE(error2.referrer.is_empty()); |
| 20267 | EXPECT_EQ("", error2.user_agent); |
| 20268 | EXPECT_EQ(ip, error2.server_ip); |
| 20269 | EXPECT_EQ("h2", error2.protocol); |
| 20270 | EXPECT_EQ("GET", error2.method); |
| 20271 | EXPECT_EQ(421, error2.status_code); |
| 20272 | EXPECT_EQ(OK, error2.type); |
| 20273 | EXPECT_EQ(0, error2.reporting_upload_depth); |
| 20274 | |
| 20275 | const NetworkErrorLoggingService::RequestDetails& error3 = |
| 20276 | network_error_logging_service()->errors()[2]; |
| 20277 | EXPECT_EQ(GURL("https://mail.example.org/"), error3.uri); |
| 20278 | EXPECT_TRUE(error3.referrer.is_empty()); |
| 20279 | EXPECT_EQ("", error3.user_agent); |
| 20280 | EXPECT_EQ(ip, error3.server_ip); |
| 20281 | EXPECT_EQ("h2", error3.protocol); |
| 20282 | EXPECT_EQ("GET", error3.method); |
| 20283 | EXPECT_EQ(200, error3.status_code); |
| 20284 | EXPECT_EQ(OK, error3.type); |
| 20285 | EXPECT_EQ(0, error3.reporting_upload_depth); |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20286 | } |
| 20287 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 20288 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20289 | CreateReportCancelAfterStart) { |
| 20290 | StaticSocketDataProvider data; |
| 20291 | data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); |
| 20292 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20293 | |
| 20294 | TestCompletionCallback callback; |
| 20295 | auto session = CreateSession(&session_deps_); |
| 20296 | auto trans = |
| 20297 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20298 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 20299 | EXPECT_EQ(rv, ERR_IO_PENDING); |
| 20300 | |
| 20301 | // Cancel after start. |
| 20302 | trans.reset(); |
| 20303 | |
| 20304 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20305 | CheckReport(0 /* index */, 0 /* status_code */, ERR_ABORTED, |
| 20306 | IPAddress() /* server_ip */); |
| 20307 | } |
| 20308 | |
| 20309 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20310 | CreateReportCancelBeforeReadingBody) { |
| 20311 | std::string extra_header_string = extra_headers_.ToString(); |
| 20312 | MockWrite data_writes[] = { |
| 20313 | MockWrite("GET / HTTP/1.1\r\n" |
| 20314 | "Host: www.example.org\r\n" |
| 20315 | "Connection: keep-alive\r\n"), |
| 20316 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20317 | }; |
| 20318 | MockRead data_reads[] = { |
| 20319 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 20320 | MockRead("Content-Length: 100\r\n\r\n"), // Body is never read. |
| 20321 | }; |
| 20322 | |
| 20323 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20324 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20325 | |
| 20326 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 20327 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20328 | |
| 20329 | TestCompletionCallback callback; |
| 20330 | auto session = CreateSession(&session_deps_); |
| 20331 | auto trans = |
| 20332 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20333 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 20334 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20335 | |
| 20336 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20337 | ASSERT_TRUE(response); |
| 20338 | |
| 20339 | EXPECT_TRUE(response->headers); |
| 20340 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 20341 | |
| 20342 | // Cancel before reading the body. |
| 20343 | trans.reset(); |
| 20344 | |
| 20345 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20346 | CheckReport(0 /* index */, 200 /* status_code */, ERR_ABORTED); |
| 20347 | } |
| 20348 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20349 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, DontCreateReportHttp) { |
| 20350 | base::HistogramTester histograms; |
| 20351 | RequestPolicy(); |
| 20352 | EXPECT_EQ(1u, network_error_logging_service()->headers().size()); |
| 20353 | EXPECT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20354 | |
| 20355 | // Make HTTP request |
| 20356 | std::string extra_header_string = extra_headers_.ToString(); |
| 20357 | MockRead data_reads[] = { |
| 20358 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 20359 | MockRead("hello world"), |
| 20360 | MockRead(SYNCHRONOUS, OK), |
| 20361 | }; |
| 20362 | MockWrite data_writes[] = { |
| 20363 | MockWrite("GET / HTTP/1.1\r\n" |
| 20364 | "Host: www.example.org\r\n" |
| 20365 | "Connection: keep-alive\r\n"), |
| 20366 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20367 | }; |
| 20368 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 20369 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20370 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20371 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20372 | // Insecure url |
| 20373 | url_ = "http://www.example.org/"; |
| 20374 | request_.url = GURL(url_); |
| 20375 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20376 | TestCompletionCallback callback; |
| 20377 | auto session = CreateSession(&session_deps_); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20378 | auto trans = |
| 20379 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20380 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 20381 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20382 | |
| 20383 | std::string response_data; |
| 20384 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 20385 | EXPECT_EQ("hello world", response_data); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20386 | |
| 20387 | // Insecure request does not generate a report |
| 20388 | histograms.ExpectBucketCount( |
| 20389 | NetworkErrorLoggingService::kRequestOutcomeHistogram, |
Tsuyoshi Horo | 0b04223 | 2019-03-06 01:11:05 | [diff] [blame] | 20390 | NetworkErrorLoggingService::RequestOutcome::kDiscardedInsecureOrigin, 1); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20391 | |
| 20392 | EXPECT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20393 | } |
| 20394 | |
| 20395 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20396 | DontCreateReportHttpError) { |
| 20397 | base::HistogramTester histograms; |
| 20398 | RequestPolicy(); |
| 20399 | EXPECT_EQ(1u, network_error_logging_service()->headers().size()); |
| 20400 | EXPECT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20401 | |
| 20402 | // Make HTTP request that fails |
| 20403 | MockRead data_reads[] = { |
| 20404 | MockRead("hello world"), |
| 20405 | MockRead(SYNCHRONOUS, OK), |
| 20406 | }; |
| 20407 | |
| 20408 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
| 20409 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20410 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20411 | url_ = "http://www.originwithoutpolicy.com:2000/"; |
| 20412 | request_.url = GURL(url_); |
| 20413 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20414 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20415 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20416 | auto trans = |
| 20417 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20418 | TestCompletionCallback callback; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20419 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20420 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_INVALID_HTTP_RESPONSE)); |
| 20421 | |
| 20422 | // Insecure request does not generate a report, regardless of existence of a |
| 20423 | // policy for the origin. |
| 20424 | histograms.ExpectBucketCount( |
| 20425 | NetworkErrorLoggingService::kRequestOutcomeHistogram, |
Tsuyoshi Horo | 0b04223 | 2019-03-06 01:11:05 | [diff] [blame] | 20426 | NetworkErrorLoggingService::RequestOutcome::kDiscardedInsecureOrigin, 1); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20427 | |
| 20428 | EXPECT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20429 | } |
| 20430 | |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 20431 | // Don't report on proxy auth challenges, don't report if connecting through a |
| 20432 | // proxy. |
| 20433 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, DontCreateReportProxy) { |
| 20434 | HttpRequestInfo request; |
| 20435 | request.method = "GET"; |
| 20436 | request.url = GURL("https://www.example.org/"); |
| 20437 | request.traffic_annotation = |
| 20438 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20439 | |
| 20440 | // Configure against proxy server "myproxy:70". |
| 20441 | session_deps_.proxy_resolution_service = |
| 20442 | ProxyResolutionService::CreateFixedFromPacResult( |
| 20443 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20444 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20445 | |
| 20446 | // Since we have proxy, should try to establish tunnel. |
| 20447 | MockWrite data_writes1[] = { |
| 20448 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 20449 | "Host: www.example.org:443\r\n" |
| 20450 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 20451 | }; |
| 20452 | |
| 20453 | // The proxy responds to the connect with a 407, using a non-persistent |
| 20454 | // connection. |
| 20455 | MockRead data_reads1[] = { |
| 20456 | // No credentials. |
| 20457 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 20458 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 20459 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 20460 | }; |
| 20461 | |
| 20462 | MockWrite data_writes2[] = { |
| 20463 | // After calling trans->RestartWithAuth(), this is the request we should |
| 20464 | // be issuing -- the final header line contains the credentials. |
| 20465 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 20466 | "Host: www.example.org:443\r\n" |
| 20467 | "Proxy-Connection: keep-alive\r\n" |
| 20468 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 20469 | |
| 20470 | MockWrite("GET / HTTP/1.1\r\n" |
| 20471 | "Host: www.example.org\r\n" |
| 20472 | "Connection: keep-alive\r\n\r\n"), |
| 20473 | }; |
| 20474 | |
| 20475 | MockRead data_reads2[] = { |
| 20476 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 20477 | |
| 20478 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20479 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 20480 | MockRead("Content-Length: 5\r\n\r\n"), |
| 20481 | MockRead(SYNCHRONOUS, "hello"), |
| 20482 | }; |
| 20483 | |
| 20484 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 20485 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 20486 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 20487 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 20488 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 20489 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20490 | |
| 20491 | TestCompletionCallback callback1; |
| 20492 | |
| 20493 | auto trans = |
| 20494 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20495 | |
| 20496 | int rv = trans->Start(&request, callback1.callback(), NetLogWithSource()); |
| 20497 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 20498 | |
| 20499 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20500 | EXPECT_EQ(407, response->headers->response_code()); |
| 20501 | |
| 20502 | std::string response_data; |
| 20503 | rv = ReadTransaction(trans.get(), &response_data); |
| 20504 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
| 20505 | |
| 20506 | // No NEL report is generated for the 407. |
| 20507 | EXPECT_EQ(0u, network_error_logging_service()->errors().size()); |
| 20508 | |
| 20509 | TestCompletionCallback callback2; |
| 20510 | |
| 20511 | rv = |
| 20512 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
| 20513 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 20514 | |
| 20515 | response = trans->GetResponseInfo(); |
| 20516 | EXPECT_EQ(200, response->headers->response_code()); |
| 20517 | |
| 20518 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 20519 | EXPECT_EQ("hello", response_data); |
| 20520 | |
| 20521 | trans.reset(); |
| 20522 | |
| 20523 | // No NEL report is generated because we are behind a proxy. |
| 20524 | EXPECT_EQ(0u, network_error_logging_service()->errors().size()); |
| 20525 | } |
| 20526 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20527 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20528 | ReportContainsUploadDepth) { |
| 20529 | reporting_upload_depth_ = 7; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20530 | request_.reporting_upload_depth = reporting_upload_depth_; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20531 | RequestPolicy(); |
| 20532 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20533 | const NetworkErrorLoggingService::RequestDetails& error = |
| 20534 | network_error_logging_service()->errors()[0]; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20535 | EXPECT_EQ(7, error.reporting_upload_depth); |
| 20536 | } |
| 20537 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20538 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, ReportElapsedTime) { |
| 20539 | std::string extra_header_string = extra_headers_.ToString(); |
| 20540 | static const base::TimeDelta kSleepDuration = |
| 20541 | base::TimeDelta::FromMilliseconds(10); |
| 20542 | |
| 20543 | std::vector<MockWrite> data_writes = { |
| 20544 | MockWrite(ASYNC, 0, |
| 20545 | "GET / HTTP/1.1\r\n" |
| 20546 | "Host: www.example.org\r\n" |
| 20547 | "Connection: keep-alive\r\n"), |
| 20548 | MockWrite(ASYNC, 1, extra_header_string.data()), |
| 20549 | }; |
| 20550 | |
| 20551 | std::vector<MockRead> data_reads = { |
| 20552 | // Write one byte of the status line, followed by a pause. |
| 20553 | MockRead(ASYNC, 2, "H"), |
| 20554 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
| 20555 | MockRead(ASYNC, 4, "TTP/1.1 200 OK\r\n\r\n"), |
| 20556 | MockRead(ASYNC, 5, "hello world"), |
| 20557 | MockRead(SYNCHRONOUS, OK, 6), |
| 20558 | }; |
| 20559 | |
| 20560 | SequencedSocketData data(data_reads, data_writes); |
| 20561 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20562 | |
| 20563 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 20564 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20565 | |
| 20566 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20567 | |
| 20568 | auto trans = |
| 20569 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20570 | |
| 20571 | TestCompletionCallback callback; |
| 20572 | |
| 20573 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 20574 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20575 | |
| 20576 | data.RunUntilPaused(); |
| 20577 | ASSERT_TRUE(data.IsPaused()); |
| 20578 | FastForwardBy(kSleepDuration); |
| 20579 | data.Resume(); |
| 20580 | |
| 20581 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20582 | |
| 20583 | std::string response_data; |
| 20584 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 20585 | EXPECT_EQ("hello world", response_data); |
| 20586 | |
| 20587 | trans.reset(); |
| 20588 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20589 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20590 | |
| 20591 | CheckReport(0 /* index */, 200 /* status_code */, OK); |
| 20592 | |
| 20593 | const NetworkErrorLoggingService::RequestDetails& error = |
| 20594 | network_error_logging_service()->errors()[0]; |
| 20595 | |
| 20596 | // Sanity-check elapsed time in error report |
| 20597 | EXPECT_EQ(kSleepDuration, error.elapsed_time); |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20598 | } |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20599 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 20600 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 20601 | |
Batalov Vladislav | a4e97a50 | 2019-04-11 15:35:23 | [diff] [blame] | 20602 | TEST_F(HttpNetworkTransactionTest, AlwaysFailRequestToCache) { |
| 20603 | HttpRequestInfo request; |
| 20604 | request.method = "GET"; |
| 20605 | request.url = GURL("http://example.org/"); |
| 20606 | |
| 20607 | request.load_flags = LOAD_ONLY_FROM_CACHE; |
| 20608 | |
| 20609 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20610 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 20611 | TestCompletionCallback callback1; |
| 20612 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
| 20613 | |
| 20614 | EXPECT_THAT(rv, IsError(ERR_CACHE_MISS)); |
| 20615 | } |
| 20616 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20617 | TEST_F(HttpNetworkTransactionTest, ZeroRTTDoesntConfirm) { |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20618 | static const base::TimeDelta kDelay = base::TimeDelta::FromMilliseconds(10); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20619 | HttpRequestInfo request; |
| 20620 | request.method = "GET"; |
| 20621 | request.url = GURL("https://www.example.org/"); |
| 20622 | request.traffic_annotation = |
| 20623 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20624 | |
| 20625 | MockWrite data_writes[] = { |
| 20626 | MockWrite("GET / HTTP/1.1\r\n" |
| 20627 | "Host: www.example.org\r\n" |
| 20628 | "Connection: keep-alive\r\n\r\n"), |
| 20629 | }; |
| 20630 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20631 | MockRead data_reads[] = { |
| 20632 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20633 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20634 | MockRead(SYNCHRONOUS, "1"), |
| 20635 | }; |
| 20636 | |
| 20637 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20638 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20639 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20640 | ssl.connect_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20641 | ssl.confirm = MockConfirm(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20642 | ssl.confirm_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20643 | session_deps_.enable_early_data = true; |
| 20644 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20645 | |
| 20646 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20647 | |
| 20648 | TestCompletionCallback callback; |
| 20649 | auto trans = |
| 20650 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20651 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20652 | base::TimeTicks start_time = base::TimeTicks::Now(); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20653 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 20654 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20655 | |
| 20656 | rv = callback.WaitForResult(); |
| 20657 | EXPECT_THAT(rv, IsOk()); |
| 20658 | |
| 20659 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20660 | ASSERT_TRUE(response); |
| 20661 | ASSERT_TRUE(response->headers); |
| 20662 | EXPECT_EQ(200, response->headers->response_code()); |
| 20663 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 20664 | |
| 20665 | // Check that ConfirmHandshake wasn't called. |
| 20666 | ASSERT_FALSE(ssl.ConfirmDataConsumed()); |
| 20667 | ASSERT_TRUE(ssl.WriteBeforeConfirm()); |
| 20668 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20669 | // The handshake time should include the time it took to run Connect(), but |
| 20670 | // not ConfirmHandshake(). |
| 20671 | LoadTimingInfo load_timing_info; |
| 20672 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 20673 | EXPECT_EQ(load_timing_info.connect_timing.connect_start, start_time); |
| 20674 | EXPECT_EQ(load_timing_info.connect_timing.ssl_start, start_time); |
| 20675 | EXPECT_EQ(load_timing_info.connect_timing.ssl_end, start_time + kDelay); |
| 20676 | EXPECT_EQ(load_timing_info.connect_timing.connect_end, start_time + kDelay); |
| 20677 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20678 | trans.reset(); |
| 20679 | |
| 20680 | session->CloseAllConnections(); |
| 20681 | } |
| 20682 | |
| 20683 | TEST_F(HttpNetworkTransactionTest, ZeroRTTSyncConfirmSyncWrite) { |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20684 | static const base::TimeDelta kDelay = base::TimeDelta::FromMilliseconds(10); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20685 | HttpRequestInfo request; |
| 20686 | request.method = "POST"; |
| 20687 | request.url = GURL("https://www.example.org/"); |
| 20688 | request.traffic_annotation = |
| 20689 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20690 | |
| 20691 | MockWrite data_writes[] = { |
| 20692 | MockWrite(SYNCHRONOUS, |
| 20693 | "POST / HTTP/1.1\r\n" |
| 20694 | "Host: www.example.org\r\n" |
| 20695 | "Connection: keep-alive\r\n" |
| 20696 | "Content-Length: 0\r\n\r\n"), |
| 20697 | }; |
| 20698 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20699 | MockRead data_reads[] = { |
| 20700 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20701 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20702 | MockRead(SYNCHRONOUS, "1"), |
| 20703 | }; |
| 20704 | |
| 20705 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20706 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20707 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20708 | ssl.connect_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20709 | ssl.confirm = MockConfirm(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20710 | ssl.confirm_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20711 | session_deps_.enable_early_data = true; |
| 20712 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20713 | |
| 20714 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20715 | |
| 20716 | TestCompletionCallback callback; |
| 20717 | auto trans = |
| 20718 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20719 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20720 | base::TimeTicks start_time = base::TimeTicks::Now(); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20721 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 20722 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20723 | |
| 20724 | rv = callback.WaitForResult(); |
| 20725 | EXPECT_THAT(rv, IsOk()); |
| 20726 | |
| 20727 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20728 | ASSERT_TRUE(response); |
| 20729 | ASSERT_TRUE(response->headers); |
| 20730 | EXPECT_EQ(200, response->headers->response_code()); |
| 20731 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 20732 | |
| 20733 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 20734 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 20735 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20736 | // The handshake time should include the time it took to run Connect(), but |
| 20737 | // not ConfirmHandshake(). If ConfirmHandshake() returns synchronously, we |
| 20738 | // assume the connection did not negotiate 0-RTT or the handshake was already |
| 20739 | // confirmed. |
| 20740 | LoadTimingInfo load_timing_info; |
| 20741 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 20742 | EXPECT_EQ(load_timing_info.connect_timing.connect_start, start_time); |
| 20743 | EXPECT_EQ(load_timing_info.connect_timing.ssl_start, start_time); |
| 20744 | EXPECT_EQ(load_timing_info.connect_timing.ssl_end, start_time + kDelay); |
| 20745 | EXPECT_EQ(load_timing_info.connect_timing.connect_end, start_time + kDelay); |
| 20746 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20747 | trans.reset(); |
| 20748 | |
| 20749 | session->CloseAllConnections(); |
| 20750 | } |
| 20751 | |
| 20752 | TEST_F(HttpNetworkTransactionTest, ZeroRTTSyncConfirmAsyncWrite) { |
| 20753 | HttpRequestInfo request; |
| 20754 | request.method = "POST"; |
| 20755 | request.url = GURL("https://www.example.org/"); |
| 20756 | request.traffic_annotation = |
| 20757 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20758 | |
| 20759 | MockWrite data_writes[] = { |
| 20760 | MockWrite(ASYNC, |
| 20761 | "POST / HTTP/1.1\r\n" |
| 20762 | "Host: www.example.org\r\n" |
| 20763 | "Connection: keep-alive\r\n" |
| 20764 | "Content-Length: 0\r\n\r\n"), |
| 20765 | }; |
| 20766 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20767 | MockRead data_reads[] = { |
| 20768 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20769 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20770 | MockRead(SYNCHRONOUS, "1"), |
| 20771 | }; |
| 20772 | |
| 20773 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20774 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20775 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 20776 | ssl.confirm = MockConfirm(SYNCHRONOUS, OK); |
| 20777 | session_deps_.enable_early_data = true; |
| 20778 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20779 | |
| 20780 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20781 | |
| 20782 | TestCompletionCallback callback; |
| 20783 | auto trans = |
| 20784 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20785 | |
| 20786 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 20787 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20788 | |
| 20789 | rv = callback.WaitForResult(); |
| 20790 | EXPECT_THAT(rv, IsOk()); |
| 20791 | |
| 20792 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20793 | ASSERT_TRUE(response); |
| 20794 | ASSERT_TRUE(response->headers); |
| 20795 | EXPECT_EQ(200, response->headers->response_code()); |
| 20796 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 20797 | |
| 20798 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 20799 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 20800 | |
| 20801 | trans.reset(); |
| 20802 | |
| 20803 | session->CloseAllConnections(); |
| 20804 | } |
| 20805 | |
| 20806 | TEST_F(HttpNetworkTransactionTest, ZeroRTTAsyncConfirmSyncWrite) { |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20807 | static const base::TimeDelta kDelay = base::TimeDelta::FromMilliseconds(10); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20808 | HttpRequestInfo request; |
| 20809 | request.method = "POST"; |
| 20810 | request.url = GURL("https://www.example.org/"); |
| 20811 | request.traffic_annotation = |
| 20812 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20813 | |
| 20814 | MockWrite data_writes[] = { |
| 20815 | MockWrite(SYNCHRONOUS, |
| 20816 | "POST / HTTP/1.1\r\n" |
| 20817 | "Host: www.example.org\r\n" |
| 20818 | "Connection: keep-alive\r\n" |
| 20819 | "Content-Length: 0\r\n\r\n"), |
| 20820 | }; |
| 20821 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20822 | MockRead data_reads[] = { |
| 20823 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20824 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20825 | MockRead(SYNCHRONOUS, "1"), |
| 20826 | }; |
| 20827 | |
| 20828 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20829 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20830 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20831 | ssl.connect_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20832 | ssl.confirm = MockConfirm(ASYNC, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20833 | ssl.confirm_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20834 | session_deps_.enable_early_data = true; |
| 20835 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20836 | |
| 20837 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20838 | |
| 20839 | TestCompletionCallback callback; |
| 20840 | auto trans = |
| 20841 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20842 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20843 | base::TimeTicks start_time = base::TimeTicks::Now(); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20844 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 20845 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20846 | |
| 20847 | rv = callback.WaitForResult(); |
| 20848 | EXPECT_THAT(rv, IsOk()); |
| 20849 | |
| 20850 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20851 | ASSERT_TRUE(response); |
| 20852 | ASSERT_TRUE(response->headers); |
| 20853 | EXPECT_EQ(200, response->headers->response_code()); |
| 20854 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 20855 | |
| 20856 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 20857 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 20858 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20859 | // The handshake time should include the time it took to run Connect() and |
| 20860 | // ConfirmHandshake(). |
| 20861 | LoadTimingInfo load_timing_info; |
| 20862 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 20863 | EXPECT_EQ(load_timing_info.connect_timing.connect_start, start_time); |
| 20864 | EXPECT_EQ(load_timing_info.connect_timing.ssl_start, start_time); |
| 20865 | EXPECT_EQ(load_timing_info.connect_timing.ssl_end, start_time + 2 * kDelay); |
| 20866 | EXPECT_EQ(load_timing_info.connect_timing.connect_end, |
| 20867 | start_time + 2 * kDelay); |
| 20868 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20869 | trans.reset(); |
| 20870 | |
| 20871 | session->CloseAllConnections(); |
| 20872 | } |
| 20873 | |
| 20874 | TEST_F(HttpNetworkTransactionTest, ZeroRTTAsyncConfirmAsyncWrite) { |
| 20875 | HttpRequestInfo request; |
| 20876 | request.method = "POST"; |
| 20877 | request.url = GURL("https://www.example.org/"); |
| 20878 | request.traffic_annotation = |
| 20879 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20880 | |
| 20881 | MockWrite data_writes[] = { |
| 20882 | MockWrite(ASYNC, |
| 20883 | "POST / HTTP/1.1\r\n" |
| 20884 | "Host: www.example.org\r\n" |
| 20885 | "Connection: keep-alive\r\n" |
| 20886 | "Content-Length: 0\r\n\r\n"), |
| 20887 | }; |
| 20888 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20889 | MockRead data_reads[] = { |
| 20890 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20891 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20892 | MockRead(SYNCHRONOUS, "1"), |
| 20893 | }; |
| 20894 | |
| 20895 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20896 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20897 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 20898 | ssl.confirm = MockConfirm(ASYNC, OK); |
| 20899 | session_deps_.enable_early_data = true; |
| 20900 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20901 | |
| 20902 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20903 | |
| 20904 | TestCompletionCallback callback; |
| 20905 | auto trans = |
| 20906 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20907 | |
| 20908 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 20909 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20910 | |
| 20911 | rv = callback.WaitForResult(); |
| 20912 | EXPECT_THAT(rv, IsOk()); |
| 20913 | |
| 20914 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20915 | ASSERT_TRUE(response); |
| 20916 | ASSERT_TRUE(response->headers); |
| 20917 | EXPECT_EQ(200, response->headers->response_code()); |
| 20918 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 20919 | |
| 20920 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 20921 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 20922 | |
| 20923 | trans.reset(); |
| 20924 | |
| 20925 | session->CloseAllConnections(); |
| 20926 | } |
| 20927 | |
David Benjamin | fb97693 | 2019-05-15 13:39:15 | [diff] [blame] | 20928 | // 0-RTT rejects are handled at HttpNetworkTransaction. |
| 20929 | TEST_F(HttpNetworkTransactionTest, ZeroRTTReject) { |
| 20930 | enum class RejectType { |
| 20931 | kRead, |
| 20932 | kWrite, |
| 20933 | kConfirm, |
| 20934 | }; |
| 20935 | |
| 20936 | for (RejectType type : |
| 20937 | {RejectType::kRead, RejectType::kWrite, RejectType::kConfirm}) { |
| 20938 | SCOPED_TRACE(static_cast<int>(type)); |
| 20939 | for (Error reject_error : |
| 20940 | {ERR_EARLY_DATA_REJECTED, ERR_WRONG_VERSION_ON_EARLY_DATA}) { |
| 20941 | SCOPED_TRACE(reject_error); |
| 20942 | session_deps_.socket_factory = |
| 20943 | std::make_unique<MockClientSocketFactory>(); |
| 20944 | |
| 20945 | HttpRequestInfo request; |
| 20946 | request.method = type == RejectType::kConfirm ? "POST" : "GET"; |
| 20947 | request.url = GURL("https://www.example.org/"); |
| 20948 | request.traffic_annotation = |
| 20949 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20950 | |
| 20951 | // The first request fails. |
| 20952 | std::vector<MockWrite> data1_writes; |
| 20953 | std::vector<MockRead> data1_reads; |
| 20954 | SSLSocketDataProvider ssl1(SYNCHRONOUS, OK); |
| 20955 | switch (type) { |
| 20956 | case RejectType::kRead: |
| 20957 | data1_writes.emplace_back( |
| 20958 | "GET / HTTP/1.1\r\n" |
| 20959 | "Host: www.example.org\r\n" |
| 20960 | "Connection: keep-alive\r\n\r\n"); |
| 20961 | data1_reads.emplace_back(ASYNC, reject_error); |
| 20962 | // Cause ConfirmHandshake to hang (it should not be called). |
| 20963 | ssl1.confirm = MockConfirm(SYNCHRONOUS, ERR_IO_PENDING); |
| 20964 | break; |
| 20965 | case RejectType::kWrite: |
| 20966 | data1_writes.emplace_back(ASYNC, reject_error); |
| 20967 | // Cause ConfirmHandshake to hang (it should not be called). |
| 20968 | ssl1.confirm = MockConfirm(SYNCHRONOUS, ERR_IO_PENDING); |
| 20969 | break; |
| 20970 | case RejectType::kConfirm: |
| 20971 | // The request never gets far enough to read or write. |
| 20972 | ssl1.confirm = MockConfirm(ASYNC, reject_error); |
| 20973 | break; |
| 20974 | } |
| 20975 | |
| 20976 | StaticSocketDataProvider data1(data1_reads, data1_writes); |
| 20977 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 20978 | session_deps_.enable_early_data = true; |
| 20979 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 20980 | |
| 20981 | // The retry succeeds. |
| 20982 | // |
| 20983 | // TODO(https://crbug.com/950705): If |reject_error| is |
| 20984 | // ERR_EARLY_DATA_REJECTED, the retry should happen over the same socket. |
| 20985 | MockWrite data2_writes[] = { |
| 20986 | request.method == "POST" |
| 20987 | ? MockWrite("POST / HTTP/1.1\r\n" |
| 20988 | "Host: www.example.org\r\n" |
| 20989 | "Connection: keep-alive\r\n" |
| 20990 | "Content-Length: 0\r\n\r\n") |
| 20991 | : MockWrite("GET / HTTP/1.1\r\n" |
| 20992 | "Host: www.example.org\r\n" |
| 20993 | "Connection: keep-alive\r\n\r\n"), |
| 20994 | }; |
| 20995 | |
| 20996 | MockRead data2_reads[] = { |
| 20997 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20998 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20999 | MockRead(SYNCHRONOUS, "1"), |
| 21000 | }; |
| 21001 | |
| 21002 | StaticSocketDataProvider data2(data2_reads, data2_writes); |
| 21003 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 21004 | SSLSocketDataProvider ssl2(SYNCHRONOUS, OK); |
| 21005 | ssl2.confirm = MockConfirm(ASYNC, OK); |
| 21006 | session_deps_.enable_early_data = true; |
| 21007 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 21008 | |
| 21009 | std::unique_ptr<HttpNetworkSession> session( |
| 21010 | CreateSession(&session_deps_)); |
| 21011 | |
| 21012 | TestCompletionCallback callback; |
| 21013 | auto trans = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 21014 | session.get()); |
| 21015 | |
| 21016 | EXPECT_THAT(callback.GetResult(trans->Start(&request, callback.callback(), |
| 21017 | NetLogWithSource())), |
| 21018 | IsOk()); |
| 21019 | |
| 21020 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 21021 | ASSERT_TRUE(response); |
| 21022 | ASSERT_TRUE(response->headers); |
| 21023 | EXPECT_EQ(200, response->headers->response_code()); |
| 21024 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 21025 | } |
| 21026 | } |
| 21027 | } |
| 21028 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 21029 | TEST_F(HttpNetworkTransactionTest, ZeroRTTConfirmErrorSync) { |
| 21030 | HttpRequestInfo request; |
| 21031 | request.method = "POST"; |
| 21032 | request.url = GURL("https://www.example.org/"); |
| 21033 | request.traffic_annotation = |
| 21034 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21035 | |
| 21036 | MockWrite data_writes[] = { |
| 21037 | MockWrite("POST / HTTP/1.1\r\n" |
| 21038 | "Host: www.example.org\r\n" |
| 21039 | "Connection: keep-alive\r\n" |
| 21040 | "Content-Length: 0\r\n\r\n"), |
| 21041 | }; |
| 21042 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 21043 | MockRead data_reads[] = { |
| 21044 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 21045 | MockRead("Content-Length: 1\r\n\r\n"), |
| 21046 | MockRead(SYNCHRONOUS, "1"), |
| 21047 | }; |
| 21048 | |
| 21049 | StaticSocketDataProvider data(data_reads, data_writes); |
| 21050 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 21051 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 21052 | ssl.confirm = MockConfirm(SYNCHRONOUS, ERR_SSL_PROTOCOL_ERROR); |
| 21053 | session_deps_.enable_early_data = true; |
| 21054 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 21055 | |
| 21056 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21057 | |
| 21058 | TestCompletionCallback callback; |
| 21059 | auto trans = |
| 21060 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21061 | |
| 21062 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 21063 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 21064 | |
| 21065 | rv = callback.WaitForResult(); |
| 21066 | EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
| 21067 | |
| 21068 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 21069 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 21070 | |
| 21071 | trans.reset(); |
| 21072 | |
| 21073 | session->CloseAllConnections(); |
| 21074 | } |
| 21075 | |
| 21076 | TEST_F(HttpNetworkTransactionTest, ZeroRTTConfirmErrorAsync) { |
| 21077 | HttpRequestInfo request; |
| 21078 | request.method = "POST"; |
| 21079 | request.url = GURL("https://www.example.org/"); |
| 21080 | request.traffic_annotation = |
| 21081 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21082 | |
| 21083 | MockWrite data_writes[] = { |
| 21084 | MockWrite("POST / HTTP/1.1\r\n" |
| 21085 | "Host: www.example.org\r\n" |
| 21086 | "Connection: keep-alive\r\n" |
| 21087 | "Content-Length: 0\r\n\r\n"), |
| 21088 | }; |
| 21089 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 21090 | MockRead data_reads[] = { |
| 21091 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 21092 | MockRead("Content-Length: 1\r\n\r\n"), |
| 21093 | MockRead(SYNCHRONOUS, "1"), |
| 21094 | }; |
| 21095 | |
| 21096 | StaticSocketDataProvider data(data_reads, data_writes); |
| 21097 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 21098 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 21099 | ssl.confirm = MockConfirm(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
| 21100 | session_deps_.enable_early_data = true; |
| 21101 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 21102 | |
| 21103 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21104 | |
| 21105 | TestCompletionCallback callback; |
| 21106 | auto trans = |
| 21107 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21108 | |
| 21109 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 21110 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 21111 | |
| 21112 | rv = callback.WaitForResult(); |
| 21113 | EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
| 21114 | |
| 21115 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 21116 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 21117 | |
| 21118 | trans.reset(); |
| 21119 | |
| 21120 | session->CloseAllConnections(); |
| 21121 | } |
| 21122 | |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21123 | // Test the proxy and origin server each requesting both TLS client certificates |
| 21124 | // and HTTP auth. This is a regression test for https://crbug.com/946406. |
| 21125 | TEST_F(HttpNetworkTransactionTest, AuthEverything) { |
| 21126 | // Note these hosts must match the CheckBasic*Auth() functions. |
| 21127 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 21128 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21129 | |
| 21130 | auto cert_request_info_proxy = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21131 | cert_request_info_proxy->host_and_port = HostPortPair("myproxy", 70); |
| 21132 | |
| 21133 | std::unique_ptr<FakeClientCertIdentity> identity_proxy = |
| 21134 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21135 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 21136 | ASSERT_TRUE(identity_proxy); |
| 21137 | |
| 21138 | auto cert_request_info_origin = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21139 | cert_request_info_origin->host_and_port = |
| 21140 | HostPortPair("www.example.org", 443); |
| 21141 | |
| 21142 | std::unique_ptr<FakeClientCertIdentity> identity_origin = |
| 21143 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21144 | GetTestCertsDirectory(), "client_2.pem", "client_2.pk8"); |
| 21145 | ASSERT_TRUE(identity_origin); |
| 21146 | |
| 21147 | HttpRequestInfo request; |
| 21148 | request.method = "GET"; |
| 21149 | request.url = GURL("https://www.example.org/"); |
| 21150 | request.traffic_annotation = |
| 21151 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21152 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21153 | // First, the client connects to the proxy, which requests a client |
| 21154 | // certificate. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21155 | SSLSocketDataProvider ssl_proxy1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21156 | ssl_proxy1.cert_request_info = cert_request_info_proxy.get(); |
| 21157 | ssl_proxy1.expected_send_client_cert = false; |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21158 | StaticSocketDataProvider data1; |
| 21159 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21160 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy1); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21161 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21162 | // The client responds with a certificate on a new connection. The handshake |
| 21163 | // succeeds. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21164 | SSLSocketDataProvider ssl_proxy2(ASYNC, OK); |
| 21165 | ssl_proxy2.expected_send_client_cert = true; |
| 21166 | ssl_proxy2.expected_client_cert = identity_proxy->certificate(); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21167 | // The client attempts an HTTP CONNECT, but the proxy requests basic auth. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21168 | std::vector<MockWrite> mock_writes2; |
| 21169 | std::vector<MockRead> mock_reads2; |
| 21170 | mock_writes2.emplace_back( |
| 21171 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21172 | "Host: www.example.org:443\r\n" |
| 21173 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 21174 | mock_reads2.emplace_back( |
| 21175 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 21176 | "Content-Length: 0\r\n" |
| 21177 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21178 | // The client retries with credentials. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21179 | mock_writes2.emplace_back( |
| 21180 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21181 | "Host: www.example.org:443\r\n" |
| 21182 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21183 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21184 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21185 | mock_reads2.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21186 | // The origin requests client certificates. |
| 21187 | SSLSocketDataProvider ssl_origin2(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21188 | ssl_origin2.cert_request_info = cert_request_info_origin.get(); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21189 | StaticSocketDataProvider data2(mock_reads2, mock_writes2); |
| 21190 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21191 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy2); |
| 21192 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin2); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21193 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21194 | // The client responds to the origin client certificate request on a new |
| 21195 | // connection. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21196 | SSLSocketDataProvider ssl_proxy3(ASYNC, OK); |
| 21197 | ssl_proxy3.expected_send_client_cert = true; |
| 21198 | ssl_proxy3.expected_client_cert = identity_proxy->certificate(); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21199 | std::vector<MockWrite> mock_writes3; |
| 21200 | std::vector<MockRead> mock_reads3; |
| 21201 | mock_writes3.emplace_back( |
| 21202 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21203 | "Host: www.example.org:443\r\n" |
| 21204 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21205 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21206 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21207 | mock_reads3.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21208 | SSLSocketDataProvider ssl_origin3(ASYNC, OK); |
| 21209 | ssl_origin3.expected_send_client_cert = true; |
| 21210 | ssl_origin3.expected_client_cert = identity_origin->certificate(); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21211 | // The client sends the origin HTTP request, which results in another HTTP |
| 21212 | // auth request. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21213 | mock_writes3.emplace_back( |
| 21214 | "GET / HTTP/1.1\r\n" |
| 21215 | "Host: www.example.org\r\n" |
| 21216 | "Connection: keep-alive\r\n\r\n"); |
| 21217 | mock_reads3.emplace_back( |
| 21218 | "HTTP/1.1 401 Unauthorized\r\n" |
| 21219 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 21220 | "Content-Length: 0\r\n\r\n"); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21221 | // The client retries with credentials, and the request finally succeeds. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21222 | mock_writes3.emplace_back( |
| 21223 | "GET / HTTP/1.1\r\n" |
| 21224 | "Host: www.example.org\r\n" |
| 21225 | "Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21226 | // Authenticate as user:pass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21227 | "Authorization: Basic dXNlcjpwYXNz\r\n\r\n"); |
| 21228 | mock_reads3.emplace_back( |
| 21229 | "HTTP/1.1 200 OK\r\n" |
| 21230 | "Content-Length: 0\r\n\r\n"); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 21231 | // The client makes another request. This should reuse the socket with all |
| 21232 | // credentials cached. |
| 21233 | mock_writes3.emplace_back( |
| 21234 | "GET / HTTP/1.1\r\n" |
| 21235 | "Host: www.example.org\r\n" |
| 21236 | "Connection: keep-alive\r\n" |
| 21237 | // Authenticate as user:pass. |
| 21238 | "Authorization: Basic dXNlcjpwYXNz\r\n\r\n"); |
| 21239 | mock_reads3.emplace_back( |
| 21240 | "HTTP/1.1 200 OK\r\n" |
| 21241 | "Content-Length: 0\r\n\r\n"); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21242 | StaticSocketDataProvider data3(mock_reads3, mock_writes3); |
| 21243 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21244 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy3); |
| 21245 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin3); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21246 | |
| 21247 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21248 | |
| 21249 | // Start the request. |
| 21250 | TestCompletionCallback callback; |
| 21251 | auto trans = |
| 21252 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21253 | int rv = callback.GetResult( |
| 21254 | trans->Start(&request, callback.callback(), NetLogWithSource())); |
| 21255 | |
| 21256 | // Handle the proxy client certificate challenge. |
| 21257 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21258 | SSLCertRequestInfo* cert_request_info = |
| 21259 | trans->GetResponseInfo()->cert_request_info.get(); |
| 21260 | ASSERT_TRUE(cert_request_info); |
| 21261 | EXPECT_TRUE(cert_request_info->is_proxy); |
| 21262 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21263 | cert_request_info_proxy->host_and_port); |
| 21264 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21265 | identity_proxy->certificate(), identity_proxy->ssl_private_key(), |
| 21266 | callback.callback())); |
| 21267 | |
| 21268 | // Handle the proxy HTTP auth challenge. |
| 21269 | ASSERT_THAT(rv, IsOk()); |
| 21270 | EXPECT_EQ(407, trans->GetResponseInfo()->headers->response_code()); |
| 21271 | EXPECT_TRUE( |
| 21272 | CheckBasicSecureProxyAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21273 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21274 | AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass")), |
| 21275 | callback.callback())); |
| 21276 | |
| 21277 | // Handle the origin client certificate challenge. |
| 21278 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21279 | cert_request_info = trans->GetResponseInfo()->cert_request_info.get(); |
| 21280 | ASSERT_TRUE(cert_request_info); |
| 21281 | EXPECT_FALSE(cert_request_info->is_proxy); |
| 21282 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21283 | cert_request_info_origin->host_and_port); |
| 21284 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21285 | identity_origin->certificate(), identity_origin->ssl_private_key(), |
| 21286 | callback.callback())); |
| 21287 | |
| 21288 | // Handle the origin HTTP auth challenge. |
| 21289 | ASSERT_THAT(rv, IsOk()); |
| 21290 | EXPECT_EQ(401, trans->GetResponseInfo()->headers->response_code()); |
| 21291 | EXPECT_TRUE( |
| 21292 | CheckBasicSecureServerAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21293 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21294 | AuthCredentials(ASCIIToUTF16("user"), ASCIIToUTF16("pass")), |
| 21295 | callback.callback())); |
| 21296 | |
| 21297 | // The request completes. |
| 21298 | ASSERT_THAT(rv, IsOk()); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21299 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 21300 | |
| 21301 | // Make a second request. This time all credentials are cached. |
| 21302 | trans = |
| 21303 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21304 | ASSERT_THAT(callback.GetResult(trans->Start(&request, callback.callback(), |
| 21305 | NetLogWithSource())), |
| 21306 | IsOk()); |
| 21307 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21308 | } |
| 21309 | |
| 21310 | // Test the proxy and origin server each requesting both TLS client certificates |
| 21311 | // and HTTP auth and each HTTP auth closing the connection. This is a regression |
| 21312 | // test for https://crbug.com/946406. |
| 21313 | TEST_F(HttpNetworkTransactionTest, AuthEverythingWithConnectClose) { |
| 21314 | // Note these hosts must match the CheckBasic*Auth() functions. |
| 21315 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 21316 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21317 | |
| 21318 | auto cert_request_info_proxy = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21319 | cert_request_info_proxy->host_and_port = HostPortPair("myproxy", 70); |
| 21320 | |
| 21321 | std::unique_ptr<FakeClientCertIdentity> identity_proxy = |
| 21322 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21323 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 21324 | ASSERT_TRUE(identity_proxy); |
| 21325 | |
| 21326 | auto cert_request_info_origin = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21327 | cert_request_info_origin->host_and_port = |
| 21328 | HostPortPair("www.example.org", 443); |
| 21329 | |
| 21330 | std::unique_ptr<FakeClientCertIdentity> identity_origin = |
| 21331 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21332 | GetTestCertsDirectory(), "client_2.pem", "client_2.pk8"); |
| 21333 | ASSERT_TRUE(identity_origin); |
| 21334 | |
| 21335 | HttpRequestInfo request; |
| 21336 | request.method = "GET"; |
| 21337 | request.url = GURL("https://www.example.org/"); |
| 21338 | request.traffic_annotation = |
| 21339 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21340 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21341 | // First, the client connects to the proxy, which requests a client |
| 21342 | // certificate. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21343 | SSLSocketDataProvider ssl_proxy1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21344 | ssl_proxy1.cert_request_info = cert_request_info_proxy.get(); |
| 21345 | ssl_proxy1.expected_send_client_cert = false; |
| 21346 | StaticSocketDataProvider data1; |
| 21347 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21348 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy1); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21349 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21350 | // The client responds with a certificate on a new connection. The handshake |
| 21351 | // succeeds. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21352 | SSLSocketDataProvider ssl_proxy2(ASYNC, OK); |
| 21353 | ssl_proxy2.expected_send_client_cert = true; |
| 21354 | ssl_proxy2.expected_client_cert = identity_proxy->certificate(); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21355 | // The client attempts an HTTP CONNECT, but the proxy requests basic auth. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21356 | std::vector<MockWrite> mock_writes2; |
| 21357 | std::vector<MockRead> mock_reads2; |
| 21358 | mock_writes2.emplace_back( |
| 21359 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21360 | "Host: www.example.org:443\r\n" |
| 21361 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 21362 | mock_reads2.emplace_back( |
| 21363 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 21364 | "Content-Length: 0\r\n" |
| 21365 | "Proxy-Connection: close\r\n" |
| 21366 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"); |
| 21367 | StaticSocketDataProvider data2(mock_reads2, mock_writes2); |
| 21368 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21369 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy2); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21370 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21371 | // The client retries with credentials on a new connection. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21372 | SSLSocketDataProvider ssl_proxy3(ASYNC, OK); |
| 21373 | ssl_proxy3.expected_send_client_cert = true; |
| 21374 | ssl_proxy3.expected_client_cert = identity_proxy->certificate(); |
| 21375 | std::vector<MockWrite> mock_writes3; |
| 21376 | std::vector<MockRead> mock_reads3; |
| 21377 | mock_writes3.emplace_back( |
| 21378 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21379 | "Host: www.example.org:443\r\n" |
| 21380 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21381 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21382 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21383 | mock_reads3.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21384 | // The origin requests client certificates. |
| 21385 | SSLSocketDataProvider ssl_origin3(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21386 | ssl_origin3.cert_request_info = cert_request_info_origin.get(); |
| 21387 | StaticSocketDataProvider data3(mock_reads3, mock_writes3); |
| 21388 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21389 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy3); |
| 21390 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin3); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21391 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21392 | // The client responds to the origin client certificate request on a new |
| 21393 | // connection. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21394 | SSLSocketDataProvider ssl_proxy4(ASYNC, OK); |
| 21395 | ssl_proxy4.expected_send_client_cert = true; |
| 21396 | ssl_proxy4.expected_client_cert = identity_proxy->certificate(); |
| 21397 | std::vector<MockWrite> mock_writes4; |
| 21398 | std::vector<MockRead> mock_reads4; |
| 21399 | mock_writes4.emplace_back( |
| 21400 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21401 | "Host: www.example.org:443\r\n" |
| 21402 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21403 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21404 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21405 | mock_reads4.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21406 | SSLSocketDataProvider ssl_origin4(ASYNC, OK); |
| 21407 | ssl_origin4.expected_send_client_cert = true; |
| 21408 | ssl_origin4.expected_client_cert = identity_origin->certificate(); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21409 | // The client sends the origin HTTP request, which results in another HTTP |
| 21410 | // auth request and closed connection. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21411 | mock_writes4.emplace_back( |
| 21412 | "GET / HTTP/1.1\r\n" |
| 21413 | "Host: www.example.org\r\n" |
| 21414 | "Connection: keep-alive\r\n\r\n"); |
| 21415 | mock_reads4.emplace_back( |
| 21416 | "HTTP/1.1 401 Unauthorized\r\n" |
| 21417 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 21418 | "Connection: close\r\n" |
| 21419 | "Content-Length: 0\r\n\r\n"); |
| 21420 | StaticSocketDataProvider data4(mock_reads4, mock_writes4); |
| 21421 | session_deps_.socket_factory->AddSocketDataProvider(&data4); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21422 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy4); |
| 21423 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin4); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21424 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21425 | // The client retries with credentials on a new connection, and the request |
| 21426 | // finally succeeds. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21427 | SSLSocketDataProvider ssl_proxy5(ASYNC, OK); |
| 21428 | ssl_proxy5.expected_send_client_cert = true; |
| 21429 | ssl_proxy5.expected_client_cert = identity_proxy->certificate(); |
| 21430 | std::vector<MockWrite> mock_writes5; |
| 21431 | std::vector<MockRead> mock_reads5; |
| 21432 | mock_writes5.emplace_back( |
| 21433 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21434 | "Host: www.example.org:443\r\n" |
| 21435 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21436 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21437 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21438 | mock_reads5.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21439 | SSLSocketDataProvider ssl_origin5(ASYNC, OK); |
| 21440 | ssl_origin5.expected_send_client_cert = true; |
| 21441 | ssl_origin5.expected_client_cert = identity_origin->certificate(); |
| 21442 | mock_writes5.emplace_back( |
| 21443 | "GET / HTTP/1.1\r\n" |
| 21444 | "Host: www.example.org\r\n" |
| 21445 | "Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21446 | // Authenticate as user:pass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21447 | "Authorization: Basic dXNlcjpwYXNz\r\n\r\n"); |
| 21448 | mock_reads5.emplace_back( |
| 21449 | "HTTP/1.1 200 OK\r\n" |
| 21450 | "Connection: close\r\n" |
| 21451 | "Content-Length: 0\r\n\r\n"); |
| 21452 | StaticSocketDataProvider data5(mock_reads5, mock_writes5); |
| 21453 | session_deps_.socket_factory->AddSocketDataProvider(&data5); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21454 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy5); |
| 21455 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin5); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21456 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 21457 | // The client makes a second request. This needs yet another connection, but |
| 21458 | // all credentials are cached. |
| 21459 | SSLSocketDataProvider ssl_proxy6(ASYNC, OK); |
| 21460 | ssl_proxy6.expected_send_client_cert = true; |
| 21461 | ssl_proxy6.expected_client_cert = identity_proxy->certificate(); |
| 21462 | std::vector<MockWrite> mock_writes6; |
| 21463 | std::vector<MockRead> mock_reads6; |
| 21464 | mock_writes6.emplace_back( |
| 21465 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21466 | "Host: www.example.org:443\r\n" |
| 21467 | "Proxy-Connection: keep-alive\r\n" |
| 21468 | // Authenticate as proxyuser:proxypass. |
| 21469 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21470 | mock_reads6.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21471 | SSLSocketDataProvider ssl_origin6(ASYNC, OK); |
| 21472 | ssl_origin6.expected_send_client_cert = true; |
| 21473 | ssl_origin6.expected_client_cert = identity_origin->certificate(); |
| 21474 | mock_writes6.emplace_back( |
| 21475 | "GET / HTTP/1.1\r\n" |
| 21476 | "Host: www.example.org\r\n" |
| 21477 | "Connection: keep-alive\r\n" |
| 21478 | // Authenticate as user:pass. |
| 21479 | "Authorization: Basic dXNlcjpwYXNz\r\n\r\n"); |
| 21480 | mock_reads6.emplace_back( |
| 21481 | "HTTP/1.1 200 OK\r\n" |
| 21482 | "Connection: close\r\n" |
| 21483 | "Content-Length: 0\r\n\r\n"); |
| 21484 | StaticSocketDataProvider data6(mock_reads6, mock_writes6); |
| 21485 | session_deps_.socket_factory->AddSocketDataProvider(&data6); |
| 21486 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy6); |
| 21487 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin6); |
| 21488 | |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21489 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21490 | |
| 21491 | // Start the request. |
| 21492 | TestCompletionCallback callback; |
| 21493 | auto trans = |
| 21494 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21495 | int rv = callback.GetResult( |
| 21496 | trans->Start(&request, callback.callback(), NetLogWithSource())); |
| 21497 | |
| 21498 | // Handle the proxy client certificate challenge. |
| 21499 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21500 | SSLCertRequestInfo* cert_request_info = |
| 21501 | trans->GetResponseInfo()->cert_request_info.get(); |
| 21502 | ASSERT_TRUE(cert_request_info); |
| 21503 | EXPECT_TRUE(cert_request_info->is_proxy); |
| 21504 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21505 | cert_request_info_proxy->host_and_port); |
| 21506 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21507 | identity_proxy->certificate(), identity_proxy->ssl_private_key(), |
| 21508 | callback.callback())); |
| 21509 | |
| 21510 | // Handle the proxy HTTP auth challenge. |
| 21511 | ASSERT_THAT(rv, IsOk()); |
| 21512 | EXPECT_EQ(407, trans->GetResponseInfo()->headers->response_code()); |
| 21513 | EXPECT_TRUE( |
| 21514 | CheckBasicSecureProxyAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21515 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21516 | AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass")), |
| 21517 | callback.callback())); |
| 21518 | |
| 21519 | // Handle the origin client certificate challenge. |
| 21520 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21521 | cert_request_info = trans->GetResponseInfo()->cert_request_info.get(); |
| 21522 | ASSERT_TRUE(cert_request_info); |
| 21523 | EXPECT_FALSE(cert_request_info->is_proxy); |
| 21524 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21525 | cert_request_info_origin->host_and_port); |
| 21526 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21527 | identity_origin->certificate(), identity_origin->ssl_private_key(), |
| 21528 | callback.callback())); |
| 21529 | |
| 21530 | // Handle the origin HTTP auth challenge. |
| 21531 | ASSERT_THAT(rv, IsOk()); |
| 21532 | EXPECT_EQ(401, trans->GetResponseInfo()->headers->response_code()); |
| 21533 | EXPECT_TRUE( |
| 21534 | CheckBasicSecureServerAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21535 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21536 | AuthCredentials(ASCIIToUTF16("user"), ASCIIToUTF16("pass")), |
| 21537 | callback.callback())); |
| 21538 | |
| 21539 | // The request completes. |
| 21540 | ASSERT_THAT(rv, IsOk()); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21541 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 21542 | |
| 21543 | // Make a second request. This time all credentials are cached. |
| 21544 | trans = |
| 21545 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21546 | ASSERT_THAT(callback.GetResult(trans->Start(&request, callback.callback(), |
| 21547 | NetLogWithSource())), |
| 21548 | IsOk()); |
| 21549 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21550 | } |
| 21551 | |
| 21552 | // Test the proxy requesting HTTP auth and the server requesting TLS client |
| 21553 | // certificates. This is a regression test for https://crbug.com/946406. |
| 21554 | TEST_F(HttpNetworkTransactionTest, ProxyHTTPAndServerTLSAuth) { |
| 21555 | // Note these hosts must match the CheckBasic*Auth() functions. |
| 21556 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 21557 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21558 | |
| 21559 | auto cert_request_info_origin = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21560 | cert_request_info_origin->host_and_port = |
| 21561 | HostPortPair("www.example.org", 443); |
| 21562 | |
| 21563 | std::unique_ptr<FakeClientCertIdentity> identity_origin = |
| 21564 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21565 | GetTestCertsDirectory(), "client_2.pem", "client_2.pk8"); |
| 21566 | ASSERT_TRUE(identity_origin); |
| 21567 | |
| 21568 | HttpRequestInfo request; |
| 21569 | request.method = "GET"; |
| 21570 | request.url = GURL("https://www.example.org/"); |
| 21571 | request.traffic_annotation = |
| 21572 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21573 | |
| 21574 | // The client connects to the proxy. The handshake succeeds. |
| 21575 | SSLSocketDataProvider ssl_proxy1(ASYNC, OK); |
| 21576 | // The client attempts an HTTP CONNECT, but the proxy requests basic auth. |
| 21577 | std::vector<MockWrite> mock_writes1; |
| 21578 | std::vector<MockRead> mock_reads1; |
| 21579 | mock_writes1.emplace_back( |
| 21580 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21581 | "Host: www.example.org:443\r\n" |
| 21582 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 21583 | mock_reads1.emplace_back( |
| 21584 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 21585 | "Content-Length: 0\r\n" |
| 21586 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"); |
| 21587 | // The client retries with credentials, and the request finally succeeds. |
| 21588 | mock_writes1.emplace_back( |
| 21589 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21590 | "Host: www.example.org:443\r\n" |
| 21591 | "Proxy-Connection: keep-alive\r\n" |
| 21592 | // Authenticate as proxyuser:proxypass. |
| 21593 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21594 | mock_reads1.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21595 | // The origin requests client certificates. |
| 21596 | SSLSocketDataProvider ssl_origin1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21597 | ssl_origin1.cert_request_info = cert_request_info_origin.get(); |
| 21598 | StaticSocketDataProvider data1(mock_reads1, mock_writes1); |
| 21599 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 21600 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy1); |
| 21601 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin1); |
| 21602 | |
| 21603 | // The client responds to the origin client certificate request on a new |
| 21604 | // connection. |
| 21605 | SSLSocketDataProvider ssl_proxy2(ASYNC, OK); |
| 21606 | std::vector<MockWrite> mock_writes2; |
| 21607 | std::vector<MockRead> mock_reads2; |
| 21608 | mock_writes2.emplace_back( |
| 21609 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21610 | "Host: www.example.org:443\r\n" |
| 21611 | "Proxy-Connection: keep-alive\r\n" |
| 21612 | // Authenticate as proxyuser:proxypass. |
| 21613 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21614 | mock_reads2.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21615 | SSLSocketDataProvider ssl_origin2(ASYNC, OK); |
| 21616 | ssl_origin2.expected_send_client_cert = true; |
| 21617 | ssl_origin2.expected_client_cert = identity_origin->certificate(); |
| 21618 | // The client sends the origin HTTP request, which succeeds. |
| 21619 | mock_writes2.emplace_back( |
| 21620 | "GET / HTTP/1.1\r\n" |
| 21621 | "Host: www.example.org\r\n" |
| 21622 | "Connection: keep-alive\r\n\r\n"); |
| 21623 | mock_reads2.emplace_back( |
| 21624 | "HTTP/1.1 200 OK\r\n" |
| 21625 | "Content-Length: 0\r\n\r\n"); |
| 21626 | StaticSocketDataProvider data2(mock_reads2, mock_writes2); |
| 21627 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 21628 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy2); |
| 21629 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin2); |
| 21630 | |
| 21631 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21632 | |
| 21633 | // Start the request. |
| 21634 | TestCompletionCallback callback; |
| 21635 | auto trans = |
| 21636 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21637 | int rv = callback.GetResult( |
| 21638 | trans->Start(&request, callback.callback(), NetLogWithSource())); |
| 21639 | |
| 21640 | // Handle the proxy HTTP auth challenge. |
| 21641 | ASSERT_THAT(rv, IsOk()); |
| 21642 | EXPECT_EQ(407, trans->GetResponseInfo()->headers->response_code()); |
| 21643 | EXPECT_TRUE( |
| 21644 | CheckBasicSecureProxyAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21645 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21646 | AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass")), |
| 21647 | callback.callback())); |
| 21648 | |
| 21649 | // Handle the origin client certificate challenge. |
| 21650 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21651 | SSLCertRequestInfo* cert_request_info = |
| 21652 | trans->GetResponseInfo()->cert_request_info.get(); |
| 21653 | ASSERT_TRUE(cert_request_info); |
| 21654 | EXPECT_FALSE(cert_request_info->is_proxy); |
| 21655 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21656 | cert_request_info_origin->host_and_port); |
| 21657 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21658 | identity_origin->certificate(), identity_origin->ssl_private_key(), |
| 21659 | callback.callback())); |
| 21660 | |
| 21661 | // The request completes. |
| 21662 | ASSERT_THAT(rv, IsOk()); |
| 21663 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21664 | } |
| 21665 | |
David Benjamin | 6e673a8 | 2019-04-30 22:52:58 | [diff] [blame] | 21666 | // Test that socket reuse works with client certificates. |
| 21667 | TEST_F(HttpNetworkTransactionTest, ClientCertSocketReuse) { |
| 21668 | auto cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21669 | cert_request_info->host_and_port = HostPortPair("www.example.org", 443); |
| 21670 | |
| 21671 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 21672 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21673 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 21674 | ASSERT_TRUE(identity); |
| 21675 | |
| 21676 | HttpRequestInfo request1; |
| 21677 | request1.method = "GET"; |
| 21678 | request1.url = GURL("https://www.example.org/a"); |
| 21679 | request1.traffic_annotation = |
| 21680 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21681 | |
| 21682 | HttpRequestInfo request2; |
| 21683 | request2.method = "GET"; |
| 21684 | request2.url = GURL("https://www.example.org/b"); |
| 21685 | request2.traffic_annotation = |
| 21686 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21687 | |
| 21688 | // The first connection results in a client certificate request. |
| 21689 | StaticSocketDataProvider data1; |
| 21690 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 21691 | SSLSocketDataProvider ssl1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21692 | ssl1.cert_request_info = cert_request_info.get(); |
| 21693 | ssl1.expected_send_client_cert = false; |
| 21694 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 21695 | |
| 21696 | // The second connection succeeds and is usable for both requests. |
| 21697 | MockWrite mock_writes[] = { |
| 21698 | MockWrite("GET /a HTTP/1.1\r\n" |
| 21699 | "Host: www.example.org\r\n" |
| 21700 | "Connection: keep-alive\r\n\r\n"), |
| 21701 | MockWrite("GET /b HTTP/1.1\r\n" |
| 21702 | "Host: www.example.org\r\n" |
| 21703 | "Connection: keep-alive\r\n\r\n"), |
| 21704 | }; |
| 21705 | MockRead mock_reads[] = { |
| 21706 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21707 | "Content-Length: 0\r\n\r\n"), |
| 21708 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21709 | "Content-Length: 0\r\n\r\n"), |
| 21710 | }; |
| 21711 | StaticSocketDataProvider data2(mock_reads, mock_writes); |
| 21712 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 21713 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 21714 | ssl2.expected_send_client_cert = true; |
| 21715 | ssl2.expected_client_cert = identity->certificate(); |
| 21716 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 21717 | |
| 21718 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21719 | |
| 21720 | // Start the first request. It succeeds after providing client certificates. |
| 21721 | TestCompletionCallback callback; |
| 21722 | auto trans = |
| 21723 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21724 | ASSERT_THAT(callback.GetResult(trans->Start(&request1, callback.callback(), |
| 21725 | NetLogWithSource())), |
| 21726 | IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21727 | |
| 21728 | SSLCertRequestInfo* info = trans->GetResponseInfo()->cert_request_info.get(); |
| 21729 | ASSERT_TRUE(info); |
| 21730 | EXPECT_FALSE(info->is_proxy); |
| 21731 | EXPECT_EQ(info->host_and_port, cert_request_info->host_and_port); |
| 21732 | |
| 21733 | ASSERT_THAT(callback.GetResult(trans->RestartWithCertificate( |
| 21734 | identity->certificate(), identity->ssl_private_key(), |
| 21735 | callback.callback())), |
| 21736 | IsOk()); |
| 21737 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
| 21738 | |
| 21739 | // Make the second request. It completes without requesting client |
| 21740 | // certificates. |
| 21741 | trans = |
| 21742 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21743 | ASSERT_THAT(callback.GetResult(trans->Start(&request2, callback.callback(), |
| 21744 | NetLogWithSource())), |
| 21745 | IsOk()); |
| 21746 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
| 21747 | } |
| 21748 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 21749 | // Test for kPartitionConnectionsByNetworkIsolationKey. Runs 3 requests in |
| 21750 | // sequence with two different NetworkIsolationKeys, the first and last have the |
| 21751 | // same key, the second a different one. Checks that the requests are |
| 21752 | // partitioned across sockets as expected. |
| 21753 | TEST_F(HttpNetworkTransactionTest, NetworkIsolation) { |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 21754 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 21755 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 21756 | NetworkIsolationKey network_isolation_key1(kOrigin1, kOrigin1); |
| 21757 | NetworkIsolationKey network_isolation_key2(kOrigin2, kOrigin2); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 21758 | |
| 21759 | for (bool partition_connections : {false, true}) { |
| 21760 | SCOPED_TRACE(partition_connections); |
| 21761 | |
| 21762 | base::test::ScopedFeatureList feature_list; |
| 21763 | if (partition_connections) { |
| 21764 | feature_list.InitAndEnableFeature( |
| 21765 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 21766 | } else { |
| 21767 | feature_list.InitAndDisableFeature( |
| 21768 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 21769 | } |
| 21770 | |
| 21771 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21772 | |
| 21773 | // Reads and writes for the unpartitioned case, where only one socket is |
| 21774 | // used. |
| 21775 | |
| 21776 | const MockWrite kUnpartitionedWrites[] = { |
| 21777 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 21778 | "Host: foo.test\r\n" |
| 21779 | "Connection: keep-alive\r\n\r\n"), |
| 21780 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 21781 | "Host: foo.test\r\n" |
| 21782 | "Connection: keep-alive\r\n\r\n"), |
| 21783 | MockWrite("GET /3 HTTP/1.1\r\n" |
| 21784 | "Host: foo.test\r\n" |
| 21785 | "Connection: keep-alive\r\n\r\n"), |
| 21786 | }; |
| 21787 | |
| 21788 | const MockRead kUnpartitionedReads[] = { |
| 21789 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21790 | "Connection: keep-alive\r\n" |
| 21791 | "Content-Length: 1\r\n\r\n" |
| 21792 | "1"), |
| 21793 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21794 | "Connection: keep-alive\r\n" |
| 21795 | "Content-Length: 1\r\n\r\n" |
| 21796 | "2"), |
| 21797 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21798 | "Connection: keep-alive\r\n" |
| 21799 | "Content-Length: 1\r\n\r\n" |
| 21800 | "3"), |
| 21801 | }; |
| 21802 | |
| 21803 | StaticSocketDataProvider unpartitioned_data(kUnpartitionedReads, |
| 21804 | kUnpartitionedWrites); |
| 21805 | |
| 21806 | // Reads and writes for the partitioned case, where two sockets are used. |
| 21807 | |
| 21808 | const MockWrite kPartitionedWrites1[] = { |
| 21809 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 21810 | "Host: foo.test\r\n" |
| 21811 | "Connection: keep-alive\r\n\r\n"), |
| 21812 | MockWrite("GET /3 HTTP/1.1\r\n" |
| 21813 | "Host: foo.test\r\n" |
| 21814 | "Connection: keep-alive\r\n\r\n"), |
| 21815 | }; |
| 21816 | |
| 21817 | const MockRead kPartitionedReads1[] = { |
| 21818 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21819 | "Connection: keep-alive\r\n" |
| 21820 | "Content-Length: 1\r\n\r\n" |
| 21821 | "1"), |
| 21822 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21823 | "Connection: keep-alive\r\n" |
| 21824 | "Content-Length: 1\r\n\r\n" |
| 21825 | "3"), |
| 21826 | }; |
| 21827 | |
| 21828 | const MockWrite kPartitionedWrites2[] = { |
| 21829 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 21830 | "Host: foo.test\r\n" |
| 21831 | "Connection: keep-alive\r\n\r\n"), |
| 21832 | }; |
| 21833 | |
| 21834 | const MockRead kPartitionedReads2[] = { |
| 21835 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21836 | "Connection: keep-alive\r\n" |
| 21837 | "Content-Length: 1\r\n\r\n" |
| 21838 | "2"), |
| 21839 | }; |
| 21840 | |
| 21841 | StaticSocketDataProvider partitioned_data1(kPartitionedReads1, |
| 21842 | kPartitionedWrites1); |
| 21843 | StaticSocketDataProvider partitioned_data2(kPartitionedReads2, |
| 21844 | kPartitionedWrites2); |
| 21845 | |
| 21846 | if (partition_connections) { |
| 21847 | session_deps_.socket_factory->AddSocketDataProvider(&partitioned_data1); |
| 21848 | session_deps_.socket_factory->AddSocketDataProvider(&partitioned_data2); |
| 21849 | } else { |
| 21850 | session_deps_.socket_factory->AddSocketDataProvider(&unpartitioned_data); |
| 21851 | } |
| 21852 | |
| 21853 | TestCompletionCallback callback; |
| 21854 | HttpRequestInfo request1; |
| 21855 | request1.method = "GET"; |
| 21856 | request1.url = GURL("http://foo.test/1"); |
| 21857 | request1.traffic_annotation = |
| 21858 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21859 | request1.network_isolation_key = network_isolation_key1; |
| 21860 | auto trans1 = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 21861 | session.get()); |
| 21862 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 21863 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 21864 | std::string response_data1; |
| 21865 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 21866 | EXPECT_EQ("1", response_data1); |
| 21867 | trans1.reset(); |
| 21868 | |
| 21869 | HttpRequestInfo request2; |
| 21870 | request2.method = "GET"; |
| 21871 | request2.url = GURL("http://foo.test/2"); |
| 21872 | request2.traffic_annotation = |
| 21873 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21874 | request2.network_isolation_key = network_isolation_key2; |
| 21875 | auto trans2 = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 21876 | session.get()); |
| 21877 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 21878 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 21879 | std::string response_data2; |
| 21880 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 21881 | EXPECT_EQ("2", response_data2); |
| 21882 | trans2.reset(); |
| 21883 | |
| 21884 | HttpRequestInfo request3; |
| 21885 | request3.method = "GET"; |
| 21886 | request3.url = GURL("http://foo.test/3"); |
| 21887 | request3.traffic_annotation = |
| 21888 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21889 | request3.network_isolation_key = network_isolation_key1; |
| 21890 | auto trans3 = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 21891 | session.get()); |
| 21892 | rv = trans3->Start(&request3, callback.callback(), NetLogWithSource()); |
| 21893 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 21894 | std::string response_data3; |
| 21895 | EXPECT_THAT(ReadTransaction(trans3.get(), &response_data3), IsOk()); |
| 21896 | EXPECT_EQ("3", response_data3); |
| 21897 | trans3.reset(); |
| 21898 | } |
| 21899 | } |
| 21900 | |
Matt Menke | ae58eeb | 2019-05-24 21:09:50 | [diff] [blame] | 21901 | TEST_F(HttpNetworkTransactionTest, NetworkIsolationH2) { |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 21902 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 21903 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 21904 | NetworkIsolationKey network_isolation_key1(kOrigin1, kOrigin1); |
| 21905 | NetworkIsolationKey network_isolation_key2(kOrigin2, kOrigin2); |
Matt Menke | ae58eeb | 2019-05-24 21:09:50 | [diff] [blame] | 21906 | |
| 21907 | // Whether to use an H2 proxy. When false, uses HTTPS H2 requests without a |
| 21908 | // proxy, when true, uses HTTP requests over an H2 proxy. It's unnecessary to |
| 21909 | // test tunneled HTTPS over an H2 proxy, since that path sets up H2 sessions |
| 21910 | // the same way as the HTTP over H2 proxy case. |
| 21911 | for (bool use_proxy : {false, true}) { |
| 21912 | SCOPED_TRACE(use_proxy); |
| 21913 | if (use_proxy) { |
| 21914 | session_deps_.proxy_resolution_service = |
| 21915 | ProxyResolutionService::CreateFixedFromPacResult( |
| 21916 | "HTTPS proxy:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21917 | } else { |
| 21918 | session_deps_.proxy_resolution_service = |
| 21919 | ProxyResolutionService::CreateDirect(); |
| 21920 | } |
| 21921 | const char* url1 = nullptr; |
| 21922 | const char* url2 = nullptr; |
| 21923 | const char* url3 = nullptr; |
| 21924 | if (use_proxy) { |
| 21925 | url1 = "http://foo.test/1"; |
| 21926 | url2 = "http://foo.test/2"; |
| 21927 | url3 = "http://foo.test/3"; |
| 21928 | } else { |
| 21929 | url1 = "https://foo.test/1"; |
| 21930 | url2 = "https://foo.test/2"; |
| 21931 | url3 = "https://foo.test/3"; |
| 21932 | } |
| 21933 | |
| 21934 | for (bool partition_connections : {false, true}) { |
| 21935 | SCOPED_TRACE(partition_connections); |
| 21936 | |
| 21937 | base::test::ScopedFeatureList feature_list; |
| 21938 | if (partition_connections) { |
| 21939 | feature_list.InitAndEnableFeature( |
| 21940 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 21941 | } else { |
| 21942 | feature_list.InitAndDisableFeature( |
| 21943 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 21944 | } |
| 21945 | |
| 21946 | std::unique_ptr<HttpNetworkSession> session( |
| 21947 | CreateSession(&session_deps_)); |
| 21948 | |
| 21949 | // Reads and writes for the unpartitioned case, where only one socket is |
| 21950 | // used. |
| 21951 | |
| 21952 | SpdyTestUtil spdy_util; |
| 21953 | spdy::SpdySerializedFrame unpartitioned_req1( |
| 21954 | spdy_util.ConstructSpdyGet(url1, 1, LOWEST)); |
| 21955 | spdy::SpdySerializedFrame unpartitioned_response1( |
| 21956 | spdy_util.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 21957 | spdy::SpdySerializedFrame unpartitioned_body1( |
| 21958 | spdy_util.ConstructSpdyDataFrame(1, "1", true)); |
| 21959 | spdy_util.UpdateWithStreamDestruction(1); |
| 21960 | |
| 21961 | spdy::SpdySerializedFrame unpartitioned_req2( |
| 21962 | spdy_util.ConstructSpdyGet(url2, 3, LOWEST)); |
| 21963 | spdy::SpdySerializedFrame unpartitioned_response2( |
| 21964 | spdy_util.ConstructSpdyGetReply(nullptr, 0, 3)); |
| 21965 | spdy::SpdySerializedFrame unpartitioned_body2( |
| 21966 | spdy_util.ConstructSpdyDataFrame(3, "2", true)); |
| 21967 | spdy_util.UpdateWithStreamDestruction(3); |
| 21968 | |
| 21969 | spdy::SpdySerializedFrame unpartitioned_req3( |
| 21970 | spdy_util.ConstructSpdyGet(url3, 5, LOWEST)); |
| 21971 | spdy::SpdySerializedFrame unpartitioned_response3( |
| 21972 | spdy_util.ConstructSpdyGetReply(nullptr, 0, 5)); |
| 21973 | spdy::SpdySerializedFrame unpartitioned_body3( |
| 21974 | spdy_util.ConstructSpdyDataFrame(5, "3", true)); |
| 21975 | |
| 21976 | const MockWrite kUnpartitionedWrites[] = { |
| 21977 | CreateMockWrite(unpartitioned_req1, 0), |
| 21978 | CreateMockWrite(unpartitioned_req2, 3), |
| 21979 | CreateMockWrite(unpartitioned_req3, 6), |
| 21980 | }; |
| 21981 | |
| 21982 | const MockRead kUnpartitionedReads[] = { |
| 21983 | CreateMockRead(unpartitioned_response1, 1), |
| 21984 | CreateMockRead(unpartitioned_body1, 2), |
| 21985 | CreateMockRead(unpartitioned_response2, 4), |
| 21986 | CreateMockRead(unpartitioned_body2, 5), |
| 21987 | CreateMockRead(unpartitioned_response3, 7), |
| 21988 | CreateMockRead(unpartitioned_body3, 8), |
| 21989 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 9), |
| 21990 | }; |
| 21991 | |
| 21992 | SequencedSocketData unpartitioned_data(kUnpartitionedReads, |
| 21993 | kUnpartitionedWrites); |
| 21994 | |
| 21995 | // Reads and writes for the partitioned case, where two sockets are used. |
| 21996 | |
| 21997 | SpdyTestUtil spdy_util2; |
| 21998 | spdy::SpdySerializedFrame partitioned_req1( |
| 21999 | spdy_util2.ConstructSpdyGet(url1, 1, LOWEST)); |
| 22000 | spdy::SpdySerializedFrame partitioned_response1( |
| 22001 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 22002 | spdy::SpdySerializedFrame partitioned_body1( |
| 22003 | spdy_util2.ConstructSpdyDataFrame(1, "1", true)); |
| 22004 | spdy_util2.UpdateWithStreamDestruction(1); |
| 22005 | |
| 22006 | spdy::SpdySerializedFrame partitioned_req3( |
| 22007 | spdy_util2.ConstructSpdyGet(url3, 3, LOWEST)); |
| 22008 | spdy::SpdySerializedFrame partitioned_response3( |
| 22009 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 3)); |
| 22010 | spdy::SpdySerializedFrame partitioned_body3( |
| 22011 | spdy_util2.ConstructSpdyDataFrame(3, "3", true)); |
| 22012 | |
| 22013 | const MockWrite kPartitionedWrites1[] = { |
| 22014 | CreateMockWrite(partitioned_req1, 0), |
| 22015 | CreateMockWrite(partitioned_req3, 3), |
| 22016 | }; |
| 22017 | |
| 22018 | const MockRead kPartitionedReads1[] = { |
| 22019 | CreateMockRead(partitioned_response1, 1), |
| 22020 | CreateMockRead(partitioned_body1, 2), |
| 22021 | CreateMockRead(partitioned_response3, 4), |
| 22022 | CreateMockRead(partitioned_body3, 5), |
| 22023 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 6), |
| 22024 | }; |
| 22025 | |
| 22026 | SpdyTestUtil spdy_util3; |
| 22027 | spdy::SpdySerializedFrame partitioned_req2( |
| 22028 | spdy_util3.ConstructSpdyGet(url2, 1, LOWEST)); |
| 22029 | spdy::SpdySerializedFrame partitioned_response2( |
| 22030 | spdy_util3.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 22031 | spdy::SpdySerializedFrame partitioned_body2( |
| 22032 | spdy_util3.ConstructSpdyDataFrame(1, "2", true)); |
| 22033 | |
| 22034 | const MockWrite kPartitionedWrites2[] = { |
| 22035 | CreateMockWrite(partitioned_req2, 0), |
| 22036 | }; |
| 22037 | |
| 22038 | const MockRead kPartitionedReads2[] = { |
| 22039 | CreateMockRead(partitioned_response2, 1), |
| 22040 | CreateMockRead(partitioned_body2, 2), |
| 22041 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 3), |
| 22042 | }; |
| 22043 | |
| 22044 | SequencedSocketData partitioned_data1(kPartitionedReads1, |
| 22045 | kPartitionedWrites1); |
| 22046 | SequencedSocketData partitioned_data2(kPartitionedReads2, |
| 22047 | kPartitionedWrites2); |
| 22048 | |
| 22049 | // No need to segment SSLDataProviders by whether or not partitioning is |
| 22050 | // enabled. |
| 22051 | SSLSocketDataProvider ssl_data1(ASYNC, OK); |
| 22052 | ssl_data1.next_proto = kProtoHTTP2; |
| 22053 | SSLSocketDataProvider ssl_data2(ASYNC, OK); |
| 22054 | ssl_data2.next_proto = kProtoHTTP2; |
| 22055 | |
| 22056 | if (partition_connections) { |
| 22057 | session_deps_.socket_factory->AddSocketDataProvider(&partitioned_data1); |
| 22058 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 22059 | session_deps_.socket_factory->AddSocketDataProvider(&partitioned_data2); |
| 22060 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
| 22061 | } else { |
| 22062 | session_deps_.socket_factory->AddSocketDataProvider( |
| 22063 | &unpartitioned_data); |
| 22064 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 22065 | } |
| 22066 | |
| 22067 | TestCompletionCallback callback; |
| 22068 | HttpRequestInfo request1; |
| 22069 | request1.method = "GET"; |
| 22070 | request1.url = GURL(url1); |
| 22071 | request1.traffic_annotation = |
| 22072 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22073 | request1.network_isolation_key = network_isolation_key1; |
| 22074 | auto trans1 = |
| 22075 | std::make_unique<HttpNetworkTransaction>(LOWEST, session.get()); |
| 22076 | int rv = |
| 22077 | trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 22078 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22079 | std::string response_data1; |
| 22080 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 22081 | EXPECT_EQ("1", response_data1); |
| 22082 | trans1.reset(); |
| 22083 | |
| 22084 | HttpRequestInfo request2; |
| 22085 | request2.method = "GET"; |
| 22086 | request2.url = GURL(url2); |
| 22087 | request2.traffic_annotation = |
| 22088 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22089 | request2.network_isolation_key = network_isolation_key2; |
| 22090 | auto trans2 = |
| 22091 | std::make_unique<HttpNetworkTransaction>(LOWEST, session.get()); |
| 22092 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 22093 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22094 | std::string response_data2; |
| 22095 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 22096 | EXPECT_EQ("2", response_data2); |
| 22097 | trans2.reset(); |
| 22098 | |
| 22099 | HttpRequestInfo request3; |
| 22100 | request3.method = "GET"; |
| 22101 | request3.url = GURL(url3); |
| 22102 | request3.traffic_annotation = |
| 22103 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22104 | request3.network_isolation_key = network_isolation_key1; |
| 22105 | auto trans3 = |
| 22106 | std::make_unique<HttpNetworkTransaction>(LOWEST, session.get()); |
| 22107 | rv = trans3->Start(&request3, callback.callback(), NetLogWithSource()); |
| 22108 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22109 | std::string response_data3; |
| 22110 | EXPECT_THAT(ReadTransaction(trans3.get(), &response_data3), IsOk()); |
| 22111 | EXPECT_EQ("3", response_data3); |
| 22112 | trans3.reset(); |
| 22113 | } |
| 22114 | } |
| 22115 | } |
| 22116 | |
Matt Menke | 7281f87 | 2019-06-25 19:29:25 | [diff] [blame] | 22117 | // Preconnect two sockets with different NetworkIsolationKeys when |
| 22118 | // features::kPartitionConnectionsByNetworkIsolationKey is enabled. Then issue a |
| 22119 | // request and make sure the correct socket is used. Loops three times, |
| 22120 | // expecting to use the first preconnect, second preconnect, and neither. |
| 22121 | TEST_F(HttpNetworkTransactionTest, NetworkIsolationPreconnect) { |
| 22122 | base::test::ScopedFeatureList feature_list; |
| 22123 | feature_list.InitAndEnableFeature( |
| 22124 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 22125 | |
| 22126 | enum class TestCase { |
| 22127 | kUseFirstPreconnect, |
| 22128 | kUseSecondPreconnect, |
| 22129 | kDontUsePreconnect, |
| 22130 | }; |
| 22131 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 22132 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 22133 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 22134 | const auto kOrigin3 = url::Origin::Create(GURL("http://origin3/")); |
| 22135 | NetworkIsolationKey preconnect1_isolation_key(kOrigin1, kOrigin1); |
| 22136 | NetworkIsolationKey preconnect2_isolation_key(kOrigin2, kOrigin2); |
| 22137 | NetworkIsolationKey not_preconnected_isolation_key(kOrigin3, kOrigin3); |
Matt Menke | 7281f87 | 2019-06-25 19:29:25 | [diff] [blame] | 22138 | |
| 22139 | // Test that only preconnects with |
| 22140 | for (TestCase test_case : |
| 22141 | {TestCase::kUseFirstPreconnect, TestCase::kUseSecondPreconnect, |
| 22142 | TestCase::kDontUsePreconnect}) { |
| 22143 | SpdySessionDependencies session_deps; |
| 22144 | // Make DNS lookups completely synchronously, so preconnects complete |
| 22145 | // immediately. |
| 22146 | session_deps.host_resolver->set_synchronous_mode(true); |
| 22147 | |
| 22148 | const MockWrite kMockWrites[] = { |
| 22149 | MockWrite(ASYNC, 0, |
| 22150 | "GET / HTTP/1.1\r\n" |
| 22151 | "Host: www.foo.com\r\n" |
| 22152 | "Connection: keep-alive\r\n\r\n"), |
| 22153 | }; |
| 22154 | |
| 22155 | const MockRead kMockReads[] = { |
| 22156 | MockRead(ASYNC, 1, |
| 22157 | "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n" |
| 22158 | "hello"), |
| 22159 | }; |
| 22160 | |
| 22161 | // Used for the socket that will actually be used, which may or may not be |
| 22162 | // one of the preconnects |
| 22163 | SequencedSocketData used_socket_data(MockConnect(SYNCHRONOUS, OK), |
| 22164 | kMockReads, kMockWrites); |
| 22165 | |
| 22166 | // Used for the preconnects that won't actually be used. |
| 22167 | SequencedSocketData preconnect1_data(MockConnect(SYNCHRONOUS, OK), |
| 22168 | base::span<const MockRead>(), |
| 22169 | base::span<const MockWrite>()); |
| 22170 | SequencedSocketData preconnect2_data(MockConnect(SYNCHRONOUS, OK), |
| 22171 | base::span<const MockRead>(), |
| 22172 | base::span<const MockWrite>()); |
| 22173 | |
| 22174 | NetworkIsolationKey network_isolation_key_for_request; |
| 22175 | |
| 22176 | switch (test_case) { |
| 22177 | case TestCase::kUseFirstPreconnect: |
| 22178 | session_deps.socket_factory->AddSocketDataProvider(&used_socket_data); |
| 22179 | session_deps.socket_factory->AddSocketDataProvider(&preconnect2_data); |
| 22180 | network_isolation_key_for_request = preconnect1_isolation_key; |
| 22181 | break; |
| 22182 | case TestCase::kUseSecondPreconnect: |
| 22183 | session_deps.socket_factory->AddSocketDataProvider(&preconnect1_data); |
| 22184 | session_deps.socket_factory->AddSocketDataProvider(&used_socket_data); |
| 22185 | network_isolation_key_for_request = preconnect2_isolation_key; |
| 22186 | break; |
| 22187 | case TestCase::kDontUsePreconnect: |
| 22188 | session_deps.socket_factory->AddSocketDataProvider(&preconnect1_data); |
| 22189 | session_deps.socket_factory->AddSocketDataProvider(&preconnect2_data); |
| 22190 | session_deps.socket_factory->AddSocketDataProvider(&used_socket_data); |
| 22191 | network_isolation_key_for_request = not_preconnected_isolation_key; |
| 22192 | break; |
| 22193 | } |
| 22194 | |
| 22195 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 22196 | |
| 22197 | // Preconnect sockets. |
| 22198 | HttpRequestInfo request; |
| 22199 | request.method = "GET"; |
| 22200 | request.url = GURL("http://www.foo.com/"); |
| 22201 | request.traffic_annotation = |
| 22202 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22203 | |
| 22204 | request.network_isolation_key = preconnect1_isolation_key; |
| 22205 | session->http_stream_factory()->PreconnectStreams(1, request); |
| 22206 | |
| 22207 | request.network_isolation_key = preconnect2_isolation_key; |
| 22208 | session->http_stream_factory()->PreconnectStreams(1, request); |
| 22209 | |
| 22210 | request.network_isolation_key = network_isolation_key_for_request; |
| 22211 | |
| 22212 | EXPECT_EQ(2, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 22213 | |
| 22214 | // Make the request. |
| 22215 | TestCompletionCallback callback; |
| 22216 | |
| 22217 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 22218 | |
| 22219 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 22220 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 22221 | |
| 22222 | rv = callback.WaitForResult(); |
| 22223 | EXPECT_THAT(rv, IsOk()); |
| 22224 | |
| 22225 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 22226 | ASSERT_TRUE(response); |
| 22227 | ASSERT_TRUE(response->headers); |
| 22228 | EXPECT_EQ(200, response->headers->response_code()); |
| 22229 | |
| 22230 | std::string response_data; |
| 22231 | rv = ReadTransaction(&trans, &response_data); |
| 22232 | EXPECT_THAT(rv, IsOk()); |
| 22233 | EXPECT_EQ("hello", response_data); |
| 22234 | |
| 22235 | if (test_case != TestCase::kDontUsePreconnect) { |
| 22236 | EXPECT_EQ(2, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 22237 | } else { |
| 22238 | EXPECT_EQ(3, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 22239 | } |
| 22240 | } |
| 22241 | } |
| 22242 | |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 22243 | // Test that the NetworkIsolationKey is passed down to SSLConfig so the session |
| 22244 | // cache is isolated. |
| 22245 | TEST_F(HttpNetworkTransactionTest, NetworkIsolationSSL) { |
| 22246 | base::test::ScopedFeatureList feature_list; |
| 22247 | feature_list.InitWithFeatures( |
| 22248 | {features::kPartitionConnectionsByNetworkIsolationKey, |
| 22249 | features::kPartitionSSLSessionsByNetworkIsolationKey}, |
| 22250 | {}); |
| 22251 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 22252 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 22253 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 22254 | const NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 22255 | const NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 22256 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 22257 | |
| 22258 | // The server always sends Connection: close, so each request goes over a |
| 22259 | // distinct socket. |
| 22260 | |
| 22261 | const MockWrite kWrites1[] = { |
| 22262 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 22263 | "Host: foo.test\r\n" |
| 22264 | "Connection: keep-alive\r\n\r\n")}; |
| 22265 | |
| 22266 | const MockRead kReads1[] = { |
| 22267 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22268 | "Connection: close\r\n" |
| 22269 | "Content-Length: 1\r\n\r\n" |
| 22270 | "1")}; |
| 22271 | |
| 22272 | const MockWrite kWrites2[] = { |
| 22273 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 22274 | "Host: foo.test\r\n" |
| 22275 | "Connection: keep-alive\r\n\r\n")}; |
| 22276 | |
| 22277 | const MockRead kReads2[] = { |
| 22278 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22279 | "Connection: close\r\n" |
| 22280 | "Content-Length: 1\r\n\r\n" |
| 22281 | "2")}; |
| 22282 | |
| 22283 | const MockWrite kWrites3[] = { |
| 22284 | MockWrite("GET /3 HTTP/1.1\r\n" |
| 22285 | "Host: foo.test\r\n" |
| 22286 | "Connection: keep-alive\r\n\r\n")}; |
| 22287 | |
| 22288 | const MockRead kReads3[] = { |
| 22289 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22290 | "Connection: close\r\n" |
| 22291 | "Content-Length: 1\r\n\r\n" |
| 22292 | "3")}; |
| 22293 | |
| 22294 | StaticSocketDataProvider data1(kReads1, kWrites1); |
| 22295 | StaticSocketDataProvider data2(kReads2, kWrites2); |
| 22296 | StaticSocketDataProvider data3(kReads3, kWrites3); |
| 22297 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 22298 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 22299 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 22300 | |
| 22301 | SSLSocketDataProvider ssl_data1(ASYNC, OK); |
| 22302 | ssl_data1.expected_host_and_port = HostPortPair("foo.test", 443); |
| 22303 | ssl_data1.expected_network_isolation_key = kNetworkIsolationKey1; |
| 22304 | SSLSocketDataProvider ssl_data2(ASYNC, OK); |
| 22305 | ssl_data2.expected_host_and_port = HostPortPair("foo.test", 443); |
| 22306 | ssl_data2.expected_network_isolation_key = kNetworkIsolationKey2; |
| 22307 | SSLSocketDataProvider ssl_data3(ASYNC, OK); |
| 22308 | ssl_data3.expected_host_and_port = HostPortPair("foo.test", 443); |
| 22309 | ssl_data3.expected_network_isolation_key = kNetworkIsolationKey1; |
| 22310 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 22311 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
| 22312 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data3); |
| 22313 | |
| 22314 | TestCompletionCallback callback; |
| 22315 | HttpRequestInfo request1; |
| 22316 | request1.method = "GET"; |
| 22317 | request1.url = GURL("https://foo.test/1"); |
| 22318 | request1.traffic_annotation = |
| 22319 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22320 | request1.network_isolation_key = kNetworkIsolationKey1; |
| 22321 | auto trans1 = |
| 22322 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22323 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 22324 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22325 | std::string response_data1; |
| 22326 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 22327 | EXPECT_EQ("1", response_data1); |
| 22328 | trans1.reset(); |
| 22329 | |
| 22330 | HttpRequestInfo request2; |
| 22331 | request2.method = "GET"; |
| 22332 | request2.url = GURL("https://foo.test/2"); |
| 22333 | request2.traffic_annotation = |
| 22334 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22335 | request2.network_isolation_key = kNetworkIsolationKey2; |
| 22336 | auto trans2 = |
| 22337 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22338 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 22339 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22340 | std::string response_data2; |
| 22341 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 22342 | EXPECT_EQ("2", response_data2); |
| 22343 | trans2.reset(); |
| 22344 | |
| 22345 | HttpRequestInfo request3; |
| 22346 | request3.method = "GET"; |
| 22347 | request3.url = GURL("https://foo.test/3"); |
| 22348 | request3.traffic_annotation = |
| 22349 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22350 | request3.network_isolation_key = kNetworkIsolationKey1; |
| 22351 | auto trans3 = |
| 22352 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22353 | rv = trans3->Start(&request3, callback.callback(), NetLogWithSource()); |
| 22354 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22355 | std::string response_data3; |
| 22356 | EXPECT_THAT(ReadTransaction(trans3.get(), &response_data3), IsOk()); |
| 22357 | EXPECT_EQ("3", response_data3); |
| 22358 | trans3.reset(); |
| 22359 | } |
| 22360 | |
| 22361 | // Test that the NetworkIsolationKey is passed down to SSLConfig so the session |
| 22362 | // cache is isolated, for both origins and proxies. |
| 22363 | TEST_F(HttpNetworkTransactionTest, NetworkIsolationSSLProxy) { |
| 22364 | base::test::ScopedFeatureList feature_list; |
| 22365 | feature_list.InitWithFeatures( |
| 22366 | {features::kPartitionConnectionsByNetworkIsolationKey, |
| 22367 | features::kPartitionSSLSessionsByNetworkIsolationKey}, |
| 22368 | {}); |
| 22369 | |
| 22370 | session_deps_.proxy_resolution_service = ProxyResolutionService::CreateFixed( |
| 22371 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22372 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 22373 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 22374 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 22375 | const NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 22376 | const NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 22377 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 22378 | |
| 22379 | // Make both a tunneled and non-tunneled request. |
| 22380 | HttpRequestInfo request1; |
| 22381 | request1.method = "GET"; |
| 22382 | request1.url = GURL("https://foo.test/1"); |
| 22383 | request1.traffic_annotation = |
| 22384 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22385 | request1.network_isolation_key = kNetworkIsolationKey1; |
| 22386 | |
| 22387 | HttpRequestInfo request2; |
| 22388 | request2.method = "GET"; |
| 22389 | request2.url = GURL("http://foo.test/2"); |
| 22390 | request2.traffic_annotation = |
| 22391 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22392 | request2.network_isolation_key = kNetworkIsolationKey2; |
| 22393 | |
| 22394 | const MockWrite kWrites1[] = { |
| 22395 | MockWrite("CONNECT foo.test:443 HTTP/1.1\r\n" |
| 22396 | "Host: foo.test:443\r\n" |
| 22397 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 22398 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 22399 | "Host: foo.test\r\n" |
| 22400 | "Connection: keep-alive\r\n\r\n")}; |
| 22401 | |
| 22402 | const MockRead kReads1[] = { |
| 22403 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 22404 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22405 | "Connection: close\r\n" |
| 22406 | "Content-Length: 1\r\n\r\n" |
| 22407 | "1")}; |
| 22408 | |
| 22409 | const MockWrite kWrites2[] = { |
| 22410 | MockWrite("GET http://foo.test/2 HTTP/1.1\r\n" |
| 22411 | "Host: foo.test\r\n" |
| 22412 | "Proxy-Connection: keep-alive\r\n\r\n")}; |
| 22413 | |
| 22414 | const MockRead kReads2[] = { |
| 22415 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22416 | "Connection: close\r\n" |
| 22417 | "Content-Length: 1\r\n\r\n" |
| 22418 | "2")}; |
| 22419 | |
| 22420 | StaticSocketDataProvider data1(kReads1, kWrites1); |
| 22421 | StaticSocketDataProvider data2(kReads2, kWrites2); |
| 22422 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 22423 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 22424 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 22425 | |
| 22426 | SSLSocketDataProvider ssl_proxy1(ASYNC, OK); |
| 22427 | ssl_proxy1.expected_host_and_port = HostPortPair("myproxy", 70); |
| 22428 | ssl_proxy1.expected_network_isolation_key = kNetworkIsolationKey1; |
| 22429 | SSLSocketDataProvider ssl_origin1(ASYNC, OK); |
| 22430 | ssl_origin1.expected_host_and_port = HostPortPair("foo.test", 443); |
| 22431 | ssl_origin1.expected_network_isolation_key = kNetworkIsolationKey1; |
| 22432 | SSLSocketDataProvider ssl_proxy2(ASYNC, OK); |
| 22433 | ssl_proxy2.expected_host_and_port = HostPortPair("myproxy", 70); |
| 22434 | ssl_proxy2.expected_network_isolation_key = kNetworkIsolationKey2; |
| 22435 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy1); |
| 22436 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin1); |
| 22437 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy2); |
| 22438 | |
| 22439 | TestCompletionCallback callback; |
| 22440 | auto trans1 = |
| 22441 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22442 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 22443 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22444 | std::string response_data1; |
| 22445 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 22446 | EXPECT_EQ("1", response_data1); |
| 22447 | trans1.reset(); |
| 22448 | |
| 22449 | auto trans2 = |
| 22450 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22451 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 22452 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22453 | std::string response_data2; |
| 22454 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 22455 | EXPECT_EQ("2", response_data2); |
| 22456 | trans2.reset(); |
| 22457 | } |
| 22458 | |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22459 | // Test that SSLConfig changes from SSLConfigService are picked up even when |
| 22460 | // there are live sockets. |
| 22461 | TEST_F(HttpNetworkTransactionTest, SSLConfigChanged) { |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 22462 | SSLContextConfig ssl_context_config; |
| 22463 | ssl_context_config.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 22464 | auto ssl_config_service = |
| 22465 | std::make_unique<TestSSLConfigService>(ssl_context_config); |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22466 | TestSSLConfigService* ssl_config_service_raw = ssl_config_service.get(); |
| 22467 | |
| 22468 | session_deps_.ssl_config_service = std::move(ssl_config_service); |
| 22469 | |
| 22470 | // Make three requests. Between the second and third, the SSL config will |
| 22471 | // change. |
| 22472 | HttpRequestInfo request1; |
| 22473 | request1.method = "GET"; |
| 22474 | request1.url = GURL("https://foo.test/1"); |
| 22475 | request1.traffic_annotation = |
| 22476 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22477 | |
| 22478 | HttpRequestInfo request2; |
| 22479 | request2.method = "GET"; |
| 22480 | request2.url = GURL("https://foo.test/2"); |
| 22481 | request2.traffic_annotation = |
| 22482 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22483 | |
| 22484 | HttpRequestInfo request3; |
| 22485 | request3.method = "GET"; |
| 22486 | request3.url = GURL("https://foo.test/3"); |
| 22487 | request3.traffic_annotation = |
| 22488 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22489 | |
| 22490 | const MockWrite kWrites1[] = { |
| 22491 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 22492 | "Host: foo.test\r\n" |
| 22493 | "Connection: keep-alive\r\n\r\n"), |
| 22494 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 22495 | "Host: foo.test\r\n" |
| 22496 | "Connection: keep-alive\r\n\r\n"), |
| 22497 | }; |
| 22498 | |
| 22499 | const MockRead kReads1[] = { |
| 22500 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22501 | "Connection: keep-alive\r\n" |
| 22502 | "Content-Length: 1\r\n\r\n" |
| 22503 | "1"), |
| 22504 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22505 | "Connection: keep-alive\r\n" |
| 22506 | "Content-Length: 1\r\n\r\n" |
| 22507 | "2"), |
| 22508 | }; |
| 22509 | |
| 22510 | // The third request goes on a different socket because the SSL config has |
| 22511 | // changed. |
| 22512 | const MockWrite kWrites2[] = { |
| 22513 | MockWrite("GET /3 HTTP/1.1\r\n" |
| 22514 | "Host: foo.test\r\n" |
| 22515 | "Connection: keep-alive\r\n\r\n")}; |
| 22516 | |
| 22517 | const MockRead kReads2[] = { |
| 22518 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22519 | "Connection: keep-alive\r\n" |
| 22520 | "Content-Length: 1\r\n\r\n" |
| 22521 | "3")}; |
| 22522 | |
| 22523 | StaticSocketDataProvider data1(kReads1, kWrites1); |
| 22524 | StaticSocketDataProvider data2(kReads2, kWrites2); |
| 22525 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 22526 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 22527 | |
| 22528 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 22529 | ssl1.expected_ssl_version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 22530 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 22531 | ssl2.expected_ssl_version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
| 22532 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 22533 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 22534 | |
| 22535 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 22536 | |
| 22537 | TestCompletionCallback callback; |
| 22538 | auto trans1 = |
| 22539 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22540 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 22541 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22542 | std::string response_data1; |
| 22543 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 22544 | EXPECT_EQ("1", response_data1); |
| 22545 | trans1.reset(); |
| 22546 | |
| 22547 | auto trans2 = |
| 22548 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22549 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 22550 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22551 | std::string response_data2; |
| 22552 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 22553 | EXPECT_EQ("2", response_data2); |
| 22554 | trans2.reset(); |
| 22555 | |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 22556 | ssl_context_config.version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
| 22557 | ssl_config_service_raw->UpdateSSLConfigAndNotify(ssl_context_config); |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22558 | |
| 22559 | auto trans3 = |
| 22560 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22561 | rv = trans3->Start(&request3, callback.callback(), NetLogWithSource()); |
| 22562 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22563 | std::string response_data3; |
| 22564 | EXPECT_THAT(ReadTransaction(trans3.get(), &response_data3), IsOk()); |
| 22565 | EXPECT_EQ("3", response_data3); |
| 22566 | trans3.reset(); |
| 22567 | } |
| 22568 | |
| 22569 | TEST_F(HttpNetworkTransactionTest, SSLConfigChangedPendingConnect) { |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 22570 | SSLContextConfig ssl_context_config; |
| 22571 | ssl_context_config.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 22572 | auto ssl_config_service = |
| 22573 | std::make_unique<TestSSLConfigService>(ssl_context_config); |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22574 | TestSSLConfigService* ssl_config_service_raw = ssl_config_service.get(); |
| 22575 | |
| 22576 | session_deps_.ssl_config_service = std::move(ssl_config_service); |
| 22577 | |
| 22578 | HttpRequestInfo request; |
| 22579 | request.method = "GET"; |
| 22580 | request.url = GURL("https://foo.test/1"); |
| 22581 | request.traffic_annotation = |
| 22582 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22583 | |
| 22584 | // Make a socket which never connects. |
| 22585 | StaticSocketDataProvider data({}, {}); |
| 22586 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 22587 | SSLSocketDataProvider ssl_data(SYNCHRONOUS, ERR_IO_PENDING); |
| 22588 | ssl_data.expected_ssl_version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 22589 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data); |
| 22590 | |
| 22591 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 22592 | |
| 22593 | TestCompletionCallback callback; |
| 22594 | auto trans = |
| 22595 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22596 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 22597 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 22598 | |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 22599 | ssl_context_config.version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
| 22600 | ssl_config_service_raw->UpdateSSLConfigAndNotify(ssl_context_config); |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22601 | |
| 22602 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_NETWORK_CHANGED)); |
| 22603 | } |
| 22604 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 22605 | // Test that HttpNetworkTransaction correctly handles existing sockets when the |
| 22606 | // server requests a client certificate post-handshake (via a TLS |
| 22607 | // renegotiation). This is a regression test for https://crbug.com/829184. |
| 22608 | TEST_F(HttpNetworkTransactionTest, PostHandshakeClientCertWithSockets) { |
| 22609 | const MutableNetworkTrafficAnnotationTag kTrafficAnnotation( |
| 22610 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22611 | |
| 22612 | auto cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 22613 | cert_request_info->host_and_port = HostPortPair("foo.test", 443); |
| 22614 | |
| 22615 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 22616 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 22617 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 22618 | ASSERT_TRUE(identity); |
| 22619 | |
| 22620 | // This test will make several requests so that, when the client certificate |
| 22621 | // request comes in, we have a socket in use, an idle socket, and a socket for |
| 22622 | // an unrelated host. |
| 22623 | // |
| 22624 | // First, two long-lived requests which do not complete until after the client |
| 22625 | // certificate request. This arranges for sockets to be in use during the |
| 22626 | // request. They should not be interrupted. |
| 22627 | HttpRequestInfo request_long_lived; |
| 22628 | request_long_lived.method = "GET"; |
| 22629 | request_long_lived.url = GURL("https://foo.test/long-lived"); |
| 22630 | request_long_lived.traffic_annotation = kTrafficAnnotation; |
| 22631 | |
| 22632 | HttpRequestInfo request_long_lived_bar; |
| 22633 | request_long_lived_bar.method = "GET"; |
| 22634 | request_long_lived_bar.url = GURL("https://bar.test/long-lived"); |
| 22635 | request_long_lived_bar.traffic_annotation = kTrafficAnnotation; |
| 22636 | |
| 22637 | // Next, make a request that needs client certificates. |
| 22638 | HttpRequestInfo request_auth; |
| 22639 | request_auth.method = "GET"; |
| 22640 | request_auth.url = GURL("https://foo.test/auth"); |
| 22641 | request_auth.traffic_annotation = kTrafficAnnotation; |
| 22642 | |
| 22643 | // Before responding to the challenge, make a request to an unauthenticated |
| 22644 | // endpoint. This will result in an idle socket when the client certificate |
| 22645 | // challenge is resolved. |
| 22646 | HttpRequestInfo request_unauth; |
| 22647 | request_unauth.method = "GET"; |
| 22648 | request_unauth.url = GURL("https://foo.test/unauth"); |
| 22649 | request_unauth.traffic_annotation = kTrafficAnnotation; |
| 22650 | |
| 22651 | // After all the preceding requests complete, end with two additional requests |
| 22652 | // to ensure pre-authentication foo.test sockets are not used and bar.test |
| 22653 | // sockets are unaffected. |
| 22654 | HttpRequestInfo request_post_auth; |
| 22655 | request_post_auth.method = "GET"; |
| 22656 | request_post_auth.url = GURL("https://foo.test/post-auth"); |
| 22657 | request_post_auth.traffic_annotation = kTrafficAnnotation; |
| 22658 | |
| 22659 | HttpRequestInfo request_post_auth_bar; |
| 22660 | request_post_auth_bar.method = "GET"; |
| 22661 | request_post_auth_bar.url = GURL("https://bar.test/post-auth"); |
| 22662 | request_post_auth_bar.traffic_annotation = kTrafficAnnotation; |
| 22663 | |
| 22664 | // The sockets for /long-lived and /unauth complete their request but are |
| 22665 | // not allocated for /post-auth or /retry because SSL state has since changed. |
| 22666 | const MockWrite kLongLivedWrites[] = { |
| 22667 | MockWrite(ASYNC, 0, |
| 22668 | "GET /long-lived HTTP/1.1\r\n" |
| 22669 | "Host: foo.test\r\n" |
| 22670 | "Connection: keep-alive\r\n\r\n"), |
| 22671 | }; |
| 22672 | const MockRead kLongLivedReads[] = { |
| 22673 | // Pause so /long-lived completes after the client presents client |
| 22674 | // certificates. |
| 22675 | MockRead(ASYNC, ERR_IO_PENDING, 1), |
| 22676 | MockRead(ASYNC, 2, |
| 22677 | "HTTP/1.1 200 OK\r\n" |
| 22678 | "Connection: keep-alive\r\n" |
| 22679 | "Content-Length: 10\r\n\r\n" |
| 22680 | "long-lived"), |
| 22681 | }; |
| 22682 | SequencedSocketData data_long_lived(kLongLivedReads, kLongLivedWrites); |
| 22683 | SSLSocketDataProvider ssl_long_lived(ASYNC, OK); |
| 22684 | session_deps_.socket_factory->AddSocketDataProvider(&data_long_lived); |
| 22685 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_long_lived); |
| 22686 | |
| 22687 | // Requests for bar.test should be unaffected by foo.test and get allocated |
| 22688 | // a single socket. |
| 22689 | const MockWrite kBarWrites[] = { |
| 22690 | MockWrite(ASYNC, 0, |
| 22691 | "GET /long-lived HTTP/1.1\r\n" |
| 22692 | "Host: bar.test\r\n" |
| 22693 | "Connection: keep-alive\r\n\r\n"), |
| 22694 | MockWrite(ASYNC, 3, |
| 22695 | "GET /post-auth HTTP/1.1\r\n" |
| 22696 | "Host: bar.test\r\n" |
| 22697 | "Connection: keep-alive\r\n\r\n"), |
| 22698 | }; |
| 22699 | const MockRead kBarReads[] = { |
| 22700 | // Pause on /long-lived so it completes after foo.test's authentication. |
| 22701 | MockRead(ASYNC, ERR_IO_PENDING, 1), |
| 22702 | MockRead(ASYNC, 2, |
| 22703 | "HTTP/1.1 200 OK\r\n" |
| 22704 | "Connection: keep-alive\r\n" |
| 22705 | "Content-Length: 10\r\n\r\n" |
| 22706 | "long-lived"), |
| 22707 | MockRead(ASYNC, 4, |
| 22708 | "HTTP/1.1 200 OK\r\n" |
| 22709 | "Connection: keep-alive\r\n" |
| 22710 | "Content-Length: 9\r\n\r\n" |
| 22711 | "post-auth"), |
| 22712 | }; |
| 22713 | SequencedSocketData data_bar(kBarReads, kBarWrites); |
| 22714 | SSLSocketDataProvider ssl_bar(ASYNC, OK); |
| 22715 | session_deps_.socket_factory->AddSocketDataProvider(&data_bar); |
| 22716 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bar); |
| 22717 | |
| 22718 | // Requesting /auth results in a post-handshake client certificate challenge. |
| 22719 | const MockWrite kAuthWrites[] = { |
| 22720 | MockWrite(ASYNC, 0, |
| 22721 | "GET /auth HTTP/1.1\r\n" |
| 22722 | "Host: foo.test\r\n" |
| 22723 | "Connection: keep-alive\r\n\r\n"), |
| 22724 | }; |
| 22725 | const MockRead kAuthReads[] = { |
| 22726 | MockRead(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 1), |
| 22727 | }; |
| 22728 | SequencedSocketData data_auth(kAuthReads, kAuthWrites); |
| 22729 | SSLSocketDataProvider ssl_auth(ASYNC, OK); |
| 22730 | ssl_auth.cert_request_info = cert_request_info.get(); |
| 22731 | session_deps_.socket_factory->AddSocketDataProvider(&data_auth); |
| 22732 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_auth); |
| 22733 | |
| 22734 | // Requesting /unauth completes. |
| 22735 | const MockWrite kUnauthWrites[] = { |
| 22736 | MockWrite(ASYNC, 0, |
| 22737 | "GET /unauth HTTP/1.1\r\n" |
| 22738 | "Host: foo.test\r\n" |
| 22739 | "Connection: keep-alive\r\n\r\n"), |
| 22740 | }; |
| 22741 | const MockRead kUnauthReads[] = { |
| 22742 | MockRead(ASYNC, 1, |
| 22743 | "HTTP/1.1 200 OK\r\n" |
| 22744 | "Connection: keep-alive\r\n" |
| 22745 | "Content-Length: 6\r\n\r\n" |
| 22746 | "unauth"), |
| 22747 | }; |
| 22748 | SequencedSocketData data_unauth(kUnauthReads, kUnauthWrites); |
| 22749 | SSLSocketDataProvider ssl_unauth(ASYNC, OK); |
| 22750 | session_deps_.socket_factory->AddSocketDataProvider(&data_unauth); |
| 22751 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_unauth); |
| 22752 | |
| 22753 | // When the client certificate is selected, /auth is retried on a new |
| 22754 | // connection. In particular, it should not be retried on |data_unauth|, |
| 22755 | // which would not honor the new client certificate configuration. |
| 22756 | const MockWrite kRetryWrites[] = { |
| 22757 | MockWrite(ASYNC, 0, |
| 22758 | "GET /auth HTTP/1.1\r\n" |
| 22759 | "Host: foo.test\r\n" |
| 22760 | "Connection: keep-alive\r\n\r\n"), |
| 22761 | }; |
| 22762 | const MockRead kRetryReads[] = { |
| 22763 | MockRead(ASYNC, 1, |
| 22764 | "HTTP/1.1 200 OK\r\n" |
| 22765 | // Close the connection so we test that /post-auth is not |
| 22766 | // allocated to |data_unauth| or |data_long_lived|. |
| 22767 | "Connection: close\r\n" |
| 22768 | "Content-Length: 4\r\n\r\n" |
| 22769 | "auth"), |
| 22770 | }; |
| 22771 | SequencedSocketData data_retry(kRetryReads, kRetryWrites); |
| 22772 | SSLSocketDataProvider ssl_retry(ASYNC, OK); |
| 22773 | ssl_retry.expected_send_client_cert = true; |
| 22774 | ssl_retry.expected_client_cert = identity->certificate(); |
| 22775 | session_deps_.socket_factory->AddSocketDataProvider(&data_retry); |
| 22776 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_retry); |
| 22777 | |
| 22778 | // /post-auth gets its own socket. |
| 22779 | const MockWrite kPostAuthWrites[] = { |
| 22780 | MockWrite(ASYNC, 0, |
| 22781 | "GET /post-auth HTTP/1.1\r\n" |
| 22782 | "Host: foo.test\r\n" |
| 22783 | "Connection: keep-alive\r\n\r\n"), |
| 22784 | }; |
| 22785 | const MockRead kPostAuthReads[] = { |
| 22786 | MockRead(ASYNC, 1, |
| 22787 | "HTTP/1.1 200 OK\r\n" |
| 22788 | "Connection: keep-alive\r\n" |
| 22789 | "Content-Length: 9\r\n\r\n" |
| 22790 | "post-auth"), |
| 22791 | }; |
| 22792 | SequencedSocketData data_post_auth(kPostAuthReads, kPostAuthWrites); |
| 22793 | SSLSocketDataProvider ssl_post_auth(ASYNC, OK); |
| 22794 | ssl_post_auth.expected_send_client_cert = true; |
| 22795 | ssl_post_auth.expected_client_cert = identity->certificate(); |
| 22796 | session_deps_.socket_factory->AddSocketDataProvider(&data_post_auth); |
| 22797 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_post_auth); |
| 22798 | |
| 22799 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 22800 | |
| 22801 | // Start the two long-lived requests. |
| 22802 | TestCompletionCallback callback_long_lived; |
| 22803 | auto trans_long_lived = |
| 22804 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22805 | int rv = trans_long_lived->Start( |
| 22806 | &request_long_lived, callback_long_lived.callback(), NetLogWithSource()); |
| 22807 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 22808 | data_long_lived.RunUntilPaused(); |
| 22809 | |
| 22810 | TestCompletionCallback callback_long_lived_bar; |
| 22811 | auto trans_long_lived_bar = |
| 22812 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22813 | rv = trans_long_lived_bar->Start(&request_long_lived_bar, |
| 22814 | callback_long_lived_bar.callback(), |
| 22815 | NetLogWithSource()); |
| 22816 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 22817 | data_bar.RunUntilPaused(); |
| 22818 | |
| 22819 | // Request /auth. This gives a client certificate challenge. |
| 22820 | TestCompletionCallback callback_auth; |
| 22821 | auto trans_auth = |
| 22822 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22823 | rv = trans_auth->Start(&request_auth, callback_auth.callback(), |
| 22824 | NetLogWithSource()); |
| 22825 | EXPECT_THAT(callback_auth.GetResult(rv), |
| 22826 | IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 22827 | |
| 22828 | // Make an unauthenticated request. This completes. |
| 22829 | TestCompletionCallback callback_unauth; |
| 22830 | auto trans_unauth = |
| 22831 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22832 | rv = trans_unauth->Start(&request_unauth, callback_unauth.callback(), |
| 22833 | NetLogWithSource()); |
| 22834 | EXPECT_THAT(callback_unauth.GetResult(rv), IsOk()); |
| 22835 | std::string response_unauth; |
| 22836 | EXPECT_THAT(ReadTransaction(trans_unauth.get(), &response_unauth), IsOk()); |
| 22837 | EXPECT_EQ("unauth", response_unauth); |
| 22838 | trans_unauth.reset(); |
| 22839 | |
| 22840 | // Complete the authenticated request. |
| 22841 | rv = trans_auth->RestartWithCertificate(identity->certificate(), |
| 22842 | identity->ssl_private_key(), |
| 22843 | callback_auth.callback()); |
| 22844 | EXPECT_THAT(callback_auth.GetResult(rv), IsOk()); |
| 22845 | std::string response_auth; |
| 22846 | EXPECT_THAT(ReadTransaction(trans_auth.get(), &response_auth), IsOk()); |
| 22847 | EXPECT_EQ("auth", response_auth); |
| 22848 | trans_auth.reset(); |
| 22849 | |
| 22850 | // Complete the long-lived requests. |
| 22851 | data_long_lived.Resume(); |
| 22852 | EXPECT_THAT(callback_long_lived.GetResult(ERR_IO_PENDING), IsOk()); |
| 22853 | std::string response_long_lived; |
| 22854 | EXPECT_THAT(ReadTransaction(trans_long_lived.get(), &response_long_lived), |
| 22855 | IsOk()); |
| 22856 | EXPECT_EQ("long-lived", response_long_lived); |
| 22857 | trans_long_lived.reset(); |
| 22858 | |
| 22859 | data_bar.Resume(); |
| 22860 | EXPECT_THAT(callback_long_lived_bar.GetResult(ERR_IO_PENDING), IsOk()); |
| 22861 | std::string response_long_lived_bar; |
| 22862 | EXPECT_THAT( |
| 22863 | ReadTransaction(trans_long_lived_bar.get(), &response_long_lived_bar), |
| 22864 | IsOk()); |
| 22865 | EXPECT_EQ("long-lived", response_long_lived_bar); |
| 22866 | trans_long_lived_bar.reset(); |
| 22867 | |
| 22868 | // Run the post-authentication requests. |
| 22869 | TestCompletionCallback callback_post_auth; |
| 22870 | auto trans_post_auth = |
| 22871 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22872 | rv = trans_post_auth->Start(&request_post_auth, callback_post_auth.callback(), |
| 22873 | NetLogWithSource()); |
| 22874 | EXPECT_THAT(callback_post_auth.GetResult(rv), IsOk()); |
| 22875 | std::string response_post_auth; |
| 22876 | EXPECT_THAT(ReadTransaction(trans_post_auth.get(), &response_post_auth), |
| 22877 | IsOk()); |
| 22878 | EXPECT_EQ("post-auth", response_post_auth); |
| 22879 | trans_post_auth.reset(); |
| 22880 | |
| 22881 | TestCompletionCallback callback_post_auth_bar; |
| 22882 | auto trans_post_auth_bar = |
| 22883 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22884 | rv = trans_post_auth_bar->Start(&request_post_auth_bar, |
| 22885 | callback_post_auth_bar.callback(), |
| 22886 | NetLogWithSource()); |
| 22887 | EXPECT_THAT(callback_post_auth_bar.GetResult(rv), IsOk()); |
| 22888 | std::string response_post_auth_bar; |
| 22889 | EXPECT_THAT( |
| 22890 | ReadTransaction(trans_post_auth_bar.get(), &response_post_auth_bar), |
| 22891 | IsOk()); |
| 22892 | EXPECT_EQ("post-auth", response_post_auth_bar); |
| 22893 | trans_post_auth_bar.reset(); |
| 22894 | } |
| 22895 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 22896 | } // namespace net |