[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 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> |
| 9 | #include <string> |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 11 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12 | #include "base/basictypes.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 14 | #include "base/file_path.h" |
| 15 | #include "base/file_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/scoped_ptr.h" |
[email protected] | 5e6efa5 | 2011-06-27 17:26:41 | [diff] [blame] | 17 | #include "base/metrics/histogram.h" |
[email protected] | f36a813 | 2011-09-02 18:36:33 | [diff] [blame] | 18 | #include "base/test/test_file_util.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 19 | #include "base/utf_string_conversions.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 20 | #include "net/base/auth.h" |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 21 | #include "net/base/capturing_net_log.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 22 | #include "net/base/completion_callback.h" |
[email protected] | 98e1cd01 | 2011-11-08 15:33:09 | [diff] [blame] | 23 | #include "net/base/host_cache.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 24 | #include "net/base/mock_host_resolver.h" |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 25 | #include "net/base/net_log.h" |
| 26 | #include "net/base/net_log_unittest.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 27 | #include "net/base/request_priority.h" |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 28 | #include "net/base/ssl_cert_request_info.h" |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 29 | #include "net/base/ssl_config_service_defaults.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 30 | #include "net/base/ssl_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | #include "net/base/test_completion_callback.h" |
| 32 | #include "net/base/upload_data.h" |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 33 | #include "net/http/http_auth_handler_digest.h" |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 34 | #include "net/http/http_auth_handler_mock.h" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 35 | #include "net/http/http_auth_handler_ntlm.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 36 | #include "net/http/http_basic_stream.h" |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 37 | #include "net/http/http_net_log_params.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | #include "net/http/http_network_session.h" |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 39 | #include "net/http/http_network_session_peer.h" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 40 | #include "net/http/http_server_properties_impl.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 41 | #include "net/http/http_stream.h" |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 42 | #include "net/http/http_stream_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | #include "net/http/http_transaction_unittest.h" |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 44 | #include "net/proxy/proxy_config_service_fixed.h" |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 45 | #include "net/proxy/proxy_resolver.h" |
| 46 | #include "net/proxy/proxy_service.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 47 | #include "net/socket/client_socket_factory.h" |
| 48 | #include "net/socket/socket_test_util.h" |
| 49 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 50 | #include "net/spdy/spdy_framer.h" |
| 51 | #include "net/spdy/spdy_session.h" |
| 52 | #include "net/spdy/spdy_session_pool.h" |
| 53 | #include "net/spdy/spdy_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 55 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 56 | |
| 57 | //----------------------------------------------------------------------------- |
| 58 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 59 | namespace { |
| 60 | |
| 61 | const string16 kBar(ASCIIToUTF16("bar")); |
| 62 | const string16 kBar2(ASCIIToUTF16("bar2")); |
| 63 | const string16 kBar3(ASCIIToUTF16("bar3")); |
| 64 | const string16 kBaz(ASCIIToUTF16("baz")); |
| 65 | const string16 kFirst(ASCIIToUTF16("first")); |
| 66 | const string16 kFoo(ASCIIToUTF16("foo")); |
| 67 | const string16 kFoo2(ASCIIToUTF16("foo2")); |
| 68 | const string16 kFoo3(ASCIIToUTF16("foo3")); |
| 69 | const string16 kFou(ASCIIToUTF16("fou")); |
| 70 | const string16 kSecond(ASCIIToUTF16("second")); |
| 71 | const string16 kTestingNTLM(ASCIIToUTF16("testing-ntlm")); |
| 72 | const string16 kWrongPassword(ASCIIToUTF16("wrongpassword")); |
| 73 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 74 | // MakeNextProtos is a utility function that returns a vector of std::strings |
| 75 | // from its arguments. Don't forget to terminate the argument list with a NULL. |
| 76 | std::vector<std::string> MakeNextProtos(const char* a, ...) { |
| 77 | std::vector<std::string> ret; |
| 78 | ret.push_back(a); |
| 79 | |
| 80 | va_list args; |
| 81 | va_start(args, a); |
| 82 | |
| 83 | for (;;) { |
| 84 | const char* value = va_arg(args, const char*); |
| 85 | if (value == NULL) |
| 86 | break; |
| 87 | ret.push_back(value); |
| 88 | } |
| 89 | va_end(args); |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | // SpdyNextProtos returns a vector of NPN protocol strings for negotiating |
| 95 | // SPDY. |
| 96 | std::vector<std::string> SpdyNextProtos() { |
| 97 | return MakeNextProtos("http/1.1", "spdy/2", NULL); |
| 98 | } |
| 99 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 100 | } // namespace |
| 101 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 102 | namespace net { |
| 103 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 104 | // Helper to manage the lifetimes of the dependencies for a |
| 105 | // HttpNetworkTransaction. |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 106 | struct SessionDependencies { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 107 | // Default set of dependencies -- "null" proxy service. |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 108 | SessionDependencies() |
| 109 | : host_resolver(new MockHostResolver), |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 110 | cert_verifier(new CertVerifier), |
[email protected] | ebeefff3 | 2010-09-15 05:10:02 | [diff] [blame] | 111 | proxy_service(ProxyService::CreateDirect()), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 112 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 113 | http_auth_handler_factory( |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 114 | HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 115 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 116 | |
| 117 | // Custom proxy service dependency. |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 118 | explicit SessionDependencies(ProxyService* proxy_service) |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 119 | : host_resolver(new MockHostResolver), |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 120 | cert_verifier(new CertVerifier), |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 121 | proxy_service(proxy_service), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 122 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 123 | http_auth_handler_factory( |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 124 | HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 125 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 126 | |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 127 | scoped_ptr<MockHostResolverBase> host_resolver; |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 128 | scoped_ptr<CertVerifier> cert_verifier; |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 129 | scoped_ptr<ProxyService> proxy_service; |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 130 | scoped_refptr<SSLConfigService> ssl_config_service; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 131 | MockClientSocketFactory socket_factory; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 132 | scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 133 | HttpServerPropertiesImpl http_server_properties; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 134 | NetLog* net_log; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 135 | }; |
| 136 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 137 | HttpNetworkSession* CreateSession(SessionDependencies* session_deps) { |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 138 | net::HttpNetworkSession::Params params; |
| 139 | params.client_socket_factory = &session_deps->socket_factory; |
| 140 | params.host_resolver = session_deps->host_resolver.get(); |
| 141 | params.cert_verifier = session_deps->cert_verifier.get(); |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 142 | params.proxy_service = session_deps->proxy_service.get(); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 143 | params.ssl_config_service = session_deps->ssl_config_service; |
| 144 | params.http_auth_handler_factory = |
| 145 | session_deps->http_auth_handler_factory.get(); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 146 | params.http_server_properties = &session_deps->http_server_properties; |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 147 | params.net_log = session_deps->net_log; |
| 148 | return new HttpNetworkSession(params); |
[email protected] | e8d53619 | 2008-10-17 22:21:14 | [diff] [blame] | 149 | } |
| 150 | |
[email protected] | 89836e2 | 2008-09-25 20:33:42 | [diff] [blame] | 151 | class HttpNetworkTransactionTest : public PlatformTest { |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 152 | protected: |
| 153 | struct SimpleGetHelperResult { |
| 154 | int rv; |
| 155 | std::string status_line; |
| 156 | std::string response_data; |
| 157 | }; |
| 158 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 159 | virtual void SetUp() { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 160 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 161 | MessageLoop::current()->RunAllPending(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 162 | spdy::SpdyFramer::set_enable_compression_default(false); |
| 163 | } |
| 164 | |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 165 | virtual void TearDown() { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 166 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 167 | MessageLoop::current()->RunAllPending(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 168 | spdy::SpdyFramer::set_enable_compression_default(true); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 169 | // Empty the current queue. |
| 170 | MessageLoop::current()->RunAllPending(); |
| 171 | PlatformTest::TearDown(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 172 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 173 | MessageLoop::current()->RunAllPending(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 176 | void KeepAliveConnectionResendRequestTest(const MockRead& read_failure); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 177 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 178 | SimpleGetHelperResult SimpleGetHelperForData(StaticSocketDataProvider* data[], |
| 179 | size_t data_count) { |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 180 | SimpleGetHelperResult out; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 181 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 182 | HttpRequestInfo request; |
| 183 | request.method = "GET"; |
| 184 | request.url = GURL("http://www.google.com/"); |
| 185 | request.load_flags = 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 186 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 187 | SessionDependencies session_deps; |
| 188 | scoped_ptr<HttpTransaction> trans( |
| 189 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 190 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 191 | for (size_t i = 0; i < data_count; ++i) { |
| 192 | session_deps.socket_factory.AddSocketDataProvider(data[i]); |
| 193 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 194 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 195 | TestOldCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 196 | |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 197 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 198 | EXPECT_TRUE(log.bound().IsLoggingAllEvents()); |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 199 | int rv = trans->Start(&request, &callback, log.bound()); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 200 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 201 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 202 | out.rv = callback.WaitForResult(); |
| 203 | if (out.rv != OK) |
| 204 | return out; |
| 205 | |
| 206 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 207 | // Can't use ASSERT_* inside helper functions like this, so |
| 208 | // return an error. |
| 209 | if (response == NULL || response->headers == NULL) { |
| 210 | out.rv = ERR_UNEXPECTED; |
| 211 | return out; |
| 212 | } |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 213 | out.status_line = response->headers->GetStatusLine(); |
| 214 | |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 215 | EXPECT_EQ("192.0.2.33", response->socket_address.host()); |
| 216 | EXPECT_EQ(0, response->socket_address.port()); |
| 217 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 218 | rv = ReadTransaction(trans.get(), &out.response_data); |
| 219 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 220 | |
| 221 | net::CapturingNetLog::EntryList entries; |
| 222 | log.GetEntries(&entries); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 223 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 224 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 225 | NetLog::PHASE_NONE); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 226 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 227 | entries, pos, |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 228 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 229 | NetLog::PHASE_NONE); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 230 | |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 231 | CapturingNetLog::Entry entry = entries[pos]; |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 232 | NetLogHttpRequestParameter* request_params = |
| 233 | static_cast<NetLogHttpRequestParameter*>(entry.extra_parameters.get()); |
| 234 | EXPECT_EQ("GET / HTTP/1.1\r\n", request_params->GetLine()); |
| 235 | EXPECT_EQ("Host: www.google.com\r\n" |
| 236 | "Connection: keep-alive\r\n\r\n", |
| 237 | request_params->GetHeaders().ToString()); |
| 238 | |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 239 | return out; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 240 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 241 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 242 | SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[], |
| 243 | size_t reads_count) { |
| 244 | StaticSocketDataProvider reads(data_reads, reads_count, NULL, 0); |
| 245 | StaticSocketDataProvider* data[] = { &reads }; |
| 246 | return SimpleGetHelperForData(data, 1); |
| 247 | } |
| 248 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 249 | void ConnectStatusHelperWithExpectedStatus(const MockRead& status, |
| 250 | int expected_status); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 251 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 252 | void ConnectStatusHelper(const MockRead& status); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 253 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 254 | |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 255 | // Fill |str| with a long header list that consumes >= |size| bytes. |
| 256 | void FillLargeHeadersString(std::string* str, int size) { |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 257 | const char* row = |
| 258 | "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; |
| 259 | const int sizeof_row = strlen(row); |
| 260 | const int num_rows = static_cast<int>( |
| 261 | ceil(static_cast<float>(size) / sizeof_row)); |
| 262 | const int sizeof_data = num_rows * sizeof_row; |
| 263 | DCHECK(sizeof_data >= size); |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 264 | str->reserve(sizeof_data); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 265 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 266 | for (int i = 0; i < num_rows; ++i) |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 267 | str->append(row, sizeof_row); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 270 | // Alternative functions that eliminate randomness and dependency on the local |
| 271 | // host name so that the generated NTLM messages are reproducible. |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 272 | void MockGenerateRandom1(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 273 | static const uint8 bytes[] = { |
| 274 | 0x55, 0x29, 0x66, 0x26, 0x6b, 0x9c, 0x73, 0x54 |
| 275 | }; |
| 276 | static size_t current_byte = 0; |
| 277 | for (size_t i = 0; i < n; ++i) { |
| 278 | output[i] = bytes[current_byte++]; |
| 279 | current_byte %= arraysize(bytes); |
| 280 | } |
| 281 | } |
| 282 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 283 | void MockGenerateRandom2(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 284 | static const uint8 bytes[] = { |
| 285 | 0x96, 0x79, 0x85, 0xe7, 0x49, 0x93, 0x70, 0xa1, |
| 286 | 0x4e, 0xe7, 0x87, 0x45, 0x31, 0x5b, 0xd3, 0x1f |
| 287 | }; |
| 288 | static size_t current_byte = 0; |
| 289 | for (size_t i = 0; i < n; ++i) { |
| 290 | output[i] = bytes[current_byte++]; |
| 291 | current_byte %= arraysize(bytes); |
| 292 | } |
| 293 | } |
| 294 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 295 | std::string MockGetHostName() { |
| 296 | return "WTC-WIN7"; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 299 | template<typename ParentPool> |
| 300 | class CaptureGroupNameSocketPool : public ParentPool { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 301 | public: |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 302 | CaptureGroupNameSocketPool(HostResolver* host_resolver, |
| 303 | CertVerifier* cert_verifier); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 304 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 305 | const std::string last_group_name_received() const { |
| 306 | return last_group_name_; |
| 307 | } |
| 308 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 309 | virtual int RequestSocket(const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 310 | const void* socket_params, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 311 | RequestPriority priority, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 312 | ClientSocketHandle* handle, |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 313 | OldCompletionCallback* callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 314 | const BoundNetLog& net_log) { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 315 | last_group_name_ = group_name; |
| 316 | return ERR_IO_PENDING; |
| 317 | } |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 318 | virtual void CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 319 | ClientSocketHandle* handle) {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 320 | virtual void ReleaseSocket(const std::string& group_name, |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 321 | StreamSocket* socket, |
[email protected] | 9f95c69 | 2011-02-11 19:20:19 | [diff] [blame] | 322 | int id) {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 323 | virtual void CloseIdleSockets() {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 324 | virtual int IdleSocketCount() const { |
| 325 | return 0; |
| 326 | } |
| 327 | virtual int IdleSocketCountInGroup(const std::string& group_name) const { |
| 328 | return 0; |
| 329 | } |
| 330 | virtual LoadState GetLoadState(const std::string& group_name, |
| 331 | const ClientSocketHandle* handle) const { |
| 332 | return LOAD_STATE_IDLE; |
| 333 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 334 | virtual base::TimeDelta ConnectionTimeout() const { |
| 335 | return base::TimeDelta(); |
| 336 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 337 | |
| 338 | private: |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 339 | std::string last_group_name_; |
| 340 | }; |
| 341 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 342 | typedef CaptureGroupNameSocketPool<TransportClientSocketPool> |
| 343 | CaptureGroupNameTransportSocketPool; |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 344 | typedef CaptureGroupNameSocketPool<HttpProxyClientSocketPool> |
| 345 | CaptureGroupNameHttpProxySocketPool; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 346 | typedef CaptureGroupNameSocketPool<SOCKSClientSocketPool> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 347 | CaptureGroupNameSOCKSSocketPool; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 348 | typedef CaptureGroupNameSocketPool<SSLClientSocketPool> |
| 349 | CaptureGroupNameSSLSocketPool; |
| 350 | |
| 351 | template<typename ParentPool> |
| 352 | CaptureGroupNameSocketPool<ParentPool>::CaptureGroupNameSocketPool( |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 353 | HostResolver* host_resolver, |
| 354 | CertVerifier* /* cert_verifier */) |
| 355 | : ParentPool(0, 0, NULL, host_resolver, NULL, NULL) {} |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 356 | |
| 357 | template<> |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 358 | CaptureGroupNameHttpProxySocketPool::CaptureGroupNameSocketPool( |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 359 | HostResolver* host_resolver, |
| 360 | CertVerifier* /* cert_verifier */) |
| 361 | : HttpProxyClientSocketPool(0, 0, NULL, host_resolver, NULL, NULL, NULL) {} |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 362 | |
| 363 | template<> |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 364 | CaptureGroupNameSSLSocketPool::CaptureGroupNameSocketPool( |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 365 | HostResolver* host_resolver, |
| 366 | CertVerifier* cert_verifier) |
| 367 | : SSLClientSocketPool(0, 0, NULL, host_resolver, cert_verifier, NULL, NULL, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 368 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) {} |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 369 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 370 | //----------------------------------------------------------------------------- |
| 371 | |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 372 | // This is the expected return from a current server advertising SPDY. |
| 373 | static const char kAlternateProtocolHttpHeader[] = |
| 374 | "Alternate-Protocol: 443:npn-spdy/2\r\n\r\n"; |
| 375 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 376 | // Helper functions for validating that AuthChallengeInfo's are correctly |
| 377 | // configured for common cases. |
| 378 | bool CheckBasicServerAuth(const AuthChallengeInfo* auth_challenge) { |
| 379 | if (!auth_challenge) |
| 380 | return false; |
| 381 | EXPECT_FALSE(auth_challenge->is_proxy); |
| 382 | EXPECT_EQ("www.google.com:80", auth_challenge->challenger.ToString()); |
| 383 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
| 384 | EXPECT_EQ("basic", auth_challenge->scheme); |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | bool CheckBasicProxyAuth(const AuthChallengeInfo* auth_challenge) { |
| 389 | if (!auth_challenge) |
| 390 | return false; |
| 391 | EXPECT_TRUE(auth_challenge->is_proxy); |
| 392 | EXPECT_EQ("myproxy:70", auth_challenge->challenger.ToString()); |
| 393 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
| 394 | EXPECT_EQ("basic", auth_challenge->scheme); |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | bool CheckDigestServerAuth(const AuthChallengeInfo* auth_challenge) { |
| 399 | if (!auth_challenge) |
| 400 | return false; |
| 401 | EXPECT_FALSE(auth_challenge->is_proxy); |
| 402 | EXPECT_EQ("www.google.com:80", auth_challenge->challenger.ToString()); |
| 403 | EXPECT_EQ("digestive", auth_challenge->realm); |
| 404 | EXPECT_EQ("digest", auth_challenge->scheme); |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | bool CheckNTLMServerAuth(const AuthChallengeInfo* auth_challenge) { |
| 409 | if (!auth_challenge) |
| 410 | return false; |
| 411 | EXPECT_FALSE(auth_challenge->is_proxy); |
| 412 | EXPECT_EQ("172.22.68.17:80", auth_challenge->challenger.ToString()); |
| 413 | EXPECT_EQ(std::string(), auth_challenge->realm); |
| 414 | EXPECT_EQ("ntlm", auth_challenge->scheme); |
| 415 | return true; |
| 416 | } |
| 417 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 418 | TEST_F(HttpNetworkTransactionTest, Basic) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 419 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 420 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 421 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TEST_F(HttpNetworkTransactionTest, SimpleGET) { |
| 425 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 426 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 427 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 428 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 429 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 430 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 431 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 432 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 433 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 434 | EXPECT_EQ("hello world", out.response_data); |
| 435 | } |
| 436 | |
| 437 | // Response with no status line. |
| 438 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) { |
| 439 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 440 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 441 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 442 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 443 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 444 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 445 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 446 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 447 | EXPECT_EQ("hello world", out.response_data); |
| 448 | } |
| 449 | |
| 450 | // Allow up to 4 bytes of junk to precede status line. |
| 451 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk2Bytes) { |
| 452 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 453 | MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 454 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 455 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 456 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 457 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 458 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 459 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 460 | EXPECT_EQ("DATA", out.response_data); |
| 461 | } |
| 462 | |
| 463 | // Allow up to 4 bytes of junk to precede status line. |
| 464 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) { |
| 465 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 466 | MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 467 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 468 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 469 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 470 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 471 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 472 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 473 | EXPECT_EQ("DATA", out.response_data); |
| 474 | } |
| 475 | |
| 476 | // Beyond 4 bytes of slop and it should fail to find a status line. |
| 477 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) { |
| 478 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 479 | MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 480 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 481 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 482 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 483 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 484 | EXPECT_EQ(OK, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 485 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 486 | EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // Same as StatusLineJunk4Bytes, except the read chunks are smaller. |
| 490 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) { |
| 491 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 492 | MockRead("\n"), |
| 493 | MockRead("\n"), |
| 494 | MockRead("Q"), |
| 495 | MockRead("J"), |
| 496 | MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 497 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 498 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 499 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 500 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 501 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 502 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 503 | EXPECT_EQ("DATA", out.response_data); |
| 504 | } |
| 505 | |
| 506 | // Close the connection before enough bytes to have a status line. |
| 507 | TEST_F(HttpNetworkTransactionTest, StatusLinePartial) { |
| 508 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 509 | MockRead("HTT"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 510 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 511 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 512 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 513 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 514 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 515 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 516 | EXPECT_EQ("HTT", out.response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 517 | } |
| 518 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 519 | // Simulate a 204 response, lacking a Content-Length header, sent over a |
| 520 | // persistent connection. The response should still terminate since a 204 |
| 521 | // cannot have a response body. |
| 522 | TEST_F(HttpNetworkTransactionTest, StopsReading204) { |
| 523 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 524 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 525 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 526 | MockRead(false, OK), |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 527 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 528 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 529 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 530 | EXPECT_EQ(OK, out.rv); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 531 | EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line); |
| 532 | EXPECT_EQ("", out.response_data); |
| 533 | } |
| 534 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 535 | // A simple request using chunked encoding with some extra data after. |
| 536 | // (Like might be seen in a pipelined response.) |
| 537 | TEST_F(HttpNetworkTransactionTest, ChunkedEncoding) { |
| 538 | MockRead data_reads[] = { |
| 539 | MockRead("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"), |
| 540 | MockRead("5\r\nHello\r\n"), |
| 541 | MockRead("1\r\n"), |
| 542 | MockRead(" \r\n"), |
| 543 | MockRead("5\r\nworld\r\n"), |
| 544 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
| 545 | MockRead(false, OK), |
| 546 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 547 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 548 | arraysize(data_reads)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 549 | EXPECT_EQ(OK, out.rv); |
| 550 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 551 | EXPECT_EQ("Hello world", out.response_data); |
| 552 | } |
| 553 | |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 554 | // Next tests deal with http://crbug.com/56344. |
| 555 | |
| 556 | TEST_F(HttpNetworkTransactionTest, |
| 557 | MultipleContentLengthHeadersNoTransferEncoding) { |
| 558 | MockRead data_reads[] = { |
| 559 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 560 | MockRead("Content-Length: 10\r\n"), |
| 561 | MockRead("Content-Length: 5\r\n\r\n"), |
| 562 | }; |
| 563 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 564 | arraysize(data_reads)); |
| 565 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, out.rv); |
| 566 | } |
| 567 | |
| 568 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 569 | DuplicateContentLengthHeadersNoTransferEncoding) { |
| 570 | MockRead data_reads[] = { |
| 571 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 572 | MockRead("Content-Length: 5\r\n"), |
| 573 | MockRead("Content-Length: 5\r\n\r\n"), |
| 574 | MockRead("Hello"), |
| 575 | }; |
| 576 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 577 | arraysize(data_reads)); |
| 578 | EXPECT_EQ(OK, out.rv); |
| 579 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 580 | EXPECT_EQ("Hello", out.response_data); |
| 581 | } |
| 582 | |
| 583 | TEST_F(HttpNetworkTransactionTest, |
| 584 | ComplexContentLengthHeadersNoTransferEncoding) { |
| 585 | // More than 2 dupes. |
| 586 | { |
| 587 | MockRead data_reads[] = { |
| 588 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 589 | MockRead("Content-Length: 5\r\n"), |
| 590 | MockRead("Content-Length: 5\r\n"), |
| 591 | MockRead("Content-Length: 5\r\n\r\n"), |
| 592 | MockRead("Hello"), |
| 593 | }; |
| 594 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 595 | arraysize(data_reads)); |
| 596 | EXPECT_EQ(OK, out.rv); |
| 597 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 598 | EXPECT_EQ("Hello", out.response_data); |
| 599 | } |
| 600 | // HTTP/1.0 |
| 601 | { |
| 602 | MockRead data_reads[] = { |
| 603 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 604 | MockRead("Content-Length: 5\r\n"), |
| 605 | MockRead("Content-Length: 5\r\n"), |
| 606 | MockRead("Content-Length: 5\r\n\r\n"), |
| 607 | MockRead("Hello"), |
| 608 | }; |
| 609 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 610 | arraysize(data_reads)); |
| 611 | EXPECT_EQ(OK, out.rv); |
| 612 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 613 | EXPECT_EQ("Hello", out.response_data); |
| 614 | } |
| 615 | // 2 dupes and one mismatched. |
| 616 | { |
| 617 | MockRead data_reads[] = { |
| 618 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 619 | MockRead("Content-Length: 10\r\n"), |
| 620 | MockRead("Content-Length: 10\r\n"), |
| 621 | MockRead("Content-Length: 5\r\n\r\n"), |
| 622 | }; |
| 623 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 624 | arraysize(data_reads)); |
| 625 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, out.rv); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 630 | MultipleContentLengthHeadersTransferEncoding) { |
| 631 | MockRead data_reads[] = { |
| 632 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 633 | MockRead("Content-Length: 666\r\n"), |
| 634 | MockRead("Content-Length: 1337\r\n"), |
| 635 | MockRead("Transfer-Encoding: chunked\r\n\r\n"), |
| 636 | MockRead("5\r\nHello\r\n"), |
| 637 | MockRead("1\r\n"), |
| 638 | MockRead(" \r\n"), |
| 639 | MockRead("5\r\nworld\r\n"), |
| 640 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
| 641 | MockRead(false, OK), |
| 642 | }; |
| 643 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 644 | arraysize(data_reads)); |
| 645 | EXPECT_EQ(OK, out.rv); |
| 646 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 647 | EXPECT_EQ("Hello world", out.response_data); |
| 648 | } |
| 649 | |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 650 | // Next tests deal with http://crbug.com/98895. |
| 651 | |
| 652 | // Checks that a single Content-Disposition header results in no error. |
| 653 | TEST_F(HttpNetworkTransactionTest, SingleContentDispositionHeader) { |
| 654 | MockRead data_reads[] = { |
| 655 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 656 | MockRead("Content-Disposition: attachment;filename=\"salutations.txt\"r\n"), |
| 657 | MockRead("Content-Length: 5\r\n\r\n"), |
| 658 | MockRead("Hello"), |
| 659 | }; |
| 660 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 661 | arraysize(data_reads)); |
| 662 | EXPECT_EQ(OK, out.rv); |
| 663 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 664 | EXPECT_EQ("Hello", out.response_data); |
| 665 | } |
| 666 | |
| 667 | // Checks that two identical Content-Disposition headers result in an error. |
| 668 | TEST_F(HttpNetworkTransactionTest, |
| 669 | DuplicateIdenticalContentDispositionHeaders) { |
| 670 | MockRead data_reads[] = { |
| 671 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 672 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 673 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 674 | MockRead("Content-Length: 5\r\n\r\n"), |
| 675 | MockRead("Hello"), |
| 676 | }; |
| 677 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 678 | arraysize(data_reads)); |
| 679 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION, out.rv); |
| 680 | } |
| 681 | |
| 682 | // Checks that two distinct Content-Disposition headers result in an error. |
| 683 | TEST_F(HttpNetworkTransactionTest, DuplicateDistinctContentDispositionHeaders) { |
| 684 | MockRead data_reads[] = { |
| 685 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 686 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 687 | MockRead("Content-Disposition: attachment;filename=\"hi.txt\"r\n"), |
| 688 | MockRead("Content-Length: 5\r\n\r\n"), |
| 689 | MockRead("Hello"), |
| 690 | }; |
| 691 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 692 | arraysize(data_reads)); |
| 693 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION, out.rv); |
| 694 | } |
| 695 | |
| 696 | // Checks the behavior of a single Location header. |
| 697 | TEST_F(HttpNetworkTransactionTest, SingleLocationHeader) { |
| 698 | MockRead data_reads[] = { |
| 699 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 700 | MockRead("Location: http://good.com/\r\n"), |
| 701 | MockRead("Content-Length: 0\r\n\r\n"), |
| 702 | MockRead(false, OK), |
| 703 | }; |
| 704 | |
| 705 | HttpRequestInfo request; |
| 706 | request.method = "GET"; |
| 707 | request.url = GURL("http://redirect.com/"); |
| 708 | request.load_flags = 0; |
| 709 | |
| 710 | SessionDependencies session_deps; |
| 711 | scoped_ptr<HttpTransaction> trans( |
| 712 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 713 | |
| 714 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 715 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 716 | |
| 717 | TestOldCompletionCallback callback; |
| 718 | |
| 719 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 720 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 721 | |
| 722 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 723 | |
| 724 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 725 | ASSERT_TRUE(response != NULL && response->headers != NULL); |
| 726 | EXPECT_EQ("HTTP/1.1 302 Redirect", response->headers->GetStatusLine()); |
| 727 | std::string url; |
| 728 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 729 | EXPECT_EQ("http://good.com/", url); |
| 730 | } |
| 731 | |
| 732 | // Checks that two identical Location headers result in an error. |
| 733 | TEST_F(HttpNetworkTransactionTest, DuplicateIdenticalLocationHeaders) { |
| 734 | MockRead data_reads[] = { |
| 735 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 736 | MockRead("Location: http://good.com/\r\n"), |
| 737 | MockRead("Location: http://good.com/\r\n"), |
| 738 | MockRead("Content-Length: 0\r\n\r\n"), |
| 739 | MockRead(false, OK), |
| 740 | }; |
| 741 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 742 | arraysize(data_reads)); |
| 743 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION, out.rv); |
| 744 | } |
| 745 | |
| 746 | // Checks that two distinct Location headers result in an error. |
| 747 | TEST_F(HttpNetworkTransactionTest, DuplicateDistinctLocationHeaders) { |
| 748 | MockRead data_reads[] = { |
| 749 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 750 | MockRead("Location: http://good.com/\r\n"), |
| 751 | MockRead("Location: http://evil.com/\r\n"), |
| 752 | MockRead("Content-Length: 0\r\n\r\n"), |
| 753 | MockRead(false, OK), |
| 754 | }; |
| 755 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 756 | arraysize(data_reads)); |
| 757 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION, out.rv); |
| 758 | } |
| 759 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 760 | // Do a request using the HEAD method. Verify that we don't try to read the |
| 761 | // message body (since HEAD has none). |
| 762 | TEST_F(HttpNetworkTransactionTest, Head) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 763 | HttpRequestInfo request; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 764 | request.method = "HEAD"; |
| 765 | request.url = GURL("http://www.google.com/"); |
| 766 | request.load_flags = 0; |
| 767 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 768 | SessionDependencies session_deps; |
| 769 | scoped_ptr<HttpTransaction> trans( |
| 770 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 771 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 772 | MockWrite data_writes1[] = { |
| 773 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 774 | "Host: www.google.com\r\n" |
| 775 | "Connection: keep-alive\r\n" |
| 776 | "Content-Length: 0\r\n\r\n"), |
| 777 | }; |
| 778 | MockRead data_reads1[] = { |
| 779 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 780 | MockRead("Server: Blah\r\n"), |
| 781 | MockRead("Content-Length: 1234\r\n\r\n"), |
| 782 | |
| 783 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 784 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 785 | }; |
| 786 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 787 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 788 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 789 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 790 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 791 | TestOldCompletionCallback callback1; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 792 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 793 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 794 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 795 | |
| 796 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 797 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 798 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 799 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 800 | ASSERT_TRUE(response != NULL); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 801 | |
| 802 | // Check that the headers got parsed. |
| 803 | EXPECT_TRUE(response->headers != NULL); |
| 804 | EXPECT_EQ(1234, response->headers->GetContentLength()); |
| 805 | EXPECT_EQ("HTTP/1.1 404 Not Found", response->headers->GetStatusLine()); |
| 806 | |
| 807 | std::string server_header; |
| 808 | void* iter = NULL; |
| 809 | bool has_server_header = response->headers->EnumerateHeader( |
| 810 | &iter, "Server", &server_header); |
| 811 | EXPECT_TRUE(has_server_header); |
| 812 | EXPECT_EQ("Blah", server_header); |
| 813 | |
| 814 | // Reading should give EOF right away, since there is no message body |
| 815 | // (despite non-zero content-length). |
| 816 | std::string response_data; |
| 817 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 818 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 819 | EXPECT_EQ("", response_data); |
| 820 | } |
| 821 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 822 | TEST_F(HttpNetworkTransactionTest, ReuseConnection) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 823 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 824 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 825 | |
| 826 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 827 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 828 | MockRead("hello"), |
| 829 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 830 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 831 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 832 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 833 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 834 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 835 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 836 | const char* const kExpectedResponseData[] = { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 837 | "hello", "world" |
| 838 | }; |
| 839 | |
| 840 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 841 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 842 | request.method = "GET"; |
| 843 | request.url = GURL("http://www.google.com/"); |
| 844 | request.load_flags = 0; |
| 845 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 846 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 847 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 848 | TestOldCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 849 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 850 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 851 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 852 | |
| 853 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 854 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 855 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 856 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 857 | ASSERT_TRUE(response != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 858 | |
| 859 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 860 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 861 | |
| 862 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 863 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 864 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 865 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
| 869 | TEST_F(HttpNetworkTransactionTest, Ignores100) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 870 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 871 | request.method = "POST"; |
| 872 | request.url = GURL("http://www.foo.com/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 873 | request.upload_data = new UploadData; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 874 | request.upload_data->AppendBytes("foo", 3); |
| 875 | request.load_flags = 0; |
| 876 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 877 | SessionDependencies session_deps; |
| 878 | scoped_ptr<HttpTransaction> trans( |
| 879 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 880 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 881 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 882 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 883 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 884 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 885 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 886 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 887 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 888 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 889 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 890 | TestOldCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 891 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 892 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 893 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 894 | |
| 895 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 896 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 897 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 898 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 899 | ASSERT_TRUE(response != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 900 | |
| 901 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 902 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 903 | |
| 904 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 905 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 906 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 907 | EXPECT_EQ("hello world", response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 908 | } |
| 909 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 910 | // This test is almost the same as Ignores100 above, but the response contains |
| 911 | // 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] | 912 | // HTTP/1.1 and the two status headers are read in one read. |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 913 | TEST_F(HttpNetworkTransactionTest, Ignores1xx) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 914 | HttpRequestInfo request; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 915 | request.method = "GET"; |
| 916 | request.url = GURL("http://www.foo.com/"); |
| 917 | request.load_flags = 0; |
| 918 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 919 | SessionDependencies session_deps; |
| 920 | scoped_ptr<HttpTransaction> trans( |
| 921 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 922 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 923 | MockRead data_reads[] = { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 924 | MockRead("HTTP/1.1 102 Unspecified status code\r\n\r\n" |
| 925 | "HTTP/1.1 200 OK\r\n\r\n"), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 926 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 927 | MockRead(false, OK), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 928 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 929 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 930 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 931 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 932 | TestOldCompletionCallback callback; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 933 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 934 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 935 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 936 | |
| 937 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 938 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 939 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 940 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 941 | ASSERT_TRUE(response != NULL); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 942 | |
| 943 | EXPECT_TRUE(response->headers != NULL); |
| 944 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 945 | |
| 946 | std::string response_data; |
| 947 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 948 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 949 | EXPECT_EQ("hello world", response_data); |
| 950 | } |
| 951 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 952 | TEST_F(HttpNetworkTransactionTest, Incomplete100ThenEOF) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 953 | HttpRequestInfo request; |
| 954 | request.method = "POST"; |
| 955 | request.url = GURL("http://www.foo.com/"); |
| 956 | request.load_flags = 0; |
| 957 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 958 | SessionDependencies session_deps; |
| 959 | scoped_ptr<HttpTransaction> trans( |
| 960 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 961 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 962 | MockRead data_reads[] = { |
| 963 | MockRead(false, "HTTP/1.0 100 Continue\r\n"), |
| 964 | MockRead(true, 0), |
| 965 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 966 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 967 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 968 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 969 | TestOldCompletionCallback callback; |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 970 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 971 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 972 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 973 | |
| 974 | rv = callback.WaitForResult(); |
| 975 | EXPECT_EQ(OK, rv); |
| 976 | |
| 977 | std::string response_data; |
| 978 | rv = ReadTransaction(trans.get(), &response_data); |
| 979 | EXPECT_EQ(OK, rv); |
| 980 | EXPECT_EQ("", response_data); |
| 981 | } |
| 982 | |
| 983 | TEST_F(HttpNetworkTransactionTest, EmptyResponse) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 984 | HttpRequestInfo request; |
| 985 | request.method = "POST"; |
| 986 | request.url = GURL("http://www.foo.com/"); |
| 987 | request.load_flags = 0; |
| 988 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 989 | SessionDependencies session_deps; |
| 990 | scoped_ptr<HttpTransaction> trans( |
| 991 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 992 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 993 | MockRead data_reads[] = { |
| 994 | MockRead(true, 0), |
| 995 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 996 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 997 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 998 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 999 | TestOldCompletionCallback callback; |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1000 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1001 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1002 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1003 | |
| 1004 | rv = callback.WaitForResult(); |
| 1005 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
| 1006 | } |
| 1007 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1008 | // read_failure specifies a read failure that should cause the network |
| 1009 | // transaction to resend the request. |
| 1010 | void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( |
| 1011 | const MockRead& read_failure) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1012 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1013 | request.method = "GET"; |
| 1014 | request.url = GURL("http://www.foo.com/"); |
| 1015 | request.load_flags = 0; |
| 1016 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1017 | SessionDependencies session_deps; |
| 1018 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1019 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1020 | MockRead data1_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1021 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1022 | MockRead("hello"), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1023 | read_failure, // Now, we reuse the connection and fail the first read. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1024 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1025 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1026 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1027 | |
| 1028 | MockRead data2_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1029 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1030 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1031 | MockRead(true, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1032 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1033 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1034 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1035 | |
| 1036 | const char* kExpectedResponseData[] = { |
| 1037 | "hello", "world" |
| 1038 | }; |
| 1039 | |
| 1040 | for (int i = 0; i < 2; ++i) { |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1041 | TestOldCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1042 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1043 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1044 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1045 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1046 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1047 | |
| 1048 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1049 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1050 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1051 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1052 | ASSERT_TRUE(response != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1053 | |
| 1054 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1055 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1056 | |
| 1057 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1058 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1059 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1060 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1061 | } |
| 1062 | } |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1063 | |
| 1064 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1065 | MockRead read_failure(true, ERR_CONNECTION_RESET); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1066 | KeepAliveConnectionResendRequestTest(read_failure); |
| 1067 | } |
| 1068 | |
| 1069 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1070 | MockRead read_failure(false, OK); // EOF |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1071 | KeepAliveConnectionResendRequestTest(read_failure); |
| 1072 | } |
| 1073 | |
| 1074 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1075 | HttpRequestInfo request; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1076 | request.method = "GET"; |
| 1077 | request.url = GURL("http://www.google.com/"); |
| 1078 | request.load_flags = 0; |
| 1079 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1080 | SessionDependencies session_deps; |
| 1081 | scoped_ptr<HttpTransaction> trans( |
| 1082 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1083 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1084 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1085 | MockRead(true, ERR_CONNECTION_RESET), |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1086 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 1087 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1088 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1089 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1090 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1091 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1092 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1093 | TestOldCompletionCallback callback; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1094 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1095 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1096 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1097 | |
| 1098 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1099 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1100 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1101 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1102 | EXPECT_TRUE(response == NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | // What do various browsers do when the server closes a non-keepalive |
| 1106 | // connection without sending any response header or body? |
| 1107 | // |
| 1108 | // IE7: error page |
| 1109 | // Safari 3.1.2 (Windows): error page |
| 1110 | // Firefox 3.0.1: blank page |
| 1111 | // Opera 9.52: after five attempts, blank page |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1112 | // Us with WinHTTP: error page (ERR_INVALID_RESPONSE) |
| 1113 | // Us: error page (EMPTY_RESPONSE) |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1114 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) { |
| 1115 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1116 | MockRead(false, OK), // EOF |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1117 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 1118 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1119 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1120 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1121 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 1122 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1123 | EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1124 | } |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1125 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1126 | // Test that we correctly reuse a keep-alive connection after not explicitly |
| 1127 | // reading the body. |
| 1128 | TEST_F(HttpNetworkTransactionTest, KeepAliveAfterUnreadBody) { |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1129 | HttpRequestInfo request; |
| 1130 | request.method = "GET"; |
| 1131 | request.url = GURL("http://www.foo.com/"); |
| 1132 | request.load_flags = 0; |
| 1133 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1134 | SessionDependencies session_deps; |
| 1135 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1136 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1137 | // Note that because all these reads happen in the same |
| 1138 | // StaticSocketDataProvider, it shows that the same socket is being reused for |
| 1139 | // all transactions. |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1140 | MockRead data1_reads[] = { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1141 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 1142 | MockRead("HTTP/1.1 205 Reset Content\r\n\r\n"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1143 | MockRead("HTTP/1.1 304 Not Modified\r\n\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1144 | MockRead("HTTP/1.1 302 Found\r\n" |
| 1145 | "Content-Length: 0\r\n\r\n"), |
| 1146 | MockRead("HTTP/1.1 302 Found\r\n" |
| 1147 | "Content-Length: 5\r\n\r\n" |
| 1148 | "hello"), |
| 1149 | MockRead("HTTP/1.1 301 Moved Permanently\r\n" |
| 1150 | "Content-Length: 0\r\n\r\n"), |
| 1151 | MockRead("HTTP/1.1 301 Moved Permanently\r\n" |
| 1152 | "Content-Length: 5\r\n\r\n" |
| 1153 | "hello"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1154 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1155 | MockRead("hello"), |
| 1156 | }; |
| 1157 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); |
| 1158 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1159 | |
| 1160 | MockRead data2_reads[] = { |
| 1161 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
| 1162 | }; |
| 1163 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
| 1164 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 1165 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1166 | const int kNumUnreadBodies = arraysize(data1_reads) - 2; |
| 1167 | std::string response_lines[kNumUnreadBodies]; |
| 1168 | |
| 1169 | for (size_t i = 0; i < arraysize(data1_reads) - 2; ++i) { |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1170 | TestOldCompletionCallback callback; |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1171 | |
| 1172 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1173 | |
| 1174 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 1175 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1176 | |
| 1177 | rv = callback.WaitForResult(); |
| 1178 | EXPECT_EQ(OK, rv); |
| 1179 | |
| 1180 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1181 | ASSERT_TRUE(response != NULL); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1182 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1183 | ASSERT_TRUE(response->headers != NULL); |
| 1184 | response_lines[i] = response->headers->GetStatusLine(); |
| 1185 | |
| 1186 | // We intentionally don't read the response bodies. |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1187 | } |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1188 | |
| 1189 | const char* const kStatusLines[] = { |
| 1190 | "HTTP/1.1 204 No Content", |
| 1191 | "HTTP/1.1 205 Reset Content", |
| 1192 | "HTTP/1.1 304 Not Modified", |
| 1193 | "HTTP/1.1 302 Found", |
| 1194 | "HTTP/1.1 302 Found", |
| 1195 | "HTTP/1.1 301 Moved Permanently", |
| 1196 | "HTTP/1.1 301 Moved Permanently", |
| 1197 | }; |
| 1198 | |
| 1199 | COMPILE_ASSERT(kNumUnreadBodies == arraysize(kStatusLines), |
| 1200 | forgot_to_update_kStatusLines); |
| 1201 | |
| 1202 | for (int i = 0; i < kNumUnreadBodies; ++i) |
| 1203 | EXPECT_EQ(kStatusLines[i], response_lines[i]); |
| 1204 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1205 | TestOldCompletionCallback callback; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1206 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1207 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 1208 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1209 | rv = callback.WaitForResult(); |
| 1210 | EXPECT_EQ(OK, rv); |
| 1211 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1212 | ASSERT_TRUE(response != NULL); |
| 1213 | ASSERT_TRUE(response->headers != NULL); |
| 1214 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1215 | std::string response_data; |
| 1216 | rv = ReadTransaction(trans.get(), &response_data); |
| 1217 | EXPECT_EQ(OK, rv); |
| 1218 | EXPECT_EQ("hello", response_data); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1219 | } |
| 1220 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1221 | // Test the request-challenge-retry sequence for basic auth. |
| 1222 | // (basic auth is the easiest to mock, because it has no randomness). |
| 1223 | TEST_F(HttpNetworkTransactionTest, BasicAuth) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1224 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1225 | request.method = "GET"; |
| 1226 | request.url = GURL("http://www.google.com/"); |
| 1227 | request.load_flags = 0; |
| 1228 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1229 | SessionDependencies session_deps; |
| 1230 | scoped_ptr<HttpTransaction> trans( |
| 1231 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1232 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1233 | MockWrite data_writes1[] = { |
| 1234 | MockWrite("GET / HTTP/1.1\r\n" |
| 1235 | "Host: www.google.com\r\n" |
| 1236 | "Connection: keep-alive\r\n\r\n"), |
| 1237 | }; |
| 1238 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1239 | MockRead data_reads1[] = { |
| 1240 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1241 | // Give a couple authenticate options (only the middle one is actually |
| 1242 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 1243 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1244 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1245 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 1246 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1247 | // Large content-length -- won't matter, as connection will be reset. |
| 1248 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1249 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1250 | }; |
| 1251 | |
| 1252 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1253 | // be issuing -- the final header line contains the credentials. |
| 1254 | MockWrite data_writes2[] = { |
| 1255 | MockWrite("GET / HTTP/1.1\r\n" |
| 1256 | "Host: www.google.com\r\n" |
| 1257 | "Connection: keep-alive\r\n" |
| 1258 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1259 | }; |
| 1260 | |
| 1261 | // Lastly, the server responds with the actual content. |
| 1262 | MockRead data_reads2[] = { |
| 1263 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 1264 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1265 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1266 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1267 | }; |
| 1268 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1269 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1270 | data_writes1, arraysize(data_writes1)); |
| 1271 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1272 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1273 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1274 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1275 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1276 | TestOldCompletionCallback callback1; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1277 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1278 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1279 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1280 | |
| 1281 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1282 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1283 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1284 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1285 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1286 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1287 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1288 | TestOldCompletionCallback callback2; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1289 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1290 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1291 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1292 | |
| 1293 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1294 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1295 | |
| 1296 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1297 | ASSERT_TRUE(response != NULL); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1298 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1299 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1300 | } |
| 1301 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1302 | TEST_F(HttpNetworkTransactionTest, DoNotSendAuth) { |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1303 | HttpRequestInfo request; |
| 1304 | request.method = "GET"; |
| 1305 | request.url = GURL("http://www.google.com/"); |
| 1306 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1307 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1308 | SessionDependencies session_deps; |
| 1309 | scoped_ptr<HttpTransaction> trans( |
| 1310 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1311 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1312 | MockWrite data_writes[] = { |
| 1313 | MockWrite("GET / HTTP/1.1\r\n" |
| 1314 | "Host: www.google.com\r\n" |
| 1315 | "Connection: keep-alive\r\n\r\n"), |
| 1316 | }; |
| 1317 | |
| 1318 | MockRead data_reads[] = { |
| 1319 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1320 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1321 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1322 | // Large content-length -- won't matter, as connection will be reset. |
| 1323 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1324 | MockRead(false, ERR_FAILED), |
| 1325 | }; |
| 1326 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1327 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1328 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1329 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1330 | TestOldCompletionCallback callback; |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1331 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1332 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1333 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1334 | |
| 1335 | rv = callback.WaitForResult(); |
| 1336 | EXPECT_EQ(0, rv); |
| 1337 | |
| 1338 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1339 | ASSERT_TRUE(response != NULL); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1340 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1341 | } |
| 1342 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1343 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1344 | // connection. |
| 1345 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1346 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1347 | request.method = "GET"; |
| 1348 | request.url = GURL("http://www.google.com/"); |
| 1349 | request.load_flags = 0; |
| 1350 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1351 | SessionDependencies session_deps; |
| 1352 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1353 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1354 | MockWrite data_writes1[] = { |
| 1355 | MockWrite("GET / HTTP/1.1\r\n" |
| 1356 | "Host: www.google.com\r\n" |
| 1357 | "Connection: keep-alive\r\n\r\n"), |
| 1358 | |
| 1359 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1360 | // be issuing -- the final header line contains the credentials. |
| 1361 | MockWrite("GET / HTTP/1.1\r\n" |
| 1362 | "Host: www.google.com\r\n" |
| 1363 | "Connection: keep-alive\r\n" |
| 1364 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1365 | }; |
| 1366 | |
| 1367 | MockRead data_reads1[] = { |
| 1368 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1369 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1370 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1371 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1372 | MockRead("Unauthorized\r\n"), |
| 1373 | |
| 1374 | // Lastly, the server responds with the actual content. |
| 1375 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1376 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1377 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1378 | MockRead("Hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1379 | }; |
| 1380 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1381 | // If there is a regression where we disconnect a Keep-Alive |
| 1382 | // connection during an auth roundtrip, we'll end up reading this. |
| 1383 | MockRead data_reads2[] = { |
| 1384 | MockRead(false, ERR_FAILED), |
| 1385 | }; |
| 1386 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1387 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1388 | data_writes1, arraysize(data_writes1)); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1389 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1390 | NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1391 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1392 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1393 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1394 | TestOldCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1395 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1396 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1397 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1398 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1399 | |
| 1400 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1401 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1402 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1403 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1404 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1405 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1406 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1407 | TestOldCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1408 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1409 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1410 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1411 | |
| 1412 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1413 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1414 | |
| 1415 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1416 | ASSERT_TRUE(response != NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1417 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1418 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1422 | // connection and with no response body to drain. |
| 1423 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1424 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1425 | request.method = "GET"; |
| 1426 | request.url = GURL("http://www.google.com/"); |
| 1427 | request.load_flags = 0; |
| 1428 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1429 | SessionDependencies session_deps; |
| 1430 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1431 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1432 | MockWrite data_writes1[] = { |
| 1433 | MockWrite("GET / HTTP/1.1\r\n" |
| 1434 | "Host: www.google.com\r\n" |
| 1435 | "Connection: keep-alive\r\n\r\n"), |
| 1436 | |
| 1437 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1438 | // be issuing -- the final header line contains the credentials. |
| 1439 | MockWrite("GET / HTTP/1.1\r\n" |
| 1440 | "Host: www.google.com\r\n" |
| 1441 | "Connection: keep-alive\r\n" |
| 1442 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1443 | }; |
| 1444 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1445 | MockRead data_reads1[] = { |
| 1446 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1447 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1448 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1449 | |
| 1450 | // Lastly, the server responds with the actual content. |
| 1451 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1452 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1453 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1454 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1455 | }; |
| 1456 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1457 | // An incorrect reconnect would cause this to be read. |
| 1458 | MockRead data_reads2[] = { |
| 1459 | MockRead(false, ERR_FAILED), |
| 1460 | }; |
| 1461 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1462 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1463 | data_writes1, arraysize(data_writes1)); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1464 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1465 | NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1466 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1467 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1468 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1469 | TestOldCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1470 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1471 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1472 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1473 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1474 | |
| 1475 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1476 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1477 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1478 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1479 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1480 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1481 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1482 | TestOldCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1483 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1484 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1485 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1486 | |
| 1487 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1488 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1489 | |
| 1490 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1491 | ASSERT_TRUE(response != NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1492 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1493 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1497 | // connection and with a large response body to drain. |
| 1498 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1499 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1500 | request.method = "GET"; |
| 1501 | request.url = GURL("http://www.google.com/"); |
| 1502 | request.load_flags = 0; |
| 1503 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1504 | SessionDependencies session_deps; |
| 1505 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1506 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1507 | MockWrite data_writes1[] = { |
| 1508 | MockWrite("GET / HTTP/1.1\r\n" |
| 1509 | "Host: www.google.com\r\n" |
| 1510 | "Connection: keep-alive\r\n\r\n"), |
| 1511 | |
| 1512 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1513 | // be issuing -- the final header line contains the credentials. |
| 1514 | MockWrite("GET / HTTP/1.1\r\n" |
| 1515 | "Host: www.google.com\r\n" |
| 1516 | "Connection: keep-alive\r\n" |
| 1517 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1518 | }; |
| 1519 | |
| 1520 | // Respond with 5 kb of response body. |
| 1521 | std::string large_body_string("Unauthorized"); |
| 1522 | large_body_string.append(5 * 1024, ' '); |
| 1523 | large_body_string.append("\r\n"); |
| 1524 | |
| 1525 | MockRead data_reads1[] = { |
| 1526 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1527 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1528 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1529 | // 5134 = 12 + 5 * 1024 + 2 |
| 1530 | MockRead("Content-Length: 5134\r\n\r\n"), |
| 1531 | MockRead(true, large_body_string.data(), large_body_string.size()), |
| 1532 | |
| 1533 | // Lastly, the server responds with the actual content. |
| 1534 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1535 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1536 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1537 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1538 | }; |
| 1539 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1540 | // An incorrect reconnect would cause this to be read. |
| 1541 | MockRead data_reads2[] = { |
| 1542 | MockRead(false, ERR_FAILED), |
| 1543 | }; |
| 1544 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1545 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1546 | data_writes1, arraysize(data_writes1)); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1547 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1548 | NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1549 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1550 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1551 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1552 | TestOldCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1553 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1554 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1555 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1556 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1557 | |
| 1558 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1559 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1560 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1561 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1562 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1563 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1564 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1565 | TestOldCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1566 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1567 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1568 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1569 | |
| 1570 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1571 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1572 | |
| 1573 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1574 | ASSERT_TRUE(response != NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1575 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1576 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1580 | // connection, but the server gets impatient and closes the connection. |
| 1581 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveImpatientServer) { |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1582 | HttpRequestInfo request; |
| 1583 | request.method = "GET"; |
| 1584 | request.url = GURL("http://www.google.com/"); |
| 1585 | request.load_flags = 0; |
| 1586 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1587 | SessionDependencies session_deps; |
| 1588 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1589 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1590 | MockWrite data_writes1[] = { |
| 1591 | MockWrite("GET / HTTP/1.1\r\n" |
| 1592 | "Host: www.google.com\r\n" |
| 1593 | "Connection: keep-alive\r\n\r\n"), |
| 1594 | // This simulates the seemingly successful write to a closed connection |
| 1595 | // if the bug is not fixed. |
| 1596 | MockWrite("GET / HTTP/1.1\r\n" |
| 1597 | "Host: www.google.com\r\n" |
| 1598 | "Connection: keep-alive\r\n" |
| 1599 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1600 | }; |
| 1601 | |
| 1602 | MockRead data_reads1[] = { |
| 1603 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1604 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1605 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1606 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1607 | // Tell MockTCPClientSocket to simulate the server closing the connection. |
| 1608 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 1609 | MockRead("Unauthorized\r\n"), |
| 1610 | MockRead(false, OK), // The server closes the connection. |
| 1611 | }; |
| 1612 | |
| 1613 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1614 | // be issuing -- the final header line contains the credentials. |
| 1615 | MockWrite data_writes2[] = { |
| 1616 | MockWrite("GET / HTTP/1.1\r\n" |
| 1617 | "Host: www.google.com\r\n" |
| 1618 | "Connection: keep-alive\r\n" |
| 1619 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1620 | }; |
| 1621 | |
| 1622 | // Lastly, the server responds with the actual content. |
| 1623 | MockRead data_reads2[] = { |
| 1624 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1625 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1626 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1627 | MockRead("hello"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1628 | }; |
| 1629 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1630 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1631 | data_writes1, arraysize(data_writes1)); |
| 1632 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1633 | data_writes2, arraysize(data_writes2)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1634 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1635 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 1636 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1637 | TestOldCompletionCallback callback1; |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1638 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1639 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1640 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1641 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1642 | |
| 1643 | rv = callback1.WaitForResult(); |
| 1644 | EXPECT_EQ(OK, rv); |
| 1645 | |
| 1646 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1647 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1648 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1649 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1650 | TestOldCompletionCallback callback2; |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1651 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1652 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1653 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1654 | |
| 1655 | rv = callback2.WaitForResult(); |
| 1656 | EXPECT_EQ(OK, rv); |
| 1657 | |
| 1658 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1659 | ASSERT_TRUE(response != NULL); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1660 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1661 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1662 | } |
| 1663 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1664 | // Test the request-challenge-retry sequence for basic auth, over a connection |
| 1665 | // that requires a restart when setting up an SSL tunnel. |
| 1666 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyNoKeepAlive) { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1667 | HttpRequestInfo request; |
| 1668 | request.method = "GET"; |
| 1669 | request.url = GURL("https://www.google.com/"); |
| 1670 | // when the no authentication data flag is set. |
| 1671 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1672 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1673 | // Configure against proxy server "myproxy:70". |
| 1674 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 1675 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1676 | session_deps.net_log = log.bound().net_log(); |
| 1677 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1678 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1679 | // Since we have proxy, should try to establish tunnel. |
| 1680 | MockWrite data_writes1[] = { |
| 1681 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1682 | "Host: www.google.com\r\n" |
| 1683 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1684 | |
| 1685 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1686 | // be issuing -- the final header line contains the credentials. |
| 1687 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1688 | "Host: www.google.com\r\n" |
| 1689 | "Proxy-Connection: keep-alive\r\n" |
| 1690 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1691 | |
| 1692 | MockWrite("GET / HTTP/1.1\r\n" |
| 1693 | "Host: www.google.com\r\n" |
| 1694 | "Connection: keep-alive\r\n\r\n"), |
| 1695 | }; |
| 1696 | |
| 1697 | // The proxy responds to the connect with a 407, using a persistent |
| 1698 | // connection. |
| 1699 | MockRead data_reads1[] = { |
| 1700 | // No credentials. |
| 1701 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1702 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1703 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 1704 | |
| 1705 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 1706 | |
| 1707 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1708 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1709 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1710 | MockRead(false, "hello"), |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1711 | }; |
| 1712 | |
| 1713 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1714 | data_writes1, arraysize(data_writes1)); |
| 1715 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1716 | SSLSocketDataProvider ssl(true, OK); |
| 1717 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1718 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1719 | TestOldCompletionCallback callback1; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1720 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1721 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1722 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1723 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1724 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1725 | |
| 1726 | rv = callback1.WaitForResult(); |
| 1727 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1728 | net::CapturingNetLog::EntryList entries; |
| 1729 | log.GetEntries(&entries); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1730 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1731 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1732 | NetLog::PHASE_NONE); |
| 1733 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1734 | entries, pos, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1735 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1736 | NetLog::PHASE_NONE); |
| 1737 | |
| 1738 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1739 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1740 | ASSERT_FALSE(response->headers == NULL); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1741 | EXPECT_EQ(407, response->headers->response_code()); |
| 1742 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1743 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1744 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1745 | TestOldCompletionCallback callback2; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1746 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1747 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1748 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1749 | |
| 1750 | rv = callback2.WaitForResult(); |
| 1751 | EXPECT_EQ(OK, rv); |
| 1752 | |
| 1753 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1754 | ASSERT_TRUE(response != NULL); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1755 | |
| 1756 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1757 | EXPECT_EQ(200, response->headers->response_code()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1758 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1759 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1760 | |
| 1761 | // The password prompt info should not be set. |
| 1762 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1763 | |
| 1764 | trans.reset(); |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 1765 | session->CloseAllConnections(); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1766 | } |
| 1767 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1768 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1769 | // proxy connection, when setting up an SSL tunnel. |
| 1770 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1771 | HttpRequestInfo request; |
| 1772 | request.method = "GET"; |
| 1773 | request.url = GURL("https://www.google.com/"); |
| 1774 | // Ensure that proxy authentication is attempted even |
| 1775 | // when the no authentication data flag is set. |
| 1776 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1777 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1778 | // Configure against proxy server "myproxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 1779 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1780 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1781 | session_deps.net_log = log.bound().net_log(); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1782 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1783 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1784 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1785 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1786 | // Since we have proxy, should try to establish tunnel. |
| 1787 | MockWrite data_writes1[] = { |
| 1788 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1789 | "Host: www.google.com\r\n" |
| 1790 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1791 | |
| 1792 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1793 | // be issuing -- the final header line contains the credentials. |
| 1794 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1795 | "Host: www.google.com\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1796 | "Proxy-Connection: keep-alive\r\n" |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1797 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 1798 | }; |
| 1799 | |
| 1800 | // The proxy responds to the connect with a 407, using a persistent |
| 1801 | // connection. |
| 1802 | MockRead data_reads1[] = { |
| 1803 | // No credentials. |
| 1804 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1805 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1806 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1807 | MockRead("0123456789"), |
| 1808 | |
| 1809 | // Wrong credentials (wrong password). |
| 1810 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1811 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1812 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1813 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1814 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1815 | }; |
| 1816 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1817 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1818 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1819 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1820 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1821 | TestOldCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1822 | |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1823 | int rv = trans->Start(&request, &callback1, log.bound()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1824 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1825 | |
| 1826 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1827 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1828 | net::CapturingNetLog::EntryList entries; |
| 1829 | log.GetEntries(&entries); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1830 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1831 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1832 | NetLog::PHASE_NONE); |
| 1833 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1834 | entries, pos, |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1835 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1836 | NetLog::PHASE_NONE); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1837 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1838 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1839 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1840 | ASSERT_FALSE(response->headers == NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1841 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1842 | EXPECT_EQ(407, response->headers->response_code()); |
| 1843 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1844 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1845 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1846 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1847 | TestOldCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1848 | |
| 1849 | // Wrong password (should be "bar"). |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1850 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBaz), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1851 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1852 | |
| 1853 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1854 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1855 | |
| 1856 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1857 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1858 | ASSERT_FALSE(response->headers == NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1859 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1860 | EXPECT_EQ(407, response->headers->response_code()); |
| 1861 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1862 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1863 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1864 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1865 | // Flush the idle socket before the NetLog and HttpNetworkTransaction go |
| 1866 | // out of scope. |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 1867 | session->CloseAllConnections(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1868 | } |
| 1869 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1870 | // Test that we don't read the response body when we fail to establish a tunnel, |
| 1871 | // even if the user cancels the proxy's auth attempt. |
| 1872 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1873 | HttpRequestInfo request; |
| 1874 | request.method = "GET"; |
| 1875 | request.url = GURL("https://www.google.com/"); |
| 1876 | request.load_flags = 0; |
| 1877 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1878 | // Configure against proxy server "myproxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 1879 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1880 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1881 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1882 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1883 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1884 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1885 | // Since we have proxy, should try to establish tunnel. |
| 1886 | MockWrite data_writes[] = { |
| 1887 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1888 | "Host: www.google.com\r\n" |
| 1889 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1890 | }; |
| 1891 | |
| 1892 | // The proxy responds to the connect with a 407. |
| 1893 | MockRead data_reads[] = { |
| 1894 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1895 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1896 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1897 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1898 | }; |
| 1899 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1900 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1901 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1902 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1903 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1904 | TestOldCompletionCallback callback; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1905 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1906 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1907 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1908 | |
| 1909 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1910 | EXPECT_EQ(OK, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1911 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1912 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1913 | ASSERT_TRUE(response != NULL); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1914 | |
| 1915 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1916 | EXPECT_EQ(407, response->headers->response_code()); |
| 1917 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1918 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1919 | |
| 1920 | std::string response_data; |
| 1921 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1922 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1923 | |
| 1924 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 1925 | session->CloseAllConnections(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1926 | } |
| 1927 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1928 | // Test when a server (non-proxy) returns a 407 (proxy-authenticate). |
| 1929 | // The request should fail with ERR_UNEXPECTED_PROXY_AUTH. |
| 1930 | TEST_F(HttpNetworkTransactionTest, UnexpectedProxyAuth) { |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1931 | HttpRequestInfo request; |
| 1932 | request.method = "GET"; |
| 1933 | request.url = GURL("http://www.google.com/"); |
| 1934 | request.load_flags = 0; |
| 1935 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1936 | // We are using a DIRECT connection (i.e. no proxy) for this session. |
| 1937 | SessionDependencies session_deps; |
| 1938 | scoped_ptr<HttpTransaction> trans( |
| 1939 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1940 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1941 | MockWrite data_writes1[] = { |
| 1942 | MockWrite("GET / HTTP/1.1\r\n" |
| 1943 | "Host: www.google.com\r\n" |
| 1944 | "Connection: keep-alive\r\n\r\n"), |
| 1945 | }; |
| 1946 | |
| 1947 | MockRead data_reads1[] = { |
| 1948 | MockRead("HTTP/1.0 407 Proxy Auth required\r\n"), |
| 1949 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1950 | // Large content-length -- won't matter, as connection will be reset. |
| 1951 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1952 | MockRead(false, ERR_FAILED), |
| 1953 | }; |
| 1954 | |
| 1955 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1956 | data_writes1, arraysize(data_writes1)); |
| 1957 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1958 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 1959 | TestOldCompletionCallback callback; |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1960 | |
| 1961 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 1962 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1963 | |
| 1964 | rv = callback.WaitForResult(); |
| 1965 | EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); |
| 1966 | } |
| 1967 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1968 | // Tests when an HTTPS server (non-proxy) returns a 407 (proxy-authentication) |
| 1969 | // through a non-authenticating proxy. The request should fail with |
| 1970 | // ERR_UNEXPECTED_PROXY_AUTH. |
| 1971 | // Note that it is impossible to detect if an HTTP server returns a 407 through |
| 1972 | // a non-authenticating proxy - there is nothing to indicate whether the |
| 1973 | // response came from the proxy or the server, so it is treated as if the proxy |
| 1974 | // issued the challenge. |
| 1975 | TEST_F(HttpNetworkTransactionTest, HttpsServerRequestsProxyAuthThroughProxy) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1976 | HttpRequestInfo request; |
| 1977 | request.method = "GET"; |
| 1978 | request.url = GURL("https://www.google.com/"); |
| 1979 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1980 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 1981 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1982 | session_deps.net_log = log.bound().net_log(); |
| 1983 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1984 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1985 | // Since we have proxy, should try to establish tunnel. |
| 1986 | MockWrite data_writes1[] = { |
| 1987 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1988 | "Host: www.google.com\r\n" |
| 1989 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1990 | |
| 1991 | MockWrite("GET / HTTP/1.1\r\n" |
| 1992 | "Host: www.google.com\r\n" |
| 1993 | "Connection: keep-alive\r\n\r\n"), |
| 1994 | }; |
| 1995 | |
| 1996 | MockRead data_reads1[] = { |
| 1997 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 1998 | |
| 1999 | MockRead("HTTP/1.1 407 Unauthorized\r\n"), |
| 2000 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2001 | MockRead("\r\n"), |
| 2002 | MockRead(false, OK), |
| 2003 | }; |
| 2004 | |
| 2005 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2006 | data_writes1, arraysize(data_writes1)); |
| 2007 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2008 | SSLSocketDataProvider ssl(true, OK); |
| 2009 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2010 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2011 | TestOldCompletionCallback callback1; |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2012 | |
| 2013 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2014 | |
| 2015 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2016 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2017 | |
| 2018 | rv = callback1.WaitForResult(); |
| 2019 | EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2020 | net::CapturingNetLog::EntryList entries; |
| 2021 | log.GetEntries(&entries); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2022 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2023 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2024 | NetLog::PHASE_NONE); |
| 2025 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2026 | entries, pos, |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2027 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 2028 | NetLog::PHASE_NONE); |
| 2029 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2030 | |
| 2031 | // Test a simple get through an HTTPS Proxy. |
| 2032 | TEST_F(HttpNetworkTransactionTest, HttpsProxyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2033 | HttpRequestInfo request; |
| 2034 | request.method = "GET"; |
| 2035 | request.url = GURL("http://www.google.com/"); |
| 2036 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2037 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2038 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2039 | "https://proxy:70")); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2040 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2041 | session_deps.net_log = log.bound().net_log(); |
| 2042 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2043 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2044 | // Since we have proxy, should use full url |
| 2045 | MockWrite data_writes1[] = { |
| 2046 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2047 | "Host: www.google.com\r\n" |
| 2048 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2049 | }; |
| 2050 | |
| 2051 | MockRead data_reads1[] = { |
| 2052 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2053 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2054 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2055 | MockRead(false, OK), |
| 2056 | }; |
| 2057 | |
| 2058 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2059 | data_writes1, arraysize(data_writes1)); |
| 2060 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2061 | SSLSocketDataProvider ssl(true, OK); |
| 2062 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2063 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2064 | TestOldCompletionCallback callback1; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2065 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2066 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2067 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2068 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2069 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2070 | |
| 2071 | rv = callback1.WaitForResult(); |
| 2072 | EXPECT_EQ(OK, rv); |
| 2073 | |
| 2074 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2075 | ASSERT_TRUE(response != NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2076 | |
| 2077 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 2078 | EXPECT_EQ(200, response->headers->response_code()); |
| 2079 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2080 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 2081 | |
| 2082 | // The password prompt info should not be set. |
| 2083 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2084 | } |
| 2085 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2086 | // Test a SPDY get through an HTTPS Proxy. |
| 2087 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2088 | HttpRequestInfo request; |
| 2089 | request.method = "GET"; |
| 2090 | request.url = GURL("http://www.google.com/"); |
| 2091 | request.load_flags = 0; |
| 2092 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2093 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2094 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2095 | "https://proxy:70")); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2096 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2097 | session_deps.net_log = log.bound().net_log(); |
| 2098 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2099 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2100 | // fetch http://www.google.com/ via SPDY |
| 2101 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST, |
| 2102 | false)); |
| 2103 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
| 2104 | |
| 2105 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2106 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 2107 | MockRead spdy_reads[] = { |
| 2108 | CreateMockRead(*resp), |
| 2109 | CreateMockRead(*data), |
| 2110 | MockRead(true, 0, 0), |
| 2111 | }; |
| 2112 | |
| 2113 | scoped_refptr<DelayedSocketData> spdy_data( |
| 2114 | new DelayedSocketData( |
| 2115 | 1, // wait for one write to finish before reading. |
| 2116 | spdy_reads, arraysize(spdy_reads), |
| 2117 | spdy_writes, arraysize(spdy_writes))); |
| 2118 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 2119 | |
| 2120 | SSLSocketDataProvider ssl(true, OK); |
| 2121 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2122 | ssl.next_proto = "spdy/2"; |
| 2123 | ssl.was_npn_negotiated = true; |
| 2124 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2125 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2126 | TestOldCompletionCallback callback1; |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2127 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2128 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2129 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2130 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2131 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2132 | |
| 2133 | rv = callback1.WaitForResult(); |
| 2134 | EXPECT_EQ(OK, rv); |
| 2135 | |
| 2136 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2137 | ASSERT_TRUE(response != NULL); |
| 2138 | ASSERT_TRUE(response->headers != NULL); |
| 2139 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2140 | |
| 2141 | std::string response_data; |
| 2142 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 2143 | EXPECT_EQ(net::kUploadData, response_data); |
| 2144 | } |
| 2145 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2146 | // Test a SPDY get through an HTTPS Proxy. |
| 2147 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGetWithProxyAuth) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2148 | HttpRequestInfo request; |
| 2149 | request.method = "GET"; |
| 2150 | request.url = GURL("http://www.google.com/"); |
| 2151 | request.load_flags = 0; |
| 2152 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2153 | // Configure against https proxy server "myproxy:70". |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2154 | SessionDependencies session_deps( |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2155 | ProxyService::CreateFixed("https://myproxy:70")); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2156 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2157 | session_deps.net_log = log.bound().net_log(); |
| 2158 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2159 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2160 | // The first request will be a bare GET, the second request will be a |
| 2161 | // GET with a Proxy-Authorization header. |
| 2162 | scoped_ptr<spdy::SpdyFrame> req_get( |
| 2163 | ConstructSpdyGet(NULL, 0, false, 1, LOWEST, false)); |
| 2164 | const char* const kExtraAuthorizationHeaders[] = { |
| 2165 | "proxy-authorization", |
| 2166 | "Basic Zm9vOmJhcg==", |
| 2167 | }; |
| 2168 | scoped_ptr<spdy::SpdyFrame> req_get_authorization( |
| 2169 | ConstructSpdyGet( |
| 2170 | kExtraAuthorizationHeaders, arraysize(kExtraAuthorizationHeaders)/2, |
| 2171 | false, 3, LOWEST, false)); |
| 2172 | MockWrite spdy_writes[] = { |
| 2173 | CreateMockWrite(*req_get, 1), |
| 2174 | CreateMockWrite(*req_get_authorization, 4), |
| 2175 | }; |
| 2176 | |
| 2177 | // The first response is a 407 proxy authentication challenge, and the second |
| 2178 | // response will be a 200 response since the second request includes a valid |
| 2179 | // Authorization header. |
| 2180 | const char* const kExtraAuthenticationHeaders[] = { |
| 2181 | "Proxy-Authenticate", |
| 2182 | "Basic realm=\"MyRealm1\"" |
| 2183 | }; |
| 2184 | scoped_ptr<spdy::SpdyFrame> resp_authentication( |
| 2185 | ConstructSpdySynReplyError( |
| 2186 | "407 Proxy Authentication Required", |
| 2187 | kExtraAuthenticationHeaders, arraysize(kExtraAuthenticationHeaders)/2, |
| 2188 | 1)); |
| 2189 | scoped_ptr<spdy::SpdyFrame> body_authentication( |
| 2190 | ConstructSpdyBodyFrame(1, true)); |
| 2191 | scoped_ptr<spdy::SpdyFrame> resp_data(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 2192 | scoped_ptr<spdy::SpdyFrame> body_data(ConstructSpdyBodyFrame(3, true)); |
| 2193 | MockRead spdy_reads[] = { |
| 2194 | CreateMockRead(*resp_authentication, 2), |
| 2195 | CreateMockRead(*body_authentication, 3), |
| 2196 | CreateMockRead(*resp_data, 5), |
| 2197 | CreateMockRead(*body_data, 6), |
| 2198 | MockRead(true, 0, 7), |
| 2199 | }; |
| 2200 | |
| 2201 | scoped_refptr<OrderedSocketData> data( |
| 2202 | new OrderedSocketData(spdy_reads, arraysize(spdy_reads), |
| 2203 | spdy_writes, arraysize(spdy_writes))); |
| 2204 | session_deps.socket_factory.AddSocketDataProvider(data); |
| 2205 | |
| 2206 | SSLSocketDataProvider ssl(true, OK); |
| 2207 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2208 | ssl.next_proto = "spdy/2"; |
| 2209 | ssl.was_npn_negotiated = true; |
| 2210 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2211 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2212 | TestOldCompletionCallback callback1; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2213 | |
| 2214 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2215 | |
| 2216 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2217 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2218 | |
| 2219 | rv = callback1.WaitForResult(); |
| 2220 | EXPECT_EQ(OK, rv); |
| 2221 | |
| 2222 | const HttpResponseInfo* const response = trans->GetResponseInfo(); |
| 2223 | |
| 2224 | ASSERT_TRUE(response != NULL); |
| 2225 | ASSERT_TRUE(response->headers != NULL); |
| 2226 | EXPECT_EQ(407, response->headers->response_code()); |
| 2227 | EXPECT_TRUE(response->was_fetched_via_spdy); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2228 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2229 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2230 | TestOldCompletionCallback callback2; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2231 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2232 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2233 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2234 | |
| 2235 | rv = callback2.WaitForResult(); |
| 2236 | EXPECT_EQ(OK, rv); |
| 2237 | |
| 2238 | const HttpResponseInfo* const response_restart = trans->GetResponseInfo(); |
| 2239 | |
| 2240 | ASSERT_TRUE(response_restart != NULL); |
| 2241 | ASSERT_TRUE(response_restart->headers != NULL); |
| 2242 | EXPECT_EQ(200, response_restart->headers->response_code()); |
| 2243 | // The password prompt info should not be set. |
| 2244 | EXPECT_TRUE(response_restart->auth_challenge.get() == NULL); |
| 2245 | } |
| 2246 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2247 | // Test a SPDY CONNECT through an HTTPS Proxy to an HTTPS (non-SPDY) Server. |
| 2248 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectHttps) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2249 | HttpRequestInfo request; |
| 2250 | request.method = "GET"; |
| 2251 | request.url = GURL("https://www.google.com/"); |
| 2252 | request.load_flags = 0; |
| 2253 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2254 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2255 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2256 | "https://proxy:70")); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2257 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2258 | session_deps.net_log = log.bound().net_log(); |
| 2259 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2260 | |
| 2261 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2262 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2263 | // CONNECT to www.google.com:443 via SPDY |
| 2264 | scoped_ptr<spdy::SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 2265 | // fetch https://www.google.com/ via HTTP |
| 2266 | |
| 2267 | const char get[] = "GET / HTTP/1.1\r\n" |
| 2268 | "Host: www.google.com\r\n" |
| 2269 | "Connection: keep-alive\r\n\r\n"; |
| 2270 | scoped_ptr<spdy::SpdyFrame> wrapped_get( |
| 2271 | ConstructSpdyBodyFrame(1, get, strlen(get), false)); |
| 2272 | MockWrite spdy_writes[] = { |
| 2273 | CreateMockWrite(*connect, 1), |
| 2274 | CreateMockWrite(*wrapped_get, 3) |
| 2275 | }; |
| 2276 | |
| 2277 | scoped_ptr<spdy::SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2278 | const char resp[] = "HTTP/1.1 200 OK\r\n" |
| 2279 | "Content-Length: 10\r\n\r\n"; |
| 2280 | |
| 2281 | scoped_ptr<spdy::SpdyFrame> wrapped_get_resp( |
| 2282 | ConstructSpdyBodyFrame(1, resp, strlen(resp), false)); |
| 2283 | scoped_ptr<spdy::SpdyFrame> wrapped_body( |
| 2284 | ConstructSpdyBodyFrame(1, "1234567890", 10, false)); |
| 2285 | MockRead spdy_reads[] = { |
| 2286 | CreateMockRead(*conn_resp, 2, true), |
| 2287 | CreateMockRead(*wrapped_get_resp, 4, true), |
| 2288 | CreateMockRead(*wrapped_body, 5, true), |
| 2289 | CreateMockRead(*wrapped_body, 6, true), |
| 2290 | MockRead(true, 0, 7), |
| 2291 | }; |
| 2292 | |
| 2293 | scoped_refptr<OrderedSocketData> spdy_data( |
| 2294 | new OrderedSocketData( |
| 2295 | spdy_reads, arraysize(spdy_reads), |
| 2296 | spdy_writes, arraysize(spdy_writes))); |
| 2297 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 2298 | |
| 2299 | SSLSocketDataProvider ssl(true, OK); |
| 2300 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2301 | ssl.next_proto = "spdy/2"; |
| 2302 | ssl.was_npn_negotiated = true; |
| 2303 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2304 | SSLSocketDataProvider ssl2(true, OK); |
| 2305 | ssl2.was_npn_negotiated = false; |
| 2306 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2307 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2308 | TestOldCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2309 | |
| 2310 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2311 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2312 | |
| 2313 | rv = callback1.WaitForResult(); |
| 2314 | EXPECT_EQ(OK, rv); |
| 2315 | |
| 2316 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2317 | ASSERT_TRUE(response != NULL); |
| 2318 | ASSERT_TRUE(response->headers != NULL); |
| 2319 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2320 | |
| 2321 | std::string response_data; |
| 2322 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 2323 | EXPECT_EQ("1234567890", response_data); |
| 2324 | } |
| 2325 | |
| 2326 | // Test a SPDY CONNECT through an HTTPS Proxy to a SPDY server. |
| 2327 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectSpdy) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2328 | HttpRequestInfo request; |
| 2329 | request.method = "GET"; |
| 2330 | request.url = GURL("https://www.google.com/"); |
| 2331 | request.load_flags = 0; |
| 2332 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2333 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2334 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2335 | "https://proxy:70")); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2336 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2337 | session_deps.net_log = log.bound().net_log(); |
| 2338 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2339 | |
| 2340 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2341 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2342 | // CONNECT to www.google.com:443 via SPDY |
| 2343 | scoped_ptr<spdy::SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 2344 | // fetch https://www.google.com/ via SPDY |
| 2345 | const char* const kMyUrl = "https://www.google.com/"; |
| 2346 | scoped_ptr<spdy::SpdyFrame> get(ConstructSpdyGet(kMyUrl, false, 1, LOWEST)); |
| 2347 | scoped_ptr<spdy::SpdyFrame> wrapped_get(ConstructWrappedSpdyFrame(get, 1)); |
| 2348 | MockWrite spdy_writes[] = { |
| 2349 | CreateMockWrite(*connect, 1), |
| 2350 | CreateMockWrite(*wrapped_get, 3) |
| 2351 | }; |
| 2352 | |
| 2353 | scoped_ptr<spdy::SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2354 | scoped_ptr<spdy::SpdyFrame> get_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2355 | scoped_ptr<spdy::SpdyFrame> wrapped_get_resp( |
| 2356 | ConstructWrappedSpdyFrame(get_resp, 1)); |
| 2357 | scoped_ptr<spdy::SpdyFrame> body(ConstructSpdyBodyFrame(1, true)); |
| 2358 | scoped_ptr<spdy::SpdyFrame> wrapped_body(ConstructWrappedSpdyFrame(body, 1)); |
| 2359 | MockRead spdy_reads[] = { |
| 2360 | CreateMockRead(*conn_resp, 2, true), |
| 2361 | CreateMockRead(*wrapped_get_resp, 4, true), |
| 2362 | CreateMockRead(*wrapped_body, 5, true), |
| 2363 | MockRead(true, 0, 1), |
| 2364 | }; |
| 2365 | |
| 2366 | scoped_refptr<OrderedSocketData> spdy_data( |
| 2367 | new OrderedSocketData( |
| 2368 | spdy_reads, arraysize(spdy_reads), |
| 2369 | spdy_writes, arraysize(spdy_writes))); |
| 2370 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 2371 | |
| 2372 | SSLSocketDataProvider ssl(true, OK); |
| 2373 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2374 | ssl.next_proto = "spdy/2"; |
| 2375 | ssl.was_npn_negotiated = true; |
| 2376 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2377 | SSLSocketDataProvider ssl2(true, OK); |
| 2378 | ssl2.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2379 | ssl2.next_proto = "spdy/2"; |
| 2380 | ssl2.was_npn_negotiated = true; |
| 2381 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2382 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2383 | TestOldCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2384 | |
| 2385 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2386 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2387 | |
| 2388 | rv = callback1.WaitForResult(); |
| 2389 | EXPECT_EQ(OK, rv); |
| 2390 | |
| 2391 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2392 | ASSERT_TRUE(response != NULL); |
| 2393 | ASSERT_TRUE(response->headers != NULL); |
| 2394 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2395 | |
| 2396 | std::string response_data; |
| 2397 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 2398 | EXPECT_EQ(net::kUploadData, response_data); |
| 2399 | } |
| 2400 | |
| 2401 | // Test a SPDY CONNECT failure through an HTTPS Proxy. |
| 2402 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectFailure) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2403 | HttpRequestInfo request; |
| 2404 | request.method = "GET"; |
| 2405 | request.url = GURL("https://www.google.com/"); |
| 2406 | request.load_flags = 0; |
| 2407 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2408 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2409 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2410 | "https://proxy:70")); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2411 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2412 | session_deps.net_log = log.bound().net_log(); |
| 2413 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2414 | |
| 2415 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2416 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2417 | // CONNECT to www.google.com:443 via SPDY |
| 2418 | scoped_ptr<spdy::SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 2419 | scoped_ptr<spdy::SpdyFrame> get(ConstructSpdyRstStream(1, spdy::CANCEL)); |
| 2420 | |
| 2421 | MockWrite spdy_writes[] = { |
| 2422 | CreateMockWrite(*connect, 1), |
| 2423 | CreateMockWrite(*get, 3), |
| 2424 | }; |
| 2425 | |
| 2426 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdySynReplyError(1)); |
| 2427 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 2428 | MockRead spdy_reads[] = { |
| 2429 | CreateMockRead(*resp, 2, true), |
| 2430 | MockRead(true, 0, 4), |
| 2431 | }; |
| 2432 | |
| 2433 | scoped_refptr<OrderedSocketData> spdy_data( |
| 2434 | new OrderedSocketData( |
| 2435 | spdy_reads, arraysize(spdy_reads), |
| 2436 | spdy_writes, arraysize(spdy_writes))); |
| 2437 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 2438 | |
| 2439 | SSLSocketDataProvider ssl(true, OK); |
| 2440 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2441 | ssl.next_proto = "spdy/2"; |
| 2442 | ssl.was_npn_negotiated = true; |
| 2443 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2444 | SSLSocketDataProvider ssl2(true, OK); |
| 2445 | ssl2.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2446 | ssl2.next_proto = "spdy/2"; |
| 2447 | ssl2.was_npn_negotiated = true; |
| 2448 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2449 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2450 | TestOldCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2451 | |
| 2452 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2453 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2454 | |
| 2455 | rv = callback1.WaitForResult(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 2456 | EXPECT_EQ(OK, rv); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2457 | |
| 2458 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2459 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 2460 | EXPECT_EQ(500, response->headers->response_code()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2461 | } |
| 2462 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2463 | // Test the challenge-response-retry sequence through an HTTPS Proxy |
| 2464 | TEST_F(HttpNetworkTransactionTest, HttpsProxyAuthRetry) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2465 | HttpRequestInfo request; |
| 2466 | request.method = "GET"; |
| 2467 | request.url = GURL("http://www.google.com/"); |
| 2468 | // when the no authentication data flag is set. |
| 2469 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 2470 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2471 | // Configure against https proxy server "myproxy:70". |
| 2472 | SessionDependencies session_deps( |
| 2473 | ProxyService::CreateFixed("https://myproxy:70")); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2474 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2475 | session_deps.net_log = log.bound().net_log(); |
| 2476 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2477 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2478 | // Since we have proxy, should use full url |
| 2479 | MockWrite data_writes1[] = { |
| 2480 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2481 | "Host: www.google.com\r\n" |
| 2482 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2483 | |
| 2484 | // After calling trans->RestartWithAuth(), this is the request we should |
| 2485 | // be issuing -- the final header line contains the credentials. |
| 2486 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2487 | "Host: www.google.com\r\n" |
| 2488 | "Proxy-Connection: keep-alive\r\n" |
| 2489 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2490 | }; |
| 2491 | |
| 2492 | // The proxy responds to the GET with a 407, using a persistent |
| 2493 | // connection. |
| 2494 | MockRead data_reads1[] = { |
| 2495 | // No credentials. |
| 2496 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 2497 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2498 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 2499 | MockRead("Content-Length: 0\r\n\r\n"), |
| 2500 | |
| 2501 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2502 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2503 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2504 | MockRead(false, OK), |
| 2505 | }; |
| 2506 | |
| 2507 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2508 | data_writes1, arraysize(data_writes1)); |
| 2509 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2510 | SSLSocketDataProvider ssl(true, OK); |
| 2511 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2512 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2513 | TestOldCompletionCallback callback1; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2514 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2515 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2516 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2517 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2518 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2519 | |
| 2520 | rv = callback1.WaitForResult(); |
| 2521 | EXPECT_EQ(OK, rv); |
| 2522 | |
| 2523 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2524 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2525 | ASSERT_FALSE(response->headers == NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2526 | EXPECT_EQ(407, response->headers->response_code()); |
| 2527 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2528 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2529 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2530 | TestOldCompletionCallback callback2; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2531 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2532 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2533 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2534 | |
| 2535 | rv = callback2.WaitForResult(); |
| 2536 | EXPECT_EQ(OK, rv); |
| 2537 | |
| 2538 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2539 | ASSERT_TRUE(response != NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2540 | |
| 2541 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 2542 | EXPECT_EQ(200, response->headers->response_code()); |
| 2543 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2544 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 2545 | |
| 2546 | // The password prompt info should not be set. |
| 2547 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2548 | } |
| 2549 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 2550 | void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus( |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2551 | const MockRead& status, int expected_status) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2552 | HttpRequestInfo request; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2553 | request.method = "GET"; |
| 2554 | request.url = GURL("https://www.google.com/"); |
| 2555 | request.load_flags = 0; |
| 2556 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2557 | // Configure against proxy server "myproxy:70". |
| 2558 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 2559 | |
| 2560 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2561 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2562 | // Since we have proxy, should try to establish tunnel. |
| 2563 | MockWrite data_writes[] = { |
| 2564 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 2565 | "Host: www.google.com\r\n" |
| 2566 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2567 | }; |
| 2568 | |
| 2569 | MockRead data_reads[] = { |
| 2570 | status, |
| 2571 | MockRead("Content-Length: 10\r\n\r\n"), |
| 2572 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2573 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2574 | }; |
| 2575 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2576 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 2577 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2578 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2579 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2580 | TestOldCompletionCallback callback; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2581 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2582 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2583 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2584 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2585 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2586 | |
| 2587 | rv = callback.WaitForResult(); |
| 2588 | EXPECT_EQ(expected_status, rv); |
| 2589 | } |
| 2590 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 2591 | void HttpNetworkTransactionTest::ConnectStatusHelper(const MockRead& status) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2592 | ConnectStatusHelperWithExpectedStatus( |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2593 | status, ERR_TUNNEL_CONNECTION_FAILED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | TEST_F(HttpNetworkTransactionTest, ConnectStatus100) { |
| 2597 | ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n")); |
| 2598 | } |
| 2599 | |
| 2600 | TEST_F(HttpNetworkTransactionTest, ConnectStatus101) { |
| 2601 | ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n")); |
| 2602 | } |
| 2603 | |
| 2604 | TEST_F(HttpNetworkTransactionTest, ConnectStatus201) { |
| 2605 | ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n")); |
| 2606 | } |
| 2607 | |
| 2608 | TEST_F(HttpNetworkTransactionTest, ConnectStatus202) { |
| 2609 | ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n")); |
| 2610 | } |
| 2611 | |
| 2612 | TEST_F(HttpNetworkTransactionTest, ConnectStatus203) { |
| 2613 | ConnectStatusHelper( |
| 2614 | MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n")); |
| 2615 | } |
| 2616 | |
| 2617 | TEST_F(HttpNetworkTransactionTest, ConnectStatus204) { |
| 2618 | ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n")); |
| 2619 | } |
| 2620 | |
| 2621 | TEST_F(HttpNetworkTransactionTest, ConnectStatus205) { |
| 2622 | ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n")); |
| 2623 | } |
| 2624 | |
| 2625 | TEST_F(HttpNetworkTransactionTest, ConnectStatus206) { |
| 2626 | ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n")); |
| 2627 | } |
| 2628 | |
| 2629 | TEST_F(HttpNetworkTransactionTest, ConnectStatus300) { |
| 2630 | ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n")); |
| 2631 | } |
| 2632 | |
| 2633 | TEST_F(HttpNetworkTransactionTest, ConnectStatus301) { |
| 2634 | ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n")); |
| 2635 | } |
| 2636 | |
| 2637 | TEST_F(HttpNetworkTransactionTest, ConnectStatus302) { |
| 2638 | ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n")); |
| 2639 | } |
| 2640 | |
| 2641 | TEST_F(HttpNetworkTransactionTest, ConnectStatus303) { |
| 2642 | ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n")); |
| 2643 | } |
| 2644 | |
| 2645 | TEST_F(HttpNetworkTransactionTest, ConnectStatus304) { |
| 2646 | ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n")); |
| 2647 | } |
| 2648 | |
| 2649 | TEST_F(HttpNetworkTransactionTest, ConnectStatus305) { |
| 2650 | ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n")); |
| 2651 | } |
| 2652 | |
| 2653 | TEST_F(HttpNetworkTransactionTest, ConnectStatus306) { |
| 2654 | ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n")); |
| 2655 | } |
| 2656 | |
| 2657 | TEST_F(HttpNetworkTransactionTest, ConnectStatus307) { |
| 2658 | ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n")); |
| 2659 | } |
| 2660 | |
| 2661 | TEST_F(HttpNetworkTransactionTest, ConnectStatus400) { |
| 2662 | ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n")); |
| 2663 | } |
| 2664 | |
| 2665 | TEST_F(HttpNetworkTransactionTest, ConnectStatus401) { |
| 2666 | ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n")); |
| 2667 | } |
| 2668 | |
| 2669 | TEST_F(HttpNetworkTransactionTest, ConnectStatus402) { |
| 2670 | ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n")); |
| 2671 | } |
| 2672 | |
| 2673 | TEST_F(HttpNetworkTransactionTest, ConnectStatus403) { |
| 2674 | ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n")); |
| 2675 | } |
| 2676 | |
| 2677 | TEST_F(HttpNetworkTransactionTest, ConnectStatus404) { |
| 2678 | ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n")); |
| 2679 | } |
| 2680 | |
| 2681 | TEST_F(HttpNetworkTransactionTest, ConnectStatus405) { |
| 2682 | ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n")); |
| 2683 | } |
| 2684 | |
| 2685 | TEST_F(HttpNetworkTransactionTest, ConnectStatus406) { |
| 2686 | ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n")); |
| 2687 | } |
| 2688 | |
| 2689 | TEST_F(HttpNetworkTransactionTest, ConnectStatus407) { |
| 2690 | ConnectStatusHelperWithExpectedStatus( |
| 2691 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 2692 | ERR_PROXY_AUTH_UNSUPPORTED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | TEST_F(HttpNetworkTransactionTest, ConnectStatus408) { |
| 2696 | ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n")); |
| 2697 | } |
| 2698 | |
| 2699 | TEST_F(HttpNetworkTransactionTest, ConnectStatus409) { |
| 2700 | ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n")); |
| 2701 | } |
| 2702 | |
| 2703 | TEST_F(HttpNetworkTransactionTest, ConnectStatus410) { |
| 2704 | ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n")); |
| 2705 | } |
| 2706 | |
| 2707 | TEST_F(HttpNetworkTransactionTest, ConnectStatus411) { |
| 2708 | ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n")); |
| 2709 | } |
| 2710 | |
| 2711 | TEST_F(HttpNetworkTransactionTest, ConnectStatus412) { |
| 2712 | ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n")); |
| 2713 | } |
| 2714 | |
| 2715 | TEST_F(HttpNetworkTransactionTest, ConnectStatus413) { |
| 2716 | ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n")); |
| 2717 | } |
| 2718 | |
| 2719 | TEST_F(HttpNetworkTransactionTest, ConnectStatus414) { |
| 2720 | ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n")); |
| 2721 | } |
| 2722 | |
| 2723 | TEST_F(HttpNetworkTransactionTest, ConnectStatus415) { |
| 2724 | ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n")); |
| 2725 | } |
| 2726 | |
| 2727 | TEST_F(HttpNetworkTransactionTest, ConnectStatus416) { |
| 2728 | ConnectStatusHelper( |
| 2729 | MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n")); |
| 2730 | } |
| 2731 | |
| 2732 | TEST_F(HttpNetworkTransactionTest, ConnectStatus417) { |
| 2733 | ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n")); |
| 2734 | } |
| 2735 | |
| 2736 | TEST_F(HttpNetworkTransactionTest, ConnectStatus500) { |
| 2737 | ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n")); |
| 2738 | } |
| 2739 | |
| 2740 | TEST_F(HttpNetworkTransactionTest, ConnectStatus501) { |
| 2741 | ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n")); |
| 2742 | } |
| 2743 | |
| 2744 | TEST_F(HttpNetworkTransactionTest, ConnectStatus502) { |
| 2745 | ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n")); |
| 2746 | } |
| 2747 | |
| 2748 | TEST_F(HttpNetworkTransactionTest, ConnectStatus503) { |
| 2749 | ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n")); |
| 2750 | } |
| 2751 | |
| 2752 | TEST_F(HttpNetworkTransactionTest, ConnectStatus504) { |
| 2753 | ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n")); |
| 2754 | } |
| 2755 | |
| 2756 | TEST_F(HttpNetworkTransactionTest, ConnectStatus505) { |
| 2757 | ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n")); |
| 2758 | } |
| 2759 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2760 | // Test the flow when both the proxy server AND origin server require |
| 2761 | // authentication. Again, this uses basic auth for both since that is |
| 2762 | // the simplest to mock. |
| 2763 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2764 | HttpRequestInfo request; |
| 2765 | request.method = "GET"; |
| 2766 | request.url = GURL("http://www.google.com/"); |
| 2767 | request.load_flags = 0; |
| 2768 | |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 2769 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 2770 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2771 | // Configure against proxy server "myproxy:70". |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2772 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2773 | CreateSession(&session_deps))); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2774 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2775 | MockWrite data_writes1[] = { |
| 2776 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2777 | "Host: www.google.com\r\n" |
| 2778 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2779 | }; |
| 2780 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2781 | MockRead data_reads1[] = { |
| 2782 | MockRead("HTTP/1.0 407 Unauthorized\r\n"), |
| 2783 | // Give a couple authenticate options (only the middle one is actually |
| 2784 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 2785 | MockRead("Proxy-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2786 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2787 | MockRead("Proxy-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2788 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2789 | // Large content-length -- won't matter, as connection will be reset. |
| 2790 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2791 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2792 | }; |
| 2793 | |
| 2794 | // After calling trans->RestartWithAuth() the first time, this is the |
| 2795 | // request we should be issuing -- the final header line contains the |
| 2796 | // proxy's credentials. |
| 2797 | MockWrite data_writes2[] = { |
| 2798 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2799 | "Host: www.google.com\r\n" |
| 2800 | "Proxy-Connection: keep-alive\r\n" |
| 2801 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2802 | }; |
| 2803 | |
| 2804 | // Now the proxy server lets the request pass through to origin server. |
| 2805 | // The origin server responds with a 401. |
| 2806 | MockRead data_reads2[] = { |
| 2807 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2808 | // Note: We are using the same realm-name as the proxy server. This is |
| 2809 | // completely valid, as realms are unique across hosts. |
| 2810 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2811 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2812 | MockRead("Content-Length: 2000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2813 | MockRead(false, ERR_FAILED), // Won't be reached. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2814 | }; |
| 2815 | |
| 2816 | // After calling trans->RestartWithAuth() the second time, we should send |
| 2817 | // the credentials for both the proxy and origin server. |
| 2818 | MockWrite data_writes3[] = { |
| 2819 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2820 | "Host: www.google.com\r\n" |
| 2821 | "Proxy-Connection: keep-alive\r\n" |
| 2822 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 2823 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 2824 | }; |
| 2825 | |
| 2826 | // Lastly we get the desired content. |
| 2827 | MockRead data_reads3[] = { |
| 2828 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2829 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2830 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2831 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2832 | }; |
| 2833 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2834 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2835 | data_writes1, arraysize(data_writes1)); |
| 2836 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2837 | data_writes2, arraysize(data_writes2)); |
| 2838 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 2839 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2840 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2841 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2842 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2843 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2844 | TestOldCompletionCallback callback1; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2845 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2846 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2847 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2848 | |
| 2849 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2850 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2851 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2852 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2853 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2854 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2855 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2856 | TestOldCompletionCallback callback2; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2857 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2858 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2859 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2860 | |
| 2861 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2862 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2863 | |
| 2864 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2865 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2866 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2867 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2868 | TestOldCompletionCallback callback3; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2869 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2870 | rv = trans->RestartWithAuth(AuthCredentials(kFoo2, kBar2), &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2871 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2872 | |
| 2873 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2874 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2875 | |
| 2876 | response = trans->GetResponseInfo(); |
| 2877 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2878 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2879 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2880 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2881 | // For the NTLM implementation using SSPI, we skip the NTLM tests since we |
| 2882 | // can't hook into its internals to cause it to generate predictable NTLM |
| 2883 | // authorization headers. |
| 2884 | #if defined(NTLM_PORTABLE) |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2885 | // The NTLM authentication unit tests were generated by capturing the HTTP |
| 2886 | // requests and responses using Fiddler 2 and inspecting the generated random |
| 2887 | // bytes in the debugger. |
| 2888 | |
| 2889 | // Enter the correct password and authenticate successfully. |
| 2890 | TEST_F(HttpNetworkTransactionTest, NTLMAuth1) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2891 | HttpRequestInfo request; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2892 | request.method = "GET"; |
| 2893 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 2894 | request.load_flags = 0; |
| 2895 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2896 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1, |
| 2897 | MockGetHostName); |
| 2898 | SessionDependencies session_deps; |
| 2899 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2900 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2901 | MockWrite data_writes1[] = { |
| 2902 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2903 | "Host: 172.22.68.17\r\n" |
| 2904 | "Connection: keep-alive\r\n\r\n"), |
| 2905 | }; |
| 2906 | |
| 2907 | MockRead data_reads1[] = { |
| 2908 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2909 | // Negotiate and NTLM are often requested together. However, we only want |
| 2910 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 2911 | // the header that requests Negotiate for this test. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2912 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2913 | MockRead("Connection: close\r\n"), |
| 2914 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2915 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2916 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2917 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2918 | }; |
| 2919 | |
| 2920 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2921 | // After restarting with a null identity, this is the |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2922 | // request we should be issuing -- the final header line contains a Type |
| 2923 | // 1 message. |
| 2924 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2925 | "Host: 172.22.68.17\r\n" |
| 2926 | "Connection: keep-alive\r\n" |
| 2927 | "Authorization: NTLM " |
| 2928 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2929 | |
| 2930 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2931 | // (the credentials for the origin server). The second request continues |
| 2932 | // on the same connection. |
| 2933 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2934 | "Host: 172.22.68.17\r\n" |
| 2935 | "Connection: keep-alive\r\n" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2936 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2937 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2938 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBVKW" |
| 2939 | "Yma5xzVAAAAAAAAAAAAAAAAAAAAACH+gWcm+YsP9Tqb9zCR3WAeZZX" |
| 2940 | "ahlhx5I=\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2941 | }; |
| 2942 | |
| 2943 | MockRead data_reads2[] = { |
| 2944 | // The origin server responds with a Type 2 message. |
| 2945 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2946 | MockRead("WWW-Authenticate: NTLM " |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2947 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCjGpMpPGlYKkAAAAAAAAAALo" |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2948 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2949 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2950 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2951 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2952 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2953 | "BtAAAAAAA=\r\n"), |
| 2954 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2955 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2956 | MockRead("You are not authorized to view this page\r\n"), |
| 2957 | |
| 2958 | // Lastly we get the desired content. |
| 2959 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2960 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 2961 | MockRead("Content-Length: 13\r\n\r\n"), |
| 2962 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2963 | MockRead(false, OK), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2964 | }; |
| 2965 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2966 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2967 | data_writes1, arraysize(data_writes1)); |
| 2968 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2969 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2970 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2971 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2972 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2973 | TestOldCompletionCallback callback1; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2974 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2975 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2976 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2977 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2978 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2979 | |
| 2980 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2981 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2982 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2983 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2984 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2985 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2986 | ASSERT_FALSE(response == NULL); |
| 2987 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge.get())); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2988 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 2989 | TestOldCompletionCallback callback2; |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 2990 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2991 | rv = trans->RestartWithAuth(AuthCredentials(kTestingNTLM, kTestingNTLM), |
| 2992 | &callback2); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 2993 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2994 | |
| 2995 | rv = callback2.WaitForResult(); |
| 2996 | EXPECT_EQ(OK, rv); |
| 2997 | |
| 2998 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2999 | |
| 3000 | response = trans->GetResponseInfo(); |
| 3001 | ASSERT_TRUE(response != NULL); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3002 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3003 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3004 | TestOldCompletionCallback callback3; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3005 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3006 | rv = trans->RestartWithAuth(AuthCredentials(), &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3007 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3008 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3009 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3010 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3011 | |
| 3012 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3013 | ASSERT_TRUE(response != NULL); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3014 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3015 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 3016 | } |
| 3017 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3018 | // Enter a wrong password, and then the correct one. |
| 3019 | TEST_F(HttpNetworkTransactionTest, NTLMAuth2) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3020 | HttpRequestInfo request; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3021 | request.method = "GET"; |
| 3022 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 3023 | request.load_flags = 0; |
| 3024 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3025 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom2, |
| 3026 | MockGetHostName); |
| 3027 | SessionDependencies session_deps; |
| 3028 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3029 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3030 | MockWrite data_writes1[] = { |
| 3031 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3032 | "Host: 172.22.68.17\r\n" |
| 3033 | "Connection: keep-alive\r\n\r\n"), |
| 3034 | }; |
| 3035 | |
| 3036 | MockRead data_reads1[] = { |
| 3037 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 3038 | // Negotiate and NTLM are often requested together. However, we only want |
| 3039 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 3040 | // the header that requests Negotiate for this test. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3041 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 3042 | MockRead("Connection: close\r\n"), |
| 3043 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 3044 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3045 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3046 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3047 | }; |
| 3048 | |
| 3049 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3050 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3051 | // request we should be issuing -- the final header line contains a Type |
| 3052 | // 1 message. |
| 3053 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3054 | "Host: 172.22.68.17\r\n" |
| 3055 | "Connection: keep-alive\r\n" |
| 3056 | "Authorization: NTLM " |
| 3057 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 3058 | |
| 3059 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 3060 | // (the credentials for the origin server). The second request continues |
| 3061 | // on the same connection. |
| 3062 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3063 | "Host: 172.22.68.17\r\n" |
| 3064 | "Connection: keep-alive\r\n" |
| 3065 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 3066 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 3067 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwCWeY" |
| 3068 | "XnSZNwoQAAAAAAAAAAAAAAAAAAAADLa34/phTTKzNTWdub+uyFleOj" |
| 3069 | "4Ww7b7E=\r\n\r\n"), |
| 3070 | }; |
| 3071 | |
| 3072 | MockRead data_reads2[] = { |
| 3073 | // The origin server responds with a Type 2 message. |
| 3074 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 3075 | MockRead("WWW-Authenticate: NTLM " |
| 3076 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCbVWUZezVGpAAAAAAAAAAALo" |
| 3077 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 3078 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 3079 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 3080 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 3081 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 3082 | "BtAAAAAAA=\r\n"), |
| 3083 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 3084 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3085 | MockRead("You are not authorized to view this page\r\n"), |
| 3086 | |
| 3087 | // Wrong password. |
| 3088 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3089 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 3090 | MockRead("Connection: close\r\n"), |
| 3091 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 3092 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3093 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3094 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3095 | }; |
| 3096 | |
| 3097 | MockWrite data_writes3[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3098 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3099 | // request we should be issuing -- the final header line contains a Type |
| 3100 | // 1 message. |
| 3101 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3102 | "Host: 172.22.68.17\r\n" |
| 3103 | "Connection: keep-alive\r\n" |
| 3104 | "Authorization: NTLM " |
| 3105 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 3106 | |
| 3107 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 3108 | // (the credentials for the origin server). The second request continues |
| 3109 | // on the same connection. |
| 3110 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3111 | "Host: 172.22.68.17\r\n" |
| 3112 | "Connection: keep-alive\r\n" |
| 3113 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 3114 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 3115 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBO54" |
| 3116 | "dFMVvTHwAAAAAAAAAAAAAAAAAAAACS7sT6Uzw7L0L//WUqlIaVWpbI" |
| 3117 | "+4MUm7c=\r\n\r\n"), |
| 3118 | }; |
| 3119 | |
| 3120 | MockRead data_reads3[] = { |
| 3121 | // The origin server responds with a Type 2 message. |
| 3122 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 3123 | MockRead("WWW-Authenticate: NTLM " |
| 3124 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCL24VN8dgOR8AAAAAAAAAALo" |
| 3125 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 3126 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 3127 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 3128 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 3129 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 3130 | "BtAAAAAAA=\r\n"), |
| 3131 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 3132 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3133 | MockRead("You are not authorized to view this page\r\n"), |
| 3134 | |
| 3135 | // Lastly we get the desired content. |
| 3136 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3137 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 3138 | MockRead("Content-Length: 13\r\n\r\n"), |
| 3139 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3140 | MockRead(false, OK), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3141 | }; |
| 3142 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3143 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3144 | data_writes1, arraysize(data_writes1)); |
| 3145 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3146 | data_writes2, arraysize(data_writes2)); |
| 3147 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 3148 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3149 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3150 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3151 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3152 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3153 | TestOldCompletionCallback callback1; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3154 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3155 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3156 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3157 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3158 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3159 | |
| 3160 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3161 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3162 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3163 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3164 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3165 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3166 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3167 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge.get())); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3168 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3169 | TestOldCompletionCallback callback2; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3170 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3171 | // Enter the wrong password. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3172 | rv = trans->RestartWithAuth(AuthCredentials(kTestingNTLM, kWrongPassword), |
| 3173 | &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3174 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3175 | |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3176 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3177 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3178 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3179 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3180 | TestOldCompletionCallback callback3; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3181 | rv = trans->RestartWithAuth(AuthCredentials(), &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3182 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3183 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3184 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3185 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3186 | |
| 3187 | response = trans->GetResponseInfo(); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3188 | ASSERT_FALSE(response == NULL); |
| 3189 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge.get())); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3190 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3191 | TestOldCompletionCallback callback4; |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3192 | |
| 3193 | // Now enter the right password. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3194 | rv = trans->RestartWithAuth(AuthCredentials(kTestingNTLM, kTestingNTLM), |
| 3195 | &callback4); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3196 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3197 | |
| 3198 | rv = callback4.WaitForResult(); |
| 3199 | EXPECT_EQ(OK, rv); |
| 3200 | |
| 3201 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3202 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3203 | TestOldCompletionCallback callback5; |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3204 | |
| 3205 | // One more roundtrip |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3206 | rv = trans->RestartWithAuth(AuthCredentials(), &callback5); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3207 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3208 | |
| 3209 | rv = callback5.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3210 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3211 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3212 | response = trans->GetResponseInfo(); |
| 3213 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3214 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 3215 | } |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3216 | #endif // NTLM_PORTABLE |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3217 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3218 | // Test reading a server response which has only headers, and no body. |
| 3219 | // After some maximum number of bytes is consumed, the transaction should |
| 3220 | // fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
| 3221 | TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3222 | HttpRequestInfo request; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3223 | request.method = "GET"; |
| 3224 | request.url = GURL("http://www.google.com/"); |
| 3225 | request.load_flags = 0; |
| 3226 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3227 | SessionDependencies session_deps; |
| 3228 | scoped_ptr<HttpTransaction> trans( |
| 3229 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3230 | |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 3231 | // Respond with 300 kb of headers (we should fail after 256 kb). |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 3232 | std::string large_headers_string; |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 3233 | FillLargeHeadersString(&large_headers_string, 300 * 1024); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3234 | |
| 3235 | MockRead data_reads[] = { |
| 3236 | MockRead("HTTP/1.0 200 OK\r\n"), |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 3237 | MockRead(true, large_headers_string.data(), large_headers_string.size()), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3238 | MockRead("\r\nBODY"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3239 | MockRead(false, OK), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3240 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3241 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3242 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3243 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3244 | TestOldCompletionCallback callback; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3245 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3246 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3247 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3248 | |
| 3249 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3250 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3251 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3252 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3253 | EXPECT_TRUE(response == NULL); |
| 3254 | } |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3255 | |
| 3256 | // Make sure that we don't try to reuse a TCPClientSocket when failing to |
| 3257 | // establish tunnel. |
| 3258 | // http://code.google.com/p/chromium/issues/detail?id=3772 |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3259 | TEST_F(HttpNetworkTransactionTest, DontRecycleTransportSocketForSSLTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3260 | HttpRequestInfo request; |
| 3261 | request.method = "GET"; |
| 3262 | request.url = GURL("https://www.google.com/"); |
| 3263 | request.load_flags = 0; |
| 3264 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3265 | // Configure against proxy server "myproxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 3266 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 3267 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3268 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3269 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3270 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3271 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3272 | // Since we have proxy, should try to establish tunnel. |
| 3273 | MockWrite data_writes1[] = { |
| 3274 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 3275 | "Host: www.google.com\r\n" |
| 3276 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3277 | }; |
| 3278 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 3279 | // The proxy responds to the connect with a 404, using a persistent |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3280 | // connection. Usually a proxy would return 501 (not implemented), |
| 3281 | // or 200 (tunnel established). |
| 3282 | MockRead data_reads1[] = { |
| 3283 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 3284 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3285 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3286 | }; |
| 3287 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3288 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3289 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3290 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3291 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3292 | TestOldCompletionCallback callback1; |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3293 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3294 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3295 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3296 | |
| 3297 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3298 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3299 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3300 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 3301 | EXPECT_TRUE(response == NULL); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3302 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3303 | // Empty the current queue. This is necessary because idle sockets are |
| 3304 | // added to the connection pool asynchronously with a PostTask. |
| 3305 | MessageLoop::current()->RunAllPending(); |
| 3306 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3307 | // We now check to make sure the TCPClientSocket was not added back to |
| 3308 | // the pool. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3309 | EXPECT_EQ(0, session->transport_socket_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3310 | trans.reset(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3311 | MessageLoop::current()->RunAllPending(); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3312 | // Make sure that the socket didn't get recycled after calling the destructor. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3313 | EXPECT_EQ(0, session->transport_socket_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3314 | } |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3315 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3316 | // Make sure that we recycle a socket after reading all of the response body. |
| 3317 | TEST_F(HttpNetworkTransactionTest, RecycleSocket) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3318 | HttpRequestInfo request; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3319 | request.method = "GET"; |
| 3320 | request.url = GURL("http://www.google.com/"); |
| 3321 | request.load_flags = 0; |
| 3322 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3323 | SessionDependencies session_deps; |
| 3324 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3325 | |
| 3326 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3327 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3328 | MockRead data_reads[] = { |
| 3329 | // A part of the response body is received with the response headers. |
| 3330 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 3331 | // The rest of the response body is received in two parts. |
| 3332 | MockRead("lo"), |
| 3333 | MockRead(" world"), |
| 3334 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3335 | MockRead(false, OK), |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3336 | }; |
| 3337 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3338 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3339 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3340 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3341 | TestOldCompletionCallback callback; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3342 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3343 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3344 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3345 | |
| 3346 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3347 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3348 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3349 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3350 | ASSERT_TRUE(response != NULL); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3351 | |
| 3352 | EXPECT_TRUE(response->headers != NULL); |
| 3353 | std::string status_line = response->headers->GetStatusLine(); |
| 3354 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 3355 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3356 | EXPECT_EQ(0, session->transport_socket_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3357 | |
| 3358 | std::string response_data; |
| 3359 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3360 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3361 | EXPECT_EQ("hello world", response_data); |
| 3362 | |
| 3363 | // Empty the current queue. This is necessary because idle sockets are |
| 3364 | // added to the connection pool asynchronously with a PostTask. |
| 3365 | MessageLoop::current()->RunAllPending(); |
| 3366 | |
| 3367 | // We now check to make sure the socket was added back to the pool. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3368 | EXPECT_EQ(1, session->transport_socket_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3369 | } |
| 3370 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3371 | // Make sure that we recycle a SSL socket after reading all of the response |
| 3372 | // body. |
| 3373 | TEST_F(HttpNetworkTransactionTest, RecycleSSLSocket) { |
| 3374 | SessionDependencies session_deps; |
| 3375 | HttpRequestInfo request; |
| 3376 | request.method = "GET"; |
| 3377 | request.url = GURL("https://www.google.com/"); |
| 3378 | request.load_flags = 0; |
| 3379 | |
| 3380 | MockWrite data_writes[] = { |
| 3381 | MockWrite("GET / HTTP/1.1\r\n" |
| 3382 | "Host: www.google.com\r\n" |
| 3383 | "Connection: keep-alive\r\n\r\n"), |
| 3384 | }; |
| 3385 | |
| 3386 | MockRead data_reads[] = { |
| 3387 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3388 | MockRead("Content-Length: 11\r\n\r\n"), |
| 3389 | MockRead("hello world"), |
| 3390 | MockRead(false, OK), |
| 3391 | }; |
| 3392 | |
| 3393 | SSLSocketDataProvider ssl(true, OK); |
| 3394 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 3395 | |
| 3396 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3397 | data_writes, arraysize(data_writes)); |
| 3398 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3399 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3400 | TestOldCompletionCallback callback; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3401 | |
| 3402 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3403 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3404 | |
| 3405 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 3406 | |
| 3407 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3408 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3409 | |
| 3410 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3411 | ASSERT_TRUE(response != NULL); |
| 3412 | ASSERT_TRUE(response->headers != NULL); |
| 3413 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3414 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3415 | EXPECT_EQ(0, session->transport_socket_pool()->IdleSocketCount()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3416 | |
| 3417 | std::string response_data; |
| 3418 | rv = ReadTransaction(trans.get(), &response_data); |
| 3419 | EXPECT_EQ(OK, rv); |
| 3420 | EXPECT_EQ("hello world", response_data); |
| 3421 | |
| 3422 | // Empty the current queue. This is necessary because idle sockets are |
| 3423 | // added to the connection pool asynchronously with a PostTask. |
| 3424 | MessageLoop::current()->RunAllPending(); |
| 3425 | |
| 3426 | // We now check to make sure the socket was added back to the pool. |
| 3427 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 3428 | } |
| 3429 | |
| 3430 | // Grab a SSL socket, use it, and put it back into the pool. Then, reuse it |
| 3431 | // from the pool and make sure that we recover okay. |
| 3432 | TEST_F(HttpNetworkTransactionTest, RecycleDeadSSLSocket) { |
| 3433 | SessionDependencies session_deps; |
| 3434 | HttpRequestInfo request; |
| 3435 | request.method = "GET"; |
| 3436 | request.url = GURL("https://www.google.com/"); |
| 3437 | request.load_flags = 0; |
| 3438 | |
| 3439 | MockWrite data_writes[] = { |
| 3440 | MockWrite("GET / HTTP/1.1\r\n" |
| 3441 | "Host: www.google.com\r\n" |
| 3442 | "Connection: keep-alive\r\n\r\n"), |
| 3443 | MockWrite("GET / HTTP/1.1\r\n" |
| 3444 | "Host: www.google.com\r\n" |
| 3445 | "Connection: keep-alive\r\n\r\n"), |
| 3446 | }; |
| 3447 | |
| 3448 | MockRead data_reads[] = { |
| 3449 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3450 | MockRead("Content-Length: 11\r\n\r\n"), |
| 3451 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 3452 | MockRead("hello world"), |
| 3453 | MockRead(true, 0, 0) // EOF |
| 3454 | }; |
| 3455 | |
| 3456 | SSLSocketDataProvider ssl(true, OK); |
| 3457 | SSLSocketDataProvider ssl2(true, OK); |
| 3458 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 3459 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 3460 | |
| 3461 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3462 | data_writes, arraysize(data_writes)); |
| 3463 | StaticSocketDataProvider data2(data_reads, arraysize(data_reads), |
| 3464 | data_writes, arraysize(data_writes)); |
| 3465 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3466 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3467 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3468 | TestOldCompletionCallback callback; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3469 | |
| 3470 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3471 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3472 | |
| 3473 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 3474 | |
| 3475 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3476 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3477 | |
| 3478 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3479 | ASSERT_TRUE(response != NULL); |
| 3480 | ASSERT_TRUE(response->headers != NULL); |
| 3481 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3482 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3483 | EXPECT_EQ(0, session->transport_socket_pool()->IdleSocketCount()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3484 | |
| 3485 | std::string response_data; |
| 3486 | rv = ReadTransaction(trans.get(), &response_data); |
| 3487 | EXPECT_EQ(OK, rv); |
| 3488 | EXPECT_EQ("hello world", response_data); |
| 3489 | |
| 3490 | // Empty the current queue. This is necessary because idle sockets are |
| 3491 | // added to the connection pool asynchronously with a PostTask. |
| 3492 | MessageLoop::current()->RunAllPending(); |
| 3493 | |
| 3494 | // We now check to make sure the socket was added back to the pool. |
| 3495 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 3496 | |
| 3497 | // Now start the second transaction, which should reuse the previous socket. |
| 3498 | |
| 3499 | trans.reset(new HttpNetworkTransaction(session)); |
| 3500 | |
| 3501 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 3502 | |
| 3503 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3504 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3505 | |
| 3506 | response = trans->GetResponseInfo(); |
| 3507 | ASSERT_TRUE(response != NULL); |
| 3508 | ASSERT_TRUE(response->headers != NULL); |
| 3509 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3510 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3511 | EXPECT_EQ(0, session->transport_socket_pool()->IdleSocketCount()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3512 | |
| 3513 | rv = ReadTransaction(trans.get(), &response_data); |
| 3514 | EXPECT_EQ(OK, rv); |
| 3515 | EXPECT_EQ("hello world", response_data); |
| 3516 | |
| 3517 | // Empty the current queue. This is necessary because idle sockets are |
| 3518 | // added to the connection pool asynchronously with a PostTask. |
| 3519 | MessageLoop::current()->RunAllPending(); |
| 3520 | |
| 3521 | // We now check to make sure the socket was added back to the pool. |
| 3522 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 3523 | } |
| 3524 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3525 | // Make sure that we recycle a socket after a zero-length response. |
| 3526 | // http://crbug.com/9880 |
| 3527 | TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3528 | HttpRequestInfo request; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3529 | request.method = "GET"; |
| 3530 | request.url = GURL("http://www.google.com/csi?v=3&s=web&action=&" |
| 3531 | "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" |
| 3532 | "e=17259,18167,19592,19773,19981,20133,20173,20233&" |
| 3533 | "rt=prt.2642,ol.2649,xjs.2951"); |
| 3534 | request.load_flags = 0; |
| 3535 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3536 | SessionDependencies session_deps; |
| 3537 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3538 | |
| 3539 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3540 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3541 | MockRead data_reads[] = { |
| 3542 | MockRead("HTTP/1.1 204 No Content\r\n" |
| 3543 | "Content-Length: 0\r\n" |
| 3544 | "Content-Type: text/html\r\n\r\n"), |
| 3545 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3546 | MockRead(false, OK), |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3547 | }; |
| 3548 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3549 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3550 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3551 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3552 | TestOldCompletionCallback callback; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3553 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3554 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3555 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3556 | |
| 3557 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3558 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3559 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3560 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3561 | ASSERT_TRUE(response != NULL); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3562 | |
| 3563 | EXPECT_TRUE(response->headers != NULL); |
| 3564 | std::string status_line = response->headers->GetStatusLine(); |
| 3565 | EXPECT_EQ("HTTP/1.1 204 No Content", status_line); |
| 3566 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3567 | EXPECT_EQ(0, session->transport_socket_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3568 | |
| 3569 | std::string response_data; |
| 3570 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3571 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3572 | EXPECT_EQ("", response_data); |
| 3573 | |
| 3574 | // Empty the current queue. This is necessary because idle sockets are |
| 3575 | // added to the connection pool asynchronously with a PostTask. |
| 3576 | MessageLoop::current()->RunAllPending(); |
| 3577 | |
| 3578 | // We now check to make sure the socket was added back to the pool. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3579 | EXPECT_EQ(1, session->transport_socket_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3580 | } |
| 3581 | |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3582 | TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3583 | HttpRequestInfo request[2]; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3584 | // Transaction 1: a GET request that succeeds. The socket is recycled |
| 3585 | // after use. |
| 3586 | request[0].method = "GET"; |
| 3587 | request[0].url = GURL("http://www.google.com/"); |
| 3588 | request[0].load_flags = 0; |
| 3589 | // Transaction 2: a POST request. Reuses the socket kept alive from |
| 3590 | // transaction 1. The first attempts fails when writing the POST data. |
| 3591 | // This causes the transaction to retry with a new socket. The second |
| 3592 | // attempt succeeds. |
| 3593 | request[1].method = "POST"; |
| 3594 | request[1].url = GURL("http://www.google.com/login.cgi"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3595 | request[1].upload_data = new UploadData; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3596 | request[1].upload_data->AppendBytes("foo", 3); |
| 3597 | request[1].load_flags = 0; |
| 3598 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3599 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 3600 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3601 | |
| 3602 | // The first socket is used for transaction 1 and the first attempt of |
| 3603 | // transaction 2. |
| 3604 | |
| 3605 | // The response of transaction 1. |
| 3606 | MockRead data_reads1[] = { |
| 3607 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"), |
| 3608 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3609 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3610 | }; |
| 3611 | // The mock write results of transaction 1 and the first attempt of |
| 3612 | // transaction 2. |
| 3613 | MockWrite data_writes1[] = { |
| 3614 | MockWrite(false, 64), // GET |
| 3615 | MockWrite(false, 93), // POST |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3616 | MockWrite(false, ERR_CONNECTION_ABORTED), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3617 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3618 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3619 | data_writes1, arraysize(data_writes1)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3620 | |
| 3621 | // The second socket is used for the second attempt of transaction 2. |
| 3622 | |
| 3623 | // The response of transaction 2. |
| 3624 | MockRead data_reads2[] = { |
| 3625 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\n"), |
| 3626 | MockRead("welcome"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3627 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3628 | }; |
| 3629 | // The mock write results of the second attempt of transaction 2. |
| 3630 | MockWrite data_writes2[] = { |
| 3631 | MockWrite(false, 93), // POST |
| 3632 | MockWrite(false, 3), // POST data |
| 3633 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3634 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3635 | data_writes2, arraysize(data_writes2)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3636 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3637 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3638 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3639 | |
| 3640 | const char* kExpectedResponseData[] = { |
| 3641 | "hello world", "welcome" |
| 3642 | }; |
| 3643 | |
| 3644 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3645 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3646 | new HttpNetworkTransaction(session)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3647 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3648 | TestOldCompletionCallback callback; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3649 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3650 | int rv = trans->Start(&request[i], &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3651 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3652 | |
| 3653 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3654 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3655 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3656 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3657 | ASSERT_TRUE(response != NULL); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3658 | |
| 3659 | EXPECT_TRUE(response->headers != NULL); |
| 3660 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3661 | |
| 3662 | std::string response_data; |
| 3663 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3664 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3665 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 3666 | } |
| 3667 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3668 | |
| 3669 | // Test the request-challenge-retry sequence for basic auth when there is |
| 3670 | // an identity in the URL. The request should be sent as normal, but when |
| 3671 | // it fails the identity from the URL is used to answer the challenge. |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3672 | TEST_F(HttpNetworkTransactionTest, AuthIdentityInURL) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3673 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3674 | request.method = "GET"; |
| 3675 | // Note: the URL has a username:password in it. |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3676 | request.url = GURL("http://foo:b@[email protected]/"); |
| 3677 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3678 | SessionDependencies session_deps; |
| 3679 | scoped_ptr<HttpTransaction> trans( |
| 3680 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3681 | |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3682 | // The password contains an escaped character -- for this test to pass it |
| 3683 | // will need to be unescaped by HttpNetworkTransaction. |
| 3684 | EXPECT_EQ("b%40r", request.url.password()); |
| 3685 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3686 | request.load_flags = LOAD_NORMAL; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3687 | |
| 3688 | MockWrite data_writes1[] = { |
| 3689 | MockWrite("GET / HTTP/1.1\r\n" |
| 3690 | "Host: www.google.com\r\n" |
| 3691 | "Connection: keep-alive\r\n\r\n"), |
| 3692 | }; |
| 3693 | |
| 3694 | MockRead data_reads1[] = { |
| 3695 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3696 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3697 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3698 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3699 | }; |
| 3700 | |
| 3701 | // After the challenge above, the transaction will be restarted using the |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3702 | // identity from the url (foo, b@r) to answer the challenge. |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3703 | MockWrite data_writes2[] = { |
| 3704 | MockWrite("GET / HTTP/1.1\r\n" |
| 3705 | "Host: www.google.com\r\n" |
| 3706 | "Connection: keep-alive\r\n" |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3707 | "Authorization: Basic Zm9vOmJAcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3708 | }; |
| 3709 | |
| 3710 | MockRead data_reads2[] = { |
| 3711 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3712 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3713 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3714 | }; |
| 3715 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3716 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3717 | data_writes1, arraysize(data_writes1)); |
| 3718 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3719 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3720 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3721 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3722 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3723 | TestOldCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3724 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3725 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3726 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3727 | |
| 3728 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3729 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3730 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3731 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3732 | TestOldCompletionCallback callback2; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3733 | rv = trans->RestartWithAuth(AuthCredentials(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3734 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3735 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3736 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3737 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3738 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3739 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3740 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3741 | |
| 3742 | // There is no challenge info, since the identity in URL worked. |
| 3743 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3744 | |
| 3745 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3746 | |
| 3747 | // Empty the current queue. |
| 3748 | MessageLoop::current()->RunAllPending(); |
| 3749 | } |
| 3750 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3751 | // Test the request-challenge-retry sequence for basic auth when there is |
| 3752 | // an incorrect identity in the URL. The identity from the URL should be used |
| 3753 | // only once. |
| 3754 | TEST_F(HttpNetworkTransactionTest, WrongAuthIdentityInURL) { |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3755 | HttpRequestInfo request; |
| 3756 | request.method = "GET"; |
| 3757 | // Note: the URL has a username:password in it. The password "baz" is |
| 3758 | // wrong (should be "bar"). |
| 3759 | request.url = GURL("http://foo:[email protected]/"); |
| 3760 | |
| 3761 | request.load_flags = LOAD_NORMAL; |
| 3762 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3763 | SessionDependencies session_deps; |
| 3764 | scoped_ptr<HttpTransaction> trans( |
| 3765 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3766 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3767 | MockWrite data_writes1[] = { |
| 3768 | MockWrite("GET / HTTP/1.1\r\n" |
| 3769 | "Host: www.google.com\r\n" |
| 3770 | "Connection: keep-alive\r\n\r\n"), |
| 3771 | }; |
| 3772 | |
| 3773 | MockRead data_reads1[] = { |
| 3774 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3775 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3776 | MockRead("Content-Length: 10\r\n\r\n"), |
| 3777 | MockRead(false, ERR_FAILED), |
| 3778 | }; |
| 3779 | |
| 3780 | // After the challenge above, the transaction will be restarted using the |
| 3781 | // identity from the url (foo, baz) to answer the challenge. |
| 3782 | MockWrite data_writes2[] = { |
| 3783 | MockWrite("GET / HTTP/1.1\r\n" |
| 3784 | "Host: www.google.com\r\n" |
| 3785 | "Connection: keep-alive\r\n" |
| 3786 | "Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 3787 | }; |
| 3788 | |
| 3789 | MockRead data_reads2[] = { |
| 3790 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3791 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3792 | MockRead("Content-Length: 10\r\n\r\n"), |
| 3793 | MockRead(false, ERR_FAILED), |
| 3794 | }; |
| 3795 | |
| 3796 | // After the challenge above, the transaction will be restarted using the |
| 3797 | // identity supplied by the user (foo, bar) to answer the challenge. |
| 3798 | MockWrite data_writes3[] = { |
| 3799 | MockWrite("GET / HTTP/1.1\r\n" |
| 3800 | "Host: www.google.com\r\n" |
| 3801 | "Connection: keep-alive\r\n" |
| 3802 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3803 | }; |
| 3804 | |
| 3805 | MockRead data_reads3[] = { |
| 3806 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3807 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3808 | MockRead(false, OK), |
| 3809 | }; |
| 3810 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3811 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3812 | data_writes1, arraysize(data_writes1)); |
| 3813 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3814 | data_writes2, arraysize(data_writes2)); |
| 3815 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 3816 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3817 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3818 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3819 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3820 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3821 | TestOldCompletionCallback callback1; |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3822 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3823 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3824 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3825 | |
| 3826 | rv = callback1.WaitForResult(); |
| 3827 | EXPECT_EQ(OK, rv); |
| 3828 | |
| 3829 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3830 | TestOldCompletionCallback callback2; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3831 | rv = trans->RestartWithAuth(AuthCredentials(), &callback2); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3832 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3833 | rv = callback2.WaitForResult(); |
| 3834 | EXPECT_EQ(OK, rv); |
| 3835 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3836 | |
| 3837 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3838 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3839 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3840 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3841 | TestOldCompletionCallback callback3; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3842 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback3); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3843 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3844 | rv = callback3.WaitForResult(); |
| 3845 | EXPECT_EQ(OK, rv); |
| 3846 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3847 | |
| 3848 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3849 | ASSERT_TRUE(response != NULL); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3850 | |
| 3851 | // There is no challenge info, since the identity worked. |
| 3852 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3853 | |
| 3854 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3855 | |
| 3856 | // Empty the current queue. |
| 3857 | MessageLoop::current()->RunAllPending(); |
| 3858 | } |
| 3859 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3860 | // Test that previously tried username/passwords for a realm get re-used. |
| 3861 | TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3862 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 3863 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3864 | |
| 3865 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 3866 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3867 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3868 | request.method = "GET"; |
| 3869 | request.url = GURL("http://www.google.com/x/y/z"); |
| 3870 | request.load_flags = 0; |
| 3871 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3872 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3873 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3874 | MockWrite data_writes1[] = { |
| 3875 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3876 | "Host: www.google.com\r\n" |
| 3877 | "Connection: keep-alive\r\n\r\n"), |
| 3878 | }; |
| 3879 | |
| 3880 | MockRead data_reads1[] = { |
| 3881 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3882 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3883 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3884 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3885 | }; |
| 3886 | |
| 3887 | // Resend with authorization (username=foo, password=bar) |
| 3888 | MockWrite data_writes2[] = { |
| 3889 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3890 | "Host: www.google.com\r\n" |
| 3891 | "Connection: keep-alive\r\n" |
| 3892 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3893 | }; |
| 3894 | |
| 3895 | // Sever accepts the authorization. |
| 3896 | MockRead data_reads2[] = { |
| 3897 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3898 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3899 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3900 | }; |
| 3901 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3902 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3903 | data_writes1, arraysize(data_writes1)); |
| 3904 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3905 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3906 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3907 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3908 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3909 | TestOldCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3910 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3911 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3912 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3913 | |
| 3914 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3915 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3916 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3917 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3918 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3919 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3920 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3921 | TestOldCompletionCallback callback2; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3922 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3923 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3924 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3925 | |
| 3926 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3927 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3928 | |
| 3929 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3930 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3931 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3932 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3933 | } |
| 3934 | |
| 3935 | // ------------------------------------------------------------------------ |
| 3936 | |
| 3937 | // Transaction 2: authenticate (foo2, bar2) on MyRealm2 |
| 3938 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3939 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3940 | request.method = "GET"; |
| 3941 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 3942 | // protection space as MyRealm1. |
| 3943 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 3944 | request.load_flags = 0; |
| 3945 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3946 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3947 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3948 | MockWrite data_writes1[] = { |
| 3949 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3950 | "Host: www.google.com\r\n" |
| 3951 | "Connection: keep-alive\r\n" |
| 3952 | // Send preemptive authorization for MyRealm1 |
| 3953 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3954 | }; |
| 3955 | |
| 3956 | // The server didn't like the preemptive authorization, and |
| 3957 | // challenges us for a different realm (MyRealm2). |
| 3958 | MockRead data_reads1[] = { |
| 3959 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3960 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm2\"\r\n"), |
| 3961 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3962 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3963 | }; |
| 3964 | |
| 3965 | // Resend with authorization for MyRealm2 (username=foo2, password=bar2) |
| 3966 | MockWrite data_writes2[] = { |
| 3967 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3968 | "Host: www.google.com\r\n" |
| 3969 | "Connection: keep-alive\r\n" |
| 3970 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 3971 | }; |
| 3972 | |
| 3973 | // Sever accepts the authorization. |
| 3974 | MockRead data_reads2[] = { |
| 3975 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3976 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3977 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3978 | }; |
| 3979 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3980 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3981 | data_writes1, arraysize(data_writes1)); |
| 3982 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3983 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3984 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3985 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3986 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 3987 | TestOldCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3988 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3989 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3990 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3991 | |
| 3992 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3993 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3994 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3995 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3996 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3997 | ASSERT_TRUE(response->auth_challenge.get()); |
| 3998 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 3999 | EXPECT_EQ("www.google.com:80", |
| 4000 | response->auth_challenge->challenger.ToString()); |
| 4001 | EXPECT_EQ("MyRealm2", response->auth_challenge->realm); |
| 4002 | EXPECT_EQ("basic", response->auth_challenge->scheme); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4003 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4004 | TestOldCompletionCallback callback2; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4005 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4006 | rv = trans->RestartWithAuth(AuthCredentials(kFoo2, kBar2), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4007 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4008 | |
| 4009 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4010 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4011 | |
| 4012 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4013 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4014 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4015 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4016 | } |
| 4017 | |
| 4018 | // ------------------------------------------------------------------------ |
| 4019 | |
| 4020 | // Transaction 3: Resend a request in MyRealm's protection space -- |
| 4021 | // succeed with preemptive authorization. |
| 4022 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4023 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4024 | request.method = "GET"; |
| 4025 | request.url = GURL("http://www.google.com/x/y/z2"); |
| 4026 | request.load_flags = 0; |
| 4027 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4028 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4029 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4030 | MockWrite data_writes1[] = { |
| 4031 | MockWrite("GET /x/y/z2 HTTP/1.1\r\n" |
| 4032 | "Host: www.google.com\r\n" |
| 4033 | "Connection: keep-alive\r\n" |
| 4034 | // The authorization for MyRealm1 gets sent preemptively |
| 4035 | // (since the url is in the same protection space) |
| 4036 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4037 | }; |
| 4038 | |
| 4039 | // Sever accepts the preemptive authorization |
| 4040 | MockRead data_reads1[] = { |
| 4041 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4042 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4043 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4044 | }; |
| 4045 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4046 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4047 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4048 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4049 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4050 | TestOldCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4051 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4052 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4053 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4054 | |
| 4055 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4056 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4057 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4058 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4059 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4060 | |
| 4061 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4062 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4063 | } |
| 4064 | |
| 4065 | // ------------------------------------------------------------------------ |
| 4066 | |
| 4067 | // Transaction 4: request another URL in MyRealm (however the |
| 4068 | // url is not known to belong to the protection space, so no pre-auth). |
| 4069 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4070 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4071 | request.method = "GET"; |
| 4072 | request.url = GURL("http://www.google.com/x/1"); |
| 4073 | request.load_flags = 0; |
| 4074 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4075 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4076 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4077 | MockWrite data_writes1[] = { |
| 4078 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 4079 | "Host: www.google.com\r\n" |
| 4080 | "Connection: keep-alive\r\n\r\n"), |
| 4081 | }; |
| 4082 | |
| 4083 | MockRead data_reads1[] = { |
| 4084 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4085 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4086 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4087 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4088 | }; |
| 4089 | |
| 4090 | // Resend with authorization from MyRealm's cache. |
| 4091 | MockWrite data_writes2[] = { |
| 4092 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 4093 | "Host: www.google.com\r\n" |
| 4094 | "Connection: keep-alive\r\n" |
| 4095 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4096 | }; |
| 4097 | |
| 4098 | // Sever accepts the authorization. |
| 4099 | MockRead data_reads2[] = { |
| 4100 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4101 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4102 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4103 | }; |
| 4104 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4105 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4106 | data_writes1, arraysize(data_writes1)); |
| 4107 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 4108 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4109 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4110 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4111 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4112 | TestOldCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4113 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4114 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4115 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4116 | |
| 4117 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4118 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4119 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4120 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4121 | TestOldCompletionCallback callback2; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4122 | rv = trans->RestartWithAuth(AuthCredentials(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4123 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4124 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4125 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4126 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 4127 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4128 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4129 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4130 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4131 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4132 | } |
| 4133 | |
| 4134 | // ------------------------------------------------------------------------ |
| 4135 | |
| 4136 | // Transaction 5: request a URL in MyRealm, but the server rejects the |
| 4137 | // cached identity. Should invalidate and re-prompt. |
| 4138 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4139 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4140 | request.method = "GET"; |
| 4141 | request.url = GURL("http://www.google.com/p/q/t"); |
| 4142 | request.load_flags = 0; |
| 4143 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4144 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4145 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4146 | MockWrite data_writes1[] = { |
| 4147 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 4148 | "Host: www.google.com\r\n" |
| 4149 | "Connection: keep-alive\r\n\r\n"), |
| 4150 | }; |
| 4151 | |
| 4152 | MockRead data_reads1[] = { |
| 4153 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4154 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4155 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4156 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4157 | }; |
| 4158 | |
| 4159 | // Resend with authorization from cache for MyRealm. |
| 4160 | MockWrite data_writes2[] = { |
| 4161 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 4162 | "Host: www.google.com\r\n" |
| 4163 | "Connection: keep-alive\r\n" |
| 4164 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4165 | }; |
| 4166 | |
| 4167 | // Sever rejects the authorization. |
| 4168 | MockRead data_reads2[] = { |
| 4169 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4170 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4171 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4172 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4173 | }; |
| 4174 | |
| 4175 | // At this point we should prompt for new credentials for MyRealm. |
| 4176 | // Restart with username=foo3, password=foo4. |
| 4177 | MockWrite data_writes3[] = { |
| 4178 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 4179 | "Host: www.google.com\r\n" |
| 4180 | "Connection: keep-alive\r\n" |
| 4181 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 4182 | }; |
| 4183 | |
| 4184 | // Sever accepts the authorization. |
| 4185 | MockRead data_reads3[] = { |
| 4186 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4187 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4188 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4189 | }; |
| 4190 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4191 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4192 | data_writes1, arraysize(data_writes1)); |
| 4193 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 4194 | data_writes2, arraysize(data_writes2)); |
| 4195 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 4196 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4197 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4198 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 4199 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4200 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4201 | TestOldCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4202 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4203 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4204 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4205 | |
| 4206 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4207 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4208 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4209 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4210 | TestOldCompletionCallback callback2; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4211 | rv = trans->RestartWithAuth(AuthCredentials(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4212 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4213 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4214 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4215 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 4216 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4217 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4218 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 4219 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4220 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4221 | TestOldCompletionCallback callback3; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4222 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4223 | rv = trans->RestartWithAuth(AuthCredentials(kFoo3, kBar3), &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4224 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4225 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4226 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4227 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4228 | |
| 4229 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4230 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4231 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4232 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4233 | } |
| 4234 | } |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4235 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4236 | // Tests that nonce count increments when multiple auth attempts |
| 4237 | // are started with the same nonce. |
| 4238 | TEST_F(HttpNetworkTransactionTest, DigestPreAuthNonceCount) { |
| 4239 | SessionDependencies session_deps; |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 4240 | HttpAuthHandlerDigest::Factory* digest_factory = |
| 4241 | new HttpAuthHandlerDigest::Factory(); |
| 4242 | HttpAuthHandlerDigest::FixedNonceGenerator* nonce_generator = |
| 4243 | new HttpAuthHandlerDigest::FixedNonceGenerator("0123456789abcdef"); |
| 4244 | digest_factory->set_nonce_generator(nonce_generator); |
| 4245 | session_deps.http_auth_handler_factory.reset(digest_factory); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4246 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4247 | |
| 4248 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 4249 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4250 | HttpRequestInfo request; |
| 4251 | request.method = "GET"; |
| 4252 | request.url = GURL("http://www.google.com/x/y/z"); |
| 4253 | request.load_flags = 0; |
| 4254 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4255 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4256 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4257 | MockWrite data_writes1[] = { |
| 4258 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 4259 | "Host: www.google.com\r\n" |
| 4260 | "Connection: keep-alive\r\n\r\n"), |
| 4261 | }; |
| 4262 | |
| 4263 | MockRead data_reads1[] = { |
| 4264 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4265 | MockRead("WWW-Authenticate: Digest realm=\"digestive\", nonce=\"OU812\", " |
| 4266 | "algorithm=MD5, qop=\"auth\"\r\n\r\n"), |
| 4267 | MockRead(false, OK), |
| 4268 | }; |
| 4269 | |
| 4270 | // Resend with authorization (username=foo, password=bar) |
| 4271 | MockWrite data_writes2[] = { |
| 4272 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 4273 | "Host: www.google.com\r\n" |
| 4274 | "Connection: keep-alive\r\n" |
| 4275 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 4276 | "nonce=\"OU812\", uri=\"/x/y/z\", algorithm=MD5, " |
| 4277 | "response=\"03ffbcd30add722589c1de345d7a927f\", qop=auth, " |
| 4278 | "nc=00000001, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 4279 | }; |
| 4280 | |
| 4281 | // Sever accepts the authorization. |
| 4282 | MockRead data_reads2[] = { |
| 4283 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4284 | MockRead(false, OK), |
| 4285 | }; |
| 4286 | |
| 4287 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4288 | data_writes1, arraysize(data_writes1)); |
| 4289 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 4290 | data_writes2, arraysize(data_writes2)); |
| 4291 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4292 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 4293 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4294 | TestOldCompletionCallback callback1; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4295 | |
| 4296 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
| 4297 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4298 | |
| 4299 | rv = callback1.WaitForResult(); |
| 4300 | EXPECT_EQ(OK, rv); |
| 4301 | |
| 4302 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4303 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 4304 | EXPECT_TRUE(CheckDigestServerAuth(response->auth_challenge.get())); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4305 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4306 | TestOldCompletionCallback callback2; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4307 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4308 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4309 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4310 | |
| 4311 | rv = callback2.WaitForResult(); |
| 4312 | EXPECT_EQ(OK, rv); |
| 4313 | |
| 4314 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4315 | ASSERT_TRUE(response != NULL); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4316 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4317 | } |
| 4318 | |
| 4319 | // ------------------------------------------------------------------------ |
| 4320 | |
| 4321 | // Transaction 2: Request another resource in digestive's protection space. |
| 4322 | // This will preemptively add an Authorization header which should have an |
| 4323 | // "nc" value of 2 (as compared to 1 in the first use. |
| 4324 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4325 | HttpRequestInfo request; |
| 4326 | request.method = "GET"; |
| 4327 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 4328 | // protection space as digest. |
| 4329 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 4330 | request.load_flags = 0; |
| 4331 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4332 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4333 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4334 | MockWrite data_writes1[] = { |
| 4335 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 4336 | "Host: www.google.com\r\n" |
| 4337 | "Connection: keep-alive\r\n" |
| 4338 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 4339 | "nonce=\"OU812\", uri=\"/x/y/a/b\", algorithm=MD5, " |
| 4340 | "response=\"d6f9a2c07d1c5df7b89379dca1269b35\", qop=auth, " |
| 4341 | "nc=00000002, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 4342 | }; |
| 4343 | |
| 4344 | // Sever accepts the authorization. |
| 4345 | MockRead data_reads1[] = { |
| 4346 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4347 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4348 | MockRead(false, OK), |
| 4349 | }; |
| 4350 | |
| 4351 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4352 | data_writes1, arraysize(data_writes1)); |
| 4353 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4354 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4355 | TestOldCompletionCallback callback1; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4356 | |
| 4357 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
| 4358 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4359 | |
| 4360 | rv = callback1.WaitForResult(); |
| 4361 | EXPECT_EQ(OK, rv); |
| 4362 | |
| 4363 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4364 | ASSERT_TRUE(response != NULL); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4365 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4366 | } |
| 4367 | } |
| 4368 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4369 | // Test the ResetStateForRestart() private method. |
| 4370 | TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { |
| 4371 | // Create a transaction (the dependencies aren't important). |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4372 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4373 | scoped_ptr<HttpNetworkTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4374 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4375 | |
| 4376 | // Setup some state (which we expect ResetStateForRestart() will clear). |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4377 | trans->read_buf_ = new IOBuffer(15); |
| 4378 | trans->read_buf_len_ = 15; |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 4379 | trans->request_headers_.SetHeader("Authorization", "NTLM"); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4380 | |
| 4381 | // Setup state in response_ |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 4382 | HttpResponseInfo* response = &trans->response_; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4383 | response->auth_challenge = new AuthChallengeInfo(); |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 4384 | response->ssl_info.cert_status = static_cast<CertStatus>(-1); // Nonsensical. |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4385 | response->response_time = base::Time::Now(); |
| 4386 | response->was_cached = true; // (Wouldn't ever actually be true...) |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4387 | |
| 4388 | { // Setup state for response_.vary_data |
| 4389 | HttpRequestInfo request; |
| 4390 | std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 4391 | std::replace(temp.begin(), temp.end(), '\n', '\0'); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4392 | scoped_refptr<HttpResponseHeaders> headers(new HttpResponseHeaders(temp)); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4393 | request.extra_headers.SetHeader("Foo", "1"); |
| 4394 | request.extra_headers.SetHeader("bar", "23"); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4395 | EXPECT_TRUE(response->vary_data.Init(request, *headers)); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | // Cause the above state to be reset. |
| 4399 | trans->ResetStateForRestart(); |
| 4400 | |
| 4401 | // Verify that the state that needed to be reset, has been reset. |
[email protected] | 9b6fee1 | 2009-09-29 18:13:07 | [diff] [blame] | 4402 | EXPECT_TRUE(trans->read_buf_.get() == NULL); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4403 | EXPECT_EQ(0, trans->read_buf_len_); |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 4404 | EXPECT_TRUE(trans->request_headers_.IsEmpty()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4405 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4406 | EXPECT_TRUE(response->headers.get() == NULL); |
[email protected] | 34f4094 | 2010-10-04 00:34:04 | [diff] [blame] | 4407 | EXPECT_FALSE(response->was_cached); |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 4408 | EXPECT_EQ(0U, response->ssl_info.cert_status); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4409 | EXPECT_FALSE(response->vary_data.is_valid()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4410 | } |
| 4411 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4412 | // Test HTTPS connections to a site with a bad certificate |
| 4413 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4414 | HttpRequestInfo request; |
| 4415 | request.method = "GET"; |
| 4416 | request.url = GURL("https://www.google.com/"); |
| 4417 | request.load_flags = 0; |
| 4418 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4419 | SessionDependencies session_deps; |
| 4420 | scoped_ptr<HttpTransaction> trans( |
| 4421 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4422 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4423 | MockWrite data_writes[] = { |
| 4424 | MockWrite("GET / HTTP/1.1\r\n" |
| 4425 | "Host: www.google.com\r\n" |
| 4426 | "Connection: keep-alive\r\n\r\n"), |
| 4427 | }; |
| 4428 | |
| 4429 | MockRead data_reads[] = { |
| 4430 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4431 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4432 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4433 | MockRead(false, OK), |
| 4434 | }; |
| 4435 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4436 | StaticSocketDataProvider ssl_bad_certificate; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4437 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4438 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4439 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4440 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4441 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4442 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4443 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4444 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4445 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4446 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4447 | TestOldCompletionCallback callback; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4448 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4449 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4450 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4451 | |
| 4452 | rv = callback.WaitForResult(); |
| 4453 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4454 | |
| 4455 | rv = trans->RestartIgnoringLastError(&callback); |
| 4456 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4457 | |
| 4458 | rv = callback.WaitForResult(); |
| 4459 | EXPECT_EQ(OK, rv); |
| 4460 | |
| 4461 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4462 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4463 | ASSERT_TRUE(response != NULL); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4464 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4465 | } |
| 4466 | |
| 4467 | // Test HTTPS connections to a site with a bad certificate, going through a |
| 4468 | // proxy |
| 4469 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaProxy) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 4470 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4471 | |
| 4472 | HttpRequestInfo request; |
| 4473 | request.method = "GET"; |
| 4474 | request.url = GURL("https://www.google.com/"); |
| 4475 | request.load_flags = 0; |
| 4476 | |
| 4477 | MockWrite proxy_writes[] = { |
| 4478 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 4479 | "Host: www.google.com\r\n" |
| 4480 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4481 | }; |
| 4482 | |
| 4483 | MockRead proxy_reads[] = { |
| 4484 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4485 | MockRead(false, OK) |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4486 | }; |
| 4487 | |
| 4488 | MockWrite data_writes[] = { |
| 4489 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 4490 | "Host: www.google.com\r\n" |
| 4491 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4492 | MockWrite("GET / HTTP/1.1\r\n" |
| 4493 | "Host: www.google.com\r\n" |
| 4494 | "Connection: keep-alive\r\n\r\n"), |
| 4495 | }; |
| 4496 | |
| 4497 | MockRead data_reads[] = { |
| 4498 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4499 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4500 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4501 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4502 | MockRead(false, OK), |
| 4503 | }; |
| 4504 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4505 | StaticSocketDataProvider ssl_bad_certificate( |
| 4506 | proxy_reads, arraysize(proxy_reads), |
| 4507 | proxy_writes, arraysize(proxy_writes)); |
| 4508 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4509 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4510 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4511 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4512 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4513 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4514 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4515 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4516 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4517 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4518 | TestOldCompletionCallback callback; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4519 | |
| 4520 | for (int i = 0; i < 2; i++) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4521 | session_deps.socket_factory.ResetNextMockIndexes(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4522 | |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4523 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4524 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4525 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4526 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4527 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4528 | |
| 4529 | rv = callback.WaitForResult(); |
| 4530 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4531 | |
| 4532 | rv = trans->RestartIgnoringLastError(&callback); |
| 4533 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4534 | |
| 4535 | rv = callback.WaitForResult(); |
| 4536 | EXPECT_EQ(OK, rv); |
| 4537 | |
| 4538 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4539 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4540 | ASSERT_TRUE(response != NULL); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4541 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4542 | } |
| 4543 | } |
| 4544 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4545 | |
| 4546 | // Test HTTPS connections to a site, going through an HTTPS proxy |
| 4547 | TEST_F(HttpNetworkTransactionTest, HTTPSViaHttpsProxy) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 4548 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 4549 | "https://proxy:70")); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4550 | |
| 4551 | HttpRequestInfo request; |
| 4552 | request.method = "GET"; |
| 4553 | request.url = GURL("https://www.google.com/"); |
| 4554 | request.load_flags = 0; |
| 4555 | |
| 4556 | MockWrite data_writes[] = { |
| 4557 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4558 | "Host: www.google.com\r\n" |
| 4559 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4560 | MockWrite("GET / HTTP/1.1\r\n" |
| 4561 | "Host: www.google.com\r\n" |
| 4562 | "Connection: keep-alive\r\n\r\n"), |
| 4563 | }; |
| 4564 | |
| 4565 | MockRead data_reads[] = { |
| 4566 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4567 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 4568 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4569 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4570 | MockRead(false, OK), |
| 4571 | }; |
| 4572 | |
| 4573 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4574 | data_writes, arraysize(data_writes)); |
| 4575 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4576 | SSLSocketDataProvider tunnel_ssl(true, OK); // SSL through the tunnel |
| 4577 | |
| 4578 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4579 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4580 | session_deps.socket_factory.AddSSLSocketDataProvider(&tunnel_ssl); |
| 4581 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4582 | TestOldCompletionCallback callback; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4583 | |
| 4584 | scoped_ptr<HttpTransaction> trans( |
| 4585 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4586 | |
| 4587 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4588 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4589 | |
| 4590 | rv = callback.WaitForResult(); |
| 4591 | EXPECT_EQ(OK, rv); |
| 4592 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4593 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4594 | ASSERT_TRUE(response != NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4595 | |
| 4596 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 4597 | EXPECT_EQ(200, response->headers->response_code()); |
| 4598 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4599 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4600 | } |
| 4601 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4602 | // Test an HTTPS Proxy's ability to redirect a CONNECT request |
| 4603 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaHttpsProxy) { |
| 4604 | SessionDependencies session_deps( |
| 4605 | ProxyService::CreateFixed("https://proxy:70")); |
| 4606 | |
| 4607 | HttpRequestInfo request; |
| 4608 | request.method = "GET"; |
| 4609 | request.url = GURL("https://www.google.com/"); |
| 4610 | request.load_flags = 0; |
| 4611 | |
| 4612 | MockWrite data_writes[] = { |
| 4613 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4614 | "Host: www.google.com\r\n" |
| 4615 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4616 | }; |
| 4617 | |
| 4618 | MockRead data_reads[] = { |
| 4619 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 4620 | MockRead("Location: http://login.example.com/\r\n"), |
| 4621 | MockRead("Content-Length: 0\r\n\r\n"), |
| 4622 | MockRead(false, OK), |
| 4623 | }; |
| 4624 | |
| 4625 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4626 | data_writes, arraysize(data_writes)); |
| 4627 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4628 | |
| 4629 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4630 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4631 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4632 | TestOldCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4633 | |
| 4634 | scoped_ptr<HttpTransaction> trans( |
| 4635 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4636 | |
| 4637 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4638 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4639 | |
| 4640 | rv = callback.WaitForResult(); |
| 4641 | EXPECT_EQ(OK, rv); |
| 4642 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4643 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4644 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4645 | |
| 4646 | EXPECT_EQ(302, response->headers->response_code()); |
| 4647 | std::string url; |
| 4648 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 4649 | EXPECT_EQ("http://login.example.com/", url); |
| 4650 | } |
| 4651 | |
| 4652 | // Test an HTTPS (SPDY) Proxy's ability to redirect a CONNECT request |
| 4653 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaSpdyProxy) { |
| 4654 | SessionDependencies session_deps( |
| 4655 | ProxyService::CreateFixed("https://proxy:70")); |
| 4656 | |
| 4657 | HttpRequestInfo request; |
| 4658 | request.method = "GET"; |
| 4659 | request.url = GURL("https://www.google.com/"); |
| 4660 | request.load_flags = 0; |
| 4661 | |
| 4662 | scoped_ptr<spdy::SpdyFrame> conn(ConstructSpdyConnect(NULL, 0, 1)); |
| 4663 | scoped_ptr<spdy::SpdyFrame> goaway(ConstructSpdyRstStream(1, spdy::CANCEL)); |
| 4664 | MockWrite data_writes[] = { |
| 4665 | CreateMockWrite(*conn.get(), 0, false), |
| 4666 | }; |
| 4667 | |
| 4668 | static const char* const kExtraHeaders[] = { |
| 4669 | "location", |
| 4670 | "http://login.example.com/", |
| 4671 | }; |
| 4672 | scoped_ptr<spdy::SpdyFrame> resp( |
| 4673 | ConstructSpdySynReplyError("302 Redirect", kExtraHeaders, |
| 4674 | arraysize(kExtraHeaders)/2, 1)); |
| 4675 | MockRead data_reads[] = { |
| 4676 | CreateMockRead(*resp.get(), 1, false), |
| 4677 | MockRead(true, 0, 2), // EOF |
| 4678 | }; |
| 4679 | |
| 4680 | scoped_refptr<DelayedSocketData> data( |
| 4681 | new DelayedSocketData( |
| 4682 | 1, // wait for one write to finish before reading. |
| 4683 | data_reads, arraysize(data_reads), |
| 4684 | data_writes, arraysize(data_writes))); |
| 4685 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4686 | proxy_ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 4687 | proxy_ssl.next_proto = "spdy/2"; |
| 4688 | proxy_ssl.was_npn_negotiated = true; |
| 4689 | |
| 4690 | session_deps.socket_factory.AddSocketDataProvider(data.get()); |
| 4691 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4692 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4693 | TestOldCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4694 | |
| 4695 | scoped_ptr<HttpTransaction> trans( |
| 4696 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4697 | |
| 4698 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4699 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4700 | |
| 4701 | rv = callback.WaitForResult(); |
| 4702 | EXPECT_EQ(OK, rv); |
| 4703 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4704 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4705 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4706 | |
| 4707 | EXPECT_EQ(302, response->headers->response_code()); |
| 4708 | std::string url; |
| 4709 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 4710 | EXPECT_EQ("http://login.example.com/", url); |
| 4711 | } |
| 4712 | |
| 4713 | // Test an HTTPS Proxy's ability to provide a response to a CONNECT request |
| 4714 | TEST_F(HttpNetworkTransactionTest, ErrorResponseTofHttpsConnectViaHttpsProxy) { |
| 4715 | SessionDependencies session_deps( |
| 4716 | ProxyService::CreateFixed("https://proxy:70")); |
| 4717 | |
| 4718 | HttpRequestInfo request; |
| 4719 | request.method = "GET"; |
| 4720 | request.url = GURL("https://www.google.com/"); |
| 4721 | request.load_flags = 0; |
| 4722 | |
| 4723 | MockWrite data_writes[] = { |
| 4724 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4725 | "Host: www.google.com\r\n" |
| 4726 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4727 | }; |
| 4728 | |
| 4729 | MockRead data_reads[] = { |
| 4730 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 4731 | MockRead("Content-Length: 23\r\n\r\n"), |
| 4732 | MockRead("The host does not exist"), |
| 4733 | MockRead(false, OK), |
| 4734 | }; |
| 4735 | |
| 4736 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4737 | data_writes, arraysize(data_writes)); |
| 4738 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4739 | |
| 4740 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4741 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4742 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4743 | TestOldCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4744 | |
| 4745 | scoped_ptr<HttpTransaction> trans( |
| 4746 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4747 | |
| 4748 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4749 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4750 | |
| 4751 | rv = callback.WaitForResult(); |
| 4752 | EXPECT_EQ(OK, rv); |
| 4753 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4754 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4755 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4756 | |
| 4757 | EXPECT_EQ(404, response->headers->response_code()); |
| 4758 | EXPECT_EQ(23, response->headers->GetContentLength()); |
| 4759 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4760 | EXPECT_FALSE(response->ssl_info.is_valid()); |
| 4761 | |
| 4762 | std::string response_data; |
| 4763 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 4764 | EXPECT_EQ("The host does not exist", response_data); |
| 4765 | } |
| 4766 | |
| 4767 | // Test an HTTPS (SPDY) Proxy's ability to provide a response to a CONNECT |
| 4768 | // request |
| 4769 | TEST_F(HttpNetworkTransactionTest, ErrorResponseTofHttpsConnectViaSpdyProxy) { |
| 4770 | SessionDependencies session_deps( |
| 4771 | ProxyService::CreateFixed("https://proxy:70")); |
| 4772 | |
| 4773 | HttpRequestInfo request; |
| 4774 | request.method = "GET"; |
| 4775 | request.url = GURL("https://www.google.com/"); |
| 4776 | request.load_flags = 0; |
| 4777 | |
| 4778 | scoped_ptr<spdy::SpdyFrame> conn(ConstructSpdyConnect(NULL, 0, 1)); |
| 4779 | scoped_ptr<spdy::SpdyFrame> goaway(ConstructSpdyRstStream(1, spdy::CANCEL)); |
| 4780 | MockWrite data_writes[] = { |
| 4781 | CreateMockWrite(*conn.get(), 0, false), |
| 4782 | }; |
| 4783 | |
| 4784 | static const char* const kExtraHeaders[] = { |
| 4785 | "location", |
| 4786 | "http://login.example.com/", |
| 4787 | }; |
| 4788 | scoped_ptr<spdy::SpdyFrame> resp( |
| 4789 | ConstructSpdySynReplyError("404 Not Found", kExtraHeaders, |
| 4790 | arraysize(kExtraHeaders)/2, 1)); |
| 4791 | scoped_ptr<spdy::SpdyFrame> body( |
| 4792 | ConstructSpdyBodyFrame(1, "The host does not exist", 23, true)); |
| 4793 | MockRead data_reads[] = { |
| 4794 | CreateMockRead(*resp.get(), 1, false), |
| 4795 | CreateMockRead(*body.get(), 2, false), |
| 4796 | MockRead(true, 0, 3), // EOF |
| 4797 | }; |
| 4798 | |
| 4799 | scoped_refptr<DelayedSocketData> data( |
| 4800 | new DelayedSocketData( |
| 4801 | 1, // wait for one write to finish before reading. |
| 4802 | data_reads, arraysize(data_reads), |
| 4803 | data_writes, arraysize(data_writes))); |
| 4804 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4805 | proxy_ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 4806 | proxy_ssl.next_proto = "spdy/2"; |
| 4807 | proxy_ssl.was_npn_negotiated = true; |
| 4808 | |
| 4809 | session_deps.socket_factory.AddSocketDataProvider(data.get()); |
| 4810 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4811 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4812 | TestOldCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4813 | |
| 4814 | scoped_ptr<HttpTransaction> trans( |
| 4815 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4816 | |
| 4817 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4818 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4819 | |
| 4820 | rv = callback.WaitForResult(); |
| 4821 | EXPECT_EQ(OK, rv); |
| 4822 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4823 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4824 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4825 | |
| 4826 | EXPECT_EQ(404, response->headers->response_code()); |
| 4827 | EXPECT_FALSE(response->ssl_info.is_valid()); |
| 4828 | |
| 4829 | std::string response_data; |
| 4830 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 4831 | EXPECT_EQ("The host does not exist", response_data); |
| 4832 | } |
| 4833 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4834 | // Test HTTPS connections to a site with a bad certificate, going through an |
| 4835 | // HTTPS proxy |
| 4836 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaHttpsProxy) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 4837 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 4838 | "https://proxy:70")); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4839 | |
| 4840 | HttpRequestInfo request; |
| 4841 | request.method = "GET"; |
| 4842 | request.url = GURL("https://www.google.com/"); |
| 4843 | request.load_flags = 0; |
| 4844 | |
| 4845 | // Attempt to fetch the URL from a server with a bad cert |
| 4846 | MockWrite bad_cert_writes[] = { |
| 4847 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4848 | "Host: www.google.com\r\n" |
| 4849 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4850 | }; |
| 4851 | |
| 4852 | MockRead bad_cert_reads[] = { |
| 4853 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4854 | MockRead(false, OK) |
| 4855 | }; |
| 4856 | |
| 4857 | // Attempt to fetch the URL with a good cert |
| 4858 | MockWrite good_data_writes[] = { |
| 4859 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4860 | "Host: www.google.com\r\n" |
| 4861 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4862 | MockWrite("GET / HTTP/1.1\r\n" |
| 4863 | "Host: www.google.com\r\n" |
| 4864 | "Connection: keep-alive\r\n\r\n"), |
| 4865 | }; |
| 4866 | |
| 4867 | MockRead good_cert_reads[] = { |
| 4868 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4869 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4870 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4871 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4872 | MockRead(false, OK), |
| 4873 | }; |
| 4874 | |
| 4875 | StaticSocketDataProvider ssl_bad_certificate( |
| 4876 | bad_cert_reads, arraysize(bad_cert_reads), |
| 4877 | bad_cert_writes, arraysize(bad_cert_writes)); |
| 4878 | StaticSocketDataProvider data(good_cert_reads, arraysize(good_cert_reads), |
| 4879 | good_data_writes, arraysize(good_data_writes)); |
| 4880 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4881 | SSLSocketDataProvider ssl(true, OK); |
| 4882 | |
| 4883 | // SSL to the proxy, then CONNECT request, then SSL with bad certificate |
| 4884 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4885 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4886 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4887 | |
| 4888 | // SSL to the proxy, then CONNECT request, then valid SSL certificate |
| 4889 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4890 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4891 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4892 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4893 | TestOldCompletionCallback callback; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4894 | |
| 4895 | scoped_ptr<HttpTransaction> trans( |
| 4896 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4897 | |
| 4898 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4899 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4900 | |
| 4901 | rv = callback.WaitForResult(); |
| 4902 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4903 | |
| 4904 | rv = trans->RestartIgnoringLastError(&callback); |
| 4905 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4906 | |
| 4907 | rv = callback.WaitForResult(); |
| 4908 | EXPECT_EQ(OK, rv); |
| 4909 | |
| 4910 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4911 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4912 | ASSERT_TRUE(response != NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4913 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4914 | } |
| 4915 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4916 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4917 | HttpRequestInfo request; |
| 4918 | request.method = "GET"; |
| 4919 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4920 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 4921 | "Chromium Ultra Awesome X Edition"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4922 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4923 | SessionDependencies session_deps; |
| 4924 | scoped_ptr<HttpTransaction> trans( |
| 4925 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4926 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4927 | MockWrite data_writes[] = { |
| 4928 | MockWrite("GET / HTTP/1.1\r\n" |
| 4929 | "Host: www.google.com\r\n" |
| 4930 | "Connection: keep-alive\r\n" |
| 4931 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4932 | }; |
| 4933 | |
| 4934 | // Lastly, the server responds with the actual content. |
| 4935 | MockRead data_reads[] = { |
| 4936 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4937 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4938 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4939 | MockRead(false, OK), |
| 4940 | }; |
| 4941 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4942 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4943 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4944 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4945 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4946 | TestOldCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4947 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4948 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4949 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4950 | |
| 4951 | rv = callback.WaitForResult(); |
| 4952 | EXPECT_EQ(OK, rv); |
| 4953 | } |
| 4954 | |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4955 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgentOverTunnel) { |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4956 | HttpRequestInfo request; |
| 4957 | request.method = "GET"; |
| 4958 | request.url = GURL("https://www.google.com/"); |
| 4959 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 4960 | "Chromium Ultra Awesome X Edition"); |
| 4961 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4962 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 4963 | scoped_ptr<HttpTransaction> trans( |
| 4964 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4965 | |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4966 | MockWrite data_writes[] = { |
| 4967 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4968 | "Host: www.google.com\r\n" |
| 4969 | "Proxy-Connection: keep-alive\r\n" |
| 4970 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4971 | }; |
| 4972 | MockRead data_reads[] = { |
| 4973 | // Return an error, so the transaction stops here (this test isn't |
| 4974 | // interested in the rest). |
| 4975 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4976 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4977 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 4978 | }; |
| 4979 | |
| 4980 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4981 | data_writes, arraysize(data_writes)); |
| 4982 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4983 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 4984 | TestOldCompletionCallback callback; |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4985 | |
| 4986 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4987 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4988 | |
| 4989 | rv = callback.WaitForResult(); |
| 4990 | EXPECT_EQ(OK, rv); |
| 4991 | } |
| 4992 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4993 | TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4994 | HttpRequestInfo request; |
| 4995 | request.method = "GET"; |
| 4996 | request.url = GURL("http://www.google.com/"); |
| 4997 | request.load_flags = 0; |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 4998 | request.extra_headers.SetHeader(HttpRequestHeaders::kReferer, |
| 4999 | "http://the.previous.site.com/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5000 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5001 | SessionDependencies session_deps; |
| 5002 | scoped_ptr<HttpTransaction> trans( |
| 5003 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5004 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5005 | MockWrite data_writes[] = { |
| 5006 | MockWrite("GET / HTTP/1.1\r\n" |
| 5007 | "Host: www.google.com\r\n" |
| 5008 | "Connection: keep-alive\r\n" |
| 5009 | "Referer: http://the.previous.site.com/\r\n\r\n"), |
| 5010 | }; |
| 5011 | |
| 5012 | // Lastly, the server responds with the actual content. |
| 5013 | MockRead data_reads[] = { |
| 5014 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5015 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5016 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5017 | MockRead(false, OK), |
| 5018 | }; |
| 5019 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5020 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5021 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5022 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5023 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5024 | TestOldCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5025 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5026 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5027 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5028 | |
| 5029 | rv = callback.WaitForResult(); |
| 5030 | EXPECT_EQ(OK, rv); |
| 5031 | } |
| 5032 | |
| 5033 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5034 | HttpRequestInfo request; |
| 5035 | request.method = "POST"; |
| 5036 | request.url = GURL("http://www.google.com/"); |
| 5037 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5038 | SessionDependencies session_deps; |
| 5039 | scoped_ptr<HttpTransaction> trans( |
| 5040 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5041 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5042 | MockWrite data_writes[] = { |
| 5043 | MockWrite("POST / HTTP/1.1\r\n" |
| 5044 | "Host: www.google.com\r\n" |
| 5045 | "Connection: keep-alive\r\n" |
| 5046 | "Content-Length: 0\r\n\r\n"), |
| 5047 | }; |
| 5048 | |
| 5049 | // Lastly, the server responds with the actual content. |
| 5050 | MockRead data_reads[] = { |
| 5051 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5052 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5053 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5054 | MockRead(false, OK), |
| 5055 | }; |
| 5056 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5057 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5058 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5059 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5060 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5061 | TestOldCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5062 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5063 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5064 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5065 | |
| 5066 | rv = callback.WaitForResult(); |
| 5067 | EXPECT_EQ(OK, rv); |
| 5068 | } |
| 5069 | |
| 5070 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5071 | HttpRequestInfo request; |
| 5072 | request.method = "PUT"; |
| 5073 | request.url = GURL("http://www.google.com/"); |
| 5074 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5075 | SessionDependencies session_deps; |
| 5076 | scoped_ptr<HttpTransaction> trans( |
| 5077 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5078 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5079 | MockWrite data_writes[] = { |
| 5080 | MockWrite("PUT / HTTP/1.1\r\n" |
| 5081 | "Host: www.google.com\r\n" |
| 5082 | "Connection: keep-alive\r\n" |
| 5083 | "Content-Length: 0\r\n\r\n"), |
| 5084 | }; |
| 5085 | |
| 5086 | // Lastly, the server responds with the actual content. |
| 5087 | MockRead data_reads[] = { |
| 5088 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5089 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5090 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5091 | MockRead(false, OK), |
| 5092 | }; |
| 5093 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5094 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5095 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5096 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5097 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5098 | TestOldCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5099 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5100 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5101 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5102 | |
| 5103 | rv = callback.WaitForResult(); |
| 5104 | EXPECT_EQ(OK, rv); |
| 5105 | } |
| 5106 | |
| 5107 | TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5108 | HttpRequestInfo request; |
| 5109 | request.method = "HEAD"; |
| 5110 | request.url = GURL("http://www.google.com/"); |
| 5111 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5112 | SessionDependencies session_deps; |
| 5113 | scoped_ptr<HttpTransaction> trans( |
| 5114 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5115 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5116 | MockWrite data_writes[] = { |
| 5117 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 5118 | "Host: www.google.com\r\n" |
| 5119 | "Connection: keep-alive\r\n" |
| 5120 | "Content-Length: 0\r\n\r\n"), |
| 5121 | }; |
| 5122 | |
| 5123 | // Lastly, the server responds with the actual content. |
| 5124 | MockRead data_reads[] = { |
| 5125 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5126 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5127 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5128 | MockRead(false, OK), |
| 5129 | }; |
| 5130 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5131 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5132 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5133 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5134 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5135 | TestOldCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5136 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5137 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5138 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5139 | |
| 5140 | rv = callback.WaitForResult(); |
| 5141 | EXPECT_EQ(OK, rv); |
| 5142 | } |
| 5143 | |
| 5144 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5145 | HttpRequestInfo request; |
| 5146 | request.method = "GET"; |
| 5147 | request.url = GURL("http://www.google.com/"); |
| 5148 | request.load_flags = LOAD_BYPASS_CACHE; |
| 5149 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5150 | SessionDependencies session_deps; |
| 5151 | scoped_ptr<HttpTransaction> trans( |
| 5152 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5153 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5154 | MockWrite data_writes[] = { |
| 5155 | MockWrite("GET / HTTP/1.1\r\n" |
| 5156 | "Host: www.google.com\r\n" |
| 5157 | "Connection: keep-alive\r\n" |
| 5158 | "Pragma: no-cache\r\n" |
| 5159 | "Cache-Control: no-cache\r\n\r\n"), |
| 5160 | }; |
| 5161 | |
| 5162 | // Lastly, the server responds with the actual content. |
| 5163 | MockRead data_reads[] = { |
| 5164 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5165 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5166 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5167 | MockRead(false, OK), |
| 5168 | }; |
| 5169 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5170 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5171 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5172 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5173 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5174 | TestOldCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5175 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5176 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5177 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5178 | |
| 5179 | rv = callback.WaitForResult(); |
| 5180 | EXPECT_EQ(OK, rv); |
| 5181 | } |
| 5182 | |
| 5183 | TEST_F(HttpNetworkTransactionTest, |
| 5184 | BuildRequest_CacheControlValidateCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5185 | HttpRequestInfo request; |
| 5186 | request.method = "GET"; |
| 5187 | request.url = GURL("http://www.google.com/"); |
| 5188 | request.load_flags = LOAD_VALIDATE_CACHE; |
| 5189 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5190 | SessionDependencies session_deps; |
| 5191 | scoped_ptr<HttpTransaction> trans( |
| 5192 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5193 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5194 | MockWrite data_writes[] = { |
| 5195 | MockWrite("GET / HTTP/1.1\r\n" |
| 5196 | "Host: www.google.com\r\n" |
| 5197 | "Connection: keep-alive\r\n" |
| 5198 | "Cache-Control: max-age=0\r\n\r\n"), |
| 5199 | }; |
| 5200 | |
| 5201 | // Lastly, the server responds with the actual content. |
| 5202 | MockRead data_reads[] = { |
| 5203 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5204 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5205 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5206 | MockRead(false, OK), |
| 5207 | }; |
| 5208 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5209 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5210 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5211 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5212 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5213 | TestOldCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5214 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5215 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5216 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5217 | |
| 5218 | rv = callback.WaitForResult(); |
| 5219 | EXPECT_EQ(OK, rv); |
| 5220 | } |
| 5221 | |
| 5222 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5223 | HttpRequestInfo request; |
| 5224 | request.method = "GET"; |
| 5225 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5226 | request.extra_headers.SetHeader("FooHeader", "Bar"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5227 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5228 | SessionDependencies session_deps; |
| 5229 | scoped_ptr<HttpTransaction> trans( |
| 5230 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5231 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5232 | MockWrite data_writes[] = { |
| 5233 | MockWrite("GET / HTTP/1.1\r\n" |
| 5234 | "Host: www.google.com\r\n" |
| 5235 | "Connection: keep-alive\r\n" |
| 5236 | "FooHeader: Bar\r\n\r\n"), |
| 5237 | }; |
| 5238 | |
| 5239 | // Lastly, the server responds with the actual content. |
| 5240 | MockRead data_reads[] = { |
| 5241 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5242 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5243 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5244 | MockRead(false, OK), |
| 5245 | }; |
| 5246 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5247 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5248 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5249 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5250 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5251 | TestOldCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5252 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5253 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5254 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5255 | |
| 5256 | rv = callback.WaitForResult(); |
| 5257 | EXPECT_EQ(OK, rv); |
| 5258 | } |
| 5259 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5260 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeadersStripped) { |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5261 | HttpRequestInfo request; |
| 5262 | request.method = "GET"; |
| 5263 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5264 | request.extra_headers.SetHeader("referer", "www.foo.com"); |
| 5265 | request.extra_headers.SetHeader("hEllo", "Kitty"); |
| 5266 | request.extra_headers.SetHeader("FoO", "bar"); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5267 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5268 | SessionDependencies session_deps; |
| 5269 | scoped_ptr<HttpTransaction> trans( |
| 5270 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5271 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5272 | MockWrite data_writes[] = { |
| 5273 | MockWrite("GET / HTTP/1.1\r\n" |
| 5274 | "Host: www.google.com\r\n" |
| 5275 | "Connection: keep-alive\r\n" |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 5276 | "referer: www.foo.com\r\n" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5277 | "hEllo: Kitty\r\n" |
| 5278 | "FoO: bar\r\n\r\n"), |
| 5279 | }; |
| 5280 | |
| 5281 | // Lastly, the server responds with the actual content. |
| 5282 | MockRead data_reads[] = { |
| 5283 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5284 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5285 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5286 | MockRead(false, OK), |
| 5287 | }; |
| 5288 | |
| 5289 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5290 | data_writes, arraysize(data_writes)); |
| 5291 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5292 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5293 | TestOldCompletionCallback callback; |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5294 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5295 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5296 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5297 | |
| 5298 | rv = callback.WaitForResult(); |
| 5299 | EXPECT_EQ(OK, rv); |
| 5300 | } |
| 5301 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5302 | TEST_F(HttpNetworkTransactionTest, SOCKS4_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5303 | HttpRequestInfo request; |
| 5304 | request.method = "GET"; |
| 5305 | request.url = GURL("http://www.google.com/"); |
| 5306 | request.load_flags = 0; |
| 5307 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5308 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5309 | ProxyService::CreateFixed("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5310 | |
| 5311 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5312 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5313 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5314 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 5315 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 5316 | |
| 5317 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5318 | MockWrite(true, write_buffer, arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5319 | MockWrite("GET / HTTP/1.1\r\n" |
| 5320 | "Host: www.google.com\r\n" |
| 5321 | "Connection: keep-alive\r\n\r\n") |
| 5322 | }; |
| 5323 | |
| 5324 | MockRead data_reads[] = { |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5325 | MockRead(true, read_buffer, arraysize(read_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5326 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5327 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5328 | MockRead("Payload"), |
| 5329 | MockRead(false, OK) |
| 5330 | }; |
| 5331 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5332 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5333 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5334 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5335 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5336 | TestOldCompletionCallback callback; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5337 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5338 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5339 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5340 | |
| 5341 | rv = callback.WaitForResult(); |
| 5342 | EXPECT_EQ(OK, rv); |
| 5343 | |
| 5344 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5345 | ASSERT_TRUE(response != NULL); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5346 | |
| 5347 | std::string response_text; |
| 5348 | rv = ReadTransaction(trans.get(), &response_text); |
| 5349 | EXPECT_EQ(OK, rv); |
| 5350 | EXPECT_EQ("Payload", response_text); |
| 5351 | } |
| 5352 | |
| 5353 | TEST_F(HttpNetworkTransactionTest, SOCKS4_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5354 | HttpRequestInfo request; |
| 5355 | request.method = "GET"; |
| 5356 | request.url = GURL("https://www.google.com/"); |
| 5357 | request.load_flags = 0; |
| 5358 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5359 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5360 | ProxyService::CreateFixed("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5361 | |
| 5362 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5363 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5364 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5365 | unsigned char write_buffer[] = { 0x04, 0x01, 0x01, 0xBB, 127, 0, 0, 1, 0 }; |
| 5366 | unsigned char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 5367 | |
| 5368 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5369 | MockWrite(true, reinterpret_cast<char*>(write_buffer), |
| 5370 | arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5371 | MockWrite("GET / HTTP/1.1\r\n" |
| 5372 | "Host: www.google.com\r\n" |
| 5373 | "Connection: keep-alive\r\n\r\n") |
| 5374 | }; |
| 5375 | |
| 5376 | MockRead data_reads[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5377 | MockWrite(true, reinterpret_cast<char*>(read_buffer), |
| 5378 | arraysize(read_buffer)), |
| 5379 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5380 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5381 | MockRead("Payload"), |
| 5382 | MockRead(false, OK) |
| 5383 | }; |
| 5384 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5385 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5386 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5387 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5388 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5389 | SSLSocketDataProvider ssl(true, OK); |
| 5390 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5391 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5392 | TestOldCompletionCallback callback; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5393 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5394 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5395 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5396 | |
| 5397 | rv = callback.WaitForResult(); |
| 5398 | EXPECT_EQ(OK, rv); |
| 5399 | |
| 5400 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5401 | ASSERT_TRUE(response != NULL); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5402 | |
| 5403 | std::string response_text; |
| 5404 | rv = ReadTransaction(trans.get(), &response_text); |
| 5405 | EXPECT_EQ(OK, rv); |
| 5406 | EXPECT_EQ("Payload", response_text); |
| 5407 | } |
| 5408 | |
| 5409 | TEST_F(HttpNetworkTransactionTest, SOCKS5_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5410 | HttpRequestInfo request; |
| 5411 | request.method = "GET"; |
| 5412 | request.url = GURL("http://www.google.com/"); |
| 5413 | request.load_flags = 0; |
| 5414 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5415 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5416 | ProxyService::CreateFixed("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5417 | |
| 5418 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5419 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5420 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5421 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 5422 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 5423 | const char kSOCKS5OkRequest[] = { |
| 5424 | 0x05, // Version |
| 5425 | 0x01, // Command (CONNECT) |
| 5426 | 0x00, // Reserved. |
| 5427 | 0x03, // Address type (DOMAINNAME). |
| 5428 | 0x0E, // Length of domain (14) |
| 5429 | // Domain string: |
| 5430 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 5431 | 0x00, 0x50, // 16-bit port (80) |
| 5432 | }; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5433 | const char kSOCKS5OkResponse[] = |
| 5434 | { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 5435 | |
| 5436 | MockWrite data_writes[] = { |
| 5437 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 5438 | MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)), |
| 5439 | MockWrite("GET / HTTP/1.1\r\n" |
| 5440 | "Host: www.google.com\r\n" |
| 5441 | "Connection: keep-alive\r\n\r\n") |
| 5442 | }; |
| 5443 | |
| 5444 | MockRead data_reads[] = { |
| 5445 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 5446 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
| 5447 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5448 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5449 | MockRead("Payload"), |
| 5450 | MockRead(false, OK) |
| 5451 | }; |
| 5452 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5453 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5454 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5455 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5456 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5457 | TestOldCompletionCallback callback; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5458 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5459 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5460 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5461 | |
| 5462 | rv = callback.WaitForResult(); |
| 5463 | EXPECT_EQ(OK, rv); |
| 5464 | |
| 5465 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5466 | ASSERT_TRUE(response != NULL); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5467 | |
| 5468 | std::string response_text; |
| 5469 | rv = ReadTransaction(trans.get(), &response_text); |
| 5470 | EXPECT_EQ(OK, rv); |
| 5471 | EXPECT_EQ("Payload", response_text); |
| 5472 | } |
| 5473 | |
| 5474 | TEST_F(HttpNetworkTransactionTest, SOCKS5_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5475 | HttpRequestInfo request; |
| 5476 | request.method = "GET"; |
| 5477 | request.url = GURL("https://www.google.com/"); |
| 5478 | request.load_flags = 0; |
| 5479 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5480 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5481 | ProxyService::CreateFixed("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5482 | |
| 5483 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5484 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5485 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5486 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 5487 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 5488 | const unsigned char kSOCKS5OkRequest[] = { |
| 5489 | 0x05, // Version |
| 5490 | 0x01, // Command (CONNECT) |
| 5491 | 0x00, // Reserved. |
| 5492 | 0x03, // Address type (DOMAINNAME). |
| 5493 | 0x0E, // Length of domain (14) |
| 5494 | // Domain string: |
| 5495 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 5496 | 0x01, 0xBB, // 16-bit port (443) |
| 5497 | }; |
| 5498 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5499 | const char kSOCKS5OkResponse[] = |
| 5500 | { 0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0x00, 0x00 }; |
| 5501 | |
| 5502 | MockWrite data_writes[] = { |
| 5503 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 5504 | MockWrite(true, reinterpret_cast<const char*>(kSOCKS5OkRequest), |
| 5505 | arraysize(kSOCKS5OkRequest)), |
| 5506 | MockWrite("GET / HTTP/1.1\r\n" |
| 5507 | "Host: www.google.com\r\n" |
| 5508 | "Connection: keep-alive\r\n\r\n") |
| 5509 | }; |
| 5510 | |
| 5511 | MockRead data_reads[] = { |
| 5512 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 5513 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5514 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5515 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5516 | MockRead("Payload"), |
| 5517 | MockRead(false, OK) |
| 5518 | }; |
| 5519 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5520 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5521 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5522 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5523 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5524 | SSLSocketDataProvider ssl(true, OK); |
| 5525 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5526 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5527 | TestOldCompletionCallback callback; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5528 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5529 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5530 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5531 | |
| 5532 | rv = callback.WaitForResult(); |
| 5533 | EXPECT_EQ(OK, rv); |
| 5534 | |
| 5535 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5536 | ASSERT_TRUE(response != NULL); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5537 | |
| 5538 | std::string response_text; |
| 5539 | rv = ReadTransaction(trans.get(), &response_text); |
| 5540 | EXPECT_EQ(OK, rv); |
| 5541 | EXPECT_EQ("Payload", response_text); |
| 5542 | } |
| 5543 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5544 | // Tests that for connection endpoints the group names are correctly set. |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5545 | |
| 5546 | struct GroupNameTest { |
| 5547 | std::string proxy_server; |
| 5548 | std::string url; |
| 5549 | std::string expected_group_name; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5550 | bool ssl; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5551 | }; |
| 5552 | |
| 5553 | scoped_refptr<HttpNetworkSession> SetupSessionForGroupNameTests( |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5554 | SessionDependencies* session_deps) { |
| 5555 | scoped_refptr<HttpNetworkSession> session(CreateSession(session_deps)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5556 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 5557 | HttpServerProperties* http_server_properties = |
| 5558 | session->http_server_properties(); |
| 5559 | http_server_properties->SetAlternateProtocol( |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5560 | HostPortPair("host.with.alternate", 80), 443, |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 5561 | NPN_SPDY_2); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5562 | |
| 5563 | return session; |
| 5564 | } |
| 5565 | |
| 5566 | int GroupNameTransactionHelper( |
| 5567 | const std::string& url, |
| 5568 | const scoped_refptr<HttpNetworkSession>& session) { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5569 | HttpRequestInfo request; |
| 5570 | request.method = "GET"; |
| 5571 | request.url = GURL(url); |
| 5572 | request.load_flags = 0; |
| 5573 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5574 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5575 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5576 | TestOldCompletionCallback callback; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5577 | |
| 5578 | // We do not complete this request, the dtor will clean the transaction up. |
| 5579 | return trans->Start(&request, &callback, BoundNetLog()); |
| 5580 | } |
| 5581 | |
| 5582 | TEST_F(HttpNetworkTransactionTest, GroupNameForDirectConnections) { |
| 5583 | const GroupNameTest tests[] = { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5584 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5585 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5586 | "http://www.google.com/direct", |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5587 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5588 | false, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5589 | }, |
| 5590 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5591 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5592 | "http://[2001:1418:13:1::25]/direct", |
| 5593 | "[2001:1418:13:1::25]:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5594 | false, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5595 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5596 | |
| 5597 | // SSL Tests |
| 5598 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5599 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5600 | "https://www.google.com/direct_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5601 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5602 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5603 | }, |
| 5604 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5605 | "", // unused |
| 5606 | "https://[2001:1418:13:1::25]/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5607 | "ssl/[2001:1418:13:1::25]:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5608 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5609 | }, |
| 5610 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5611 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5612 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5613 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5614 | true, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5615 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5616 | }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5617 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5618 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5619 | |
| 5620 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5621 | SessionDependencies session_deps( |
| 5622 | ProxyService::CreateFixed(tests[i].proxy_server)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5623 | scoped_refptr<HttpNetworkSession> session( |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5624 | SetupSessionForGroupNameTests(&session_deps)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5625 | |
| 5626 | HttpNetworkSessionPeer peer(session); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 5627 | CaptureGroupNameTransportSocketPool* transport_conn_pool = |
| 5628 | new CaptureGroupNameTransportSocketPool(NULL, NULL); |
| 5629 | peer.SetTransportSocketPool(transport_conn_pool); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5630 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5631 | new CaptureGroupNameSSLSocketPool(NULL, NULL); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5632 | peer.SetSSLSocketPool(ssl_conn_pool); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5633 | |
| 5634 | EXPECT_EQ(ERR_IO_PENDING, |
| 5635 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5636 | if (tests[i].ssl) |
| 5637 | EXPECT_EQ(tests[i].expected_group_name, |
| 5638 | ssl_conn_pool->last_group_name_received()); |
| 5639 | else |
| 5640 | EXPECT_EQ(tests[i].expected_group_name, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 5641 | transport_conn_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5642 | } |
| 5643 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5644 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5645 | } |
| 5646 | |
| 5647 | TEST_F(HttpNetworkTransactionTest, GroupNameForHTTPProxyConnections) { |
| 5648 | const GroupNameTest tests[] = { |
| 5649 | { |
| 5650 | "http_proxy", |
| 5651 | "http://www.google.com/http_proxy_normal", |
| 5652 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5653 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5654 | }, |
| 5655 | |
| 5656 | // SSL Tests |
| 5657 | { |
| 5658 | "http_proxy", |
| 5659 | "https://www.google.com/http_connect_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5660 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5661 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5662 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 5663 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5664 | { |
| 5665 | "http_proxy", |
| 5666 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5667 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5668 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5669 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5670 | }; |
| 5671 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5672 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5673 | |
| 5674 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5675 | SessionDependencies session_deps( |
| 5676 | ProxyService::CreateFixed(tests[i].proxy_server)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5677 | scoped_refptr<HttpNetworkSession> session( |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5678 | SetupSessionForGroupNameTests(&session_deps)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5679 | |
| 5680 | HttpNetworkSessionPeer peer(session); |
| 5681 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5682 | HostPortPair proxy_host("http_proxy", 80); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5683 | CaptureGroupNameHttpProxySocketPool* http_proxy_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5684 | new CaptureGroupNameHttpProxySocketPool(NULL, NULL); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5685 | peer.SetSocketPoolForHTTPProxy(proxy_host, http_proxy_pool); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5686 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5687 | new CaptureGroupNameSSLSocketPool(NULL, NULL); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5688 | peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5689 | |
| 5690 | EXPECT_EQ(ERR_IO_PENDING, |
| 5691 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5692 | if (tests[i].ssl) |
| 5693 | EXPECT_EQ(tests[i].expected_group_name, |
| 5694 | ssl_conn_pool->last_group_name_received()); |
| 5695 | else |
| 5696 | EXPECT_EQ(tests[i].expected_group_name, |
| 5697 | http_proxy_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5698 | } |
| 5699 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5700 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5701 | } |
| 5702 | |
| 5703 | TEST_F(HttpNetworkTransactionTest, GroupNameForSOCKSConnections) { |
| 5704 | const GroupNameTest tests[] = { |
| 5705 | { |
| 5706 | "socks4://socks_proxy:1080", |
| 5707 | "http://www.google.com/socks4_direct", |
| 5708 | "socks4/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5709 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5710 | }, |
| 5711 | { |
| 5712 | "socks5://socks_proxy:1080", |
| 5713 | "http://www.google.com/socks5_direct", |
| 5714 | "socks5/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5715 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5716 | }, |
| 5717 | |
| 5718 | // SSL Tests |
| 5719 | { |
| 5720 | "socks4://socks_proxy:1080", |
| 5721 | "https://www.google.com/socks4_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5722 | "socks4/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5723 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5724 | }, |
| 5725 | { |
| 5726 | "socks5://socks_proxy:1080", |
| 5727 | "https://www.google.com/socks5_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5728 | "socks5/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5729 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5730 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 5731 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5732 | { |
| 5733 | "socks4://socks_proxy:1080", |
| 5734 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5735 | "socks4/ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5736 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5737 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5738 | }; |
| 5739 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5740 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5741 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5742 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5743 | SessionDependencies session_deps( |
| 5744 | ProxyService::CreateFixed(tests[i].proxy_server)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5745 | scoped_refptr<HttpNetworkSession> session( |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5746 | SetupSessionForGroupNameTests(&session_deps)); |
| 5747 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5748 | HttpNetworkSessionPeer peer(session); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5749 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5750 | HostPortPair proxy_host("socks_proxy", 1080); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5751 | CaptureGroupNameSOCKSSocketPool* socks_conn_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5752 | new CaptureGroupNameSOCKSSocketPool(NULL, NULL); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5753 | peer.SetSocketPoolForSOCKSProxy(proxy_host, socks_conn_pool); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5754 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5755 | new CaptureGroupNameSSLSocketPool(NULL, NULL); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5756 | peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5757 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5758 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5759 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5760 | EXPECT_EQ(ERR_IO_PENDING, |
| 5761 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5762 | if (tests[i].ssl) |
| 5763 | EXPECT_EQ(tests[i].expected_group_name, |
| 5764 | ssl_conn_pool->last_group_name_received()); |
| 5765 | else |
| 5766 | EXPECT_EQ(tests[i].expected_group_name, |
| 5767 | socks_conn_pool->last_group_name_received()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5768 | } |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5769 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5770 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5771 | } |
| 5772 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5773 | TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5774 | HttpRequestInfo request; |
| 5775 | request.method = "GET"; |
| 5776 | request.url = GURL("http://www.google.com/"); |
| 5777 | |
[email protected] | 5c6a17e | 2009-06-10 00:54:54 | [diff] [blame] | 5778 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5779 | ProxyService::CreateFixed("myproxy:70;foobar:80")); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5780 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 5781 | // This simulates failure resolving all hostnames; that means we will fail |
| 5782 | // connecting to both proxies (myproxy:70 and foobar:80). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5783 | session_deps.host_resolver->rules()->AddSimulatedFailure("*"); |
| 5784 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5785 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5786 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5787 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5788 | TestOldCompletionCallback callback; |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5789 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5790 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5791 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5792 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5793 | rv = callback.WaitForResult(); |
[email protected] | f7fccee | 2010-09-16 20:53:01 | [diff] [blame] | 5794 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, rv); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5795 | } |
| 5796 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 5797 | // Base test to make sure that when the load flags for a request specify to |
| 5798 | // bypass the cache, the DNS cache is not used. |
| 5799 | void BypassHostCacheOnRefreshHelper(int load_flags) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5800 | // Issue a request, asking to bypass the cache(s). |
| 5801 | HttpRequestInfo request; |
| 5802 | request.method = "GET"; |
| 5803 | request.load_flags = load_flags; |
| 5804 | request.url = GURL("http://www.google.com/"); |
| 5805 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5806 | SessionDependencies session_deps; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5807 | |
[email protected] | a2c2fb9 | 2009-07-18 07:31:04 | [diff] [blame] | 5808 | // Select a host resolver that does caching. |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 5809 | session_deps.host_resolver.reset(new MockCachingHostResolver); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5810 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5811 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5812 | CreateSession(&session_deps))); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5813 | |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 5814 | // Warm up the host cache so it has an entry for "www.google.com". |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5815 | AddressList addrlist; |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 5816 | TestCompletionCallback callback; |
| 5817 | TestOldCompletionCallback old_callback; |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 5818 | int rv = session_deps.host_resolver->Resolve( |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 5819 | HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 5820 | callback.callback(), NULL, BoundNetLog()); |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 5821 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5822 | rv = callback.WaitForResult(); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5823 | EXPECT_EQ(OK, rv); |
| 5824 | |
| 5825 | // Verify that it was added to host cache, by doing a subsequent async lookup |
| 5826 | // and confirming it completes synchronously. |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 5827 | rv = session_deps.host_resolver->Resolve( |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 5828 | HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 5829 | callback.callback(), NULL, BoundNetLog()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5830 | ASSERT_EQ(OK, rv); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5831 | |
| 5832 | // Inject a failure the next time that "www.google.com" is resolved. This way |
| 5833 | // we can tell if the next lookup hit the cache, or the "network". |
| 5834 | // (cache --> success, "network" --> failure). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5835 | session_deps.host_resolver->rules()->AddSimulatedFailure("www.google.com"); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5836 | |
| 5837 | // Connect up a mock socket which will fail with ERR_UNEXPECTED during the |
| 5838 | // first read -- this won't be reached as the host resolution will fail first. |
| 5839 | MockRead data_reads[] = { MockRead(false, ERR_UNEXPECTED) }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5840 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5841 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5842 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5843 | // Run the request. |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 5844 | rv = trans->Start(&request, &old_callback, BoundNetLog()); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5845 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 5846 | rv = old_callback.WaitForResult(); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5847 | |
| 5848 | // If we bypassed the cache, we would have gotten a failure while resolving |
| 5849 | // "www.google.com". |
| 5850 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 5851 | } |
| 5852 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 5853 | // There are multiple load flags that should trigger the host cache bypass. |
| 5854 | // Test each in isolation: |
| 5855 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh1) { |
| 5856 | BypassHostCacheOnRefreshHelper(LOAD_BYPASS_CACHE); |
| 5857 | } |
| 5858 | |
| 5859 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh2) { |
| 5860 | BypassHostCacheOnRefreshHelper(LOAD_VALIDATE_CACHE); |
| 5861 | } |
| 5862 | |
| 5863 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh3) { |
| 5864 | BypassHostCacheOnRefreshHelper(LOAD_DISABLE_CACHE); |
| 5865 | } |
| 5866 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5867 | // Make sure we can handle an error when writing the request. |
| 5868 | TEST_F(HttpNetworkTransactionTest, RequestWriteError) { |
| 5869 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 5870 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5871 | |
| 5872 | HttpRequestInfo request; |
| 5873 | request.method = "GET"; |
| 5874 | request.url = GURL("http://www.foo.com/"); |
| 5875 | request.load_flags = 0; |
| 5876 | |
| 5877 | MockWrite write_failure[] = { |
| 5878 | MockWrite(true, ERR_CONNECTION_RESET), |
| 5879 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5880 | StaticSocketDataProvider data(NULL, 0, |
| 5881 | write_failure, arraysize(write_failure)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5882 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5883 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5884 | TestOldCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5885 | |
| 5886 | scoped_ptr<HttpTransaction> trans( |
| 5887 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5888 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5889 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5890 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5891 | |
| 5892 | rv = callback.WaitForResult(); |
| 5893 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
| 5894 | } |
| 5895 | |
| 5896 | // Check that a connection closed after the start of the headers finishes ok. |
| 5897 | TEST_F(HttpNetworkTransactionTest, ConnectionClosedAfterStartOfHeaders) { |
| 5898 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 5899 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5900 | |
| 5901 | HttpRequestInfo request; |
| 5902 | request.method = "GET"; |
| 5903 | request.url = GURL("http://www.foo.com/"); |
| 5904 | request.load_flags = 0; |
| 5905 | |
| 5906 | MockRead data_reads[] = { |
| 5907 | MockRead("HTTP/1."), |
| 5908 | MockRead(false, OK), |
| 5909 | }; |
| 5910 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5911 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5912 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5913 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5914 | TestOldCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5915 | |
| 5916 | scoped_ptr<HttpTransaction> trans( |
| 5917 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5918 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5919 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5920 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5921 | |
| 5922 | rv = callback.WaitForResult(); |
| 5923 | EXPECT_EQ(OK, rv); |
| 5924 | |
| 5925 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5926 | ASSERT_TRUE(response != NULL); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5927 | |
| 5928 | EXPECT_TRUE(response->headers != NULL); |
| 5929 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5930 | |
| 5931 | std::string response_data; |
| 5932 | rv = ReadTransaction(trans.get(), &response_data); |
| 5933 | EXPECT_EQ(OK, rv); |
| 5934 | EXPECT_EQ("", response_data); |
| 5935 | } |
| 5936 | |
| 5937 | // Make sure that a dropped connection while draining the body for auth |
| 5938 | // restart does the right thing. |
| 5939 | TEST_F(HttpNetworkTransactionTest, DrainResetOK) { |
| 5940 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 5941 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5942 | |
| 5943 | HttpRequestInfo request; |
| 5944 | request.method = "GET"; |
| 5945 | request.url = GURL("http://www.google.com/"); |
| 5946 | request.load_flags = 0; |
| 5947 | |
| 5948 | MockWrite data_writes1[] = { |
| 5949 | MockWrite("GET / HTTP/1.1\r\n" |
| 5950 | "Host: www.google.com\r\n" |
| 5951 | "Connection: keep-alive\r\n\r\n"), |
| 5952 | }; |
| 5953 | |
| 5954 | MockRead data_reads1[] = { |
| 5955 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 5956 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 5957 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5958 | MockRead("Content-Length: 14\r\n\r\n"), |
| 5959 | MockRead("Unauth"), |
| 5960 | MockRead(true, ERR_CONNECTION_RESET), |
| 5961 | }; |
| 5962 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5963 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 5964 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5965 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5966 | |
| 5967 | // After calling trans->RestartWithAuth(), this is the request we should |
| 5968 | // be issuing -- the final header line contains the credentials. |
| 5969 | MockWrite data_writes2[] = { |
| 5970 | MockWrite("GET / HTTP/1.1\r\n" |
| 5971 | "Host: www.google.com\r\n" |
| 5972 | "Connection: keep-alive\r\n" |
| 5973 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 5974 | }; |
| 5975 | |
| 5976 | // Lastly, the server responds with the actual content. |
| 5977 | MockRead data_reads2[] = { |
| 5978 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5979 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5980 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5981 | MockRead(false, OK), |
| 5982 | }; |
| 5983 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5984 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 5985 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5986 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5987 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 5988 | TestOldCompletionCallback callback1; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5989 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 5990 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5991 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5992 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5993 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5994 | |
| 5995 | rv = callback1.WaitForResult(); |
| 5996 | EXPECT_EQ(OK, rv); |
| 5997 | |
| 5998 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5999 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6000 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 6001 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6002 | TestOldCompletionCallback callback2; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 6003 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 6004 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 6005 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6006 | |
| 6007 | rv = callback2.WaitForResult(); |
| 6008 | EXPECT_EQ(OK, rv); |
| 6009 | |
| 6010 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6011 | ASSERT_TRUE(response != NULL); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 6012 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6013 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 6014 | } |
| 6015 | |
| 6016 | // Test HTTPS connections going through a proxy that sends extra data. |
| 6017 | TEST_F(HttpNetworkTransactionTest, HTTPSViaProxyWithExtraData) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 6018 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 6019 | |
| 6020 | HttpRequestInfo request; |
| 6021 | request.method = "GET"; |
| 6022 | request.url = GURL("https://www.google.com/"); |
| 6023 | request.load_flags = 0; |
| 6024 | |
| 6025 | MockRead proxy_reads[] = { |
| 6026 | MockRead("HTTP/1.0 200 Connected\r\n\r\nExtra data"), |
| 6027 | MockRead(false, OK) |
| 6028 | }; |
| 6029 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 6030 | StaticSocketDataProvider data(proxy_reads, arraysize(proxy_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 6031 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 6032 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 6033 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6034 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 6035 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6036 | TestOldCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 6037 | |
| 6038 | session_deps.socket_factory.ResetNextMockIndexes(); |
| 6039 | |
| 6040 | scoped_ptr<HttpTransaction> trans( |
| 6041 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6042 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6043 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 6044 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6045 | |
| 6046 | rv = callback.WaitForResult(); |
| 6047 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
| 6048 | } |
| 6049 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 6050 | TEST_F(HttpNetworkTransactionTest, LargeContentLengthThenClose) { |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6051 | HttpRequestInfo request; |
| 6052 | request.method = "GET"; |
| 6053 | request.url = GURL("http://www.google.com/"); |
| 6054 | request.load_flags = 0; |
| 6055 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6056 | SessionDependencies session_deps; |
| 6057 | scoped_ptr<HttpTransaction> trans( |
| 6058 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6059 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 6060 | MockRead data_reads[] = { |
| 6061 | MockRead("HTTP/1.0 200 OK\r\nContent-Length:6719476739\r\n\r\n"), |
| 6062 | MockRead(false, OK), |
| 6063 | }; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6064 | |
| 6065 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 6066 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6067 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6068 | TestOldCompletionCallback callback; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6069 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6070 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6071 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6072 | |
| 6073 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6074 | |
| 6075 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6076 | ASSERT_TRUE(response != NULL); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6077 | |
| 6078 | EXPECT_TRUE(response->headers != NULL); |
| 6079 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 6080 | |
| 6081 | std::string response_data; |
| 6082 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 6083 | EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 6084 | } |
| 6085 | |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6086 | TEST_F(HttpNetworkTransactionTest, UploadFileSmallerThanLength) { |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6087 | HttpRequestInfo request; |
| 6088 | request.method = "POST"; |
| 6089 | request.url = GURL("http://www.google.com/upload"); |
| 6090 | request.upload_data = new UploadData; |
| 6091 | request.load_flags = 0; |
| 6092 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6093 | SessionDependencies session_deps; |
| 6094 | scoped_ptr<HttpTransaction> trans( |
| 6095 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6096 | |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6097 | FilePath temp_file_path; |
| 6098 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
| 6099 | const uint64 kFakeSize = 100000; // file is actually blank |
| 6100 | |
| 6101 | std::vector<UploadData::Element> elements; |
| 6102 | UploadData::Element element; |
| 6103 | element.SetToFilePath(temp_file_path); |
| 6104 | element.SetContentLength(kFakeSize); |
| 6105 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 6106 | request.upload_data->SetElements(elements); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6107 | EXPECT_EQ(kFakeSize, request.upload_data->GetContentLength()); |
| 6108 | |
| 6109 | MockRead data_reads[] = { |
| 6110 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 6111 | MockRead("hello world"), |
| 6112 | MockRead(false, OK), |
| 6113 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 6114 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6115 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6116 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6117 | TestOldCompletionCallback callback; |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6118 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6119 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6120 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6121 | |
| 6122 | rv = callback.WaitForResult(); |
| 6123 | EXPECT_EQ(OK, rv); |
| 6124 | |
| 6125 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6126 | ASSERT_TRUE(response != NULL); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6127 | |
| 6128 | EXPECT_TRUE(response->headers != NULL); |
| 6129 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 6130 | |
| 6131 | std::string response_data; |
| 6132 | rv = ReadTransaction(trans.get(), &response_data); |
| 6133 | EXPECT_EQ(OK, rv); |
| 6134 | EXPECT_EQ("hello world", response_data); |
| 6135 | |
| 6136 | file_util::Delete(temp_file_path, false); |
| 6137 | } |
| 6138 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6139 | TEST_F(HttpNetworkTransactionTest, UploadUnreadableFile) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6140 | HttpRequestInfo request; |
| 6141 | request.method = "POST"; |
| 6142 | request.url = GURL("http://www.google.com/upload"); |
| 6143 | request.upload_data = new UploadData; |
| 6144 | request.load_flags = 0; |
| 6145 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6146 | // If we try to upload an unreadable file, the network stack should report |
| 6147 | // the file size as zero and upload zero bytes for that file. |
| 6148 | SessionDependencies session_deps; |
| 6149 | scoped_ptr<HttpTransaction> trans( |
| 6150 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6151 | |
| 6152 | FilePath temp_file; |
| 6153 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 6154 | std::string temp_file_content("Unreadable file."); |
| 6155 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_content.c_str(), |
| 6156 | temp_file_content.length())); |
| 6157 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 6158 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6159 | std::vector<UploadData::Element> elements; |
| 6160 | UploadData::Element element; |
| 6161 | element.SetToFilePath(temp_file); |
| 6162 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 6163 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6164 | |
| 6165 | MockRead data_reads[] = { |
| 6166 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 6167 | MockRead(false, OK), |
| 6168 | }; |
| 6169 | MockWrite data_writes[] = { |
| 6170 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 6171 | "Host: www.google.com\r\n" |
| 6172 | "Connection: keep-alive\r\n" |
| 6173 | "Content-Length: 0\r\n\r\n"), |
| 6174 | MockWrite(false, OK), |
| 6175 | }; |
| 6176 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 6177 | arraysize(data_writes)); |
| 6178 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6179 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6180 | TestOldCompletionCallback callback; |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6181 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6182 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6183 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6184 | |
| 6185 | rv = callback.WaitForResult(); |
| 6186 | EXPECT_EQ(OK, rv); |
| 6187 | |
| 6188 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6189 | ASSERT_TRUE(response != NULL); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6190 | EXPECT_TRUE(response->headers != NULL); |
| 6191 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 6192 | |
| 6193 | file_util::Delete(temp_file, false); |
| 6194 | } |
| 6195 | |
| 6196 | TEST_F(HttpNetworkTransactionTest, UnreadableUploadFileAfterAuthRestart) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6197 | HttpRequestInfo request; |
| 6198 | request.method = "POST"; |
| 6199 | request.url = GURL("http://www.google.com/upload"); |
| 6200 | request.upload_data = new UploadData; |
| 6201 | request.load_flags = 0; |
| 6202 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6203 | SessionDependencies session_deps; |
| 6204 | scoped_ptr<HttpTransaction> trans( |
| 6205 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6206 | |
| 6207 | FilePath temp_file; |
| 6208 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 6209 | std::string temp_file_contents("Unreadable file."); |
| 6210 | std::string unreadable_contents(temp_file_contents.length(), '\0'); |
| 6211 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_contents.c_str(), |
| 6212 | temp_file_contents.length())); |
| 6213 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6214 | std::vector<UploadData::Element> elements; |
| 6215 | UploadData::Element element; |
| 6216 | element.SetToFilePath(temp_file); |
| 6217 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 6218 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6219 | |
| 6220 | MockRead data_reads[] = { |
| 6221 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 6222 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 6223 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
| 6224 | |
| 6225 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 6226 | MockRead("Content-Length: 0\r\n\r\n"), |
| 6227 | MockRead(false, OK), |
| 6228 | }; |
| 6229 | MockWrite data_writes[] = { |
| 6230 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 6231 | "Host: www.google.com\r\n" |
| 6232 | "Connection: keep-alive\r\n" |
| 6233 | "Content-Length: 16\r\n\r\n"), |
| 6234 | MockWrite(false, temp_file_contents.c_str()), |
| 6235 | |
| 6236 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 6237 | "Host: www.google.com\r\n" |
| 6238 | "Connection: keep-alive\r\n" |
| 6239 | "Content-Length: 16\r\n" |
| 6240 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 6241 | MockWrite(false, unreadable_contents.c_str(), temp_file_contents.length()), |
| 6242 | MockWrite(false, OK), |
| 6243 | }; |
| 6244 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 6245 | arraysize(data_writes)); |
| 6246 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6247 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6248 | TestOldCompletionCallback callback1; |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6249 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6250 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6251 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6252 | |
| 6253 | rv = callback1.WaitForResult(); |
| 6254 | EXPECT_EQ(OK, rv); |
| 6255 | |
| 6256 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6257 | ASSERT_TRUE(response != NULL); |
| 6258 | ASSERT_TRUE(response->headers != NULL); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6259 | EXPECT_EQ("HTTP/1.1 401 Unauthorized", response->headers->GetStatusLine()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6260 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6261 | |
| 6262 | // Now make the file unreadable and try again. |
| 6263 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 6264 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6265 | TestOldCompletionCallback callback2; |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6266 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 6267 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback2); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6268 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6269 | |
| 6270 | rv = callback2.WaitForResult(); |
| 6271 | EXPECT_EQ(OK, rv); |
| 6272 | |
| 6273 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6274 | ASSERT_TRUE(response != NULL); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6275 | EXPECT_TRUE(response->headers != NULL); |
| 6276 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6277 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6278 | |
| 6279 | file_util::Delete(temp_file, false); |
| 6280 | } |
| 6281 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6282 | // Tests that changes to Auth realms are treated like auth rejections. |
| 6283 | TEST_F(HttpNetworkTransactionTest, ChangeAuthRealms) { |
| 6284 | SessionDependencies session_deps; |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6285 | |
| 6286 | HttpRequestInfo request; |
| 6287 | request.method = "GET"; |
| 6288 | request.url = GURL("http://www.google.com/"); |
| 6289 | request.load_flags = 0; |
| 6290 | |
| 6291 | // First transaction will request a resource and receive a Basic challenge |
| 6292 | // with realm="first_realm". |
| 6293 | MockWrite data_writes1[] = { |
| 6294 | MockWrite("GET / HTTP/1.1\r\n" |
| 6295 | "Host: www.google.com\r\n" |
| 6296 | "Connection: keep-alive\r\n" |
| 6297 | "\r\n"), |
| 6298 | }; |
| 6299 | MockRead data_reads1[] = { |
| 6300 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 6301 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 6302 | "\r\n"), |
| 6303 | }; |
| 6304 | |
| 6305 | // After calling trans->RestartWithAuth(), provide an Authentication header |
| 6306 | // for first_realm. The server will reject and provide a challenge with |
| 6307 | // second_realm. |
| 6308 | MockWrite data_writes2[] = { |
| 6309 | MockWrite("GET / HTTP/1.1\r\n" |
| 6310 | "Host: www.google.com\r\n" |
| 6311 | "Connection: keep-alive\r\n" |
| 6312 | "Authorization: Basic Zmlyc3Q6YmF6\r\n" |
| 6313 | "\r\n"), |
| 6314 | }; |
| 6315 | MockRead data_reads2[] = { |
| 6316 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 6317 | "WWW-Authenticate: Basic realm=\"second_realm\"\r\n" |
| 6318 | "\r\n"), |
| 6319 | }; |
| 6320 | |
| 6321 | // This again fails, and goes back to first_realm. Make sure that the |
| 6322 | // entry is removed from cache. |
| 6323 | MockWrite data_writes3[] = { |
| 6324 | MockWrite("GET / HTTP/1.1\r\n" |
| 6325 | "Host: www.google.com\r\n" |
| 6326 | "Connection: keep-alive\r\n" |
| 6327 | "Authorization: Basic c2Vjb25kOmZvdQ==\r\n" |
| 6328 | "\r\n"), |
| 6329 | }; |
| 6330 | MockRead data_reads3[] = { |
| 6331 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 6332 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 6333 | "\r\n"), |
| 6334 | }; |
| 6335 | |
| 6336 | // Try one last time (with the correct password) and get the resource. |
| 6337 | MockWrite data_writes4[] = { |
| 6338 | MockWrite("GET / HTTP/1.1\r\n" |
| 6339 | "Host: www.google.com\r\n" |
| 6340 | "Connection: keep-alive\r\n" |
| 6341 | "Authorization: Basic Zmlyc3Q6YmFy\r\n" |
| 6342 | "\r\n"), |
| 6343 | }; |
| 6344 | MockRead data_reads4[] = { |
| 6345 | MockRead("HTTP/1.1 200 OK\r\n" |
| 6346 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6347 | "Content-Length: 5\r\n" |
| 6348 | "\r\n" |
| 6349 | "hello"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6350 | }; |
| 6351 | |
| 6352 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 6353 | data_writes1, arraysize(data_writes1)); |
| 6354 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 6355 | data_writes2, arraysize(data_writes2)); |
| 6356 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 6357 | data_writes3, arraysize(data_writes3)); |
| 6358 | StaticSocketDataProvider data4(data_reads4, arraysize(data_reads4), |
| 6359 | data_writes4, arraysize(data_writes4)); |
| 6360 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 6361 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 6362 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 6363 | session_deps.socket_factory.AddSocketDataProvider(&data4); |
| 6364 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6365 | TestOldCompletionCallback callback1; |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6366 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6367 | scoped_ptr<HttpTransaction> trans( |
| 6368 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6369 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6370 | // Issue the first request with Authorize headers. There should be a |
| 6371 | // password prompt for first_realm waiting to be filled in after the |
| 6372 | // transaction completes. |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6373 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6374 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6375 | rv = callback1.WaitForResult(); |
| 6376 | EXPECT_EQ(OK, rv); |
| 6377 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6378 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6379 | const AuthChallengeInfo* challenge = response->auth_challenge.get(); |
| 6380 | ASSERT_FALSE(challenge == NULL); |
| 6381 | EXPECT_FALSE(challenge->is_proxy); |
| 6382 | EXPECT_EQ("www.google.com:80", challenge->challenger.ToString()); |
| 6383 | EXPECT_EQ("first_realm", challenge->realm); |
| 6384 | EXPECT_EQ("basic", challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6385 | |
| 6386 | // Issue the second request with an incorrect password. There should be a |
| 6387 | // password prompt for second_realm waiting to be filled in after the |
| 6388 | // transaction completes. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6389 | TestOldCompletionCallback callback2; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 6390 | rv = trans->RestartWithAuth(AuthCredentials(kFirst, kBaz), &callback2); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6391 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6392 | rv = callback2.WaitForResult(); |
| 6393 | EXPECT_EQ(OK, rv); |
| 6394 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6395 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6396 | challenge = response->auth_challenge.get(); |
| 6397 | ASSERT_FALSE(challenge == NULL); |
| 6398 | EXPECT_FALSE(challenge->is_proxy); |
| 6399 | EXPECT_EQ("www.google.com:80", challenge->challenger.ToString()); |
| 6400 | EXPECT_EQ("second_realm", challenge->realm); |
| 6401 | EXPECT_EQ("basic", challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6402 | |
| 6403 | // Issue the third request with another incorrect password. There should be |
| 6404 | // a password prompt for first_realm waiting to be filled in. If the password |
| 6405 | // prompt is not present, it indicates that the HttpAuthCacheEntry for |
| 6406 | // first_realm was not correctly removed. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6407 | TestOldCompletionCallback callback3; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 6408 | rv = trans->RestartWithAuth(AuthCredentials(kSecond, kFou), &callback3); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6409 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6410 | rv = callback3.WaitForResult(); |
| 6411 | EXPECT_EQ(OK, rv); |
| 6412 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6413 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6414 | challenge = response->auth_challenge.get(); |
| 6415 | ASSERT_FALSE(challenge == NULL); |
| 6416 | EXPECT_FALSE(challenge->is_proxy); |
| 6417 | EXPECT_EQ("www.google.com:80", challenge->challenger.ToString()); |
| 6418 | EXPECT_EQ("first_realm", challenge->realm); |
| 6419 | EXPECT_EQ("basic", challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6420 | |
| 6421 | // Issue the fourth request with the correct password and username. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6422 | TestOldCompletionCallback callback4; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 6423 | rv = trans->RestartWithAuth(AuthCredentials(kFirst, kBar), &callback4); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6424 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6425 | rv = callback4.WaitForResult(); |
| 6426 | EXPECT_EQ(OK, rv); |
| 6427 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6428 | ASSERT_TRUE(response != NULL); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6429 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6430 | } |
| 6431 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6432 | TEST_F(HttpNetworkTransactionTest, HonorAlternateProtocolHeader) { |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6433 | HttpStreamFactory::set_next_protos(MakeNextProtos("foo", "bar", NULL)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6434 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6435 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6436 | SessionDependencies session_deps; |
| 6437 | |
| 6438 | MockRead data_reads[] = { |
| 6439 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6440 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6441 | MockRead("hello world"), |
| 6442 | MockRead(false, OK), |
| 6443 | }; |
| 6444 | |
| 6445 | HttpRequestInfo request; |
| 6446 | request.method = "GET"; |
| 6447 | request.url = GURL("http://www.google.com/"); |
| 6448 | request.load_flags = 0; |
| 6449 | |
| 6450 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 6451 | |
| 6452 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6453 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6454 | TestOldCompletionCallback callback; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6455 | |
| 6456 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6457 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6458 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6459 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6460 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6461 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 6462 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6463 | const HttpServerProperties& http_server_properties = |
| 6464 | *session->http_server_properties(); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6465 | EXPECT_FALSE( |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6466 | http_server_properties.HasAlternateProtocol(http_host_port_pair)); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6467 | |
| 6468 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6469 | |
| 6470 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6471 | ASSERT_TRUE(response != NULL); |
| 6472 | ASSERT_TRUE(response->headers != NULL); |
| 6473 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6474 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 6475 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6476 | |
| 6477 | std::string response_data; |
| 6478 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6479 | EXPECT_EQ("hello world", response_data); |
| 6480 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6481 | ASSERT_TRUE(http_server_properties.HasAlternateProtocol(http_host_port_pair)); |
| 6482 | const PortAlternateProtocolPair alternate = |
| 6483 | http_server_properties.GetAlternateProtocol(http_host_port_pair); |
| 6484 | PortAlternateProtocolPair expected_alternate; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6485 | expected_alternate.port = 443; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6486 | expected_alternate.protocol = NPN_SPDY_2; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6487 | EXPECT_TRUE(expected_alternate.Equals(alternate)); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6488 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6489 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6490 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6491 | } |
| 6492 | |
[email protected] | 8b95ed6 | 2011-03-18 18:12:03 | [diff] [blame] | 6493 | TEST_F(HttpNetworkTransactionTest, MarkBrokenAlternateProtocolAndFallback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6494 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6495 | SessionDependencies session_deps; |
| 6496 | |
| 6497 | HttpRequestInfo request; |
| 6498 | request.method = "GET"; |
| 6499 | request.url = GURL("http://www.google.com/"); |
| 6500 | request.load_flags = 0; |
| 6501 | |
| 6502 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6503 | StaticSocketDataProvider first_data; |
| 6504 | first_data.set_connect_data(mock_connect); |
| 6505 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6506 | |
| 6507 | MockRead data_reads[] = { |
| 6508 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6509 | MockRead("hello world"), |
| 6510 | MockRead(true, OK), |
| 6511 | }; |
| 6512 | StaticSocketDataProvider second_data( |
| 6513 | data_reads, arraysize(data_reads), NULL, 0); |
| 6514 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6515 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6516 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6517 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6518 | HttpServerProperties* http_server_properties = |
| 6519 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6520 | // Port must be < 1024, or the header will be ignored (since initial port was |
| 6521 | // port 80 (another restricted port). |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6522 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6523 | HostPortPair::FromURL(request.url), |
| 6524 | 666 /* port is ignored by MockConnect anyway */, |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6525 | NPN_SPDY_2); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6526 | |
| 6527 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6528 | TestOldCompletionCallback callback; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6529 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6530 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6531 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6532 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6533 | |
| 6534 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6535 | ASSERT_TRUE(response != NULL); |
| 6536 | ASSERT_TRUE(response->headers != NULL); |
| 6537 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6538 | |
| 6539 | std::string response_data; |
| 6540 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6541 | EXPECT_EQ("hello world", response_data); |
| 6542 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6543 | ASSERT_TRUE(http_server_properties->HasAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6544 | HostPortPair::FromURL(request.url))); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6545 | const PortAlternateProtocolPair alternate = |
| 6546 | http_server_properties->GetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6547 | HostPortPair::FromURL(request.url)); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6548 | EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6549 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6550 | } |
| 6551 | |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6552 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedBlocked) { |
| 6553 | // Ensure that we're not allowed to redirect traffic via an alternate |
| 6554 | // protocol to an unrestricted (port >= 1024) when the original traffic was |
| 6555 | // on a restricted port (port < 1024). Ensure that we can redirect in all |
| 6556 | // other cases. |
| 6557 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6558 | SessionDependencies session_deps; |
| 6559 | |
| 6560 | HttpRequestInfo restricted_port_request; |
| 6561 | restricted_port_request.method = "GET"; |
| 6562 | restricted_port_request.url = GURL("http://www.google.com:1023/"); |
| 6563 | restricted_port_request.load_flags = 0; |
| 6564 | |
| 6565 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6566 | StaticSocketDataProvider first_data; |
| 6567 | first_data.set_connect_data(mock_connect); |
| 6568 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6569 | |
| 6570 | MockRead data_reads[] = { |
| 6571 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6572 | MockRead("hello world"), |
| 6573 | MockRead(true, OK), |
| 6574 | }; |
| 6575 | StaticSocketDataProvider second_data( |
| 6576 | data_reads, arraysize(data_reads), NULL, 0); |
| 6577 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6578 | |
| 6579 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6580 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6581 | HttpServerProperties* http_server_properties = |
| 6582 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6583 | const int kUnrestrictedAlternatePort = 1024; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6584 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6585 | HostPortPair::FromURL(restricted_port_request.url), |
| 6586 | kUnrestrictedAlternatePort, |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6587 | NPN_SPDY_2); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6588 | |
| 6589 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6590 | TestOldCompletionCallback callback; |
| 6591 | |
| 6592 | int rv = trans->Start(&restricted_port_request, &callback, BoundNetLog()); |
| 6593 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6594 | // Invalid change to unrestricted port should fail. |
| 6595 | EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult()); |
| 6596 | |
| 6597 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6598 | } |
| 6599 | |
| 6600 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedAllowed) { |
| 6601 | // Ensure that we're not allowed to redirect traffic via an alternate |
| 6602 | // protocol to an unrestricted (port >= 1024) when the original traffic was |
| 6603 | // on a restricted port (port < 1024). Ensure that we can redirect in all |
| 6604 | // other cases. |
| 6605 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6606 | SessionDependencies session_deps; |
| 6607 | |
| 6608 | HttpRequestInfo restricted_port_request; |
| 6609 | restricted_port_request.method = "GET"; |
| 6610 | restricted_port_request.url = GURL("http://www.google.com:1023/"); |
| 6611 | restricted_port_request.load_flags = 0; |
| 6612 | |
| 6613 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6614 | StaticSocketDataProvider first_data; |
| 6615 | first_data.set_connect_data(mock_connect); |
| 6616 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6617 | |
| 6618 | MockRead data_reads[] = { |
| 6619 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6620 | MockRead("hello world"), |
| 6621 | MockRead(true, OK), |
| 6622 | }; |
| 6623 | StaticSocketDataProvider second_data( |
| 6624 | data_reads, arraysize(data_reads), NULL, 0); |
| 6625 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6626 | |
| 6627 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6628 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6629 | HttpServerProperties* http_server_properties = |
| 6630 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6631 | const int kRestrictedAlternatePort = 80; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6632 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6633 | HostPortPair::FromURL(restricted_port_request.url), |
| 6634 | kRestrictedAlternatePort, |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6635 | NPN_SPDY_2); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6636 | |
| 6637 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6638 | TestOldCompletionCallback callback; |
| 6639 | |
| 6640 | int rv = trans->Start(&restricted_port_request, &callback, BoundNetLog()); |
| 6641 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6642 | // Valid change to restricted port should pass. |
| 6643 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6644 | |
| 6645 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6646 | } |
| 6647 | |
| 6648 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortUnrestrictedAllowed1) { |
| 6649 | // Ensure that we're not allowed to redirect traffic via an alternate |
| 6650 | // protocol to an unrestricted (port >= 1024) when the original traffic was |
| 6651 | // on a restricted port (port < 1024). Ensure that we can redirect in all |
| 6652 | // other cases. |
| 6653 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6654 | SessionDependencies session_deps; |
| 6655 | |
| 6656 | HttpRequestInfo unrestricted_port_request; |
| 6657 | unrestricted_port_request.method = "GET"; |
| 6658 | unrestricted_port_request.url = GURL("http://www.google.com:1024/"); |
| 6659 | unrestricted_port_request.load_flags = 0; |
| 6660 | |
| 6661 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6662 | StaticSocketDataProvider first_data; |
| 6663 | first_data.set_connect_data(mock_connect); |
| 6664 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6665 | |
| 6666 | MockRead data_reads[] = { |
| 6667 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6668 | MockRead("hello world"), |
| 6669 | MockRead(true, OK), |
| 6670 | }; |
| 6671 | StaticSocketDataProvider second_data( |
| 6672 | data_reads, arraysize(data_reads), NULL, 0); |
| 6673 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6674 | |
| 6675 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6676 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6677 | HttpServerProperties* http_server_properties = |
| 6678 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6679 | const int kRestrictedAlternatePort = 80; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6680 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6681 | HostPortPair::FromURL(unrestricted_port_request.url), |
| 6682 | kRestrictedAlternatePort, |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6683 | NPN_SPDY_2); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6684 | |
| 6685 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6686 | TestOldCompletionCallback callback; |
| 6687 | |
| 6688 | int rv = trans->Start(&unrestricted_port_request, &callback, BoundNetLog()); |
| 6689 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6690 | // Valid change to restricted port should pass. |
| 6691 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6692 | |
| 6693 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6694 | } |
| 6695 | |
| 6696 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortUnrestrictedAllowed2) { |
| 6697 | // Ensure that we're not allowed to redirect traffic via an alternate |
| 6698 | // protocol to an unrestricted (port >= 1024) when the original traffic was |
| 6699 | // on a restricted port (port < 1024). Ensure that we can redirect in all |
| 6700 | // other cases. |
| 6701 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6702 | SessionDependencies session_deps; |
| 6703 | |
| 6704 | HttpRequestInfo unrestricted_port_request; |
| 6705 | unrestricted_port_request.method = "GET"; |
| 6706 | unrestricted_port_request.url = GURL("http://www.google.com:1024/"); |
| 6707 | unrestricted_port_request.load_flags = 0; |
| 6708 | |
| 6709 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6710 | StaticSocketDataProvider first_data; |
| 6711 | first_data.set_connect_data(mock_connect); |
| 6712 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6713 | |
| 6714 | MockRead data_reads[] = { |
| 6715 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6716 | MockRead("hello world"), |
| 6717 | MockRead(true, OK), |
| 6718 | }; |
| 6719 | StaticSocketDataProvider second_data( |
| 6720 | data_reads, arraysize(data_reads), NULL, 0); |
| 6721 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6722 | |
| 6723 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6724 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6725 | HttpServerProperties* http_server_properties = |
| 6726 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6727 | const int kUnrestrictedAlternatePort = 1024; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6728 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6729 | HostPortPair::FromURL(unrestricted_port_request.url), |
| 6730 | kUnrestrictedAlternatePort, |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6731 | NPN_SPDY_2); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6732 | |
| 6733 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6734 | TestOldCompletionCallback callback; |
| 6735 | |
| 6736 | int rv = trans->Start(&unrestricted_port_request, &callback, BoundNetLog()); |
| 6737 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6738 | // Valid change to an unrestricted port should pass. |
| 6739 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6740 | |
| 6741 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6742 | } |
| 6743 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6744 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6745 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6746 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6747 | SessionDependencies session_deps; |
| 6748 | |
| 6749 | HttpRequestInfo request; |
| 6750 | request.method = "GET"; |
| 6751 | request.url = GURL("http://www.google.com/"); |
| 6752 | request.load_flags = 0; |
| 6753 | |
| 6754 | MockRead data_reads[] = { |
| 6755 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6756 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6757 | MockRead("hello world"), |
| 6758 | MockRead(true, OK), |
| 6759 | }; |
| 6760 | |
| 6761 | StaticSocketDataProvider first_transaction( |
| 6762 | data_reads, arraysize(data_reads), NULL, 0); |
| 6763 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6764 | |
| 6765 | SSLSocketDataProvider ssl(true, OK); |
| 6766 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6767 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6768 | ssl.was_npn_negotiated = true; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6769 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6770 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6771 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6772 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6773 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6774 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6775 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6776 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6777 | CreateMockRead(*resp), |
| 6778 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6779 | MockRead(true, 0, 0), |
| 6780 | }; |
| 6781 | |
| 6782 | scoped_refptr<DelayedSocketData> spdy_data( |
| 6783 | new DelayedSocketData( |
| 6784 | 1, // wait for one write to finish before reading. |
| 6785 | spdy_reads, arraysize(spdy_reads), |
| 6786 | spdy_writes, arraysize(spdy_writes))); |
| 6787 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6788 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6789 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 6790 | StaticSocketDataProvider hanging_non_alternate_protocol_socket( |
| 6791 | NULL, 0, NULL, 0); |
| 6792 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 6793 | never_finishing_connect); |
| 6794 | session_deps.socket_factory.AddSocketDataProvider( |
| 6795 | &hanging_non_alternate_protocol_socket); |
| 6796 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6797 | TestOldCompletionCallback callback; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6798 | |
| 6799 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6800 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6801 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6802 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6803 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6804 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6805 | |
| 6806 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6807 | ASSERT_TRUE(response != NULL); |
| 6808 | ASSERT_TRUE(response->headers != NULL); |
| 6809 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6810 | |
| 6811 | std::string response_data; |
| 6812 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6813 | EXPECT_EQ("hello world", response_data); |
| 6814 | |
| 6815 | trans.reset(new HttpNetworkTransaction(session)); |
| 6816 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6817 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6818 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6819 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6820 | |
| 6821 | response = trans->GetResponseInfo(); |
| 6822 | ASSERT_TRUE(response != NULL); |
| 6823 | ASSERT_TRUE(response->headers != NULL); |
| 6824 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6825 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6826 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6827 | |
| 6828 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6829 | EXPECT_EQ("hello!", response_data); |
| 6830 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6831 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6832 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6833 | } |
| 6834 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6835 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolWithSpdyLateBinding) { |
| 6836 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6837 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6838 | SessionDependencies session_deps; |
| 6839 | |
| 6840 | HttpRequestInfo request; |
| 6841 | request.method = "GET"; |
| 6842 | request.url = GURL("http://www.google.com/"); |
| 6843 | request.load_flags = 0; |
| 6844 | |
| 6845 | MockRead data_reads[] = { |
| 6846 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 6847 | MockRead(kAlternateProtocolHttpHeader), |
| 6848 | MockRead("hello world"), |
| 6849 | MockRead(true, OK), |
| 6850 | }; |
| 6851 | |
| 6852 | StaticSocketDataProvider first_transaction( |
| 6853 | data_reads, arraysize(data_reads), NULL, 0); |
| 6854 | // Socket 1 is the HTTP transaction with the Alternate-Protocol header. |
| 6855 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6856 | |
| 6857 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 6858 | StaticSocketDataProvider hanging_socket( |
| 6859 | NULL, 0, NULL, 0); |
| 6860 | hanging_socket.set_connect_data(never_finishing_connect); |
| 6861 | // Socket 2 and 3 are the hanging Alternate-Protocol and |
| 6862 | // non-Alternate-Protocol jobs from the 2nd transaction. |
| 6863 | session_deps.socket_factory.AddSocketDataProvider(&hanging_socket); |
| 6864 | session_deps.socket_factory.AddSocketDataProvider(&hanging_socket); |
| 6865 | |
| 6866 | SSLSocketDataProvider ssl(true, OK); |
| 6867 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 6868 | ssl.next_proto = "spdy/2"; |
| 6869 | ssl.was_npn_negotiated = true; |
| 6870 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6871 | |
| 6872 | scoped_ptr<spdy::SpdyFrame> req1(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 6873 | scoped_ptr<spdy::SpdyFrame> req2(ConstructSpdyGet(NULL, 0, false, 3, LOWEST)); |
| 6874 | MockWrite spdy_writes[] = { |
| 6875 | CreateMockWrite(*req1), |
| 6876 | CreateMockWrite(*req2), |
| 6877 | }; |
| 6878 | scoped_ptr<spdy::SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6879 | scoped_ptr<spdy::SpdyFrame> data1(ConstructSpdyBodyFrame(1, true)); |
| 6880 | scoped_ptr<spdy::SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 6881 | scoped_ptr<spdy::SpdyFrame> data2(ConstructSpdyBodyFrame(3, true)); |
| 6882 | MockRead spdy_reads[] = { |
| 6883 | CreateMockRead(*resp1), |
| 6884 | CreateMockRead(*data1), |
| 6885 | CreateMockRead(*resp2), |
| 6886 | CreateMockRead(*data2), |
| 6887 | MockRead(true, 0, 0), |
| 6888 | }; |
| 6889 | |
| 6890 | scoped_refptr<DelayedSocketData> spdy_data( |
| 6891 | new DelayedSocketData( |
| 6892 | 2, // wait for writes to finish before reading. |
| 6893 | spdy_reads, arraysize(spdy_reads), |
| 6894 | spdy_writes, arraysize(spdy_writes))); |
| 6895 | // Socket 4 is the successful Alternate-Protocol for transaction 3. |
| 6896 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6897 | |
| 6898 | // Socket 5 is the unsuccessful non-Alternate-Protocol for transaction 3. |
| 6899 | session_deps.socket_factory.AddSocketDataProvider(&hanging_socket); |
| 6900 | |
| 6901 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6902 | TestOldCompletionCallback callback1; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6903 | HttpNetworkTransaction trans1(session); |
| 6904 | |
| 6905 | int rv = trans1.Start(&request, &callback1, BoundNetLog()); |
| 6906 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6907 | EXPECT_EQ(OK, callback1.WaitForResult()); |
| 6908 | |
| 6909 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 6910 | ASSERT_TRUE(response != NULL); |
| 6911 | ASSERT_TRUE(response->headers != NULL); |
| 6912 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6913 | |
| 6914 | std::string response_data; |
| 6915 | ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); |
| 6916 | EXPECT_EQ("hello world", response_data); |
| 6917 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6918 | TestOldCompletionCallback callback2; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6919 | HttpNetworkTransaction trans2(session); |
| 6920 | rv = trans2.Start(&request, &callback2, BoundNetLog()); |
| 6921 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6922 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6923 | TestOldCompletionCallback callback3; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6924 | HttpNetworkTransaction trans3(session); |
| 6925 | rv = trans3.Start(&request, &callback3, BoundNetLog()); |
| 6926 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6927 | |
| 6928 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 6929 | EXPECT_EQ(OK, callback3.WaitForResult()); |
| 6930 | |
| 6931 | response = trans2.GetResponseInfo(); |
| 6932 | ASSERT_TRUE(response != NULL); |
| 6933 | ASSERT_TRUE(response->headers != NULL); |
| 6934 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6935 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6936 | EXPECT_TRUE(response->was_npn_negotiated); |
| 6937 | ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); |
| 6938 | EXPECT_EQ("hello!", response_data); |
| 6939 | |
| 6940 | response = trans3.GetResponseInfo(); |
| 6941 | ASSERT_TRUE(response != NULL); |
| 6942 | ASSERT_TRUE(response->headers != NULL); |
| 6943 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6944 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6945 | EXPECT_TRUE(response->was_npn_negotiated); |
| 6946 | ASSERT_EQ(OK, ReadTransaction(&trans3, &response_data)); |
| 6947 | EXPECT_EQ("hello!", response_data); |
| 6948 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6949 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6950 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6951 | } |
| 6952 | |
| 6953 | TEST_F(HttpNetworkTransactionTest, StallAlternateProtocolForNpnSpdy) { |
| 6954 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6955 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6956 | SessionDependencies session_deps; |
| 6957 | |
| 6958 | HttpRequestInfo request; |
| 6959 | request.method = "GET"; |
| 6960 | request.url = GURL("http://www.google.com/"); |
| 6961 | request.load_flags = 0; |
| 6962 | |
| 6963 | MockRead data_reads[] = { |
| 6964 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 6965 | MockRead(kAlternateProtocolHttpHeader), |
| 6966 | MockRead("hello world"), |
| 6967 | MockRead(true, OK), |
| 6968 | }; |
| 6969 | |
| 6970 | StaticSocketDataProvider first_transaction( |
| 6971 | data_reads, arraysize(data_reads), NULL, 0); |
| 6972 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6973 | |
| 6974 | SSLSocketDataProvider ssl(true, OK); |
| 6975 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 6976 | ssl.next_proto = "spdy/2"; |
| 6977 | ssl.was_npn_negotiated = true; |
| 6978 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6979 | |
| 6980 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 6981 | StaticSocketDataProvider hanging_alternate_protocol_socket( |
| 6982 | NULL, 0, NULL, 0); |
| 6983 | hanging_alternate_protocol_socket.set_connect_data( |
| 6984 | never_finishing_connect); |
| 6985 | session_deps.socket_factory.AddSocketDataProvider( |
| 6986 | &hanging_alternate_protocol_socket); |
| 6987 | |
| 6988 | // 2nd request is just a copy of the first one, over HTTP again. |
| 6989 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6990 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 6991 | TestOldCompletionCallback callback; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6992 | |
| 6993 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6994 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6995 | |
| 6996 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6997 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6998 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6999 | |
| 7000 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7001 | ASSERT_TRUE(response != NULL); |
| 7002 | ASSERT_TRUE(response->headers != NULL); |
| 7003 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7004 | |
| 7005 | std::string response_data; |
| 7006 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7007 | EXPECT_EQ("hello world", response_data); |
| 7008 | |
| 7009 | trans.reset(new HttpNetworkTransaction(session)); |
| 7010 | |
| 7011 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 7012 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7013 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7014 | |
| 7015 | response = trans->GetResponseInfo(); |
| 7016 | ASSERT_TRUE(response != NULL); |
| 7017 | ASSERT_TRUE(response->headers != NULL); |
| 7018 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7019 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 7020 | EXPECT_FALSE(response->was_npn_negotiated); |
| 7021 | |
| 7022 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7023 | EXPECT_EQ("hello world", response_data); |
| 7024 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7025 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 7026 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 7027 | } |
| 7028 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7029 | class CapturingProxyResolver : public ProxyResolver { |
| 7030 | public: |
| 7031 | CapturingProxyResolver() : ProxyResolver(false /* expects_pac_bytes */) {} |
| 7032 | virtual ~CapturingProxyResolver() {} |
| 7033 | |
| 7034 | virtual int GetProxyForURL(const GURL& url, |
| 7035 | ProxyInfo* results, |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 7036 | OldCompletionCallback* callback, |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7037 | RequestHandle* request, |
| 7038 | const BoundNetLog& net_log) { |
[email protected] | fae7669f | 2010-08-02 21:49:40 | [diff] [blame] | 7039 | ProxyServer proxy_server(ProxyServer::SCHEME_HTTP, |
| 7040 | HostPortPair("myproxy", 80)); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7041 | results->UseProxyServer(proxy_server); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7042 | resolved_.push_back(url); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7043 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7044 | } |
| 7045 | |
| 7046 | virtual void CancelRequest(RequestHandle request) { |
| 7047 | NOTREACHED(); |
| 7048 | } |
| 7049 | |
[email protected] | f2c971f | 2011-11-08 00:33:17 | [diff] [blame] | 7050 | virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE { |
| 7051 | NOTREACHED(); |
| 7052 | return LOAD_STATE_IDLE; |
| 7053 | } |
| 7054 | |
| 7055 | virtual LoadState GetLoadStateThreadSafe( |
| 7056 | RequestHandle request) const OVERRIDE { |
| 7057 | NOTREACHED(); |
| 7058 | return LOAD_STATE_IDLE; |
| 7059 | } |
| 7060 | |
[email protected] | 1e60547 | 2010-12-16 21:41:40 | [diff] [blame] | 7061 | virtual void CancelSetPacScript() { |
| 7062 | NOTREACHED(); |
| 7063 | } |
| 7064 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 7065 | virtual int SetPacScript(const scoped_refptr<ProxyResolverScriptData>&, |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 7066 | OldCompletionCallback* /*callback*/) { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7067 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7068 | } |
| 7069 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 7070 | const std::vector<GURL>& resolved() const { return resolved_; } |
| 7071 | |
| 7072 | private: |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7073 | std::vector<GURL> resolved_; |
| 7074 | |
| 7075 | DISALLOW_COPY_AND_ASSIGN(CapturingProxyResolver); |
| 7076 | }; |
| 7077 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7078 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForTunneledNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7079 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7080 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7081 | |
| 7082 | ProxyConfig proxy_config; |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7083 | proxy_config.set_auto_detect(true); |
| 7084 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 7085 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7086 | CapturingProxyResolver* capturing_proxy_resolver = |
| 7087 | new CapturingProxyResolver(); |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 7088 | SessionDependencies session_deps(new ProxyService( |
| 7089 | new ProxyConfigServiceFixed(proxy_config), capturing_proxy_resolver, |
| 7090 | NULL)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7091 | |
| 7092 | HttpRequestInfo request; |
| 7093 | request.method = "GET"; |
| 7094 | request.url = GURL("http://www.google.com/"); |
| 7095 | request.load_flags = 0; |
| 7096 | |
| 7097 | MockRead data_reads[] = { |
| 7098 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 7099 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7100 | MockRead("hello world"), |
| 7101 | MockRead(true, OK), |
| 7102 | }; |
| 7103 | |
| 7104 | StaticSocketDataProvider first_transaction( |
| 7105 | data_reads, arraysize(data_reads), NULL, 0); |
| 7106 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 7107 | |
| 7108 | SSLSocketDataProvider ssl(true, OK); |
| 7109 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 7110 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7111 | ssl.was_npn_negotiated = true; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7112 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7113 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7114 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7115 | MockWrite spdy_writes[] = { |
| 7116 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7117 | "Host: www.google.com\r\n" |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7118 | "Proxy-Connection: keep-alive\r\n\r\n"), // 0 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7119 | CreateMockWrite(*req) // 3 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7120 | }; |
| 7121 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7122 | const char kCONNECTResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 7123 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7124 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 7125 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7126 | MockRead spdy_reads[] = { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7127 | MockRead(true, kCONNECTResponse, arraysize(kCONNECTResponse) - 1, 1), // 1 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7128 | CreateMockRead(*resp.get(), 4), // 2, 4 |
| 7129 | CreateMockRead(*data.get(), 4), // 5 |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7130 | MockRead(true, 0, 0, 4), // 6 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7131 | }; |
| 7132 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7133 | scoped_refptr<OrderedSocketData> spdy_data( |
| 7134 | new OrderedSocketData( |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7135 | spdy_reads, arraysize(spdy_reads), |
| 7136 | spdy_writes, arraysize(spdy_writes))); |
| 7137 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 7138 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 7139 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 7140 | StaticSocketDataProvider hanging_non_alternate_protocol_socket( |
| 7141 | NULL, 0, NULL, 0); |
| 7142 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 7143 | never_finishing_connect); |
| 7144 | session_deps.socket_factory.AddSocketDataProvider( |
| 7145 | &hanging_non_alternate_protocol_socket); |
| 7146 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 7147 | TestOldCompletionCallback callback; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7148 | |
| 7149 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7150 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 7151 | |
| 7152 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 7153 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7154 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7155 | |
| 7156 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7157 | ASSERT_TRUE(response != NULL); |
| 7158 | ASSERT_TRUE(response->headers != NULL); |
| 7159 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7160 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 7161 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7162 | |
| 7163 | std::string response_data; |
| 7164 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7165 | EXPECT_EQ("hello world", response_data); |
| 7166 | |
| 7167 | trans.reset(new HttpNetworkTransaction(session)); |
| 7168 | |
| 7169 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 7170 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7171 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7172 | |
| 7173 | response = trans->GetResponseInfo(); |
| 7174 | ASSERT_TRUE(response != NULL); |
| 7175 | ASSERT_TRUE(response->headers != NULL); |
| 7176 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7177 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 7178 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7179 | |
| 7180 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7181 | EXPECT_EQ("hello!", response_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 7182 | ASSERT_EQ(3u, capturing_proxy_resolver->resolved().size()); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7183 | EXPECT_EQ("http://www.google.com/", |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7184 | capturing_proxy_resolver->resolved()[0].spec()); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7185 | EXPECT_EQ("https://www.google.com/", |
| 7186 | capturing_proxy_resolver->resolved()[1].spec()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7187 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7188 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7189 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7190 | } |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7191 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7192 | TEST_F(HttpNetworkTransactionTest, |
| 7193 | UseAlternateProtocolForNpnSpdyWithExistingSpdySession) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7194 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7195 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7196 | SessionDependencies session_deps; |
| 7197 | |
| 7198 | HttpRequestInfo request; |
| 7199 | request.method = "GET"; |
| 7200 | request.url = GURL("http://www.google.com/"); |
| 7201 | request.load_flags = 0; |
| 7202 | |
| 7203 | MockRead data_reads[] = { |
| 7204 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 7205 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7206 | MockRead("hello world"), |
| 7207 | MockRead(true, OK), |
| 7208 | }; |
| 7209 | |
| 7210 | StaticSocketDataProvider first_transaction( |
| 7211 | data_reads, arraysize(data_reads), NULL, 0); |
| 7212 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 7213 | |
| 7214 | SSLSocketDataProvider ssl(true, OK); |
| 7215 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 7216 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7217 | ssl.was_npn_negotiated = true; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7218 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7219 | // Make sure we use ssl for spdy here. |
| 7220 | SpdySession::SetSSLMode(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7221 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7222 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7223 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7224 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7225 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 7226 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7227 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7228 | CreateMockRead(*resp), |
| 7229 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7230 | MockRead(true, 0, 0), |
| 7231 | }; |
| 7232 | |
| 7233 | scoped_refptr<DelayedSocketData> spdy_data( |
| 7234 | new DelayedSocketData( |
| 7235 | 1, // wait for one write to finish before reading. |
| 7236 | spdy_reads, arraysize(spdy_reads), |
| 7237 | spdy_writes, arraysize(spdy_writes))); |
| 7238 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 7239 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 7240 | TestOldCompletionCallback callback; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7241 | |
| 7242 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7243 | |
| 7244 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 7245 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 7246 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7247 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7248 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7249 | |
| 7250 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7251 | ASSERT_TRUE(response != NULL); |
| 7252 | ASSERT_TRUE(response->headers != NULL); |
| 7253 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7254 | |
| 7255 | std::string response_data; |
| 7256 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7257 | EXPECT_EQ("hello world", response_data); |
| 7258 | |
| 7259 | // Set up an initial SpdySession in the pool to reuse. |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7260 | HostPortPair host_port_pair("www.google.com", 443); |
| 7261 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7262 | scoped_refptr<SpdySession> spdy_session = |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 7263 | session->spdy_session_pool()->Get(pair, BoundNetLog()); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7264 | scoped_refptr<TransportSocketParams> transport_params( |
[email protected] | acdda41 | 2011-11-15 21:21:29 | [diff] [blame] | 7265 | new TransportSocketParams(host_port_pair, MEDIUM, false, false)); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7266 | |
| 7267 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 7268 | EXPECT_EQ(ERR_IO_PENDING, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7269 | connection->Init(host_port_pair.ToString(), |
| 7270 | transport_params, |
| 7271 | LOWEST, |
| 7272 | &callback, |
| 7273 | session->transport_socket_pool(), |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7274 | BoundNetLog())); |
| 7275 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7276 | |
| 7277 | SSLConfig ssl_config; |
| 7278 | session->ssl_config_service()->GetSSLConfig(&ssl_config); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 7279 | scoped_ptr<ClientSocketHandle> ssl_connection(new ClientSocketHandle); |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 7280 | SSLClientSocketContext context; |
| 7281 | context.cert_verifier = session_deps.cert_verifier.get(); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 7282 | ssl_connection->set_socket(session_deps.socket_factory.CreateSSLClientSocket( |
| 7283 | connection.release(), HostPortPair("" , 443), ssl_config, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 7284 | NULL /* ssl_host_info */, context)); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 7285 | EXPECT_EQ(ERR_IO_PENDING, ssl_connection->socket()->Connect(&callback)); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7286 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7287 | |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 7288 | EXPECT_EQ(OK, spdy_session->InitializeWithSocket(ssl_connection.release(), |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7289 | true, OK)); |
| 7290 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7291 | trans.reset(new HttpNetworkTransaction(session)); |
| 7292 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 7293 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7294 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7295 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7296 | |
| 7297 | response = trans->GetResponseInfo(); |
| 7298 | ASSERT_TRUE(response != NULL); |
| 7299 | ASSERT_TRUE(response->headers != NULL); |
| 7300 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7301 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 7302 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7303 | |
| 7304 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7305 | EXPECT_EQ("hello!", response_data); |
| 7306 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7307 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7308 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 7309 | } |
| 7310 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7311 | // GenerateAuthToken is a mighty big test. |
| 7312 | // It tests all permutation of GenerateAuthToken behavior: |
| 7313 | // - Synchronous and Asynchronous completion. |
| 7314 | // - OK or error on completion. |
| 7315 | // - Direct connection, non-authenticating proxy, and authenticating proxy. |
| 7316 | // - HTTP or HTTPS backend (to include proxy tunneling). |
| 7317 | // - Non-authenticating and authenticating backend. |
| 7318 | // |
| 7319 | // In all, there are 44 reasonable permuations (for example, if there are |
| 7320 | // problems generating an auth token for an authenticating proxy, we don't |
| 7321 | // need to test all permutations of the backend server). |
| 7322 | // |
| 7323 | // The test proceeds by going over each of the configuration cases, and |
| 7324 | // potentially running up to three rounds in each of the tests. The TestConfig |
| 7325 | // specifies both the configuration for the test as well as the expectations |
| 7326 | // for the results. |
| 7327 | TEST_F(HttpNetworkTransactionTest, GenerateAuthToken) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7328 | static const char kServer[] = "http://www.example.com"; |
| 7329 | static const char kSecureServer[] = "https://www.example.com"; |
| 7330 | static const char kProxy[] = "myproxy:70"; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7331 | const int kAuthErr = ERR_INVALID_AUTH_CREDENTIALS; |
| 7332 | |
| 7333 | enum AuthTiming { |
| 7334 | AUTH_NONE, |
| 7335 | AUTH_SYNC, |
| 7336 | AUTH_ASYNC, |
| 7337 | }; |
| 7338 | |
| 7339 | const MockWrite kGet( |
| 7340 | "GET / HTTP/1.1\r\n" |
| 7341 | "Host: www.example.com\r\n" |
| 7342 | "Connection: keep-alive\r\n\r\n"); |
| 7343 | const MockWrite kGetProxy( |
| 7344 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7345 | "Host: www.example.com\r\n" |
| 7346 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 7347 | const MockWrite kGetAuth( |
| 7348 | "GET / HTTP/1.1\r\n" |
| 7349 | "Host: www.example.com\r\n" |
| 7350 | "Connection: keep-alive\r\n" |
| 7351 | "Authorization: auth_token\r\n\r\n"); |
| 7352 | const MockWrite kGetProxyAuth( |
| 7353 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7354 | "Host: www.example.com\r\n" |
| 7355 | "Proxy-Connection: keep-alive\r\n" |
| 7356 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 7357 | const MockWrite kGetAuthThroughProxy( |
| 7358 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7359 | "Host: www.example.com\r\n" |
| 7360 | "Proxy-Connection: keep-alive\r\n" |
| 7361 | "Authorization: auth_token\r\n\r\n"); |
| 7362 | const MockWrite kGetAuthWithProxyAuth( |
| 7363 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7364 | "Host: www.example.com\r\n" |
| 7365 | "Proxy-Connection: keep-alive\r\n" |
| 7366 | "Proxy-Authorization: auth_token\r\n" |
| 7367 | "Authorization: auth_token\r\n\r\n"); |
| 7368 | const MockWrite kConnect( |
| 7369 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 7370 | "Host: www.example.com\r\n" |
| 7371 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 7372 | const MockWrite kConnectProxyAuth( |
| 7373 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 7374 | "Host: www.example.com\r\n" |
| 7375 | "Proxy-Connection: keep-alive\r\n" |
| 7376 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 7377 | |
| 7378 | const MockRead kSuccess( |
| 7379 | "HTTP/1.1 200 OK\r\n" |
| 7380 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7381 | "Content-Length: 3\r\n\r\n" |
| 7382 | "Yes"); |
| 7383 | const MockRead kFailure( |
| 7384 | "Should not be called."); |
| 7385 | const MockRead kServerChallenge( |
| 7386 | "HTTP/1.1 401 Unauthorized\r\n" |
| 7387 | "WWW-Authenticate: Mock realm=server\r\n" |
| 7388 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7389 | "Content-Length: 14\r\n\r\n" |
| 7390 | "Unauthorized\r\n"); |
| 7391 | const MockRead kProxyChallenge( |
| 7392 | "HTTP/1.1 407 Unauthorized\r\n" |
| 7393 | "Proxy-Authenticate: Mock realm=proxy\r\n" |
| 7394 | "Proxy-Connection: close\r\n" |
| 7395 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7396 | "Content-Length: 14\r\n\r\n" |
| 7397 | "Unauthorized\r\n"); |
| 7398 | const MockRead kProxyConnected( |
| 7399 | "HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 7400 | |
| 7401 | // NOTE(cbentzel): I wanted TestReadWriteRound to be a simple struct with |
| 7402 | // no constructors, but the C++ compiler on Windows warns about |
| 7403 | // unspecified data in compound literals. So, moved to using constructors, |
| 7404 | // and TestRound's created with the default constructor should not be used. |
| 7405 | struct TestRound { |
| 7406 | TestRound() |
| 7407 | : expected_rv(ERR_UNEXPECTED), |
| 7408 | extra_write(NULL), |
| 7409 | extra_read(NULL) { |
| 7410 | } |
| 7411 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 7412 | int expected_rv_arg) |
| 7413 | : write(write_arg), |
| 7414 | read(read_arg), |
| 7415 | expected_rv(expected_rv_arg), |
| 7416 | extra_write(NULL), |
| 7417 | extra_read(NULL) { |
| 7418 | } |
| 7419 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 7420 | int expected_rv_arg, const MockWrite* extra_write_arg, |
| 7421 | const MockWrite* extra_read_arg) |
| 7422 | : write(write_arg), |
| 7423 | read(read_arg), |
| 7424 | expected_rv(expected_rv_arg), |
| 7425 | extra_write(extra_write_arg), |
| 7426 | extra_read(extra_read_arg) { |
| 7427 | } |
| 7428 | MockWrite write; |
| 7429 | MockRead read; |
| 7430 | int expected_rv; |
| 7431 | const MockWrite* extra_write; |
| 7432 | const MockRead* extra_read; |
| 7433 | }; |
| 7434 | |
| 7435 | static const int kNoSSL = 500; |
| 7436 | |
| 7437 | struct TestConfig { |
| 7438 | const char* proxy_url; |
| 7439 | AuthTiming proxy_auth_timing; |
| 7440 | int proxy_auth_rv; |
| 7441 | const char* server_url; |
| 7442 | AuthTiming server_auth_timing; |
| 7443 | int server_auth_rv; |
| 7444 | int num_auth_rounds; |
| 7445 | int first_ssl_round; |
| 7446 | TestRound rounds[3]; |
| 7447 | } test_configs[] = { |
| 7448 | // Non-authenticating HTTP server with a direct connection. |
| 7449 | { NULL, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 7450 | { TestRound(kGet, kSuccess, OK)}}, |
| 7451 | // Authenticating HTTP server with a direct connection. |
| 7452 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 7453 | { TestRound(kGet, kServerChallenge, OK), |
| 7454 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7455 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 7456 | { TestRound(kGet, kServerChallenge, OK), |
| 7457 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7458 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 7459 | { TestRound(kGet, kServerChallenge, OK), |
| 7460 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7461 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 7462 | { TestRound(kGet, kServerChallenge, OK), |
| 7463 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7464 | // Non-authenticating HTTP server through a non-authenticating proxy. |
| 7465 | { kProxy, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 7466 | { TestRound(kGetProxy, kSuccess, OK)}}, |
| 7467 | // Authenticating HTTP server through a non-authenticating proxy. |
| 7468 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 7469 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7470 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 7471 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 7472 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7473 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 7474 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 7475 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7476 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 7477 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 7478 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7479 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 7480 | // Non-authenticating HTTP server through an authenticating proxy. |
| 7481 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7482 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7483 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 7484 | { kProxy, AUTH_SYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7485 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7486 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 7487 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7488 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7489 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 7490 | { kProxy, AUTH_ASYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7491 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7492 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 7493 | // Authenticating HTTP server through an authenticating proxy. |
| 7494 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 7495 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7496 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7497 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7498 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 7499 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7500 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7501 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7502 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 7503 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7504 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7505 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7506 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 7507 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7508 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7509 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7510 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 7511 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7512 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7513 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7514 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 7515 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7516 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7517 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7518 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 7519 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7520 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7521 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7522 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 7523 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7524 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7525 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7526 | // Non-authenticating HTTPS server with a direct connection. |
| 7527 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 7528 | { TestRound(kGet, kSuccess, OK)}}, |
| 7529 | // Authenticating HTTPS server with a direct connection. |
| 7530 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 7531 | { TestRound(kGet, kServerChallenge, OK), |
| 7532 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7533 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 7534 | { TestRound(kGet, kServerChallenge, OK), |
| 7535 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7536 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 7537 | { TestRound(kGet, kServerChallenge, OK), |
| 7538 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7539 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 7540 | { TestRound(kGet, kServerChallenge, OK), |
| 7541 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7542 | // Non-authenticating HTTPS server with a non-authenticating proxy. |
| 7543 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 7544 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 7545 | // Authenticating HTTPS server through a non-authenticating proxy. |
| 7546 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 7547 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7548 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7549 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 7550 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7551 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7552 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 7553 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7554 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7555 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 7556 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7557 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7558 | // Non-Authenticating HTTPS server through an authenticating proxy. |
| 7559 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 7560 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7561 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 7562 | { kProxy, AUTH_SYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7563 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7564 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 7565 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 7566 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7567 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 7568 | { kProxy, AUTH_ASYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7569 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7570 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 7571 | // Authenticating HTTPS server through an authenticating proxy. |
| 7572 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 7573 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7574 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7575 | &kGet, &kServerChallenge), |
| 7576 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7577 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 7578 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7579 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7580 | &kGet, &kServerChallenge), |
| 7581 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7582 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 7583 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7584 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7585 | &kGet, &kServerChallenge), |
| 7586 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7587 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 7588 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7589 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7590 | &kGet, &kServerChallenge), |
| 7591 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7592 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 7593 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7594 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7595 | &kGet, &kServerChallenge), |
| 7596 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7597 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 7598 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7599 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7600 | &kGet, &kServerChallenge), |
| 7601 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7602 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 7603 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7604 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7605 | &kGet, &kServerChallenge), |
| 7606 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7607 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 7608 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7609 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7610 | &kGet, &kServerChallenge), |
| 7611 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7612 | }; |
| 7613 | |
| 7614 | SessionDependencies session_deps; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7615 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_configs); ++i) { |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 7616 | HttpAuthHandlerMock::Factory* auth_factory( |
| 7617 | new HttpAuthHandlerMock::Factory()); |
| 7618 | session_deps.http_auth_handler_factory.reset(auth_factory); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7619 | const TestConfig& test_config = test_configs[i]; |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 7620 | |
| 7621 | // Set up authentication handlers as necessary. |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7622 | if (test_config.proxy_auth_timing != AUTH_NONE) { |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 7623 | for (int n = 0; n < 2; n++) { |
| 7624 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 7625 | std::string auth_challenge = "Mock realm=proxy"; |
| 7626 | GURL origin(test_config.proxy_url); |
| 7627 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 7628 | auth_challenge.end()); |
| 7629 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_PROXY, |
| 7630 | origin, BoundNetLog()); |
| 7631 | auth_handler->SetGenerateExpectation( |
| 7632 | test_config.proxy_auth_timing == AUTH_ASYNC, |
| 7633 | test_config.proxy_auth_rv); |
| 7634 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
| 7635 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7636 | } |
| 7637 | if (test_config.server_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 7638 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7639 | std::string auth_challenge = "Mock realm=server"; |
| 7640 | GURL origin(test_config.server_url); |
| 7641 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 7642 | auth_challenge.end()); |
| 7643 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 7644 | origin, BoundNetLog()); |
| 7645 | auth_handler->SetGenerateExpectation( |
| 7646 | test_config.server_auth_timing == AUTH_ASYNC, |
| 7647 | test_config.server_auth_rv); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 7648 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7649 | } |
| 7650 | if (test_config.proxy_url) { |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 7651 | session_deps.proxy_service.reset( |
| 7652 | ProxyService::CreateFixed(test_config.proxy_url)); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7653 | } else { |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 7654 | session_deps.proxy_service.reset(ProxyService::CreateDirect()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7655 | } |
| 7656 | |
| 7657 | HttpRequestInfo request; |
| 7658 | request.method = "GET"; |
| 7659 | request.url = GURL(test_config.server_url); |
| 7660 | request.load_flags = 0; |
| 7661 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7662 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7663 | HttpNetworkTransaction trans(CreateSession(&session_deps)); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7664 | |
| 7665 | for (int round = 0; round < test_config.num_auth_rounds; ++round) { |
| 7666 | const TestRound& read_write_round = test_config.rounds[round]; |
| 7667 | |
| 7668 | // Set up expected reads and writes. |
| 7669 | MockRead reads[2]; |
| 7670 | reads[0] = read_write_round.read; |
| 7671 | size_t length_reads = 1; |
| 7672 | if (read_write_round.extra_read) { |
| 7673 | reads[1] = *read_write_round.extra_read; |
| 7674 | length_reads = 2; |
| 7675 | } |
| 7676 | |
| 7677 | MockWrite writes[2]; |
| 7678 | writes[0] = read_write_round.write; |
| 7679 | size_t length_writes = 1; |
| 7680 | if (read_write_round.extra_write) { |
| 7681 | writes[1] = *read_write_round.extra_write; |
| 7682 | length_writes = 2; |
| 7683 | } |
| 7684 | StaticSocketDataProvider data_provider( |
| 7685 | reads, length_reads, writes, length_writes); |
| 7686 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 7687 | |
| 7688 | // Add an SSL sequence if necessary. |
| 7689 | SSLSocketDataProvider ssl_socket_data_provider(false, OK); |
| 7690 | if (round >= test_config.first_ssl_round) |
| 7691 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7692 | &ssl_socket_data_provider); |
| 7693 | |
| 7694 | // Start or restart the transaction. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 7695 | TestOldCompletionCallback callback; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7696 | int rv; |
| 7697 | if (round == 0) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7698 | rv = trans.Start(&request, &callback, BoundNetLog()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7699 | } else { |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 7700 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), &callback); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7701 | } |
| 7702 | if (rv == ERR_IO_PENDING) |
| 7703 | rv = callback.WaitForResult(); |
| 7704 | |
| 7705 | // Compare results with expected data. |
| 7706 | EXPECT_EQ(read_write_round.expected_rv, rv); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7707 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7708 | if (read_write_round.expected_rv == OK) { |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7709 | ASSERT_TRUE(response != NULL); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7710 | } else { |
| 7711 | EXPECT_TRUE(response == NULL); |
| 7712 | EXPECT_EQ(round + 1, test_config.num_auth_rounds); |
| 7713 | continue; |
| 7714 | } |
| 7715 | if (round + 1 < test_config.num_auth_rounds) { |
| 7716 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 7717 | } else { |
| 7718 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 7719 | } |
| 7720 | } |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 7721 | } |
| 7722 | } |
| 7723 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7724 | TEST_F(HttpNetworkTransactionTest, MultiRoundAuth) { |
| 7725 | // Do multi-round authentication and make sure it works correctly. |
| 7726 | SessionDependencies session_deps; |
| 7727 | HttpAuthHandlerMock::Factory* auth_factory( |
| 7728 | new HttpAuthHandlerMock::Factory()); |
| 7729 | session_deps.http_auth_handler_factory.reset(auth_factory); |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 7730 | session_deps.proxy_service.reset(ProxyService::CreateDirect()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7731 | session_deps.host_resolver->rules()->AddRule("www.example.com", "10.0.0.1"); |
| 7732 | session_deps.host_resolver->set_synchronous_mode(true); |
| 7733 | |
| 7734 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 7735 | auth_handler->set_connection_based(true); |
| 7736 | std::string auth_challenge = "Mock realm=server"; |
| 7737 | GURL origin("http://www.example.com"); |
| 7738 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 7739 | auth_challenge.end()); |
| 7740 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 7741 | origin, BoundNetLog()); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 7742 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7743 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7744 | int rv = OK; |
| 7745 | const HttpResponseInfo* response = NULL; |
| 7746 | HttpRequestInfo request; |
| 7747 | request.method = "GET"; |
| 7748 | request.url = origin; |
| 7749 | request.load_flags = 0; |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7750 | |
| 7751 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7752 | |
| 7753 | // Use a TCP Socket Pool with only one connection per group. This is used |
| 7754 | // to validate that the TCP socket is not released to the pool between |
| 7755 | // each round of multi-round authentication. |
| 7756 | HttpNetworkSessionPeer session_peer(session); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7757 | ClientSocketPoolHistograms transport_pool_histograms("SmallTCP"); |
| 7758 | TransportClientSocketPool* transport_pool = new TransportClientSocketPool( |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7759 | 50, // Max sockets for pool |
| 7760 | 1, // Max sockets per group |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7761 | &transport_pool_histograms, |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7762 | session_deps.host_resolver.get(), |
| 7763 | &session_deps.socket_factory, |
| 7764 | session_deps.net_log); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7765 | session_peer.SetTransportSocketPool(transport_pool); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7766 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7767 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 7768 | TestOldCompletionCallback callback; |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7769 | |
| 7770 | const MockWrite kGet( |
| 7771 | "GET / HTTP/1.1\r\n" |
| 7772 | "Host: www.example.com\r\n" |
| 7773 | "Connection: keep-alive\r\n\r\n"); |
| 7774 | const MockWrite kGetAuth( |
| 7775 | "GET / HTTP/1.1\r\n" |
| 7776 | "Host: www.example.com\r\n" |
| 7777 | "Connection: keep-alive\r\n" |
| 7778 | "Authorization: auth_token\r\n\r\n"); |
| 7779 | |
| 7780 | const MockRead kServerChallenge( |
| 7781 | "HTTP/1.1 401 Unauthorized\r\n" |
| 7782 | "WWW-Authenticate: Mock realm=server\r\n" |
| 7783 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7784 | "Content-Length: 14\r\n\r\n" |
| 7785 | "Unauthorized\r\n"); |
| 7786 | const MockRead kSuccess( |
| 7787 | "HTTP/1.1 200 OK\r\n" |
| 7788 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7789 | "Content-Length: 3\r\n\r\n" |
| 7790 | "Yes"); |
| 7791 | |
| 7792 | MockWrite writes[] = { |
| 7793 | // First round |
| 7794 | kGet, |
| 7795 | // Second round |
| 7796 | kGetAuth, |
| 7797 | // Third round |
| 7798 | kGetAuth, |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7799 | // Fourth round |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7800 | kGetAuth, |
| 7801 | // Competing request |
| 7802 | kGet, |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7803 | }; |
| 7804 | MockRead reads[] = { |
| 7805 | // First round |
| 7806 | kServerChallenge, |
| 7807 | // Second round |
| 7808 | kServerChallenge, |
| 7809 | // Third round |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7810 | kServerChallenge, |
| 7811 | // Fourth round |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7812 | kSuccess, |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7813 | // Competing response |
| 7814 | kSuccess, |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7815 | }; |
| 7816 | StaticSocketDataProvider data_provider(reads, arraysize(reads), |
| 7817 | writes, arraysize(writes)); |
| 7818 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 7819 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7820 | const char* const kSocketGroup = "www.example.com:80"; |
| 7821 | |
| 7822 | // First round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7823 | auth_handler->SetGenerateExpectation(false, OK); |
| 7824 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 7825 | if (rv == ERR_IO_PENDING) |
| 7826 | rv = callback.WaitForResult(); |
| 7827 | EXPECT_EQ(OK, rv); |
| 7828 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7829 | ASSERT_TRUE(response != NULL); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7830 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7831 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7832 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7833 | // In between rounds, another request comes in for the same domain. |
| 7834 | // It should not be able to grab the TCP socket that trans has already |
| 7835 | // claimed. |
| 7836 | scoped_ptr<HttpTransaction> trans_compete( |
| 7837 | new HttpNetworkTransaction(session)); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 7838 | TestOldCompletionCallback callback_compete; |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7839 | rv = trans_compete->Start(&request, &callback_compete, BoundNetLog()); |
| 7840 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7841 | // callback_compete.WaitForResult at this point would stall forever, |
| 7842 | // since the HttpNetworkTransaction does not release the request back to |
| 7843 | // the pool until after authentication completes. |
| 7844 | |
| 7845 | // Second round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7846 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 7847 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7848 | if (rv == ERR_IO_PENDING) |
| 7849 | rv = callback.WaitForResult(); |
| 7850 | EXPECT_EQ(OK, rv); |
| 7851 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7852 | ASSERT_TRUE(response != NULL); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7853 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7854 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7855 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7856 | // Third round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7857 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 7858 | rv = trans->RestartWithAuth(AuthCredentials(), &callback); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7859 | if (rv == ERR_IO_PENDING) |
| 7860 | rv = callback.WaitForResult(); |
| 7861 | EXPECT_EQ(OK, rv); |
| 7862 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7863 | ASSERT_TRUE(response != NULL); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7864 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7865 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7866 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7867 | // Fourth round of authentication, which completes successfully. |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7868 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 7869 | rv = trans->RestartWithAuth(AuthCredentials(), &callback); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7870 | if (rv == ERR_IO_PENDING) |
| 7871 | rv = callback.WaitForResult(); |
| 7872 | EXPECT_EQ(OK, rv); |
| 7873 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7874 | ASSERT_TRUE(response != NULL); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7875 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7876 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7877 | |
| 7878 | // Read the body since the fourth round was successful. This will also |
| 7879 | // release the socket back to the pool. |
| 7880 | scoped_refptr<IOBufferWithSize> io_buf(new IOBufferWithSize(50)); |
| 7881 | rv = trans->Read(io_buf, io_buf->size(), &callback); |
| 7882 | if (rv == ERR_IO_PENDING) |
| 7883 | rv = callback.WaitForResult(); |
| 7884 | EXPECT_EQ(3, rv); |
| 7885 | rv = trans->Read(io_buf, io_buf->size(), &callback); |
| 7886 | EXPECT_EQ(0, rv); |
| 7887 | // There are still 0 idle sockets, since the trans_compete transaction |
| 7888 | // will be handed it immediately after trans releases it to the group. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7889 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7890 | |
| 7891 | // The competing request can now finish. Wait for the headers and then |
| 7892 | // read the body. |
| 7893 | rv = callback_compete.WaitForResult(); |
| 7894 | EXPECT_EQ(OK, rv); |
| 7895 | rv = trans_compete->Read(io_buf, io_buf->size(), &callback); |
| 7896 | if (rv == ERR_IO_PENDING) |
| 7897 | rv = callback.WaitForResult(); |
| 7898 | EXPECT_EQ(3, rv); |
| 7899 | rv = trans_compete->Read(io_buf, io_buf->size(), &callback); |
| 7900 | EXPECT_EQ(0, rv); |
| 7901 | |
| 7902 | // Finally, the socket is released to the group. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7903 | EXPECT_EQ(1, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7904 | } |
| 7905 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7906 | class TLSDecompressionFailureSocketDataProvider : public SocketDataProvider { |
| 7907 | public: |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 7908 | explicit TLSDecompressionFailureSocketDataProvider(bool fail_all) |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7909 | : fail_all_(fail_all) { |
| 7910 | } |
| 7911 | |
| 7912 | virtual MockRead GetNextRead() { |
| 7913 | if (fail_all_) |
| 7914 | return MockRead(false /* async */, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
| 7915 | |
| 7916 | return MockRead(false /* async */, |
| 7917 | "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nok.\r\n"); |
| 7918 | } |
| 7919 | |
| 7920 | virtual MockWriteResult OnWrite(const std::string& data) { |
| 7921 | return MockWriteResult(false /* async */, data.size()); |
| 7922 | } |
| 7923 | |
| 7924 | void Reset() { |
| 7925 | } |
| 7926 | |
| 7927 | private: |
| 7928 | const bool fail_all_; |
| 7929 | }; |
| 7930 | |
| 7931 | // Test that we restart a connection when we see a decompression failure from |
| 7932 | // the peer during the handshake. (In the real world we'll restart with SSLv3 |
| 7933 | // and we won't offer DEFLATE in that case.) |
| 7934 | TEST_F(HttpNetworkTransactionTest, RestartAfterTLSDecompressionFailure) { |
| 7935 | HttpRequestInfo request; |
| 7936 | request.method = "GET"; |
| 7937 | request.url = GURL("https://tlsdecompressionfailure.example.com/"); |
| 7938 | request.load_flags = 0; |
| 7939 | |
| 7940 | SessionDependencies session_deps; |
| 7941 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 7942 | false /* fail all reads */); |
| 7943 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 7944 | SSLSocketDataProvider ssl_socket_data_provider1( |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 7945 | false, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7946 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 7947 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 7948 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 7949 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7950 | &ssl_socket_data_provider1); |
| 7951 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7952 | &ssl_socket_data_provider2); |
| 7953 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 7954 | // Work around http://crbug.com/37454 |
| 7955 | StaticSocketDataProvider bug37454_connection; |
| 7956 | bug37454_connection.set_connect_data(MockConnect(true, ERR_UNEXPECTED)); |
| 7957 | session_deps.socket_factory.AddSocketDataProvider(&bug37454_connection); |
| 7958 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7959 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7960 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 7961 | TestOldCompletionCallback callback; |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7962 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 7963 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7964 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7965 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7966 | |
| 7967 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7968 | ASSERT_TRUE(response != NULL); |
| 7969 | ASSERT_TRUE(response->headers != NULL); |
| 7970 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7971 | |
| 7972 | std::string response_data; |
| 7973 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7974 | EXPECT_EQ("ok.", response_data); |
| 7975 | } |
| 7976 | |
| 7977 | // Test that we restart a connection if we get a decompression failure from the |
| 7978 | // peer while reading the first bytes from the connection. This occurs when the |
| 7979 | // peer cannot handle DEFLATE but we're using False Start, so we don't notice |
| 7980 | // in the handshake. |
| 7981 | TEST_F(HttpNetworkTransactionTest, |
| 7982 | RestartAfterTLSDecompressionFailureWithFalseStart) { |
| 7983 | HttpRequestInfo request; |
| 7984 | request.method = "GET"; |
| 7985 | request.url = GURL("https://tlsdecompressionfailure2.example.com/"); |
| 7986 | request.load_flags = 0; |
| 7987 | |
| 7988 | SessionDependencies session_deps; |
| 7989 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 7990 | true /* fail all reads */); |
| 7991 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 7992 | SSLSocketDataProvider ssl_socket_data_provider1(false, OK); |
| 7993 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 7994 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 7995 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 7996 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7997 | &ssl_socket_data_provider1); |
| 7998 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7999 | &ssl_socket_data_provider2); |
| 8000 | |
| 8001 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8002 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8003 | TestOldCompletionCallback callback; |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 8004 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 8005 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 8006 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8007 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 8008 | |
| 8009 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 8010 | ASSERT_TRUE(response != NULL); |
| 8011 | ASSERT_TRUE(response->headers != NULL); |
| 8012 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8013 | |
| 8014 | std::string response_data; |
| 8015 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 8016 | EXPECT_EQ("ok.", response_data); |
| 8017 | } |
| 8018 | |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8019 | // This tests the case that a request is issued via http instead of spdy after |
| 8020 | // npn is negotiated. |
| 8021 | TEST_F(HttpNetworkTransactionTest, NpnWithHttpOverSSL) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8022 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8023 | HttpStreamFactory::set_next_protos( |
| 8024 | MakeNextProtos("http/1.1", "http1.1", NULL)); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8025 | SessionDependencies session_deps; |
| 8026 | HttpRequestInfo request; |
| 8027 | request.method = "GET"; |
| 8028 | request.url = GURL("https://www.google.com/"); |
| 8029 | request.load_flags = 0; |
| 8030 | |
| 8031 | MockWrite data_writes[] = { |
| 8032 | MockWrite("GET / HTTP/1.1\r\n" |
| 8033 | "Host: www.google.com\r\n" |
| 8034 | "Connection: keep-alive\r\n\r\n"), |
| 8035 | }; |
| 8036 | |
| 8037 | MockRead data_reads[] = { |
| 8038 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 8039 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8040 | MockRead("hello world"), |
| 8041 | MockRead(false, OK), |
| 8042 | }; |
| 8043 | |
| 8044 | SSLSocketDataProvider ssl(true, OK); |
| 8045 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 8046 | ssl.next_proto = "http/1.1"; |
| 8047 | |
| 8048 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8049 | |
| 8050 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 8051 | data_writes, arraysize(data_writes)); |
| 8052 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 8053 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8054 | TestOldCompletionCallback callback; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8055 | |
| 8056 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8057 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 8058 | |
| 8059 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 8060 | |
| 8061 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8062 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 8063 | |
| 8064 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 8065 | ASSERT_TRUE(response != NULL); |
| 8066 | ASSERT_TRUE(response->headers != NULL); |
| 8067 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8068 | |
| 8069 | std::string response_data; |
| 8070 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 8071 | EXPECT_EQ("hello world", response_data); |
| 8072 | |
| 8073 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 8074 | EXPECT_TRUE(response->was_npn_negotiated); |
| 8075 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8076 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8077 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8078 | } |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8079 | |
| 8080 | TEST_F(HttpNetworkTransactionTest, SpdyPostNPNServerHangup) { |
| 8081 | // Simulate the SSL handshake completing with an NPN negotiation |
| 8082 | // followed by an immediate server closing of the socket. |
| 8083 | // Fix crash: http://crbug.com/46369 |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8084 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8085 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8086 | SessionDependencies session_deps; |
| 8087 | |
| 8088 | HttpRequestInfo request; |
| 8089 | request.method = "GET"; |
| 8090 | request.url = GURL("https://www.google.com/"); |
| 8091 | request.load_flags = 0; |
| 8092 | |
| 8093 | SSLSocketDataProvider ssl(true, OK); |
| 8094 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 8095 | ssl.next_proto = "spdy/2"; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8096 | ssl.was_npn_negotiated = true; |
| 8097 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8098 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 8099 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 8100 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8101 | |
| 8102 | MockRead spdy_reads[] = { |
| 8103 | MockRead(false, 0, 0) // Not async - return 0 immediately. |
| 8104 | }; |
| 8105 | |
| 8106 | scoped_refptr<DelayedSocketData> spdy_data( |
| 8107 | new DelayedSocketData( |
| 8108 | 0, // don't wait in this case, immediate hangup. |
| 8109 | spdy_reads, arraysize(spdy_reads), |
| 8110 | spdy_writes, arraysize(spdy_writes))); |
| 8111 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 8112 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8113 | TestOldCompletionCallback callback; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8114 | |
| 8115 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8116 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8117 | |
| 8118 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 8119 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8120 | EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult()); |
| 8121 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8122 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8123 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8124 | } |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 8125 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8126 | TEST_F(HttpNetworkTransactionTest, SpdyAlternateProtocolThroughProxy) { |
| 8127 | // This test ensures that the URL passed into the proxy is upgraded |
| 8128 | // to https when doing an Alternate Protocol upgrade. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8129 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 8130 | HttpStreamFactory::set_next_protos( |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8131 | MakeNextProtos("http/1.1", "http1.1", "spdy/2", "spdy", NULL)); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8132 | |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 8133 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8134 | HttpAuthHandlerMock::Factory* auth_factory = |
| 8135 | new HttpAuthHandlerMock::Factory(); |
| 8136 | HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 8137 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8138 | auth_factory->set_do_init_from_challenge(true); |
| 8139 | session_deps.http_auth_handler_factory.reset(auth_factory); |
| 8140 | |
| 8141 | HttpRequestInfo request; |
| 8142 | request.method = "GET"; |
| 8143 | request.url = GURL("http://www.google.com"); |
| 8144 | request.load_flags = 0; |
| 8145 | |
| 8146 | // First round goes unauthenticated through the proxy. |
| 8147 | MockWrite data_writes_1[] = { |
| 8148 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 8149 | "Host: www.google.com\r\n" |
| 8150 | "Proxy-Connection: keep-alive\r\n" |
| 8151 | "\r\n"), |
| 8152 | }; |
| 8153 | MockRead data_reads_1[] = { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8154 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8155 | MockRead("HTTP/1.1 200 OK\r\n" |
| 8156 | "Alternate-Protocol: 443:npn-spdy/2\r\n" |
| 8157 | "Proxy-Connection: close\r\n" |
| 8158 | "\r\n"), |
| 8159 | }; |
| 8160 | StaticSocketDataProvider data_1(data_reads_1, arraysize(data_reads_1), |
| 8161 | data_writes_1, arraysize(data_writes_1)); |
| 8162 | |
| 8163 | // Second round tries to tunnel to www.google.com due to the |
| 8164 | // Alternate-Protocol announcement in the first round. It fails due |
| 8165 | // to a proxy authentication challenge. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8166 | // After the failure, a tunnel is established to www.google.com using |
| 8167 | // Proxy-Authorization headers. There is then a SPDY request round. |
| 8168 | // |
| 8169 | // NOTE: Despite the "Proxy-Connection: Close", these are done on the |
| 8170 | // same MockTCPClientSocket since the underlying HttpNetworkClientSocket |
| 8171 | // does a Disconnect and Connect on the same socket, rather than trying |
| 8172 | // to obtain a new one. |
| 8173 | // |
| 8174 | // NOTE: Originally, the proxy response to the second CONNECT request |
| 8175 | // simply returned another 407 so the unit test could skip the SSL connection |
| 8176 | // establishment and SPDY framing issues. Alas, the |
| 8177 | // retry-http-when-alternate-protocol fails logic kicks in, which was more |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8178 | // complicated to set up expectations for than the SPDY session. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8179 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8180 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 8181 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 8182 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 8183 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8184 | MockWrite data_writes_2[] = { |
| 8185 | // First connection attempt without Proxy-Authorization. |
| 8186 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 8187 | "Host: www.google.com\r\n" |
| 8188 | "Proxy-Connection: keep-alive\r\n" |
| 8189 | "\r\n"), |
| 8190 | |
| 8191 | // Second connection attempt with Proxy-Authorization. |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8192 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 8193 | "Host: www.google.com\r\n" |
| 8194 | "Proxy-Connection: keep-alive\r\n" |
| 8195 | "Proxy-Authorization: auth_token\r\n" |
| 8196 | "\r\n"), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8197 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8198 | // SPDY request |
| 8199 | CreateMockWrite(*req), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8200 | }; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8201 | const char kRejectConnectResponse[] = ("HTTP/1.1 407 Unauthorized\r\n" |
| 8202 | "Proxy-Authenticate: Mock\r\n" |
| 8203 | "Proxy-Connection: close\r\n" |
| 8204 | "\r\n"); |
| 8205 | const char kAcceptConnectResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 8206 | MockRead data_reads_2[] = { |
| 8207 | // First connection attempt fails |
| 8208 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, 1), |
| 8209 | MockRead(true, kRejectConnectResponse, |
| 8210 | arraysize(kRejectConnectResponse) - 1, 1), |
| 8211 | |
| 8212 | // Second connection attempt passes |
| 8213 | MockRead(true, kAcceptConnectResponse, |
| 8214 | arraysize(kAcceptConnectResponse) -1, 4), |
| 8215 | |
| 8216 | // SPDY response |
| 8217 | CreateMockRead(*resp.get(), 6), |
| 8218 | CreateMockRead(*data.get(), 6), |
| 8219 | MockRead(true, 0, 0, 6), |
| 8220 | }; |
| 8221 | scoped_refptr<OrderedSocketData> data_2( |
| 8222 | new OrderedSocketData(data_reads_2, arraysize(data_reads_2), |
| 8223 | data_writes_2, arraysize(data_writes_2))); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8224 | |
| 8225 | SSLSocketDataProvider ssl(true, OK); |
| 8226 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 8227 | ssl.next_proto = "spdy/2"; |
| 8228 | ssl.was_npn_negotiated = true; |
| 8229 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 8230 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 8231 | StaticSocketDataProvider hanging_non_alternate_protocol_socket( |
| 8232 | NULL, 0, NULL, 0); |
| 8233 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 8234 | never_finishing_connect); |
| 8235 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8236 | session_deps.socket_factory.AddSocketDataProvider(&data_1); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8237 | session_deps.socket_factory.AddSocketDataProvider(data_2.get()); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8238 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 8239 | session_deps.socket_factory.AddSocketDataProvider( |
| 8240 | &hanging_non_alternate_protocol_socket); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8241 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8242 | |
| 8243 | // First round should work and provide the Alternate-Protocol state. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8244 | TestOldCompletionCallback callback_1; |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8245 | scoped_ptr<HttpTransaction> trans_1(new HttpNetworkTransaction(session)); |
| 8246 | int rv = trans_1->Start(&request, &callback_1, BoundNetLog()); |
| 8247 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8248 | EXPECT_EQ(OK, callback_1.WaitForResult()); |
| 8249 | |
| 8250 | // Second round should attempt a tunnel connect and get an auth challenge. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8251 | TestOldCompletionCallback callback_2; |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8252 | scoped_ptr<HttpTransaction> trans_2(new HttpNetworkTransaction(session)); |
| 8253 | rv = trans_2->Start(&request, &callback_2, BoundNetLog()); |
| 8254 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8255 | EXPECT_EQ(OK, callback_2.WaitForResult()); |
| 8256 | const HttpResponseInfo* response = trans_2->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 8257 | ASSERT_TRUE(response != NULL); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8258 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 8259 | |
| 8260 | // Restart with auth. Tunnel should work and response received. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8261 | TestOldCompletionCallback callback_3; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 8262 | rv = trans_2->RestartWithAuth(AuthCredentials(kFoo, kBar), &callback_3); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8263 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8264 | EXPECT_EQ(OK, callback_3.WaitForResult()); |
| 8265 | |
| 8266 | // After all that work, these two lines (or actually, just the scheme) are |
| 8267 | // what this test is all about. Make sure it happens correctly. |
| 8268 | const GURL& request_url = auth_handler->request_url(); |
| 8269 | EXPECT_EQ("https", request_url.scheme()); |
| 8270 | EXPECT_EQ("www.google.com", request_url.host()); |
| 8271 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8272 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8273 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 8274 | } |
| 8275 | |
| 8276 | // Test that if we cancel the transaction as the connection is completing, that |
| 8277 | // everything tears down correctly. |
| 8278 | TEST_F(HttpNetworkTransactionTest, SimpleCancel) { |
| 8279 | // Setup everything about the connection to complete synchronously, so that |
| 8280 | // after calling HttpNetworkTransaction::Start, the only thing we're waiting |
| 8281 | // for is the callback from the HttpStreamRequest. |
| 8282 | // Then cancel the transaction. |
| 8283 | // Verify that we don't crash. |
| 8284 | MockConnect mock_connect(false, OK); |
| 8285 | MockRead data_reads[] = { |
| 8286 | MockRead(false, "HTTP/1.0 200 OK\r\n\r\n"), |
| 8287 | MockRead(false, "hello world"), |
| 8288 | MockRead(false, OK), |
| 8289 | }; |
| 8290 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8291 | HttpRequestInfo request; |
| 8292 | request.method = "GET"; |
| 8293 | request.url = GURL("http://www.google.com/"); |
| 8294 | request.load_flags = 0; |
| 8295 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8296 | SessionDependencies session_deps; |
| 8297 | session_deps.host_resolver->set_synchronous_mode(true); |
| 8298 | scoped_ptr<HttpTransaction> trans( |
| 8299 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 8300 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8301 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 8302 | data.set_connect_data(mock_connect); |
| 8303 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 8304 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8305 | TestOldCompletionCallback callback; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8306 | |
| 8307 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8308 | int rv = trans->Start(&request, &callback, log.bound()); |
| 8309 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8310 | trans.reset(); // Cancel the transaction here. |
| 8311 | |
| 8312 | MessageLoop::current()->RunAllPending(); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8313 | } |
| 8314 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8315 | // Test a basic GET request through a proxy. |
| 8316 | TEST_F(HttpNetworkTransactionTest, ProxyGet) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 8317 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8318 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8319 | session_deps.net_log = log.bound().net_log(); |
| 8320 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8321 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8322 | HttpRequestInfo request; |
| 8323 | request.method = "GET"; |
| 8324 | request.url = GURL("http://www.google.com/"); |
| 8325 | |
| 8326 | MockWrite data_writes1[] = { |
| 8327 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 8328 | "Host: www.google.com\r\n" |
| 8329 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 8330 | }; |
| 8331 | |
| 8332 | MockRead data_reads1[] = { |
| 8333 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8334 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 8335 | MockRead("Content-Length: 100\r\n\r\n"), |
| 8336 | MockRead(false, OK), |
| 8337 | }; |
| 8338 | |
| 8339 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 8340 | data_writes1, arraysize(data_writes1)); |
| 8341 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8342 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8343 | TestOldCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8344 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 8345 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 8346 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8347 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 8348 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8349 | |
| 8350 | rv = callback1.WaitForResult(); |
| 8351 | EXPECT_EQ(OK, rv); |
| 8352 | |
| 8353 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 8354 | ASSERT_TRUE(response != NULL); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8355 | |
| 8356 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 8357 | EXPECT_EQ(200, response->headers->response_code()); |
| 8358 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 8359 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 8360 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 8361 | } |
| 8362 | |
| 8363 | // Test a basic HTTPS GET request through a proxy. |
| 8364 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGet) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 8365 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8366 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8367 | session_deps.net_log = log.bound().net_log(); |
| 8368 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8369 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8370 | HttpRequestInfo request; |
| 8371 | request.method = "GET"; |
| 8372 | request.url = GURL("https://www.google.com/"); |
| 8373 | |
| 8374 | // Since we have proxy, should try to establish tunnel. |
| 8375 | MockWrite data_writes1[] = { |
| 8376 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 8377 | "Host: www.google.com\r\n" |
| 8378 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 8379 | |
| 8380 | MockWrite("GET / HTTP/1.1\r\n" |
| 8381 | "Host: www.google.com\r\n" |
| 8382 | "Connection: keep-alive\r\n\r\n"), |
| 8383 | }; |
| 8384 | |
| 8385 | MockRead data_reads1[] = { |
| 8386 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 8387 | |
| 8388 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8389 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 8390 | MockRead("Content-Length: 100\r\n\r\n"), |
| 8391 | MockRead(false, OK), |
| 8392 | }; |
| 8393 | |
| 8394 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 8395 | data_writes1, arraysize(data_writes1)); |
| 8396 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8397 | SSLSocketDataProvider ssl(true, OK); |
| 8398 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8399 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8400 | TestOldCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8401 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 8402 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 8403 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8404 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 8405 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8406 | |
| 8407 | rv = callback1.WaitForResult(); |
| 8408 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8409 | net::CapturingNetLog::EntryList entries; |
| 8410 | log.GetEntries(&entries); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8411 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8412 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8413 | NetLog::PHASE_NONE); |
| 8414 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8415 | entries, pos, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8416 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 8417 | NetLog::PHASE_NONE); |
| 8418 | |
| 8419 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 8420 | ASSERT_TRUE(response != NULL); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8421 | |
| 8422 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 8423 | EXPECT_EQ(200, response->headers->response_code()); |
| 8424 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 8425 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 8426 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 8427 | } |
| 8428 | |
| 8429 | // Test a basic HTTPS GET request through a proxy, but the server hangs up |
| 8430 | // while establishing the tunnel. |
| 8431 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGetHangup) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 8432 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8433 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8434 | session_deps.net_log = log.bound().net_log(); |
| 8435 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8436 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8437 | HttpRequestInfo request; |
| 8438 | request.method = "GET"; |
| 8439 | request.url = GURL("https://www.google.com/"); |
| 8440 | |
| 8441 | // Since we have proxy, should try to establish tunnel. |
| 8442 | MockWrite data_writes1[] = { |
| 8443 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 8444 | "Host: www.google.com\r\n" |
| 8445 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 8446 | |
| 8447 | MockWrite("GET / HTTP/1.1\r\n" |
| 8448 | "Host: www.google.com\r\n" |
| 8449 | "Connection: keep-alive\r\n\r\n"), |
| 8450 | }; |
| 8451 | |
| 8452 | MockRead data_reads1[] = { |
| 8453 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 8454 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 8455 | MockRead(true, 0, 0), // EOF |
| 8456 | }; |
| 8457 | |
| 8458 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 8459 | data_writes1, arraysize(data_writes1)); |
| 8460 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8461 | SSLSocketDataProvider ssl(true, OK); |
| 8462 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8463 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8464 | TestOldCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8465 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 8466 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 8467 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8468 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 8469 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8470 | |
| 8471 | rv = callback1.WaitForResult(); |
| 8472 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8473 | net::CapturingNetLog::EntryList entries; |
| 8474 | log.GetEntries(&entries); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8475 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8476 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8477 | NetLog::PHASE_NONE); |
| 8478 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8479 | entries, pos, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8480 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 8481 | NetLog::PHASE_NONE); |
| 8482 | } |
| 8483 | |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8484 | // Test for crbug.com/55424. |
| 8485 | TEST_F(HttpNetworkTransactionTest, PreconnectWithExistingSpdySession) { |
| 8486 | SessionDependencies session_deps; |
| 8487 | |
| 8488 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet( |
| 8489 | "https://www.google.com", false, 1, LOWEST)); |
| 8490 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
| 8491 | |
| 8492 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 8493 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 8494 | MockRead spdy_reads[] = { |
| 8495 | CreateMockRead(*resp), |
| 8496 | CreateMockRead(*data), |
| 8497 | MockRead(true, 0, 0), |
| 8498 | }; |
| 8499 | |
| 8500 | scoped_refptr<DelayedSocketData> spdy_data( |
| 8501 | new DelayedSocketData( |
| 8502 | 1, // wait for one write to finish before reading. |
| 8503 | spdy_reads, arraysize(spdy_reads), |
| 8504 | spdy_writes, arraysize(spdy_writes))); |
| 8505 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 8506 | |
| 8507 | SSLSocketDataProvider ssl(true, OK); |
| 8508 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 8509 | ssl.next_proto = "spdy/2"; |
| 8510 | ssl.was_npn_negotiated = true; |
| 8511 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8512 | |
| 8513 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8514 | |
| 8515 | // Set up an initial SpdySession in the pool to reuse. |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 8516 | HostPortPair host_port_pair("www.google.com", 443); |
| 8517 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8518 | scoped_refptr<SpdySession> spdy_session = |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 8519 | session->spdy_session_pool()->Get(pair, BoundNetLog()); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 8520 | scoped_refptr<TransportSocketParams> transport_params( |
[email protected] | acdda41 | 2011-11-15 21:21:29 | [diff] [blame] | 8521 | new TransportSocketParams(host_port_pair, MEDIUM, false, false)); |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8522 | TestOldCompletionCallback callback; |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 8523 | |
| 8524 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 8525 | EXPECT_EQ(ERR_IO_PENDING, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 8526 | connection->Init(host_port_pair.ToString(), transport_params, |
| 8527 | LOWEST, &callback, |
| 8528 | session->transport_socket_pool(), BoundNetLog())); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 8529 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 8530 | spdy_session->InitializeWithSocket(connection.release(), false, OK); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8531 | |
| 8532 | HttpRequestInfo request; |
| 8533 | request.method = "GET"; |
| 8534 | request.url = GURL("https://www.google.com/"); |
| 8535 | request.load_flags = 0; |
| 8536 | |
| 8537 | // This is the important line that marks this as a preconnect. |
| 8538 | request.motivation = HttpRequestInfo::PRECONNECT_MOTIVATED; |
| 8539 | |
| 8540 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8541 | |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8542 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 8543 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8544 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 8545 | } |
| 8546 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8547 | // Given a net error, cause that error to be returned from the first Write() |
| 8548 | // call and verify that the HttpTransaction fails with that error. |
| 8549 | static void CheckErrorIsPassedBack(int error, bool async) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8550 | net::HttpRequestInfo request_info; |
| 8551 | request_info.url = GURL("https://www.example.com/"); |
| 8552 | request_info.method = "GET"; |
| 8553 | request_info.load_flags = net::LOAD_NORMAL; |
| 8554 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8555 | SessionDependencies session_deps; |
| 8556 | |
| 8557 | SSLSocketDataProvider ssl_data(async, OK); |
| 8558 | net::MockWrite data_writes[] = { |
| 8559 | net::MockWrite(async, error), |
| 8560 | }; |
| 8561 | net::StaticSocketDataProvider data(NULL, 0, |
| 8562 | data_writes, arraysize(data_writes)); |
| 8563 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 8564 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data); |
| 8565 | |
| 8566 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8567 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8568 | |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8569 | TestOldCompletionCallback callback; |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8570 | int rv = trans->Start(&request_info, &callback, net::BoundNetLog()); |
| 8571 | if (rv == net::ERR_IO_PENDING) |
| 8572 | rv = callback.WaitForResult(); |
| 8573 | ASSERT_EQ(error, rv); |
| 8574 | } |
| 8575 | |
| 8576 | TEST_F(HttpNetworkTransactionTest, SSLWriteCertError) { |
| 8577 | // Just check a grab bag of cert errors. |
| 8578 | static const int kErrors[] = { |
| 8579 | ERR_CERT_COMMON_NAME_INVALID, |
| 8580 | ERR_CERT_AUTHORITY_INVALID, |
| 8581 | ERR_CERT_DATE_INVALID, |
| 8582 | }; |
| 8583 | for (size_t i = 0; i < arraysize(kErrors); i++) { |
| 8584 | CheckErrorIsPassedBack(kErrors[i], false /* not async */); |
| 8585 | CheckErrorIsPassedBack(kErrors[i], true /* async */); |
| 8586 | } |
| 8587 | } |
| 8588 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8589 | // Ensure that a client certificate is removed from the SSL client auth |
| 8590 | // cache when: |
| 8591 | // 1) No proxy is involved. |
| 8592 | // 2) TLS False Start is disabled. |
| 8593 | // 3) The initial TLS handshake requests a client certificate. |
| 8594 | // 4) The client supplies an invalid/unacceptable certificate. |
| 8595 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_NoFalseStart) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8596 | net::HttpRequestInfo request_info; |
| 8597 | request_info.url = GURL("https://www.example.com/"); |
| 8598 | request_info.method = "GET"; |
| 8599 | request_info.load_flags = net::LOAD_NORMAL; |
| 8600 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8601 | SessionDependencies session_deps; |
| 8602 | |
| 8603 | scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); |
| 8604 | cert_request->host_and_port = "www.example.com:443"; |
| 8605 | |
| 8606 | // [ssl_]data1 contains the data for the first SSL handshake. When a |
| 8607 | // CertificateRequest is received for the first time, the handshake will |
| 8608 | // be aborted to allow the caller to provide a certificate. |
| 8609 | SSLSocketDataProvider ssl_data1(true /* async */, |
| 8610 | net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 8611 | ssl_data1.cert_request_info = cert_request.get(); |
| 8612 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8613 | net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| 8614 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8615 | |
| 8616 | // [ssl_]data2 contains the data for the second SSL handshake. When TLS |
| 8617 | // False Start is not being used, the result of the SSL handshake will be |
| 8618 | // returned as part of the SSLClientSocket::Connect() call. This test |
| 8619 | // matches the result of a server sending a handshake_failure alert, |
| 8620 | // rather than a Finished message, because it requires a client |
| 8621 | // certificate and none was supplied. |
| 8622 | SSLSocketDataProvider ssl_data2(true /* async */, |
| 8623 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8624 | ssl_data2.cert_request_info = cert_request.get(); |
| 8625 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8626 | net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); |
| 8627 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8628 | |
| 8629 | // [ssl_]data3 contains the data for the third SSL handshake. When a |
| 8630 | // connection to a server fails during an SSL handshake, |
| 8631 | // HttpNetworkTransaction will attempt to fallback to SSLv3 if the initial |
| 8632 | // connection was attempted with TLSv1. This is transparent to the caller |
| 8633 | // of the HttpNetworkTransaction. Because this test failure is due to |
| 8634 | // requiring a client certificate, this fallback handshake should also |
| 8635 | // fail. |
| 8636 | SSLSocketDataProvider ssl_data3(true /* async */, |
| 8637 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8638 | ssl_data3.cert_request_info = cert_request.get(); |
| 8639 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data3); |
| 8640 | net::StaticSocketDataProvider data3(NULL, 0, NULL, 0); |
| 8641 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 8642 | |
| 8643 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8644 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8645 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8646 | // Begin the SSL handshake with the peer. This consumes ssl_data1. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8647 | TestOldCompletionCallback callback; |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8648 | int rv = trans->Start(&request_info, &callback, net::BoundNetLog()); |
| 8649 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8650 | |
| 8651 | // Complete the SSL handshake, which should abort due to requiring a |
| 8652 | // client certificate. |
| 8653 | rv = callback.WaitForResult(); |
| 8654 | ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 8655 | |
| 8656 | // Indicate that no certificate should be supplied. From the perspective |
| 8657 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 8658 | // certificate, so this is the same as supply a |
| 8659 | // legitimate-but-unacceptable certificate. |
| 8660 | rv = trans->RestartWithCertificate(NULL, &callback); |
| 8661 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8662 | |
| 8663 | // Ensure the certificate was added to the client auth cache before |
| 8664 | // allowing the connection to continue restarting. |
| 8665 | scoped_refptr<X509Certificate> client_cert; |
| 8666 | ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8667 | &client_cert)); |
| 8668 | ASSERT_EQ(NULL, client_cert.get()); |
| 8669 | |
| 8670 | // Restart the handshake. This will consume ssl_data2, which fails, and |
| 8671 | // then consume ssl_data3, which should also fail. The result code is |
| 8672 | // checked against what ssl_data3 should return. |
| 8673 | rv = callback.WaitForResult(); |
| 8674 | ASSERT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); |
| 8675 | |
| 8676 | // Ensure that the client certificate is removed from the cache on a |
| 8677 | // handshake failure. |
| 8678 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8679 | &client_cert)); |
| 8680 | } |
| 8681 | |
| 8682 | // Ensure that a client certificate is removed from the SSL client auth |
| 8683 | // cache when: |
| 8684 | // 1) No proxy is involved. |
| 8685 | // 2) TLS False Start is enabled. |
| 8686 | // 3) The initial TLS handshake requests a client certificate. |
| 8687 | // 4) The client supplies an invalid/unacceptable certificate. |
| 8688 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_FalseStart) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8689 | net::HttpRequestInfo request_info; |
| 8690 | request_info.url = GURL("https://www.example.com/"); |
| 8691 | request_info.method = "GET"; |
| 8692 | request_info.load_flags = net::LOAD_NORMAL; |
| 8693 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8694 | SessionDependencies session_deps; |
| 8695 | |
| 8696 | scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); |
| 8697 | cert_request->host_and_port = "www.example.com:443"; |
| 8698 | |
| 8699 | // When TLS False Start is used, SSLClientSocket::Connect() calls will |
| 8700 | // return successfully after reading up to the peer's Certificate message. |
| 8701 | // This is to allow the caller to call SSLClientSocket::Write(), which can |
| 8702 | // enqueue application data to be sent in the same packet as the |
| 8703 | // ChangeCipherSpec and Finished messages. |
| 8704 | // The actual handshake will be finished when SSLClientSocket::Read() is |
| 8705 | // called, which expects to process the peer's ChangeCipherSpec and |
| 8706 | // Finished messages. If there was an error negotiating with the peer, |
| 8707 | // such as due to the peer requiring a client certificate when none was |
| 8708 | // supplied, the alert sent by the peer won't be processed until Read() is |
| 8709 | // called. |
| 8710 | |
| 8711 | // Like the non-False Start case, when a client certificate is requested by |
| 8712 | // the peer, the handshake is aborted during the Connect() call. |
| 8713 | // [ssl_]data1 represents the initial SSL handshake with the peer. |
| 8714 | SSLSocketDataProvider ssl_data1(true /* async */, |
| 8715 | net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 8716 | ssl_data1.cert_request_info = cert_request.get(); |
| 8717 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8718 | net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| 8719 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8720 | |
| 8721 | // When a client certificate is supplied, Connect() will not be aborted |
| 8722 | // when the peer requests the certificate. Instead, the handshake will |
| 8723 | // artificially succeed, allowing the caller to write the HTTP request to |
| 8724 | // the socket. The handshake messages are not processed until Read() is |
| 8725 | // called, which then detects that the handshake was aborted, due to the |
| 8726 | // peer sending a handshake_failure because it requires a client |
| 8727 | // certificate. |
| 8728 | SSLSocketDataProvider ssl_data2(true /* async */, net::OK); |
| 8729 | ssl_data2.cert_request_info = cert_request.get(); |
| 8730 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8731 | net::MockRead data2_reads[] = { |
| 8732 | net::MockRead(true /* async */, net::ERR_SSL_PROTOCOL_ERROR), |
| 8733 | }; |
| 8734 | net::StaticSocketDataProvider data2( |
| 8735 | data2_reads, arraysize(data2_reads), NULL, 0); |
| 8736 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8737 | |
| 8738 | // As described in ClientAuthCertCache_Direct_NoFalseStart, [ssl_]data3 is |
| 8739 | // the data for the SSL handshake once the TLSv1 connection falls back to |
| 8740 | // SSLv3. It has the same behaviour as [ssl_]data2. |
| 8741 | SSLSocketDataProvider ssl_data3(true /* async */, net::OK); |
| 8742 | ssl_data3.cert_request_info = cert_request.get(); |
| 8743 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data3); |
| 8744 | net::StaticSocketDataProvider data3( |
| 8745 | data2_reads, arraysize(data2_reads), NULL, 0); |
| 8746 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 8747 | |
| 8748 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8749 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8750 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8751 | // Begin the initial SSL handshake. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8752 | TestOldCompletionCallback callback; |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8753 | int rv = trans->Start(&request_info, &callback, net::BoundNetLog()); |
| 8754 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8755 | |
| 8756 | // Complete the SSL handshake, which should abort due to requiring a |
| 8757 | // client certificate. |
| 8758 | rv = callback.WaitForResult(); |
| 8759 | ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 8760 | |
| 8761 | // Indicate that no certificate should be supplied. From the perspective |
| 8762 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 8763 | // certificate, so this is the same as supply a |
| 8764 | // legitimate-but-unacceptable certificate. |
| 8765 | rv = trans->RestartWithCertificate(NULL, &callback); |
| 8766 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8767 | |
| 8768 | // Ensure the certificate was added to the client auth cache before |
| 8769 | // allowing the connection to continue restarting. |
| 8770 | scoped_refptr<X509Certificate> client_cert; |
| 8771 | ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8772 | &client_cert)); |
| 8773 | ASSERT_EQ(NULL, client_cert.get()); |
| 8774 | |
| 8775 | |
| 8776 | // Restart the handshake. This will consume ssl_data2, which fails, and |
| 8777 | // then consume ssl_data3, which should also fail. The result code is |
| 8778 | // checked against what ssl_data3 should return. |
| 8779 | rv = callback.WaitForResult(); |
| 8780 | ASSERT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); |
| 8781 | |
| 8782 | // Ensure that the client certificate is removed from the cache on a |
| 8783 | // handshake failure. |
| 8784 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8785 | &client_cert)); |
| 8786 | } |
| 8787 | |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 8788 | // Ensure that a client certificate is removed from the SSL client auth |
| 8789 | // cache when: |
| 8790 | // 1) An HTTPS proxy is involved. |
| 8791 | // 3) The HTTPS proxy requests a client certificate. |
| 8792 | // 4) The client supplies an invalid/unacceptable certificate for the |
| 8793 | // proxy. |
| 8794 | // The test is repeated twice, first for connecting to an HTTPS endpoint, |
| 8795 | // then for connecting to an HTTP endpoint. |
| 8796 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Proxy_Fail) { |
| 8797 | SessionDependencies session_deps( |
| 8798 | ProxyService::CreateFixed("https://proxy:70")); |
| 8799 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8800 | session_deps.net_log = log.bound().net_log(); |
| 8801 | |
| 8802 | scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); |
| 8803 | cert_request->host_and_port = "proxy:70"; |
| 8804 | |
| 8805 | // See ClientAuthCertCache_Direct_NoFalseStart for the explanation of |
| 8806 | // [ssl_]data[1-3]. Rather than represending the endpoint |
| 8807 | // (www.example.com:443), they represent failures with the HTTPS proxy |
| 8808 | // (proxy:70). |
| 8809 | SSLSocketDataProvider ssl_data1(true /* async */, |
| 8810 | net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 8811 | ssl_data1.cert_request_info = cert_request.get(); |
| 8812 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8813 | net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| 8814 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8815 | |
| 8816 | SSLSocketDataProvider ssl_data2(true /* async */, |
| 8817 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8818 | ssl_data2.cert_request_info = cert_request.get(); |
| 8819 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8820 | net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); |
| 8821 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8822 | |
| 8823 | SSLSocketDataProvider ssl_data3(true /* async */, |
| 8824 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8825 | ssl_data3.cert_request_info = cert_request.get(); |
| 8826 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data3); |
| 8827 | net::StaticSocketDataProvider data3(NULL, 0, NULL, 0); |
| 8828 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 8829 | |
| 8830 | net::HttpRequestInfo requests[2]; |
| 8831 | requests[0].url = GURL("https://www.example.com/"); |
| 8832 | requests[0].method = "GET"; |
| 8833 | requests[0].load_flags = net::LOAD_NORMAL; |
| 8834 | |
| 8835 | requests[1].url = GURL("http://www.example.com/"); |
| 8836 | requests[1].method = "GET"; |
| 8837 | requests[1].load_flags = net::LOAD_NORMAL; |
| 8838 | |
| 8839 | for (size_t i = 0; i < arraysize(requests); ++i) { |
| 8840 | session_deps.socket_factory.ResetNextMockIndexes(); |
| 8841 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8842 | scoped_ptr<HttpNetworkTransaction> trans( |
| 8843 | new HttpNetworkTransaction(session)); |
| 8844 | |
| 8845 | // Begin the SSL handshake with the proxy. |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 8846 | TestOldCompletionCallback callback; |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 8847 | int rv = trans->Start(&requests[i], &callback, net::BoundNetLog()); |
| 8848 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8849 | |
| 8850 | // Complete the SSL handshake, which should abort due to requiring a |
| 8851 | // client certificate. |
| 8852 | rv = callback.WaitForResult(); |
| 8853 | ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 8854 | |
| 8855 | // Indicate that no certificate should be supplied. From the perspective |
| 8856 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 8857 | // certificate, so this is the same as supply a |
| 8858 | // legitimate-but-unacceptable certificate. |
| 8859 | rv = trans->RestartWithCertificate(NULL, &callback); |
| 8860 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8861 | |
| 8862 | // Ensure the certificate was added to the client auth cache before |
| 8863 | // allowing the connection to continue restarting. |
| 8864 | scoped_refptr<X509Certificate> client_cert; |
| 8865 | ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("proxy:70", |
| 8866 | &client_cert)); |
| 8867 | ASSERT_EQ(NULL, client_cert.get()); |
| 8868 | // Ensure the certificate was NOT cached for the endpoint. This only |
| 8869 | // applies to HTTPS requests, but is fine to check for HTTP requests. |
| 8870 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8871 | &client_cert)); |
| 8872 | |
| 8873 | // Restart the handshake. This will consume ssl_data2, which fails, and |
| 8874 | // then consume ssl_data3, which should also fail. The result code is |
| 8875 | // checked against what ssl_data3 should return. |
| 8876 | rv = callback.WaitForResult(); |
| 8877 | ASSERT_EQ(net::ERR_PROXY_CONNECTION_FAILED, rv); |
| 8878 | |
| 8879 | // Now that the new handshake has failed, ensure that the client |
| 8880 | // certificate was removed from the client auth cache. |
| 8881 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("proxy:70", |
| 8882 | &client_cert)); |
| 8883 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8884 | &client_cert)); |
| 8885 | } |
| 8886 | } |
| 8887 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 8888 | void IPPoolingAddAlias(MockCachingHostResolver* host_resolver, |
| 8889 | SpdySessionPoolPeer* pool_peer, |
| 8890 | std::string host, |
| 8891 | int port, |
| 8892 | std::string iplist) { |
| 8893 | // Create a host resolver dependency that returns address |iplist| for |
| 8894 | // resolutions of |host|. |
| 8895 | host_resolver->rules()->AddIPLiteralRule(host, iplist, ""); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 8896 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 8897 | // Setup a HostPortProxyPair. |
| 8898 | HostPortPair host_port_pair(host, port); |
| 8899 | HostPortProxyPair pair = HostPortProxyPair(host_port_pair, |
| 8900 | ProxyServer::Direct()); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 8901 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 8902 | // Resolve the host and port. |
| 8903 | AddressList addresses; |
| 8904 | HostResolver::RequestInfo info(host_port_pair); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 8905 | TestCompletionCallback callback; |
| 8906 | int rv = host_resolver->Resolve(info, &addresses, callback.callback(), NULL, |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 8907 | BoundNetLog()); |
| 8908 | if (rv == ERR_IO_PENDING) |
| 8909 | rv = callback.WaitForResult(); |
| 8910 | DCHECK_EQ(OK, rv); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 8911 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 8912 | // Add the first address as an alias. It would have been better to call |
| 8913 | // MockClientSocket::GetPeerAddress but that returns 192.0.2.33 whereas |
| 8914 | // MockHostResolver returns 127.0.0.1 (MockHostResolverBase::Reset). So we use |
| 8915 | // the first address (127.0.0.1) returned by MockHostResolver as an alias for |
| 8916 | // the |pair|. |
| 8917 | const addrinfo* address = addresses.head(); |
| 8918 | pool_peer->AddAlias(address, pair); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 8919 | } |
| 8920 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8921 | TEST_F(HttpNetworkTransactionTest, UseIPConnectionPooling) { |
| 8922 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8923 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8924 | |
| 8925 | // Set up a special HttpNetworkSession with a MockCachingHostResolver. |
| 8926 | SessionDependencies session_deps; |
| 8927 | MockCachingHostResolver host_resolver; |
| 8928 | net::HttpNetworkSession::Params params; |
| 8929 | params.client_socket_factory = &session_deps.socket_factory; |
| 8930 | params.host_resolver = &host_resolver; |
| 8931 | params.cert_verifier = session_deps.cert_verifier.get(); |
| 8932 | params.proxy_service = session_deps.proxy_service.get(); |
| 8933 | params.ssl_config_service = session_deps.ssl_config_service; |
| 8934 | params.http_auth_handler_factory = |
| 8935 | session_deps.http_auth_handler_factory.get(); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 8936 | params.http_server_properties = &session_deps.http_server_properties; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8937 | params.net_log = session_deps.net_log; |
| 8938 | scoped_refptr<HttpNetworkSession> session(new HttpNetworkSession(params)); |
[email protected] | b9ec688 | 2011-07-01 07:40:26 | [diff] [blame] | 8939 | SpdySessionPoolPeer pool_peer(session->spdy_session_pool()); |
| 8940 | pool_peer.DisableDomainAuthenticationVerification(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8941 | |
| 8942 | SSLSocketDataProvider ssl(true, OK); |
| 8943 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 8944 | ssl.next_proto = "spdy/2"; |
| 8945 | ssl.was_npn_negotiated = true; |
| 8946 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8947 | |
| 8948 | scoped_ptr<spdy::SpdyFrame> host1_req(ConstructSpdyGet( |
| 8949 | "https://www.google.com", false, 1, LOWEST)); |
| 8950 | scoped_ptr<spdy::SpdyFrame> host2_req(ConstructSpdyGet( |
| 8951 | "https://www.gmail.com", false, 3, LOWEST)); |
| 8952 | MockWrite spdy_writes[] = { |
| 8953 | CreateMockWrite(*host1_req, 1), |
| 8954 | CreateMockWrite(*host2_req, 4), |
| 8955 | }; |
| 8956 | scoped_ptr<spdy::SpdyFrame> host1_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 8957 | scoped_ptr<spdy::SpdyFrame> host1_resp_body(ConstructSpdyBodyFrame(1, true)); |
| 8958 | scoped_ptr<spdy::SpdyFrame> host2_resp(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 8959 | scoped_ptr<spdy::SpdyFrame> host2_resp_body(ConstructSpdyBodyFrame(3, true)); |
| 8960 | MockRead spdy_reads[] = { |
| 8961 | CreateMockRead(*host1_resp, 2), |
| 8962 | CreateMockRead(*host1_resp_body, 3), |
| 8963 | CreateMockRead(*host2_resp, 5), |
| 8964 | CreateMockRead(*host2_resp_body, 6), |
| 8965 | MockRead(true, 0, 7), |
| 8966 | }; |
| 8967 | |
| 8968 | scoped_refptr<OrderedSocketData> spdy_data( |
| 8969 | new OrderedSocketData( |
| 8970 | spdy_reads, arraysize(spdy_reads), |
| 8971 | spdy_writes, arraysize(spdy_writes))); |
| 8972 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 8973 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 8974 | TestCompletionCallback callback; |
| 8975 | TestOldCompletionCallback old_callback; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8976 | HttpRequestInfo request1; |
| 8977 | request1.method = "GET"; |
| 8978 | request1.url = GURL("https://www.google.com/"); |
| 8979 | request1.load_flags = 0; |
| 8980 | HttpNetworkTransaction trans1(session); |
| 8981 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 8982 | int rv = trans1.Start(&request1, &old_callback, BoundNetLog()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8983 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 8984 | EXPECT_EQ(OK, old_callback.WaitForResult()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8985 | |
| 8986 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 8987 | ASSERT_TRUE(response != NULL); |
| 8988 | ASSERT_TRUE(response->headers != NULL); |
| 8989 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8990 | |
| 8991 | std::string response_data; |
| 8992 | ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); |
| 8993 | EXPECT_EQ("hello!", response_data); |
| 8994 | |
| 8995 | // Preload www.gmail.com into HostCache. |
| 8996 | HostPortPair host_port("www.gmail.com", 443); |
| 8997 | HostResolver::RequestInfo resolve_info(host_port); |
| 8998 | AddressList ignored; |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 8999 | rv = host_resolver.Resolve(resolve_info, &ignored, callback.callback(), NULL, |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 9000 | BoundNetLog()); |
| 9001 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 9002 | rv = callback.WaitForResult(); |
| 9003 | EXPECT_EQ(OK, rv); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9004 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 9005 | // MockHostResolver returns 127.0.0.1, port 443 for https://www.google.com/ |
| 9006 | // and https://www.gmail.com/. Add 127.0.0.1 as alias for host_port_pair: |
| 9007 | // (www.google.com, 443). |
| 9008 | IPPoolingAddAlias(&host_resolver, &pool_peer, "www.google.com", 443, |
| 9009 | "127.0.0.1"); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 9010 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9011 | HttpRequestInfo request2; |
| 9012 | request2.method = "GET"; |
| 9013 | request2.url = GURL("https://www.gmail.com/"); |
| 9014 | request2.load_flags = 0; |
| 9015 | HttpNetworkTransaction trans2(session); |
| 9016 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 9017 | rv = trans2.Start(&request2, &old_callback, BoundNetLog()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9018 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 9019 | EXPECT_EQ(OK, old_callback.WaitForResult()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9020 | |
| 9021 | response = trans2.GetResponseInfo(); |
| 9022 | ASSERT_TRUE(response != NULL); |
| 9023 | ASSERT_TRUE(response->headers != NULL); |
| 9024 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9025 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 9026 | EXPECT_TRUE(response->was_npn_negotiated); |
| 9027 | ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); |
| 9028 | EXPECT_EQ("hello!", response_data); |
| 9029 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 9030 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9031 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 9032 | } |
| 9033 | |
| 9034 | class OneTimeCachingHostResolver : public net::HostResolver { |
| 9035 | public: |
| 9036 | explicit OneTimeCachingHostResolver(const HostPortPair& host_port) |
| 9037 | : host_port_(host_port) {} |
| 9038 | virtual ~OneTimeCachingHostResolver() {} |
| 9039 | |
| 9040 | RuleBasedHostResolverProc* rules() { return host_resolver_.rules(); } |
| 9041 | |
| 9042 | // HostResolver methods: |
| 9043 | virtual int Resolve(const RequestInfo& info, |
| 9044 | AddressList* addresses, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 9045 | const CompletionCallback& callback, |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9046 | RequestHandle* out_req, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 9047 | const BoundNetLog& net_log) OVERRIDE { |
| 9048 | return host_resolver_.Resolve( |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9049 | info, addresses, callback, out_req, net_log); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 9050 | } |
| 9051 | |
| 9052 | virtual int ResolveFromCache(const RequestInfo& info, |
| 9053 | AddressList* addresses, |
| 9054 | const BoundNetLog& net_log) OVERRIDE { |
| 9055 | int rv = host_resolver_.ResolveFromCache(info, addresses, net_log); |
| 9056 | if (rv == OK && info.host_port_pair().Equals(host_port_)) |
[email protected] | 98e1cd01 | 2011-11-08 15:33:09 | [diff] [blame] | 9057 | host_resolver_.GetHostCache()->clear(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9058 | return rv; |
| 9059 | } |
| 9060 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 9061 | virtual void CancelRequest(RequestHandle req) OVERRIDE { |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9062 | host_resolver_.CancelRequest(req); |
| 9063 | } |
| 9064 | |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 9065 | MockCachingHostResolver* GetMockHostResolver() { |
| 9066 | return &host_resolver_; |
| 9067 | } |
| 9068 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9069 | private: |
| 9070 | MockCachingHostResolver host_resolver_; |
| 9071 | const HostPortPair host_port_; |
| 9072 | }; |
| 9073 | |
| 9074 | TEST_F(HttpNetworkTransactionTest, |
| 9075 | UseIPConnectionPoolingWithHostCacheExpiration) { |
| 9076 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 9077 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9078 | |
| 9079 | // Set up a special HttpNetworkSession with a OneTimeCachingHostResolver. |
| 9080 | SessionDependencies session_deps; |
| 9081 | OneTimeCachingHostResolver host_resolver(HostPortPair("www.gmail.com", 443)); |
| 9082 | net::HttpNetworkSession::Params params; |
| 9083 | params.client_socket_factory = &session_deps.socket_factory; |
| 9084 | params.host_resolver = &host_resolver; |
| 9085 | params.cert_verifier = session_deps.cert_verifier.get(); |
| 9086 | params.proxy_service = session_deps.proxy_service.get(); |
| 9087 | params.ssl_config_service = session_deps.ssl_config_service; |
| 9088 | params.http_auth_handler_factory = |
| 9089 | session_deps.http_auth_handler_factory.get(); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 9090 | params.http_server_properties = &session_deps.http_server_properties; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9091 | params.net_log = session_deps.net_log; |
| 9092 | scoped_refptr<HttpNetworkSession> session(new HttpNetworkSession(params)); |
[email protected] | b9ec688 | 2011-07-01 07:40:26 | [diff] [blame] | 9093 | SpdySessionPoolPeer pool_peer(session->spdy_session_pool()); |
| 9094 | pool_peer.DisableDomainAuthenticationVerification(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9095 | |
| 9096 | SSLSocketDataProvider ssl(true, OK); |
| 9097 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 9098 | ssl.next_proto = "spdy/2"; |
| 9099 | ssl.was_npn_negotiated = true; |
| 9100 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 9101 | |
| 9102 | scoped_ptr<spdy::SpdyFrame> host1_req(ConstructSpdyGet( |
| 9103 | "https://www.google.com", false, 1, LOWEST)); |
| 9104 | scoped_ptr<spdy::SpdyFrame> host2_req(ConstructSpdyGet( |
| 9105 | "https://www.gmail.com", false, 3, LOWEST)); |
| 9106 | MockWrite spdy_writes[] = { |
| 9107 | CreateMockWrite(*host1_req, 1), |
| 9108 | CreateMockWrite(*host2_req, 4), |
| 9109 | }; |
| 9110 | scoped_ptr<spdy::SpdyFrame> host1_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9111 | scoped_ptr<spdy::SpdyFrame> host1_resp_body(ConstructSpdyBodyFrame(1, true)); |
| 9112 | scoped_ptr<spdy::SpdyFrame> host2_resp(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 9113 | scoped_ptr<spdy::SpdyFrame> host2_resp_body(ConstructSpdyBodyFrame(3, true)); |
| 9114 | MockRead spdy_reads[] = { |
| 9115 | CreateMockRead(*host1_resp, 2), |
| 9116 | CreateMockRead(*host1_resp_body, 3), |
| 9117 | CreateMockRead(*host2_resp, 5), |
| 9118 | CreateMockRead(*host2_resp_body, 6), |
| 9119 | MockRead(true, 0, 7), |
| 9120 | }; |
| 9121 | |
| 9122 | scoped_refptr<OrderedSocketData> spdy_data( |
| 9123 | new OrderedSocketData( |
| 9124 | spdy_reads, arraysize(spdy_reads), |
| 9125 | spdy_writes, arraysize(spdy_writes))); |
| 9126 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 9127 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 9128 | TestCompletionCallback callback; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9129 | HttpRequestInfo request1; |
| 9130 | request1.method = "GET"; |
| 9131 | request1.url = GURL("https://www.google.com/"); |
| 9132 | request1.load_flags = 0; |
| 9133 | HttpNetworkTransaction trans1(session); |
| 9134 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 9135 | TestOldCompletionCallback old_callback; |
| 9136 | int rv = trans1.Start(&request1, &old_callback, BoundNetLog()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9137 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 9138 | EXPECT_EQ(OK, old_callback.WaitForResult()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9139 | |
| 9140 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 9141 | ASSERT_TRUE(response != NULL); |
| 9142 | ASSERT_TRUE(response->headers != NULL); |
| 9143 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9144 | |
| 9145 | std::string response_data; |
| 9146 | ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); |
| 9147 | EXPECT_EQ("hello!", response_data); |
| 9148 | |
| 9149 | // Preload cache entries into HostCache. |
| 9150 | HostResolver::RequestInfo resolve_info(HostPortPair("www.gmail.com", 443)); |
| 9151 | AddressList ignored; |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 9152 | rv = host_resolver.Resolve(resolve_info, &ignored, callback.callback(), NULL, |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 9153 | BoundNetLog()); |
| 9154 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 9155 | rv = callback.WaitForResult(); |
| 9156 | EXPECT_EQ(OK, rv); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9157 | |
| 9158 | HttpRequestInfo request2; |
| 9159 | request2.method = "GET"; |
| 9160 | request2.url = GURL("https://www.gmail.com/"); |
| 9161 | request2.load_flags = 0; |
| 9162 | HttpNetworkTransaction trans2(session); |
| 9163 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 9164 | // MockHostResolver returns 127.0.0.1, port 443 for https://www.google.com/ |
| 9165 | // and https://www.gmail.com/. Add 127.0.0.1 as alias for host_port_pair: |
| 9166 | // (www.google.com, 443). |
| 9167 | IPPoolingAddAlias(host_resolver.GetMockHostResolver(), &pool_peer, |
| 9168 | "www.google.com", 443, "127.0.0.1"); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 9169 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 9170 | rv = trans2.Start(&request2, &old_callback, BoundNetLog()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9171 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame^] | 9172 | EXPECT_EQ(OK, old_callback.WaitForResult()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9173 | |
| 9174 | response = trans2.GetResponseInfo(); |
| 9175 | ASSERT_TRUE(response != NULL); |
| 9176 | ASSERT_TRUE(response->headers != NULL); |
| 9177 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9178 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 9179 | EXPECT_TRUE(response->was_npn_negotiated); |
| 9180 | ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); |
| 9181 | EXPECT_EQ("hello!", response_data); |
| 9182 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 9183 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9184 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 9185 | } |
| 9186 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 9187 | TEST_F(HttpNetworkTransactionTest, ReadPipelineEvictionFallback) { |
| 9188 | MockRead data_reads1[] = { |
| 9189 | MockRead(false, ERR_PIPELINE_EVICTION), |
| 9190 | }; |
| 9191 | MockRead data_reads2[] = { |
| 9192 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 9193 | MockRead("hello world"), |
| 9194 | MockRead(false, OK), |
| 9195 | }; |
| 9196 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), NULL, 0); |
| 9197 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), NULL, 0); |
| 9198 | StaticSocketDataProvider* data[] = { &data1, &data2 }; |
| 9199 | |
| 9200 | SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); |
| 9201 | |
| 9202 | EXPECT_EQ(OK, out.rv); |
| 9203 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 9204 | EXPECT_EQ("hello world", out.response_data); |
| 9205 | } |
| 9206 | |
| 9207 | TEST_F(HttpNetworkTransactionTest, SendPipelineEvictionFallback) { |
| 9208 | MockWrite data_writes1[] = { |
| 9209 | MockWrite(false, ERR_PIPELINE_EVICTION), |
| 9210 | }; |
| 9211 | MockWrite data_writes2[] = { |
| 9212 | MockWrite("GET / HTTP/1.1\r\n" |
| 9213 | "Host: www.google.com\r\n" |
| 9214 | "Connection: keep-alive\r\n\r\n"), |
| 9215 | }; |
| 9216 | MockRead data_reads2[] = { |
| 9217 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 9218 | MockRead("hello world"), |
| 9219 | MockRead(false, OK), |
| 9220 | }; |
| 9221 | StaticSocketDataProvider data1(NULL, 0, |
| 9222 | data_writes1, arraysize(data_writes1)); |
| 9223 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 9224 | data_writes2, arraysize(data_writes2)); |
| 9225 | StaticSocketDataProvider* data[] = { &data1, &data2 }; |
| 9226 | |
| 9227 | SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); |
| 9228 | |
| 9229 | EXPECT_EQ(OK, out.rv); |
| 9230 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 9231 | EXPECT_EQ("hello world", out.response_data); |
| 9232 | } |
| 9233 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 9234 | } // namespace net |