[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 1 | // Copyright (c) 2010 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] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 8 | #include <vector> |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 9 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12 | #include "base/file_path.h" |
| 13 | #include "base/file_util.h" |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 14 | #include "base/scoped_ptr.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 16 | #include "net/base/auth.h" |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 17 | #include "net/base/capturing_net_log.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 18 | #include "net/base/completion_callback.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 19 | #include "net/base/mock_host_resolver.h" |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 20 | #include "net/base/net_log.h" |
| 21 | #include "net/base/net_log_unittest.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 22 | #include "net/base/request_priority.h" |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 23 | #include "net/base/ssl_cert_request_info.h" |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 24 | #include "net/base/ssl_config_service_defaults.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 25 | #include "net/base/ssl_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 26 | #include "net/base/test_completion_callback.h" |
| 27 | #include "net/base/upload_data.h" |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 28 | #include "net/http/http_auth_handler_digest.h" |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 29 | #include "net/http/http_auth_handler_mock.h" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 30 | #include "net/http/http_auth_handler_ntlm.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 31 | #include "net/http/http_basic_stream.h" |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 32 | #include "net/http/http_net_log_params.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | #include "net/http/http_network_session.h" |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 34 | #include "net/http/http_network_session_peer.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 35 | #include "net/http/http_stream.h" |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 36 | #include "net/http/http_stream_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | #include "net/http/http_transaction_unittest.h" |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 38 | #include "net/proxy/proxy_config_service_fixed.h" |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 39 | #include "net/proxy/proxy_resolver.h" |
| 40 | #include "net/proxy/proxy_service.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 41 | #include "net/socket/client_socket_factory.h" |
| 42 | #include "net/socket/socket_test_util.h" |
| 43 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 44 | #include "net/spdy/spdy_framer.h" |
| 45 | #include "net/spdy/spdy_session.h" |
| 46 | #include "net/spdy/spdy_session_pool.h" |
| 47 | #include "net/spdy/spdy_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 48 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 49 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | |
| 51 | //----------------------------------------------------------------------------- |
| 52 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 53 | namespace { |
| 54 | |
| 55 | const string16 kBar(ASCIIToUTF16("bar")); |
| 56 | const string16 kBar2(ASCIIToUTF16("bar2")); |
| 57 | const string16 kBar3(ASCIIToUTF16("bar3")); |
| 58 | const string16 kBaz(ASCIIToUTF16("baz")); |
| 59 | const string16 kFirst(ASCIIToUTF16("first")); |
| 60 | const string16 kFoo(ASCIIToUTF16("foo")); |
| 61 | const string16 kFoo2(ASCIIToUTF16("foo2")); |
| 62 | const string16 kFoo3(ASCIIToUTF16("foo3")); |
| 63 | const string16 kFou(ASCIIToUTF16("fou")); |
| 64 | const string16 kSecond(ASCIIToUTF16("second")); |
| 65 | const string16 kTestingNTLM(ASCIIToUTF16("testing-ntlm")); |
| 66 | const string16 kWrongPassword(ASCIIToUTF16("wrongpassword")); |
| 67 | |
| 68 | } // namespace |
| 69 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 70 | namespace net { |
| 71 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 72 | // Helper to manage the lifetimes of the dependencies for a |
| 73 | // HttpNetworkTransaction. |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 74 | struct SessionDependencies { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 75 | // Default set of dependencies -- "null" proxy service. |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 76 | SessionDependencies() |
| 77 | : host_resolver(new MockHostResolver), |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 78 | cert_verifier(new CertVerifier), |
[email protected] | ebeefff3 | 2010-09-15 05:10:02 | [diff] [blame] | 79 | proxy_service(ProxyService::CreateDirect()), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 80 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 81 | http_auth_handler_factory( |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 82 | HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 83 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 84 | |
| 85 | // Custom proxy service dependency. |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 86 | explicit SessionDependencies(ProxyService* proxy_service) |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 87 | : host_resolver(new MockHostResolver), |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 88 | cert_verifier(new CertVerifier), |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 89 | proxy_service(proxy_service), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 90 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 91 | http_auth_handler_factory( |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 92 | HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 93 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 94 | |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 95 | scoped_ptr<MockHostResolverBase> host_resolver; |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 96 | scoped_ptr<CertVerifier> cert_verifier; |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 97 | scoped_refptr<ProxyService> proxy_service; |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 98 | scoped_refptr<SSLConfigService> ssl_config_service; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 99 | MockClientSocketFactory socket_factory; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 100 | scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 101 | NetLog* net_log; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 102 | }; |
| 103 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 104 | HttpNetworkSession* CreateSession(SessionDependencies* session_deps) { |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 105 | return new HttpNetworkSession(session_deps->host_resolver.get(), |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 106 | session_deps->cert_verifier.get(), |
[email protected] | 2db58053 | 2010-10-08 14:32:37 | [diff] [blame] | 107 | NULL /* dnsrr_resolver */, |
[email protected] | 345c613b | 2010-11-22 19:33:18 | [diff] [blame] | 108 | NULL /* dns_cert_checker */, |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 109 | NULL /* ssl_host_info_factory */, |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 110 | session_deps->proxy_service, |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 111 | &session_deps->socket_factory, |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 112 | session_deps->ssl_config_service, |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 113 | new SpdySessionPool(NULL), |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 114 | session_deps->http_auth_handler_factory.get(), |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 115 | NULL, |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 116 | session_deps->net_log); |
[email protected] | e8d53619 | 2008-10-17 22:21:14 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 89836e2 | 2008-09-25 20:33:42 | [diff] [blame] | 119 | class HttpNetworkTransactionTest : public PlatformTest { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 120 | public: |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 121 | virtual void SetUp() { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 122 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 123 | MessageLoop::current()->RunAllPending(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 124 | spdy::SpdyFramer::set_enable_compression_default(false); |
| 125 | } |
| 126 | |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 127 | virtual void TearDown() { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 128 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 129 | MessageLoop::current()->RunAllPending(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 130 | spdy::SpdyFramer::set_enable_compression_default(true); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 131 | // Empty the current queue. |
| 132 | MessageLoop::current()->RunAllPending(); |
| 133 | PlatformTest::TearDown(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 134 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 135 | MessageLoop::current()->RunAllPending(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 138 | protected: |
| 139 | void KeepAliveConnectionResendRequestTest(const MockRead& read_failure); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 140 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 141 | struct SimpleGetHelperResult { |
| 142 | int rv; |
| 143 | std::string status_line; |
| 144 | std::string response_data; |
| 145 | }; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 146 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 147 | SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[], |
| 148 | size_t reads_count) { |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 149 | SimpleGetHelperResult out; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 150 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 151 | HttpRequestInfo request; |
| 152 | request.method = "GET"; |
| 153 | request.url = GURL("http://www.google.com/"); |
| 154 | request.load_flags = 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 155 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 156 | SessionDependencies session_deps; |
| 157 | scoped_ptr<HttpTransaction> trans( |
| 158 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 159 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 160 | StaticSocketDataProvider data(data_reads, reads_count, NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 161 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 162 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 163 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 164 | |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 165 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 166 | EXPECT_TRUE(log.bound().IsLoggingAllEvents()); |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 167 | int rv = trans->Start(&request, &callback, log.bound()); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 168 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 169 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 170 | out.rv = callback.WaitForResult(); |
| 171 | if (out.rv != OK) |
| 172 | return out; |
| 173 | |
| 174 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 175 | EXPECT_TRUE(response != NULL); |
| 176 | |
| 177 | EXPECT_TRUE(response->headers != NULL); |
| 178 | out.status_line = response->headers->GetStatusLine(); |
| 179 | |
| 180 | rv = ReadTransaction(trans.get(), &out.response_data); |
| 181 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 182 | |
| 183 | net::CapturingNetLog::EntryList entries; |
| 184 | log.GetEntries(&entries); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 185 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 186 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 187 | NetLog::PHASE_NONE); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 188 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 189 | entries, pos, |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 190 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 191 | NetLog::PHASE_NONE); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 192 | |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 193 | CapturingNetLog::Entry entry = entries[pos]; |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 194 | NetLogHttpRequestParameter* request_params = |
| 195 | static_cast<NetLogHttpRequestParameter*>(entry.extra_parameters.get()); |
| 196 | EXPECT_EQ("GET / HTTP/1.1\r\n", request_params->GetLine()); |
| 197 | EXPECT_EQ("Host: www.google.com\r\n" |
| 198 | "Connection: keep-alive\r\n\r\n", |
| 199 | request_params->GetHeaders().ToString()); |
| 200 | |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 201 | return out; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 202 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 203 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 204 | void ConnectStatusHelperWithExpectedStatus(const MockRead& status, |
| 205 | int expected_status); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 206 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 207 | void ConnectStatusHelper(const MockRead& status); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 208 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 209 | |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 210 | // Fill |str| with a long header list that consumes >= |size| bytes. |
| 211 | void FillLargeHeadersString(std::string* str, int size) { |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 212 | const char* row = |
| 213 | "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; |
| 214 | const int sizeof_row = strlen(row); |
| 215 | const int num_rows = static_cast<int>( |
| 216 | ceil(static_cast<float>(size) / sizeof_row)); |
| 217 | const int sizeof_data = num_rows * sizeof_row; |
| 218 | DCHECK(sizeof_data >= size); |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 219 | str->reserve(sizeof_data); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 220 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 221 | for (int i = 0; i < num_rows; ++i) |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 222 | str->append(row, sizeof_row); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 225 | // Alternative functions that eliminate randomness and dependency on the local |
| 226 | // host name so that the generated NTLM messages are reproducible. |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 227 | void MockGenerateRandom1(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 228 | static const uint8 bytes[] = { |
| 229 | 0x55, 0x29, 0x66, 0x26, 0x6b, 0x9c, 0x73, 0x54 |
| 230 | }; |
| 231 | static size_t current_byte = 0; |
| 232 | for (size_t i = 0; i < n; ++i) { |
| 233 | output[i] = bytes[current_byte++]; |
| 234 | current_byte %= arraysize(bytes); |
| 235 | } |
| 236 | } |
| 237 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 238 | void MockGenerateRandom2(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 239 | static const uint8 bytes[] = { |
| 240 | 0x96, 0x79, 0x85, 0xe7, 0x49, 0x93, 0x70, 0xa1, |
| 241 | 0x4e, 0xe7, 0x87, 0x45, 0x31, 0x5b, 0xd3, 0x1f |
| 242 | }; |
| 243 | static size_t current_byte = 0; |
| 244 | for (size_t i = 0; i < n; ++i) { |
| 245 | output[i] = bytes[current_byte++]; |
| 246 | current_byte %= arraysize(bytes); |
| 247 | } |
| 248 | } |
| 249 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 250 | std::string MockGetHostName() { |
| 251 | return "WTC-WIN7"; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 254 | template<typename ParentPool> |
| 255 | class CaptureGroupNameSocketPool : public ParentPool { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 256 | public: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 257 | explicit CaptureGroupNameSocketPool(HttpNetworkSession* session); |
| 258 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 259 | const std::string last_group_name_received() const { |
| 260 | return last_group_name_; |
| 261 | } |
| 262 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 263 | virtual int RequestSocket(const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 264 | const void* socket_params, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 265 | RequestPriority priority, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 266 | ClientSocketHandle* handle, |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 267 | CompletionCallback* callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 268 | const BoundNetLog& net_log) { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 269 | last_group_name_ = group_name; |
| 270 | return ERR_IO_PENDING; |
| 271 | } |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 272 | virtual void CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 273 | ClientSocketHandle* handle) {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 274 | virtual void ReleaseSocket(const std::string& group_name, |
| 275 | ClientSocket* socket) {} |
| 276 | virtual void CloseIdleSockets() {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 277 | virtual int IdleSocketCount() const { |
| 278 | return 0; |
| 279 | } |
| 280 | virtual int IdleSocketCountInGroup(const std::string& group_name) const { |
| 281 | return 0; |
| 282 | } |
| 283 | virtual LoadState GetLoadState(const std::string& group_name, |
| 284 | const ClientSocketHandle* handle) const { |
| 285 | return LOAD_STATE_IDLE; |
| 286 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 287 | virtual base::TimeDelta ConnectionTimeout() const { |
| 288 | return base::TimeDelta(); |
| 289 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 290 | |
| 291 | private: |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 292 | std::string last_group_name_; |
| 293 | }; |
| 294 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 295 | typedef CaptureGroupNameSocketPool<TCPClientSocketPool> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 296 | CaptureGroupNameTCPSocketPool; |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 297 | typedef CaptureGroupNameSocketPool<HttpProxyClientSocketPool> |
| 298 | CaptureGroupNameHttpProxySocketPool; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 299 | typedef CaptureGroupNameSocketPool<SOCKSClientSocketPool> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 300 | CaptureGroupNameSOCKSSocketPool; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 301 | typedef CaptureGroupNameSocketPool<SSLClientSocketPool> |
| 302 | CaptureGroupNameSSLSocketPool; |
| 303 | |
| 304 | template<typename ParentPool> |
| 305 | CaptureGroupNameSocketPool<ParentPool>::CaptureGroupNameSocketPool( |
| 306 | HttpNetworkSession* session) |
| 307 | : ParentPool(0, 0, NULL, session->host_resolver(), NULL, NULL) {} |
| 308 | |
| 309 | template<> |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 310 | CaptureGroupNameHttpProxySocketPool::CaptureGroupNameSocketPool( |
| 311 | HttpNetworkSession* session) |
| 312 | : HttpProxyClientSocketPool(0, 0, NULL, session->host_resolver(), NULL, |
| 313 | NULL, NULL) {} |
| 314 | |
| 315 | template<> |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 316 | CaptureGroupNameSSLSocketPool::CaptureGroupNameSocketPool( |
| 317 | HttpNetworkSession* session) |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 318 | : SSLClientSocketPool(0, 0, NULL, session->host_resolver(), |
| 319 | session->cert_verifier(), NULL, NULL, |
[email protected] | 345c613b | 2010-11-22 19:33:18 | [diff] [blame] | 320 | NULL, NULL, NULL, NULL, NULL, NULL, NULL) {} |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 321 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 322 | //----------------------------------------------------------------------------- |
| 323 | |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 324 | // This is the expected list of advertised protocols from the browser's NPN |
| 325 | // list. |
| 326 | static const char kExpectedNPNString[] = "\x08http/1.1\x06spdy/2"; |
| 327 | |
| 328 | // This is the expected return from a current server advertising SPDY. |
| 329 | static const char kAlternateProtocolHttpHeader[] = |
| 330 | "Alternate-Protocol: 443:npn-spdy/2\r\n\r\n"; |
| 331 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 332 | TEST_F(HttpNetworkTransactionTest, Basic) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 333 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 334 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 335 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | TEST_F(HttpNetworkTransactionTest, SimpleGET) { |
| 339 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 340 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 341 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 342 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 343 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 344 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 345 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 346 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 347 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 348 | EXPECT_EQ("hello world", out.response_data); |
| 349 | } |
| 350 | |
| 351 | // Response with no status line. |
| 352 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) { |
| 353 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 354 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 355 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 356 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 357 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 358 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 359 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 360 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 361 | EXPECT_EQ("hello world", out.response_data); |
| 362 | } |
| 363 | |
| 364 | // Allow up to 4 bytes of junk to precede status line. |
| 365 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk2Bytes) { |
| 366 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 367 | MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 368 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 369 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 370 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 371 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 372 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 373 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 374 | EXPECT_EQ("DATA", out.response_data); |
| 375 | } |
| 376 | |
| 377 | // Allow up to 4 bytes of junk to precede status line. |
| 378 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) { |
| 379 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 380 | MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 381 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 382 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 383 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 384 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 385 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 386 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 387 | EXPECT_EQ("DATA", out.response_data); |
| 388 | } |
| 389 | |
| 390 | // Beyond 4 bytes of slop and it should fail to find a status line. |
| 391 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) { |
| 392 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 393 | MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 394 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 395 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 396 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 397 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 398 | EXPECT_EQ(OK, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 399 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 400 | EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // Same as StatusLineJunk4Bytes, except the read chunks are smaller. |
| 404 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) { |
| 405 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 406 | MockRead("\n"), |
| 407 | MockRead("\n"), |
| 408 | MockRead("Q"), |
| 409 | MockRead("J"), |
| 410 | MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 411 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 412 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 413 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 414 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 415 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 416 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 417 | EXPECT_EQ("DATA", out.response_data); |
| 418 | } |
| 419 | |
| 420 | // Close the connection before enough bytes to have a status line. |
| 421 | TEST_F(HttpNetworkTransactionTest, StatusLinePartial) { |
| 422 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 423 | MockRead("HTT"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 424 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 425 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 426 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 427 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 428 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 429 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 430 | EXPECT_EQ("HTT", out.response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 431 | } |
| 432 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 433 | // Simulate a 204 response, lacking a Content-Length header, sent over a |
| 434 | // persistent connection. The response should still terminate since a 204 |
| 435 | // cannot have a response body. |
| 436 | TEST_F(HttpNetworkTransactionTest, StopsReading204) { |
| 437 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 438 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 439 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 440 | MockRead(false, OK), |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 441 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 442 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 443 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 444 | EXPECT_EQ(OK, out.rv); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 445 | EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line); |
| 446 | EXPECT_EQ("", out.response_data); |
| 447 | } |
| 448 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 449 | // A simple request using chunked encoding with some extra data after. |
| 450 | // (Like might be seen in a pipelined response.) |
| 451 | TEST_F(HttpNetworkTransactionTest, ChunkedEncoding) { |
| 452 | MockRead data_reads[] = { |
| 453 | MockRead("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"), |
| 454 | MockRead("5\r\nHello\r\n"), |
| 455 | MockRead("1\r\n"), |
| 456 | MockRead(" \r\n"), |
| 457 | MockRead("5\r\nworld\r\n"), |
| 458 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
| 459 | MockRead(false, OK), |
| 460 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 461 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 462 | arraysize(data_reads)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 463 | EXPECT_EQ(OK, out.rv); |
| 464 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 465 | EXPECT_EQ("Hello world", out.response_data); |
| 466 | } |
| 467 | |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 468 | // Next tests deal with http://crbug.com/56344. |
| 469 | |
| 470 | TEST_F(HttpNetworkTransactionTest, |
| 471 | MultipleContentLengthHeadersNoTransferEncoding) { |
| 472 | MockRead data_reads[] = { |
| 473 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 474 | MockRead("Content-Length: 10\r\n"), |
| 475 | MockRead("Content-Length: 5\r\n\r\n"), |
| 476 | }; |
| 477 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 478 | arraysize(data_reads)); |
| 479 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, out.rv); |
| 480 | } |
| 481 | |
| 482 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 483 | DuplicateContentLengthHeadersNoTransferEncoding) { |
| 484 | MockRead data_reads[] = { |
| 485 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 486 | MockRead("Content-Length: 5\r\n"), |
| 487 | MockRead("Content-Length: 5\r\n\r\n"), |
| 488 | MockRead("Hello"), |
| 489 | }; |
| 490 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 491 | arraysize(data_reads)); |
| 492 | EXPECT_EQ(OK, out.rv); |
| 493 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 494 | EXPECT_EQ("Hello", out.response_data); |
| 495 | } |
| 496 | |
| 497 | TEST_F(HttpNetworkTransactionTest, |
| 498 | ComplexContentLengthHeadersNoTransferEncoding) { |
| 499 | // More than 2 dupes. |
| 500 | { |
| 501 | MockRead data_reads[] = { |
| 502 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 503 | MockRead("Content-Length: 5\r\n"), |
| 504 | MockRead("Content-Length: 5\r\n"), |
| 505 | MockRead("Content-Length: 5\r\n\r\n"), |
| 506 | MockRead("Hello"), |
| 507 | }; |
| 508 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 509 | arraysize(data_reads)); |
| 510 | EXPECT_EQ(OK, out.rv); |
| 511 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 512 | EXPECT_EQ("Hello", out.response_data); |
| 513 | } |
| 514 | // HTTP/1.0 |
| 515 | { |
| 516 | MockRead data_reads[] = { |
| 517 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 518 | MockRead("Content-Length: 5\r\n"), |
| 519 | MockRead("Content-Length: 5\r\n"), |
| 520 | MockRead("Content-Length: 5\r\n\r\n"), |
| 521 | MockRead("Hello"), |
| 522 | }; |
| 523 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 524 | arraysize(data_reads)); |
| 525 | EXPECT_EQ(OK, out.rv); |
| 526 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 527 | EXPECT_EQ("Hello", out.response_data); |
| 528 | } |
| 529 | // 2 dupes and one mismatched. |
| 530 | { |
| 531 | MockRead data_reads[] = { |
| 532 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 533 | MockRead("Content-Length: 10\r\n"), |
| 534 | MockRead("Content-Length: 10\r\n"), |
| 535 | MockRead("Content-Length: 5\r\n\r\n"), |
| 536 | }; |
| 537 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 538 | arraysize(data_reads)); |
| 539 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, out.rv); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 544 | MultipleContentLengthHeadersTransferEncoding) { |
| 545 | MockRead data_reads[] = { |
| 546 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 547 | MockRead("Content-Length: 666\r\n"), |
| 548 | MockRead("Content-Length: 1337\r\n"), |
| 549 | MockRead("Transfer-Encoding: chunked\r\n\r\n"), |
| 550 | MockRead("5\r\nHello\r\n"), |
| 551 | MockRead("1\r\n"), |
| 552 | MockRead(" \r\n"), |
| 553 | MockRead("5\r\nworld\r\n"), |
| 554 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
| 555 | MockRead(false, OK), |
| 556 | }; |
| 557 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 558 | arraysize(data_reads)); |
| 559 | EXPECT_EQ(OK, out.rv); |
| 560 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 561 | EXPECT_EQ("Hello world", out.response_data); |
| 562 | } |
| 563 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 564 | // Do a request using the HEAD method. Verify that we don't try to read the |
| 565 | // message body (since HEAD has none). |
| 566 | TEST_F(HttpNetworkTransactionTest, Head) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 567 | HttpRequestInfo request; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 568 | request.method = "HEAD"; |
| 569 | request.url = GURL("http://www.google.com/"); |
| 570 | request.load_flags = 0; |
| 571 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 572 | SessionDependencies session_deps; |
| 573 | scoped_ptr<HttpTransaction> trans( |
| 574 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 575 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 576 | MockWrite data_writes1[] = { |
| 577 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 578 | "Host: www.google.com\r\n" |
| 579 | "Connection: keep-alive\r\n" |
| 580 | "Content-Length: 0\r\n\r\n"), |
| 581 | }; |
| 582 | MockRead data_reads1[] = { |
| 583 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 584 | MockRead("Server: Blah\r\n"), |
| 585 | MockRead("Content-Length: 1234\r\n\r\n"), |
| 586 | |
| 587 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 588 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 589 | }; |
| 590 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 591 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 592 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 593 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 594 | |
| 595 | TestCompletionCallback callback1; |
| 596 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 597 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 598 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 599 | |
| 600 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 601 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 602 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 603 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 604 | EXPECT_FALSE(response == NULL); |
| 605 | |
| 606 | // Check that the headers got parsed. |
| 607 | EXPECT_TRUE(response->headers != NULL); |
| 608 | EXPECT_EQ(1234, response->headers->GetContentLength()); |
| 609 | EXPECT_EQ("HTTP/1.1 404 Not Found", response->headers->GetStatusLine()); |
| 610 | |
| 611 | std::string server_header; |
| 612 | void* iter = NULL; |
| 613 | bool has_server_header = response->headers->EnumerateHeader( |
| 614 | &iter, "Server", &server_header); |
| 615 | EXPECT_TRUE(has_server_header); |
| 616 | EXPECT_EQ("Blah", server_header); |
| 617 | |
| 618 | // Reading should give EOF right away, since there is no message body |
| 619 | // (despite non-zero content-length). |
| 620 | std::string response_data; |
| 621 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 622 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 623 | EXPECT_EQ("", response_data); |
| 624 | } |
| 625 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 626 | TEST_F(HttpNetworkTransactionTest, ReuseConnection) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 627 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 628 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 629 | |
| 630 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 631 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 632 | MockRead("hello"), |
| 633 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 634 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 635 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 636 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 637 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 638 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 639 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 640 | const char* const kExpectedResponseData[] = { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 641 | "hello", "world" |
| 642 | }; |
| 643 | |
| 644 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 645 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 646 | request.method = "GET"; |
| 647 | request.url = GURL("http://www.google.com/"); |
| 648 | request.load_flags = 0; |
| 649 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 650 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 651 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 652 | TestCompletionCallback callback; |
| 653 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 654 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 655 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 656 | |
| 657 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 658 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 659 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 660 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 661 | EXPECT_TRUE(response != NULL); |
| 662 | |
| 663 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 664 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 665 | |
| 666 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 667 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 668 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 669 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | TEST_F(HttpNetworkTransactionTest, Ignores100) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 674 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 675 | request.method = "POST"; |
| 676 | request.url = GURL("http://www.foo.com/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 677 | request.upload_data = new UploadData; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 678 | request.upload_data->AppendBytes("foo", 3); |
| 679 | request.load_flags = 0; |
| 680 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 681 | SessionDependencies session_deps; |
| 682 | scoped_ptr<HttpTransaction> trans( |
| 683 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 684 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 685 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 686 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 687 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 688 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 689 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 690 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 691 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 692 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 693 | |
| 694 | TestCompletionCallback callback; |
| 695 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 696 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 697 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 698 | |
| 699 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 700 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 701 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 702 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 703 | EXPECT_TRUE(response != NULL); |
| 704 | |
| 705 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 706 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 707 | |
| 708 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 709 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 710 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 711 | EXPECT_EQ("hello world", response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 712 | } |
| 713 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 714 | // This test is almost the same as Ignores100 above, but the response contains |
| 715 | // 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] | 716 | // HTTP/1.1 and the two status headers are read in one read. |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 717 | TEST_F(HttpNetworkTransactionTest, Ignores1xx) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 718 | HttpRequestInfo request; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 719 | request.method = "GET"; |
| 720 | request.url = GURL("http://www.foo.com/"); |
| 721 | request.load_flags = 0; |
| 722 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 723 | SessionDependencies session_deps; |
| 724 | scoped_ptr<HttpTransaction> trans( |
| 725 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 726 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 727 | MockRead data_reads[] = { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 728 | MockRead("HTTP/1.1 102 Unspecified status code\r\n\r\n" |
| 729 | "HTTP/1.1 200 OK\r\n\r\n"), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 730 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 731 | MockRead(false, OK), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 732 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 733 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 734 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 735 | |
| 736 | TestCompletionCallback callback; |
| 737 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 738 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 739 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 740 | |
| 741 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 742 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 743 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 744 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 745 | EXPECT_TRUE(response != NULL); |
| 746 | |
| 747 | EXPECT_TRUE(response->headers != NULL); |
| 748 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 749 | |
| 750 | std::string response_data; |
| 751 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 752 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 753 | EXPECT_EQ("hello world", response_data); |
| 754 | } |
| 755 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 756 | TEST_F(HttpNetworkTransactionTest, Incomplete100ThenEOF) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 757 | HttpRequestInfo request; |
| 758 | request.method = "POST"; |
| 759 | request.url = GURL("http://www.foo.com/"); |
| 760 | request.load_flags = 0; |
| 761 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 762 | SessionDependencies session_deps; |
| 763 | scoped_ptr<HttpTransaction> trans( |
| 764 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 765 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 766 | MockRead data_reads[] = { |
| 767 | MockRead(false, "HTTP/1.0 100 Continue\r\n"), |
| 768 | MockRead(true, 0), |
| 769 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 770 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 771 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 772 | |
| 773 | TestCompletionCallback callback; |
| 774 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 775 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 776 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 777 | |
| 778 | rv = callback.WaitForResult(); |
| 779 | EXPECT_EQ(OK, rv); |
| 780 | |
| 781 | std::string response_data; |
| 782 | rv = ReadTransaction(trans.get(), &response_data); |
| 783 | EXPECT_EQ(OK, rv); |
| 784 | EXPECT_EQ("", response_data); |
| 785 | } |
| 786 | |
| 787 | TEST_F(HttpNetworkTransactionTest, EmptyResponse) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 788 | HttpRequestInfo request; |
| 789 | request.method = "POST"; |
| 790 | request.url = GURL("http://www.foo.com/"); |
| 791 | request.load_flags = 0; |
| 792 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 793 | SessionDependencies session_deps; |
| 794 | scoped_ptr<HttpTransaction> trans( |
| 795 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 796 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 797 | MockRead data_reads[] = { |
| 798 | MockRead(true, 0), |
| 799 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 800 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 801 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 802 | |
| 803 | TestCompletionCallback callback; |
| 804 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 805 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 806 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 807 | |
| 808 | rv = callback.WaitForResult(); |
| 809 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
| 810 | } |
| 811 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 812 | // read_failure specifies a read failure that should cause the network |
| 813 | // transaction to resend the request. |
| 814 | void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( |
| 815 | const MockRead& read_failure) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 816 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 817 | request.method = "GET"; |
| 818 | request.url = GURL("http://www.foo.com/"); |
| 819 | request.load_flags = 0; |
| 820 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 821 | SessionDependencies session_deps; |
| 822 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 823 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 824 | MockRead data1_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 825 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 826 | MockRead("hello"), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 827 | read_failure, // Now, we reuse the connection and fail the first read. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 828 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 829 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 830 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 831 | |
| 832 | MockRead data2_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 833 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 834 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 835 | MockRead(true, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 836 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 837 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 838 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 839 | |
| 840 | const char* kExpectedResponseData[] = { |
| 841 | "hello", "world" |
| 842 | }; |
| 843 | |
| 844 | for (int i = 0; i < 2; ++i) { |
| 845 | TestCompletionCallback callback; |
| 846 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 847 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 848 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 849 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 850 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 851 | |
| 852 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 853 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 854 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 855 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 856 | EXPECT_TRUE(response != NULL); |
| 857 | |
| 858 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 859 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 860 | |
| 861 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 862 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 863 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 864 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 865 | } |
| 866 | } |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 867 | |
| 868 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 869 | MockRead read_failure(true, ERR_CONNECTION_RESET); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 870 | KeepAliveConnectionResendRequestTest(read_failure); |
| 871 | } |
| 872 | |
| 873 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 874 | MockRead read_failure(false, OK); // EOF |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 875 | KeepAliveConnectionResendRequestTest(read_failure); |
| 876 | } |
| 877 | |
| 878 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 879 | HttpRequestInfo request; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 880 | request.method = "GET"; |
| 881 | request.url = GURL("http://www.google.com/"); |
| 882 | request.load_flags = 0; |
| 883 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 884 | SessionDependencies session_deps; |
| 885 | scoped_ptr<HttpTransaction> trans( |
| 886 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 887 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 888 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 889 | MockRead(true, ERR_CONNECTION_RESET), |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 890 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 891 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 892 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 893 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 894 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 895 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 896 | |
| 897 | TestCompletionCallback callback; |
| 898 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 899 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 900 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 901 | |
| 902 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 903 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 904 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 905 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 906 | EXPECT_TRUE(response == NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | // What do various browsers do when the server closes a non-keepalive |
| 910 | // connection without sending any response header or body? |
| 911 | // |
| 912 | // IE7: error page |
| 913 | // Safari 3.1.2 (Windows): error page |
| 914 | // Firefox 3.0.1: blank page |
| 915 | // Opera 9.52: after five attempts, blank page |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 916 | // Us with WinHTTP: error page (ERR_INVALID_RESPONSE) |
| 917 | // Us: error page (EMPTY_RESPONSE) |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 918 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) { |
| 919 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 920 | MockRead(false, OK), // EOF |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 921 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 922 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 923 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 924 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 925 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 926 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 927 | EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 928 | } |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 929 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 930 | // Test that we correctly reuse a keep-alive connection after not explicitly |
| 931 | // reading the body. |
| 932 | TEST_F(HttpNetworkTransactionTest, KeepAliveAfterUnreadBody) { |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 933 | HttpRequestInfo request; |
| 934 | request.method = "GET"; |
| 935 | request.url = GURL("http://www.foo.com/"); |
| 936 | request.load_flags = 0; |
| 937 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 938 | SessionDependencies session_deps; |
| 939 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 940 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 941 | // Note that because all these reads happen in the same |
| 942 | // StaticSocketDataProvider, it shows that the same socket is being reused for |
| 943 | // all transactions. |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 944 | MockRead data1_reads[] = { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 945 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 946 | MockRead("HTTP/1.1 205 Reset Content\r\n\r\n"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 947 | MockRead("HTTP/1.1 304 Not Modified\r\n\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 948 | MockRead("HTTP/1.1 302 Found\r\n" |
| 949 | "Content-Length: 0\r\n\r\n"), |
| 950 | MockRead("HTTP/1.1 302 Found\r\n" |
| 951 | "Content-Length: 5\r\n\r\n" |
| 952 | "hello"), |
| 953 | MockRead("HTTP/1.1 301 Moved Permanently\r\n" |
| 954 | "Content-Length: 0\r\n\r\n"), |
| 955 | MockRead("HTTP/1.1 301 Moved Permanently\r\n" |
| 956 | "Content-Length: 5\r\n\r\n" |
| 957 | "hello"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 958 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 959 | MockRead("hello"), |
| 960 | }; |
| 961 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); |
| 962 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 963 | |
| 964 | MockRead data2_reads[] = { |
| 965 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
| 966 | }; |
| 967 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
| 968 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 969 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 970 | const int kNumUnreadBodies = arraysize(data1_reads) - 2; |
| 971 | std::string response_lines[kNumUnreadBodies]; |
| 972 | |
| 973 | for (size_t i = 0; i < arraysize(data1_reads) - 2; ++i) { |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 974 | TestCompletionCallback callback; |
| 975 | |
| 976 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 977 | |
| 978 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 979 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 980 | |
| 981 | rv = callback.WaitForResult(); |
| 982 | EXPECT_EQ(OK, rv); |
| 983 | |
| 984 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 985 | ASSERT_TRUE(response != NULL); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 986 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 987 | ASSERT_TRUE(response->headers != NULL); |
| 988 | response_lines[i] = response->headers->GetStatusLine(); |
| 989 | |
| 990 | // We intentionally don't read the response bodies. |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 991 | } |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 992 | |
| 993 | const char* const kStatusLines[] = { |
| 994 | "HTTP/1.1 204 No Content", |
| 995 | "HTTP/1.1 205 Reset Content", |
| 996 | "HTTP/1.1 304 Not Modified", |
| 997 | "HTTP/1.1 302 Found", |
| 998 | "HTTP/1.1 302 Found", |
| 999 | "HTTP/1.1 301 Moved Permanently", |
| 1000 | "HTTP/1.1 301 Moved Permanently", |
| 1001 | }; |
| 1002 | |
| 1003 | COMPILE_ASSERT(kNumUnreadBodies == arraysize(kStatusLines), |
| 1004 | forgot_to_update_kStatusLines); |
| 1005 | |
| 1006 | for (int i = 0; i < kNumUnreadBodies; ++i) |
| 1007 | EXPECT_EQ(kStatusLines[i], response_lines[i]); |
| 1008 | |
| 1009 | TestCompletionCallback callback; |
| 1010 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1011 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 1012 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1013 | rv = callback.WaitForResult(); |
| 1014 | EXPECT_EQ(OK, rv); |
| 1015 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1016 | ASSERT_TRUE(response != NULL); |
| 1017 | ASSERT_TRUE(response->headers != NULL); |
| 1018 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1019 | std::string response_data; |
| 1020 | rv = ReadTransaction(trans.get(), &response_data); |
| 1021 | EXPECT_EQ(OK, rv); |
| 1022 | EXPECT_EQ("hello", response_data); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1023 | } |
| 1024 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1025 | // Test the request-challenge-retry sequence for basic auth. |
| 1026 | // (basic auth is the easiest to mock, because it has no randomness). |
| 1027 | TEST_F(HttpNetworkTransactionTest, BasicAuth) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1028 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1029 | request.method = "GET"; |
| 1030 | request.url = GURL("http://www.google.com/"); |
| 1031 | request.load_flags = 0; |
| 1032 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1033 | SessionDependencies session_deps; |
| 1034 | scoped_ptr<HttpTransaction> trans( |
| 1035 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1036 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1037 | MockWrite data_writes1[] = { |
| 1038 | MockWrite("GET / HTTP/1.1\r\n" |
| 1039 | "Host: www.google.com\r\n" |
| 1040 | "Connection: keep-alive\r\n\r\n"), |
| 1041 | }; |
| 1042 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1043 | MockRead data_reads1[] = { |
| 1044 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1045 | // Give a couple authenticate options (only the middle one is actually |
| 1046 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 1047 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1048 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1049 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 1050 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1051 | // Large content-length -- won't matter, as connection will be reset. |
| 1052 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1053 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1054 | }; |
| 1055 | |
| 1056 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1057 | // be issuing -- the final header line contains the credentials. |
| 1058 | MockWrite data_writes2[] = { |
| 1059 | MockWrite("GET / HTTP/1.1\r\n" |
| 1060 | "Host: www.google.com\r\n" |
| 1061 | "Connection: keep-alive\r\n" |
| 1062 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1063 | }; |
| 1064 | |
| 1065 | // Lastly, the server responds with the actual content. |
| 1066 | MockRead data_reads2[] = { |
| 1067 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 1068 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1069 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1070 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1071 | }; |
| 1072 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1073 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1074 | data_writes1, arraysize(data_writes1)); |
| 1075 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1076 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1077 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1078 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1079 | |
| 1080 | TestCompletionCallback callback1; |
| 1081 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1082 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1083 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1084 | |
| 1085 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1086 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1087 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1088 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1089 | EXPECT_FALSE(response == NULL); |
| 1090 | |
| 1091 | // The password prompt info should have been set in response->auth_challenge. |
| 1092 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1093 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1094 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1095 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1096 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1097 | |
| 1098 | TestCompletionCallback callback2; |
| 1099 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1100 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1101 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1102 | |
| 1103 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1104 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1105 | |
| 1106 | response = trans->GetResponseInfo(); |
| 1107 | EXPECT_FALSE(response == NULL); |
| 1108 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1109 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1110 | } |
| 1111 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1112 | TEST_F(HttpNetworkTransactionTest, DoNotSendAuth) { |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1113 | HttpRequestInfo request; |
| 1114 | request.method = "GET"; |
| 1115 | request.url = GURL("http://www.google.com/"); |
| 1116 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1117 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1118 | SessionDependencies session_deps; |
| 1119 | scoped_ptr<HttpTransaction> trans( |
| 1120 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1121 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1122 | MockWrite data_writes[] = { |
| 1123 | MockWrite("GET / HTTP/1.1\r\n" |
| 1124 | "Host: www.google.com\r\n" |
| 1125 | "Connection: keep-alive\r\n\r\n"), |
| 1126 | }; |
| 1127 | |
| 1128 | MockRead data_reads[] = { |
| 1129 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1130 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1131 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1132 | // Large content-length -- won't matter, as connection will be reset. |
| 1133 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1134 | MockRead(false, ERR_FAILED), |
| 1135 | }; |
| 1136 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1137 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1138 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1139 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1140 | TestCompletionCallback callback; |
| 1141 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1142 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1143 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1144 | |
| 1145 | rv = callback.WaitForResult(); |
| 1146 | EXPECT_EQ(0, rv); |
| 1147 | |
| 1148 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1149 | ASSERT_FALSE(response == NULL); |
| 1150 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1151 | } |
| 1152 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1153 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1154 | // connection. |
| 1155 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1156 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1157 | request.method = "GET"; |
| 1158 | request.url = GURL("http://www.google.com/"); |
| 1159 | request.load_flags = 0; |
| 1160 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1161 | SessionDependencies session_deps; |
| 1162 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1163 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1164 | MockWrite data_writes1[] = { |
| 1165 | MockWrite("GET / HTTP/1.1\r\n" |
| 1166 | "Host: www.google.com\r\n" |
| 1167 | "Connection: keep-alive\r\n\r\n"), |
| 1168 | |
| 1169 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1170 | // be issuing -- the final header line contains the credentials. |
| 1171 | MockWrite("GET / HTTP/1.1\r\n" |
| 1172 | "Host: www.google.com\r\n" |
| 1173 | "Connection: keep-alive\r\n" |
| 1174 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1175 | }; |
| 1176 | |
| 1177 | MockRead data_reads1[] = { |
| 1178 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1179 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1180 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1181 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1182 | MockRead("Unauthorized\r\n"), |
| 1183 | |
| 1184 | // Lastly, the server responds with the actual content. |
| 1185 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1186 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1187 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1188 | MockRead("Hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1189 | }; |
| 1190 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1191 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1192 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1193 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1194 | |
| 1195 | TestCompletionCallback callback1; |
| 1196 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1197 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1198 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1199 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1200 | |
| 1201 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1202 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1203 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1204 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1205 | EXPECT_FALSE(response == NULL); |
| 1206 | |
| 1207 | // The password prompt info should have been set in response->auth_challenge. |
| 1208 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1209 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1210 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1211 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1212 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1213 | |
| 1214 | TestCompletionCallback callback2; |
| 1215 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1216 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1217 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1218 | |
| 1219 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1220 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1221 | |
| 1222 | response = trans->GetResponseInfo(); |
| 1223 | EXPECT_FALSE(response == NULL); |
| 1224 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1225 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1229 | // connection and with no response body to drain. |
| 1230 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1231 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1232 | request.method = "GET"; |
| 1233 | request.url = GURL("http://www.google.com/"); |
| 1234 | request.load_flags = 0; |
| 1235 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1236 | SessionDependencies session_deps; |
| 1237 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1238 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1239 | MockWrite data_writes1[] = { |
| 1240 | MockWrite("GET / HTTP/1.1\r\n" |
| 1241 | "Host: www.google.com\r\n" |
| 1242 | "Connection: keep-alive\r\n\r\n"), |
| 1243 | |
| 1244 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1245 | // be issuing -- the final header line contains the credentials. |
| 1246 | MockWrite("GET / HTTP/1.1\r\n" |
| 1247 | "Host: www.google.com\r\n" |
| 1248 | "Connection: keep-alive\r\n" |
| 1249 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1250 | }; |
| 1251 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1252 | MockRead data_reads1[] = { |
| 1253 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1254 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1255 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1256 | |
| 1257 | // Lastly, the server responds with the actual content. |
| 1258 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1259 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1260 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1261 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1262 | }; |
| 1263 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1264 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1265 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1266 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1267 | |
| 1268 | TestCompletionCallback callback1; |
| 1269 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1270 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1271 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1272 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1273 | |
| 1274 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1275 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1276 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1277 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1278 | EXPECT_FALSE(response == NULL); |
| 1279 | |
| 1280 | // The password prompt info should have been set in response->auth_challenge. |
| 1281 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1282 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1283 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1284 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1285 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1286 | |
| 1287 | TestCompletionCallback callback2; |
| 1288 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1289 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1290 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1291 | |
| 1292 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1293 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1294 | |
| 1295 | response = trans->GetResponseInfo(); |
| 1296 | EXPECT_FALSE(response == NULL); |
| 1297 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1298 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1302 | // connection and with a large response body to drain. |
| 1303 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1304 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1305 | request.method = "GET"; |
| 1306 | request.url = GURL("http://www.google.com/"); |
| 1307 | request.load_flags = 0; |
| 1308 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1309 | SessionDependencies session_deps; |
| 1310 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1311 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1312 | MockWrite data_writes1[] = { |
| 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 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1318 | // be issuing -- the final header line contains the credentials. |
| 1319 | MockWrite("GET / HTTP/1.1\r\n" |
| 1320 | "Host: www.google.com\r\n" |
| 1321 | "Connection: keep-alive\r\n" |
| 1322 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1323 | }; |
| 1324 | |
| 1325 | // Respond with 5 kb of response body. |
| 1326 | std::string large_body_string("Unauthorized"); |
| 1327 | large_body_string.append(5 * 1024, ' '); |
| 1328 | large_body_string.append("\r\n"); |
| 1329 | |
| 1330 | MockRead data_reads1[] = { |
| 1331 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1332 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1333 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1334 | // 5134 = 12 + 5 * 1024 + 2 |
| 1335 | MockRead("Content-Length: 5134\r\n\r\n"), |
| 1336 | MockRead(true, large_body_string.data(), large_body_string.size()), |
| 1337 | |
| 1338 | // Lastly, the server responds with the actual content. |
| 1339 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1340 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1341 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1342 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1343 | }; |
| 1344 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1345 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1346 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1347 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1348 | |
| 1349 | TestCompletionCallback callback1; |
| 1350 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1351 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1352 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1353 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1354 | |
| 1355 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1356 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1357 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1358 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1359 | EXPECT_FALSE(response == NULL); |
| 1360 | |
| 1361 | // The password prompt info should have been set in response->auth_challenge. |
| 1362 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1363 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1364 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1365 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1366 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1367 | |
| 1368 | TestCompletionCallback callback2; |
| 1369 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1370 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1371 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1372 | |
| 1373 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1374 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1375 | |
| 1376 | response = trans->GetResponseInfo(); |
| 1377 | EXPECT_FALSE(response == NULL); |
| 1378 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1379 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1383 | // connection, but the server gets impatient and closes the connection. |
| 1384 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveImpatientServer) { |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1385 | HttpRequestInfo request; |
| 1386 | request.method = "GET"; |
| 1387 | request.url = GURL("http://www.google.com/"); |
| 1388 | request.load_flags = 0; |
| 1389 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1390 | SessionDependencies session_deps; |
| 1391 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1392 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1393 | MockWrite data_writes1[] = { |
| 1394 | MockWrite("GET / HTTP/1.1\r\n" |
| 1395 | "Host: www.google.com\r\n" |
| 1396 | "Connection: keep-alive\r\n\r\n"), |
| 1397 | // This simulates the seemingly successful write to a closed connection |
| 1398 | // if the bug is not fixed. |
| 1399 | MockWrite("GET / HTTP/1.1\r\n" |
| 1400 | "Host: www.google.com\r\n" |
| 1401 | "Connection: keep-alive\r\n" |
| 1402 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1403 | }; |
| 1404 | |
| 1405 | MockRead data_reads1[] = { |
| 1406 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1407 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1408 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1409 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1410 | // Tell MockTCPClientSocket to simulate the server closing the connection. |
| 1411 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 1412 | MockRead("Unauthorized\r\n"), |
| 1413 | MockRead(false, OK), // The server closes the connection. |
| 1414 | }; |
| 1415 | |
| 1416 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1417 | // be issuing -- the final header line contains the credentials. |
| 1418 | MockWrite data_writes2[] = { |
| 1419 | MockWrite("GET / HTTP/1.1\r\n" |
| 1420 | "Host: www.google.com\r\n" |
| 1421 | "Connection: keep-alive\r\n" |
| 1422 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1423 | }; |
| 1424 | |
| 1425 | // Lastly, the server responds with the actual content. |
| 1426 | MockRead data_reads2[] = { |
| 1427 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1428 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1429 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1430 | MockRead("hello"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1431 | }; |
| 1432 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1433 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1434 | data_writes1, arraysize(data_writes1)); |
| 1435 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1436 | data_writes2, arraysize(data_writes2)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1437 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1438 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 1439 | |
| 1440 | TestCompletionCallback callback1; |
| 1441 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1442 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1443 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1444 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1445 | |
| 1446 | rv = callback1.WaitForResult(); |
| 1447 | EXPECT_EQ(OK, rv); |
| 1448 | |
| 1449 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1450 | EXPECT_FALSE(response == NULL); |
| 1451 | |
| 1452 | // The password prompt info should have been set in response->auth_challenge. |
| 1453 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1454 | |
| 1455 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 1456 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1457 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1458 | |
| 1459 | TestCompletionCallback callback2; |
| 1460 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1461 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1462 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1463 | |
| 1464 | rv = callback2.WaitForResult(); |
| 1465 | EXPECT_EQ(OK, rv); |
| 1466 | |
| 1467 | response = trans->GetResponseInfo(); |
| 1468 | ASSERT_FALSE(response == NULL); |
| 1469 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1470 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1471 | } |
| 1472 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1473 | // Test the request-challenge-retry sequence for basic auth, over a connection |
| 1474 | // that requires a restart when setting up an SSL tunnel. |
| 1475 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyNoKeepAlive) { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1476 | HttpRequestInfo request; |
| 1477 | request.method = "GET"; |
| 1478 | request.url = GURL("https://www.google.com/"); |
| 1479 | // when the no authentication data flag is set. |
| 1480 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1481 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1482 | // Configure against proxy server "myproxy:70". |
| 1483 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 1484 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1485 | session_deps.net_log = log.bound().net_log(); |
| 1486 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1487 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1488 | // Since we have proxy, should try to establish tunnel. |
| 1489 | MockWrite data_writes1[] = { |
| 1490 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1491 | "Host: www.google.com\r\n" |
| 1492 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1493 | |
| 1494 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1495 | // be issuing -- the final header line contains the credentials. |
| 1496 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1497 | "Host: www.google.com\r\n" |
| 1498 | "Proxy-Connection: keep-alive\r\n" |
| 1499 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1500 | |
| 1501 | MockWrite("GET / HTTP/1.1\r\n" |
| 1502 | "Host: www.google.com\r\n" |
| 1503 | "Connection: keep-alive\r\n\r\n"), |
| 1504 | }; |
| 1505 | |
| 1506 | // The proxy responds to the connect with a 407, using a persistent |
| 1507 | // connection. |
| 1508 | MockRead data_reads1[] = { |
| 1509 | // No credentials. |
| 1510 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1511 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1512 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 1513 | |
| 1514 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 1515 | |
| 1516 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1517 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1518 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1519 | MockRead(false, "hello"), |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1520 | }; |
| 1521 | |
| 1522 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1523 | data_writes1, arraysize(data_writes1)); |
| 1524 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1525 | SSLSocketDataProvider ssl(true, OK); |
| 1526 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1527 | |
| 1528 | TestCompletionCallback callback1; |
| 1529 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1530 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1531 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1532 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1533 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1534 | |
| 1535 | rv = callback1.WaitForResult(); |
| 1536 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1537 | net::CapturingNetLog::EntryList entries; |
| 1538 | log.GetEntries(&entries); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1539 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1540 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1541 | NetLog::PHASE_NONE); |
| 1542 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1543 | entries, pos, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1544 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1545 | NetLog::PHASE_NONE); |
| 1546 | |
| 1547 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1548 | ASSERT_FALSE(response == NULL); |
| 1549 | |
| 1550 | EXPECT_EQ(407, response->headers->response_code()); |
| 1551 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1552 | |
| 1553 | // The password prompt info should have been set in response->auth_challenge. |
| 1554 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 1555 | |
| 1556 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
| 1557 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1558 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1559 | |
| 1560 | TestCompletionCallback callback2; |
| 1561 | |
| 1562 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
| 1563 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1564 | |
| 1565 | rv = callback2.WaitForResult(); |
| 1566 | EXPECT_EQ(OK, rv); |
| 1567 | |
| 1568 | response = trans->GetResponseInfo(); |
| 1569 | ASSERT_FALSE(response == NULL); |
| 1570 | |
| 1571 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1572 | EXPECT_EQ(200, response->headers->response_code()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1573 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1574 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1575 | |
| 1576 | // The password prompt info should not be set. |
| 1577 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1578 | |
| 1579 | trans.reset(); |
| 1580 | session->FlushSocketPools(); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1581 | } |
| 1582 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1583 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1584 | // proxy connection, when setting up an SSL tunnel. |
| 1585 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1586 | HttpRequestInfo request; |
| 1587 | request.method = "GET"; |
| 1588 | request.url = GURL("https://www.google.com/"); |
| 1589 | // Ensure that proxy authentication is attempted even |
| 1590 | // when the no authentication data flag is set. |
| 1591 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1592 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1593 | // Configure against proxy server "myproxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 1594 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1595 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1596 | session_deps.net_log = log.bound().net_log(); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1597 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1598 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1599 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1600 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1601 | // Since we have proxy, should try to establish tunnel. |
| 1602 | MockWrite data_writes1[] = { |
| 1603 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1604 | "Host: www.google.com\r\n" |
| 1605 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1606 | |
| 1607 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1608 | // be issuing -- the final header line contains the credentials. |
| 1609 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1610 | "Host: www.google.com\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1611 | "Proxy-Connection: keep-alive\r\n" |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1612 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 1613 | }; |
| 1614 | |
| 1615 | // The proxy responds to the connect with a 407, using a persistent |
| 1616 | // connection. |
| 1617 | MockRead data_reads1[] = { |
| 1618 | // No credentials. |
| 1619 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1620 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1621 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1622 | MockRead("0123456789"), |
| 1623 | |
| 1624 | // Wrong credentials (wrong password). |
| 1625 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1626 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1627 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1628 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1629 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1630 | }; |
| 1631 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1632 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1633 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1634 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1635 | |
| 1636 | TestCompletionCallback callback1; |
| 1637 | |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1638 | int rv = trans->Start(&request, &callback1, log.bound()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1639 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1640 | |
| 1641 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1642 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1643 | net::CapturingNetLog::EntryList entries; |
| 1644 | log.GetEntries(&entries); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1645 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1646 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1647 | NetLog::PHASE_NONE); |
| 1648 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1649 | entries, pos, |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1650 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1651 | NetLog::PHASE_NONE); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1652 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1653 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1654 | EXPECT_FALSE(response == NULL); |
| 1655 | |
| 1656 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1657 | EXPECT_EQ(407, response->headers->response_code()); |
| 1658 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1659 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1660 | |
| 1661 | // The password prompt info should have been set in response->auth_challenge. |
| 1662 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1663 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1664 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1665 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1666 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1667 | |
| 1668 | TestCompletionCallback callback2; |
| 1669 | |
| 1670 | // Wrong password (should be "bar"). |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1671 | rv = trans->RestartWithAuth(kFoo, kBaz, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1672 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1673 | |
| 1674 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1675 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1676 | |
| 1677 | response = trans->GetResponseInfo(); |
| 1678 | EXPECT_FALSE(response == NULL); |
| 1679 | |
| 1680 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1681 | EXPECT_EQ(407, response->headers->response_code()); |
| 1682 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1683 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1684 | |
| 1685 | // The password prompt info should have been set in response->auth_challenge. |
| 1686 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1687 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1688 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1689 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1690 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1691 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1692 | // Flush the idle socket before the NetLog and HttpNetworkTransaction go |
| 1693 | // out of scope. |
| 1694 | session->FlushSocketPools(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1695 | } |
| 1696 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1697 | // Test that we don't read the response body when we fail to establish a tunnel, |
| 1698 | // even if the user cancels the proxy's auth attempt. |
| 1699 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1700 | HttpRequestInfo request; |
| 1701 | request.method = "GET"; |
| 1702 | request.url = GURL("https://www.google.com/"); |
| 1703 | request.load_flags = 0; |
| 1704 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1705 | // Configure against proxy server "myproxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 1706 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1707 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1708 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1709 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1710 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1711 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1712 | // Since we have proxy, should try to establish tunnel. |
| 1713 | MockWrite data_writes[] = { |
| 1714 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1715 | "Host: www.google.com\r\n" |
| 1716 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1717 | }; |
| 1718 | |
| 1719 | // The proxy responds to the connect with a 407. |
| 1720 | MockRead data_reads[] = { |
| 1721 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1722 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1723 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1724 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1725 | }; |
| 1726 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1727 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1728 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1729 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1730 | |
| 1731 | TestCompletionCallback callback; |
| 1732 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1733 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1734 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1735 | |
| 1736 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1737 | EXPECT_EQ(OK, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1738 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1739 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1740 | EXPECT_FALSE(response == NULL); |
| 1741 | |
| 1742 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1743 | EXPECT_EQ(407, response->headers->response_code()); |
| 1744 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1745 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1746 | |
| 1747 | std::string response_data; |
| 1748 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1749 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1750 | |
| 1751 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
| 1752 | session->FlushSocketPools(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1753 | } |
| 1754 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1755 | // Test when a server (non-proxy) returns a 407 (proxy-authenticate). |
| 1756 | // The request should fail with ERR_UNEXPECTED_PROXY_AUTH. |
| 1757 | TEST_F(HttpNetworkTransactionTest, UnexpectedProxyAuth) { |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1758 | HttpRequestInfo request; |
| 1759 | request.method = "GET"; |
| 1760 | request.url = GURL("http://www.google.com/"); |
| 1761 | request.load_flags = 0; |
| 1762 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1763 | // We are using a DIRECT connection (i.e. no proxy) for this session. |
| 1764 | SessionDependencies session_deps; |
| 1765 | scoped_ptr<HttpTransaction> trans( |
| 1766 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1767 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1768 | MockWrite data_writes1[] = { |
| 1769 | MockWrite("GET / HTTP/1.1\r\n" |
| 1770 | "Host: www.google.com\r\n" |
| 1771 | "Connection: keep-alive\r\n\r\n"), |
| 1772 | }; |
| 1773 | |
| 1774 | MockRead data_reads1[] = { |
| 1775 | MockRead("HTTP/1.0 407 Proxy Auth required\r\n"), |
| 1776 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1777 | // Large content-length -- won't matter, as connection will be reset. |
| 1778 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1779 | MockRead(false, ERR_FAILED), |
| 1780 | }; |
| 1781 | |
| 1782 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1783 | data_writes1, arraysize(data_writes1)); |
| 1784 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1785 | |
| 1786 | TestCompletionCallback callback; |
| 1787 | |
| 1788 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 1789 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1790 | |
| 1791 | rv = callback.WaitForResult(); |
| 1792 | EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); |
| 1793 | } |
| 1794 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1795 | // Tests when an HTTPS server (non-proxy) returns a 407 (proxy-authentication) |
| 1796 | // through a non-authenticating proxy. The request should fail with |
| 1797 | // ERR_UNEXPECTED_PROXY_AUTH. |
| 1798 | // Note that it is impossible to detect if an HTTP server returns a 407 through |
| 1799 | // a non-authenticating proxy - there is nothing to indicate whether the |
| 1800 | // response came from the proxy or the server, so it is treated as if the proxy |
| 1801 | // issued the challenge. |
| 1802 | TEST_F(HttpNetworkTransactionTest, HttpsServerRequestsProxyAuthThroughProxy) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1803 | HttpRequestInfo request; |
| 1804 | request.method = "GET"; |
| 1805 | request.url = GURL("https://www.google.com/"); |
| 1806 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1807 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 1808 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1809 | session_deps.net_log = log.bound().net_log(); |
| 1810 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1811 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1812 | // Since we have proxy, should try to establish tunnel. |
| 1813 | MockWrite data_writes1[] = { |
| 1814 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1815 | "Host: www.google.com\r\n" |
| 1816 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1817 | |
| 1818 | MockWrite("GET / HTTP/1.1\r\n" |
| 1819 | "Host: www.google.com\r\n" |
| 1820 | "Connection: keep-alive\r\n\r\n"), |
| 1821 | }; |
| 1822 | |
| 1823 | MockRead data_reads1[] = { |
| 1824 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 1825 | |
| 1826 | MockRead("HTTP/1.1 407 Unauthorized\r\n"), |
| 1827 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1828 | MockRead("\r\n"), |
| 1829 | MockRead(false, OK), |
| 1830 | }; |
| 1831 | |
| 1832 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1833 | data_writes1, arraysize(data_writes1)); |
| 1834 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1835 | SSLSocketDataProvider ssl(true, OK); |
| 1836 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1837 | |
| 1838 | TestCompletionCallback callback1; |
| 1839 | |
| 1840 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1841 | |
| 1842 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1843 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1844 | |
| 1845 | rv = callback1.WaitForResult(); |
| 1846 | EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1847 | net::CapturingNetLog::EntryList entries; |
| 1848 | log.GetEntries(&entries); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1849 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1850 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1851 | NetLog::PHASE_NONE); |
| 1852 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1853 | entries, pos, |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1854 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1855 | NetLog::PHASE_NONE); |
| 1856 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1857 | |
| 1858 | // Test a simple get through an HTTPS Proxy. |
| 1859 | TEST_F(HttpNetworkTransactionTest, HttpsProxyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1860 | HttpRequestInfo request; |
| 1861 | request.method = "GET"; |
| 1862 | request.url = GURL("http://www.google.com/"); |
| 1863 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1864 | // Configure against https proxy server "proxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 1865 | SessionDependencies session_deps(ProxyService::CreateFixed("https://proxy:70")); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1866 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1867 | session_deps.net_log = log.bound().net_log(); |
| 1868 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1869 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1870 | // Since we have proxy, should use full url |
| 1871 | MockWrite data_writes1[] = { |
| 1872 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1873 | "Host: www.google.com\r\n" |
| 1874 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1875 | }; |
| 1876 | |
| 1877 | MockRead data_reads1[] = { |
| 1878 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1879 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1880 | MockRead("Content-Length: 100\r\n\r\n"), |
| 1881 | MockRead(false, OK), |
| 1882 | }; |
| 1883 | |
| 1884 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1885 | data_writes1, arraysize(data_writes1)); |
| 1886 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1887 | SSLSocketDataProvider ssl(true, OK); |
| 1888 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1889 | |
| 1890 | TestCompletionCallback callback1; |
| 1891 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1892 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1893 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1894 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1895 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1896 | |
| 1897 | rv = callback1.WaitForResult(); |
| 1898 | EXPECT_EQ(OK, rv); |
| 1899 | |
| 1900 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1901 | ASSERT_FALSE(response == NULL); |
| 1902 | |
| 1903 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1904 | EXPECT_EQ(200, response->headers->response_code()); |
| 1905 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1906 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1907 | |
| 1908 | // The password prompt info should not be set. |
| 1909 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1910 | } |
| 1911 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 1912 | // Test a SPDY get through an HTTPS Proxy. |
| 1913 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1914 | HttpRequestInfo request; |
| 1915 | request.method = "GET"; |
| 1916 | request.url = GURL("http://www.google.com/"); |
| 1917 | request.load_flags = 0; |
| 1918 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 1919 | // Configure against https proxy server "proxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 1920 | SessionDependencies session_deps(ProxyService::CreateFixed("https://proxy:70")); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 1921 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1922 | session_deps.net_log = log.bound().net_log(); |
| 1923 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1924 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 1925 | // fetch http://www.google.com/ via SPDY |
| 1926 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST, |
| 1927 | false)); |
| 1928 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
| 1929 | |
| 1930 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 1931 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 1932 | MockRead spdy_reads[] = { |
| 1933 | CreateMockRead(*resp), |
| 1934 | CreateMockRead(*data), |
| 1935 | MockRead(true, 0, 0), |
| 1936 | }; |
| 1937 | |
| 1938 | scoped_refptr<DelayedSocketData> spdy_data( |
| 1939 | new DelayedSocketData( |
| 1940 | 1, // wait for one write to finish before reading. |
| 1941 | spdy_reads, arraysize(spdy_reads), |
| 1942 | spdy_writes, arraysize(spdy_writes))); |
| 1943 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 1944 | |
| 1945 | SSLSocketDataProvider ssl(true, OK); |
| 1946 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 1947 | ssl.next_proto = "spdy/2"; |
| 1948 | ssl.was_npn_negotiated = true; |
| 1949 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1950 | |
| 1951 | TestCompletionCallback callback1; |
| 1952 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1953 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1954 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 1955 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1956 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1957 | |
| 1958 | rv = callback1.WaitForResult(); |
| 1959 | EXPECT_EQ(OK, rv); |
| 1960 | |
| 1961 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1962 | ASSERT_TRUE(response != NULL); |
| 1963 | ASSERT_TRUE(response->headers != NULL); |
| 1964 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1965 | |
| 1966 | std::string response_data; |
| 1967 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 1968 | EXPECT_EQ(net::kUploadData, response_data); |
| 1969 | } |
| 1970 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 1971 | // Test a SPDY get through an HTTPS Proxy. |
| 1972 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGetWithProxyAuth) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 1973 | HttpRequestInfo request; |
| 1974 | request.method = "GET"; |
| 1975 | request.url = GURL("http://www.google.com/"); |
| 1976 | request.load_flags = 0; |
| 1977 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 1978 | // Configure against https proxy server "proxy:70". |
| 1979 | SessionDependencies session_deps( |
| 1980 | ProxyService::CreateFixed("https://proxy: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] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 1985 | // The first request will be a bare GET, the second request will be a |
| 1986 | // GET with a Proxy-Authorization header. |
| 1987 | scoped_ptr<spdy::SpdyFrame> req_get( |
| 1988 | ConstructSpdyGet(NULL, 0, false, 1, LOWEST, false)); |
| 1989 | const char* const kExtraAuthorizationHeaders[] = { |
| 1990 | "proxy-authorization", |
| 1991 | "Basic Zm9vOmJhcg==", |
| 1992 | }; |
| 1993 | scoped_ptr<spdy::SpdyFrame> req_get_authorization( |
| 1994 | ConstructSpdyGet( |
| 1995 | kExtraAuthorizationHeaders, arraysize(kExtraAuthorizationHeaders)/2, |
| 1996 | false, 3, LOWEST, false)); |
| 1997 | MockWrite spdy_writes[] = { |
| 1998 | CreateMockWrite(*req_get, 1), |
| 1999 | CreateMockWrite(*req_get_authorization, 4), |
| 2000 | }; |
| 2001 | |
| 2002 | // The first response is a 407 proxy authentication challenge, and the second |
| 2003 | // response will be a 200 response since the second request includes a valid |
| 2004 | // Authorization header. |
| 2005 | const char* const kExtraAuthenticationHeaders[] = { |
| 2006 | "Proxy-Authenticate", |
| 2007 | "Basic realm=\"MyRealm1\"" |
| 2008 | }; |
| 2009 | scoped_ptr<spdy::SpdyFrame> resp_authentication( |
| 2010 | ConstructSpdySynReplyError( |
| 2011 | "407 Proxy Authentication Required", |
| 2012 | kExtraAuthenticationHeaders, arraysize(kExtraAuthenticationHeaders)/2, |
| 2013 | 1)); |
| 2014 | scoped_ptr<spdy::SpdyFrame> body_authentication( |
| 2015 | ConstructSpdyBodyFrame(1, true)); |
| 2016 | scoped_ptr<spdy::SpdyFrame> resp_data(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 2017 | scoped_ptr<spdy::SpdyFrame> body_data(ConstructSpdyBodyFrame(3, true)); |
| 2018 | MockRead spdy_reads[] = { |
| 2019 | CreateMockRead(*resp_authentication, 2), |
| 2020 | CreateMockRead(*body_authentication, 3), |
| 2021 | CreateMockRead(*resp_data, 5), |
| 2022 | CreateMockRead(*body_data, 6), |
| 2023 | MockRead(true, 0, 7), |
| 2024 | }; |
| 2025 | |
| 2026 | scoped_refptr<OrderedSocketData> data( |
| 2027 | new OrderedSocketData(spdy_reads, arraysize(spdy_reads), |
| 2028 | spdy_writes, arraysize(spdy_writes))); |
| 2029 | session_deps.socket_factory.AddSocketDataProvider(data); |
| 2030 | |
| 2031 | SSLSocketDataProvider ssl(true, OK); |
| 2032 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2033 | ssl.next_proto = "spdy/2"; |
| 2034 | ssl.was_npn_negotiated = true; |
| 2035 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2036 | |
| 2037 | TestCompletionCallback callback1; |
| 2038 | |
| 2039 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2040 | |
| 2041 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2042 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2043 | |
| 2044 | rv = callback1.WaitForResult(); |
| 2045 | EXPECT_EQ(OK, rv); |
| 2046 | |
| 2047 | const HttpResponseInfo* const response = trans->GetResponseInfo(); |
| 2048 | |
| 2049 | ASSERT_TRUE(response != NULL); |
| 2050 | ASSERT_TRUE(response->headers != NULL); |
| 2051 | EXPECT_EQ(407, response->headers->response_code()); |
| 2052 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 2053 | |
| 2054 | // The password prompt info should have been set in response->auth_challenge. |
| 2055 | ASSERT_TRUE(response->auth_challenge.get() != NULL); |
| 2056 | EXPECT_TRUE(response->auth_challenge->is_proxy); |
| 2057 | EXPECT_EQ(L"proxy:70", response->auth_challenge->host_and_port); |
| 2058 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2059 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2060 | |
| 2061 | TestCompletionCallback callback2; |
| 2062 | |
| 2063 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
| 2064 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2065 | |
| 2066 | rv = callback2.WaitForResult(); |
| 2067 | EXPECT_EQ(OK, rv); |
| 2068 | |
| 2069 | const HttpResponseInfo* const response_restart = trans->GetResponseInfo(); |
| 2070 | |
| 2071 | ASSERT_TRUE(response_restart != NULL); |
| 2072 | ASSERT_TRUE(response_restart->headers != NULL); |
| 2073 | EXPECT_EQ(200, response_restart->headers->response_code()); |
| 2074 | // The password prompt info should not be set. |
| 2075 | EXPECT_TRUE(response_restart->auth_challenge.get() == NULL); |
| 2076 | } |
| 2077 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2078 | // Test a SPDY CONNECT through an HTTPS Proxy to an HTTPS (non-SPDY) Server. |
| 2079 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectHttps) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 2080 | HttpRequestInfo request; |
| 2081 | request.method = "GET"; |
| 2082 | request.url = GURL("https://www.google.com/"); |
| 2083 | request.load_flags = 0; |
| 2084 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2085 | // Configure against https proxy server "proxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 2086 | SessionDependencies session_deps(ProxyService::CreateFixed("https://proxy:70")); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2087 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2088 | session_deps.net_log = log.bound().net_log(); |
| 2089 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2090 | |
| 2091 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2092 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2093 | // CONNECT to www.google.com:443 via SPDY |
| 2094 | scoped_ptr<spdy::SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 2095 | // fetch https://www.google.com/ via HTTP |
| 2096 | |
| 2097 | const char get[] = "GET / HTTP/1.1\r\n" |
| 2098 | "Host: www.google.com\r\n" |
| 2099 | "Connection: keep-alive\r\n\r\n"; |
| 2100 | scoped_ptr<spdy::SpdyFrame> wrapped_get( |
| 2101 | ConstructSpdyBodyFrame(1, get, strlen(get), false)); |
| 2102 | MockWrite spdy_writes[] = { |
| 2103 | CreateMockWrite(*connect, 1), |
| 2104 | CreateMockWrite(*wrapped_get, 3) |
| 2105 | }; |
| 2106 | |
| 2107 | scoped_ptr<spdy::SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2108 | const char resp[] = "HTTP/1.1 200 OK\r\n" |
| 2109 | "Content-Length: 10\r\n\r\n"; |
| 2110 | |
| 2111 | scoped_ptr<spdy::SpdyFrame> wrapped_get_resp( |
| 2112 | ConstructSpdyBodyFrame(1, resp, strlen(resp), false)); |
| 2113 | scoped_ptr<spdy::SpdyFrame> wrapped_body( |
| 2114 | ConstructSpdyBodyFrame(1, "1234567890", 10, false)); |
| 2115 | MockRead spdy_reads[] = { |
| 2116 | CreateMockRead(*conn_resp, 2, true), |
| 2117 | CreateMockRead(*wrapped_get_resp, 4, true), |
| 2118 | CreateMockRead(*wrapped_body, 5, true), |
| 2119 | CreateMockRead(*wrapped_body, 6, true), |
| 2120 | MockRead(true, 0, 7), |
| 2121 | }; |
| 2122 | |
| 2123 | scoped_refptr<OrderedSocketData> spdy_data( |
| 2124 | new OrderedSocketData( |
| 2125 | spdy_reads, arraysize(spdy_reads), |
| 2126 | spdy_writes, arraysize(spdy_writes))); |
| 2127 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 2128 | |
| 2129 | SSLSocketDataProvider ssl(true, OK); |
| 2130 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2131 | ssl.next_proto = "spdy/2"; |
| 2132 | ssl.was_npn_negotiated = true; |
| 2133 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2134 | SSLSocketDataProvider ssl2(true, OK); |
| 2135 | ssl2.was_npn_negotiated = false; |
| 2136 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2137 | |
| 2138 | TestCompletionCallback callback1; |
| 2139 | |
| 2140 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2141 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2142 | |
| 2143 | rv = callback1.WaitForResult(); |
| 2144 | EXPECT_EQ(OK, rv); |
| 2145 | |
| 2146 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2147 | ASSERT_TRUE(response != NULL); |
| 2148 | ASSERT_TRUE(response->headers != NULL); |
| 2149 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2150 | |
| 2151 | std::string response_data; |
| 2152 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 2153 | EXPECT_EQ("1234567890", response_data); |
| 2154 | } |
| 2155 | |
| 2156 | // Test a SPDY CONNECT through an HTTPS Proxy to a SPDY server. |
| 2157 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectSpdy) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 2158 | HttpRequestInfo request; |
| 2159 | request.method = "GET"; |
| 2160 | request.url = GURL("https://www.google.com/"); |
| 2161 | request.load_flags = 0; |
| 2162 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2163 | // Configure against https proxy server "proxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 2164 | SessionDependencies session_deps(ProxyService::CreateFixed("https://proxy:70")); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2165 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2166 | session_deps.net_log = log.bound().net_log(); |
| 2167 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2168 | |
| 2169 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2170 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2171 | // CONNECT to www.google.com:443 via SPDY |
| 2172 | scoped_ptr<spdy::SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 2173 | // fetch https://www.google.com/ via SPDY |
| 2174 | const char* const kMyUrl = "https://www.google.com/"; |
| 2175 | scoped_ptr<spdy::SpdyFrame> get(ConstructSpdyGet(kMyUrl, false, 1, LOWEST)); |
| 2176 | scoped_ptr<spdy::SpdyFrame> wrapped_get(ConstructWrappedSpdyFrame(get, 1)); |
| 2177 | MockWrite spdy_writes[] = { |
| 2178 | CreateMockWrite(*connect, 1), |
| 2179 | CreateMockWrite(*wrapped_get, 3) |
| 2180 | }; |
| 2181 | |
| 2182 | scoped_ptr<spdy::SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2183 | scoped_ptr<spdy::SpdyFrame> get_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2184 | scoped_ptr<spdy::SpdyFrame> wrapped_get_resp( |
| 2185 | ConstructWrappedSpdyFrame(get_resp, 1)); |
| 2186 | scoped_ptr<spdy::SpdyFrame> body(ConstructSpdyBodyFrame(1, true)); |
| 2187 | scoped_ptr<spdy::SpdyFrame> wrapped_body(ConstructWrappedSpdyFrame(body, 1)); |
| 2188 | MockRead spdy_reads[] = { |
| 2189 | CreateMockRead(*conn_resp, 2, true), |
| 2190 | CreateMockRead(*wrapped_get_resp, 4, true), |
| 2191 | CreateMockRead(*wrapped_body, 5, true), |
| 2192 | MockRead(true, 0, 1), |
| 2193 | }; |
| 2194 | |
| 2195 | scoped_refptr<OrderedSocketData> spdy_data( |
| 2196 | new OrderedSocketData( |
| 2197 | spdy_reads, arraysize(spdy_reads), |
| 2198 | spdy_writes, arraysize(spdy_writes))); |
| 2199 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 2200 | |
| 2201 | SSLSocketDataProvider ssl(true, OK); |
| 2202 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2203 | ssl.next_proto = "spdy/2"; |
| 2204 | ssl.was_npn_negotiated = true; |
| 2205 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2206 | SSLSocketDataProvider ssl2(true, OK); |
| 2207 | ssl2.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2208 | ssl2.next_proto = "spdy/2"; |
| 2209 | ssl2.was_npn_negotiated = true; |
| 2210 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2211 | |
| 2212 | TestCompletionCallback callback1; |
| 2213 | |
| 2214 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2215 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2216 | |
| 2217 | rv = callback1.WaitForResult(); |
| 2218 | EXPECT_EQ(OK, rv); |
| 2219 | |
| 2220 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2221 | ASSERT_TRUE(response != NULL); |
| 2222 | ASSERT_TRUE(response->headers != NULL); |
| 2223 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2224 | |
| 2225 | std::string response_data; |
| 2226 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 2227 | EXPECT_EQ(net::kUploadData, response_data); |
| 2228 | } |
| 2229 | |
| 2230 | // Test a SPDY CONNECT failure through an HTTPS Proxy. |
| 2231 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectFailure) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 2232 | HttpRequestInfo request; |
| 2233 | request.method = "GET"; |
| 2234 | request.url = GURL("https://www.google.com/"); |
| 2235 | request.load_flags = 0; |
| 2236 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2237 | // Configure against https proxy server "proxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 2238 | SessionDependencies session_deps(ProxyService::CreateFixed("https://proxy:70")); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2239 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2240 | session_deps.net_log = log.bound().net_log(); |
| 2241 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2242 | |
| 2243 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2244 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2245 | // CONNECT to www.google.com:443 via SPDY |
| 2246 | scoped_ptr<spdy::SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 2247 | scoped_ptr<spdy::SpdyFrame> get(ConstructSpdyRstStream(1, spdy::CANCEL)); |
| 2248 | |
| 2249 | MockWrite spdy_writes[] = { |
| 2250 | CreateMockWrite(*connect, 1), |
| 2251 | CreateMockWrite(*get, 3), |
| 2252 | }; |
| 2253 | |
| 2254 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdySynReplyError(1)); |
| 2255 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 2256 | MockRead spdy_reads[] = { |
| 2257 | CreateMockRead(*resp, 2, true), |
| 2258 | MockRead(true, 0, 4), |
| 2259 | }; |
| 2260 | |
| 2261 | scoped_refptr<OrderedSocketData> spdy_data( |
| 2262 | new OrderedSocketData( |
| 2263 | spdy_reads, arraysize(spdy_reads), |
| 2264 | spdy_writes, arraysize(spdy_writes))); |
| 2265 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 2266 | |
| 2267 | SSLSocketDataProvider ssl(true, OK); |
| 2268 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2269 | ssl.next_proto = "spdy/2"; |
| 2270 | ssl.was_npn_negotiated = true; |
| 2271 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2272 | SSLSocketDataProvider ssl2(true, OK); |
| 2273 | ssl2.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 2274 | ssl2.next_proto = "spdy/2"; |
| 2275 | ssl2.was_npn_negotiated = true; |
| 2276 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2277 | |
| 2278 | TestCompletionCallback callback1; |
| 2279 | |
| 2280 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2281 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2282 | |
| 2283 | rv = callback1.WaitForResult(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 2284 | EXPECT_EQ(OK, rv); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2285 | |
| 2286 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 2287 | ASSERT_FALSE(response == NULL); |
| 2288 | EXPECT_EQ(500, response->headers->response_code()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2289 | } |
| 2290 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2291 | // Test the challenge-response-retry sequence through an HTTPS Proxy |
| 2292 | TEST_F(HttpNetworkTransactionTest, HttpsProxyAuthRetry) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2293 | HttpRequestInfo request; |
| 2294 | request.method = "GET"; |
| 2295 | request.url = GURL("http://www.google.com/"); |
| 2296 | // when the no authentication data flag is set. |
| 2297 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 2298 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 2299 | // Configure against https proxy server "proxy:70". |
| 2300 | SessionDependencies session_deps(ProxyService::CreateFixed("https://proxy:70")); |
| 2301 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2302 | session_deps.net_log = log.bound().net_log(); |
| 2303 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2304 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2305 | // Since we have proxy, should use full url |
| 2306 | MockWrite data_writes1[] = { |
| 2307 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2308 | "Host: www.google.com\r\n" |
| 2309 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2310 | |
| 2311 | // After calling trans->RestartWithAuth(), this is the request we should |
| 2312 | // be issuing -- the final header line contains the credentials. |
| 2313 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2314 | "Host: www.google.com\r\n" |
| 2315 | "Proxy-Connection: keep-alive\r\n" |
| 2316 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2317 | }; |
| 2318 | |
| 2319 | // The proxy responds to the GET with a 407, using a persistent |
| 2320 | // connection. |
| 2321 | MockRead data_reads1[] = { |
| 2322 | // No credentials. |
| 2323 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 2324 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2325 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 2326 | MockRead("Content-Length: 0\r\n\r\n"), |
| 2327 | |
| 2328 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2329 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2330 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2331 | MockRead(false, OK), |
| 2332 | }; |
| 2333 | |
| 2334 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2335 | data_writes1, arraysize(data_writes1)); |
| 2336 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2337 | SSLSocketDataProvider ssl(true, OK); |
| 2338 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2339 | |
| 2340 | TestCompletionCallback callback1; |
| 2341 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2342 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2343 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2344 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 2345 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2346 | |
| 2347 | rv = callback1.WaitForResult(); |
| 2348 | EXPECT_EQ(OK, rv); |
| 2349 | |
| 2350 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2351 | ASSERT_FALSE(response == NULL); |
| 2352 | |
| 2353 | EXPECT_EQ(407, response->headers->response_code()); |
| 2354 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 2355 | |
| 2356 | // The password prompt info should have been set in response->auth_challenge. |
| 2357 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 2358 | |
| 2359 | EXPECT_EQ(L"proxy:70", response->auth_challenge->host_and_port); |
| 2360 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2361 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2362 | |
| 2363 | TestCompletionCallback callback2; |
| 2364 | |
| 2365 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
| 2366 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2367 | |
| 2368 | rv = callback2.WaitForResult(); |
| 2369 | EXPECT_EQ(OK, rv); |
| 2370 | |
| 2371 | response = trans->GetResponseInfo(); |
| 2372 | ASSERT_FALSE(response == NULL); |
| 2373 | |
| 2374 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 2375 | EXPECT_EQ(200, response->headers->response_code()); |
| 2376 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2377 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 2378 | |
| 2379 | // The password prompt info should not be set. |
| 2380 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2381 | } |
| 2382 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 2383 | void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus( |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2384 | const MockRead& status, int expected_status) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2385 | HttpRequestInfo request; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2386 | request.method = "GET"; |
| 2387 | request.url = GURL("https://www.google.com/"); |
| 2388 | request.load_flags = 0; |
| 2389 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 2390 | // Configure against proxy server "myproxy:70". |
| 2391 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 2392 | |
| 2393 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2394 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2395 | // Since we have proxy, should try to establish tunnel. |
| 2396 | MockWrite data_writes[] = { |
| 2397 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 2398 | "Host: www.google.com\r\n" |
| 2399 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2400 | }; |
| 2401 | |
| 2402 | MockRead data_reads[] = { |
| 2403 | status, |
| 2404 | MockRead("Content-Length: 10\r\n\r\n"), |
| 2405 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2406 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2407 | }; |
| 2408 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2409 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 2410 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2411 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2412 | |
| 2413 | TestCompletionCallback callback; |
| 2414 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2415 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2416 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2417 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2418 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2419 | |
| 2420 | rv = callback.WaitForResult(); |
| 2421 | EXPECT_EQ(expected_status, rv); |
| 2422 | } |
| 2423 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 2424 | void HttpNetworkTransactionTest::ConnectStatusHelper(const MockRead& status) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2425 | ConnectStatusHelperWithExpectedStatus( |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2426 | status, ERR_TUNNEL_CONNECTION_FAILED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2427 | } |
| 2428 | |
| 2429 | TEST_F(HttpNetworkTransactionTest, ConnectStatus100) { |
| 2430 | ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n")); |
| 2431 | } |
| 2432 | |
| 2433 | TEST_F(HttpNetworkTransactionTest, ConnectStatus101) { |
| 2434 | ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n")); |
| 2435 | } |
| 2436 | |
| 2437 | TEST_F(HttpNetworkTransactionTest, ConnectStatus201) { |
| 2438 | ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n")); |
| 2439 | } |
| 2440 | |
| 2441 | TEST_F(HttpNetworkTransactionTest, ConnectStatus202) { |
| 2442 | ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n")); |
| 2443 | } |
| 2444 | |
| 2445 | TEST_F(HttpNetworkTransactionTest, ConnectStatus203) { |
| 2446 | ConnectStatusHelper( |
| 2447 | MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n")); |
| 2448 | } |
| 2449 | |
| 2450 | TEST_F(HttpNetworkTransactionTest, ConnectStatus204) { |
| 2451 | ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n")); |
| 2452 | } |
| 2453 | |
| 2454 | TEST_F(HttpNetworkTransactionTest, ConnectStatus205) { |
| 2455 | ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n")); |
| 2456 | } |
| 2457 | |
| 2458 | TEST_F(HttpNetworkTransactionTest, ConnectStatus206) { |
| 2459 | ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n")); |
| 2460 | } |
| 2461 | |
| 2462 | TEST_F(HttpNetworkTransactionTest, ConnectStatus300) { |
| 2463 | ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n")); |
| 2464 | } |
| 2465 | |
| 2466 | TEST_F(HttpNetworkTransactionTest, ConnectStatus301) { |
| 2467 | ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n")); |
| 2468 | } |
| 2469 | |
| 2470 | TEST_F(HttpNetworkTransactionTest, ConnectStatus302) { |
| 2471 | ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n")); |
| 2472 | } |
| 2473 | |
| 2474 | TEST_F(HttpNetworkTransactionTest, ConnectStatus303) { |
| 2475 | ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n")); |
| 2476 | } |
| 2477 | |
| 2478 | TEST_F(HttpNetworkTransactionTest, ConnectStatus304) { |
| 2479 | ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n")); |
| 2480 | } |
| 2481 | |
| 2482 | TEST_F(HttpNetworkTransactionTest, ConnectStatus305) { |
| 2483 | ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n")); |
| 2484 | } |
| 2485 | |
| 2486 | TEST_F(HttpNetworkTransactionTest, ConnectStatus306) { |
| 2487 | ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n")); |
| 2488 | } |
| 2489 | |
| 2490 | TEST_F(HttpNetworkTransactionTest, ConnectStatus307) { |
| 2491 | ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n")); |
| 2492 | } |
| 2493 | |
| 2494 | TEST_F(HttpNetworkTransactionTest, ConnectStatus400) { |
| 2495 | ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n")); |
| 2496 | } |
| 2497 | |
| 2498 | TEST_F(HttpNetworkTransactionTest, ConnectStatus401) { |
| 2499 | ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n")); |
| 2500 | } |
| 2501 | |
| 2502 | TEST_F(HttpNetworkTransactionTest, ConnectStatus402) { |
| 2503 | ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n")); |
| 2504 | } |
| 2505 | |
| 2506 | TEST_F(HttpNetworkTransactionTest, ConnectStatus403) { |
| 2507 | ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n")); |
| 2508 | } |
| 2509 | |
| 2510 | TEST_F(HttpNetworkTransactionTest, ConnectStatus404) { |
| 2511 | ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n")); |
| 2512 | } |
| 2513 | |
| 2514 | TEST_F(HttpNetworkTransactionTest, ConnectStatus405) { |
| 2515 | ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n")); |
| 2516 | } |
| 2517 | |
| 2518 | TEST_F(HttpNetworkTransactionTest, ConnectStatus406) { |
| 2519 | ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n")); |
| 2520 | } |
| 2521 | |
| 2522 | TEST_F(HttpNetworkTransactionTest, ConnectStatus407) { |
| 2523 | ConnectStatusHelperWithExpectedStatus( |
| 2524 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 2525 | ERR_PROXY_AUTH_UNSUPPORTED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | TEST_F(HttpNetworkTransactionTest, ConnectStatus408) { |
| 2529 | ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n")); |
| 2530 | } |
| 2531 | |
| 2532 | TEST_F(HttpNetworkTransactionTest, ConnectStatus409) { |
| 2533 | ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n")); |
| 2534 | } |
| 2535 | |
| 2536 | TEST_F(HttpNetworkTransactionTest, ConnectStatus410) { |
| 2537 | ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n")); |
| 2538 | } |
| 2539 | |
| 2540 | TEST_F(HttpNetworkTransactionTest, ConnectStatus411) { |
| 2541 | ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n")); |
| 2542 | } |
| 2543 | |
| 2544 | TEST_F(HttpNetworkTransactionTest, ConnectStatus412) { |
| 2545 | ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n")); |
| 2546 | } |
| 2547 | |
| 2548 | TEST_F(HttpNetworkTransactionTest, ConnectStatus413) { |
| 2549 | ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n")); |
| 2550 | } |
| 2551 | |
| 2552 | TEST_F(HttpNetworkTransactionTest, ConnectStatus414) { |
| 2553 | ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n")); |
| 2554 | } |
| 2555 | |
| 2556 | TEST_F(HttpNetworkTransactionTest, ConnectStatus415) { |
| 2557 | ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n")); |
| 2558 | } |
| 2559 | |
| 2560 | TEST_F(HttpNetworkTransactionTest, ConnectStatus416) { |
| 2561 | ConnectStatusHelper( |
| 2562 | MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n")); |
| 2563 | } |
| 2564 | |
| 2565 | TEST_F(HttpNetworkTransactionTest, ConnectStatus417) { |
| 2566 | ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n")); |
| 2567 | } |
| 2568 | |
| 2569 | TEST_F(HttpNetworkTransactionTest, ConnectStatus500) { |
| 2570 | ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n")); |
| 2571 | } |
| 2572 | |
| 2573 | TEST_F(HttpNetworkTransactionTest, ConnectStatus501) { |
| 2574 | ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n")); |
| 2575 | } |
| 2576 | |
| 2577 | TEST_F(HttpNetworkTransactionTest, ConnectStatus502) { |
| 2578 | ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n")); |
| 2579 | } |
| 2580 | |
| 2581 | TEST_F(HttpNetworkTransactionTest, ConnectStatus503) { |
| 2582 | ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n")); |
| 2583 | } |
| 2584 | |
| 2585 | TEST_F(HttpNetworkTransactionTest, ConnectStatus504) { |
| 2586 | ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n")); |
| 2587 | } |
| 2588 | |
| 2589 | TEST_F(HttpNetworkTransactionTest, ConnectStatus505) { |
| 2590 | ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n")); |
| 2591 | } |
| 2592 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2593 | // Test the flow when both the proxy server AND origin server require |
| 2594 | // authentication. Again, this uses basic auth for both since that is |
| 2595 | // the simplest to mock. |
| 2596 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 2597 | HttpRequestInfo request; |
| 2598 | request.method = "GET"; |
| 2599 | request.url = GURL("http://www.google.com/"); |
| 2600 | request.load_flags = 0; |
| 2601 | |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 2602 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 2603 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2604 | // Configure against proxy server "myproxy:70". |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2605 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2606 | CreateSession(&session_deps))); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2607 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2608 | MockWrite data_writes1[] = { |
| 2609 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2610 | "Host: www.google.com\r\n" |
| 2611 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2612 | }; |
| 2613 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2614 | MockRead data_reads1[] = { |
| 2615 | MockRead("HTTP/1.0 407 Unauthorized\r\n"), |
| 2616 | // Give a couple authenticate options (only the middle one is actually |
| 2617 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 2618 | MockRead("Proxy-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2619 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2620 | MockRead("Proxy-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2621 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2622 | // Large content-length -- won't matter, as connection will be reset. |
| 2623 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2624 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2625 | }; |
| 2626 | |
| 2627 | // After calling trans->RestartWithAuth() the first time, this is the |
| 2628 | // request we should be issuing -- the final header line contains the |
| 2629 | // proxy's credentials. |
| 2630 | MockWrite data_writes2[] = { |
| 2631 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2632 | "Host: www.google.com\r\n" |
| 2633 | "Proxy-Connection: keep-alive\r\n" |
| 2634 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2635 | }; |
| 2636 | |
| 2637 | // Now the proxy server lets the request pass through to origin server. |
| 2638 | // The origin server responds with a 401. |
| 2639 | MockRead data_reads2[] = { |
| 2640 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2641 | // Note: We are using the same realm-name as the proxy server. This is |
| 2642 | // completely valid, as realms are unique across hosts. |
| 2643 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2644 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2645 | MockRead("Content-Length: 2000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2646 | MockRead(false, ERR_FAILED), // Won't be reached. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2647 | }; |
| 2648 | |
| 2649 | // After calling trans->RestartWithAuth() the second time, we should send |
| 2650 | // the credentials for both the proxy and origin server. |
| 2651 | MockWrite data_writes3[] = { |
| 2652 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2653 | "Host: www.google.com\r\n" |
| 2654 | "Proxy-Connection: keep-alive\r\n" |
| 2655 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 2656 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 2657 | }; |
| 2658 | |
| 2659 | // Lastly we get the desired content. |
| 2660 | MockRead data_reads3[] = { |
| 2661 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2662 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2663 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2664 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2665 | }; |
| 2666 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2667 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2668 | data_writes1, arraysize(data_writes1)); |
| 2669 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2670 | data_writes2, arraysize(data_writes2)); |
| 2671 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 2672 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2673 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2674 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2675 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2676 | |
| 2677 | TestCompletionCallback callback1; |
| 2678 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2679 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2680 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2681 | |
| 2682 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2683 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2684 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2685 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2686 | EXPECT_FALSE(response == NULL); |
| 2687 | |
| 2688 | // The password prompt info should have been set in response->auth_challenge. |
| 2689 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2690 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2691 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2692 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2693 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2694 | |
| 2695 | TestCompletionCallback callback2; |
| 2696 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2697 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2698 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2699 | |
| 2700 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2701 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2702 | |
| 2703 | response = trans->GetResponseInfo(); |
| 2704 | EXPECT_FALSE(response == NULL); |
| 2705 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2706 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2707 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2708 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2709 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2710 | |
| 2711 | TestCompletionCallback callback3; |
| 2712 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2713 | rv = trans->RestartWithAuth(kFoo2, kBar2, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2714 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2715 | |
| 2716 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2717 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2718 | |
| 2719 | response = trans->GetResponseInfo(); |
| 2720 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2721 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2722 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2723 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2724 | // For the NTLM implementation using SSPI, we skip the NTLM tests since we |
| 2725 | // can't hook into its internals to cause it to generate predictable NTLM |
| 2726 | // authorization headers. |
| 2727 | #if defined(NTLM_PORTABLE) |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2728 | // The NTLM authentication unit tests were generated by capturing the HTTP |
| 2729 | // requests and responses using Fiddler 2 and inspecting the generated random |
| 2730 | // bytes in the debugger. |
| 2731 | |
| 2732 | // Enter the correct password and authenticate successfully. |
| 2733 | TEST_F(HttpNetworkTransactionTest, NTLMAuth1) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2734 | HttpRequestInfo request; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2735 | request.method = "GET"; |
| 2736 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 2737 | request.load_flags = 0; |
| 2738 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 2739 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1, |
| 2740 | MockGetHostName); |
| 2741 | SessionDependencies session_deps; |
| 2742 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2743 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2744 | MockWrite data_writes1[] = { |
| 2745 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2746 | "Host: 172.22.68.17\r\n" |
| 2747 | "Connection: keep-alive\r\n\r\n"), |
| 2748 | }; |
| 2749 | |
| 2750 | MockRead data_reads1[] = { |
| 2751 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2752 | // Negotiate and NTLM are often requested together. However, we only want |
| 2753 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 2754 | // the header that requests Negotiate for this test. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2755 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2756 | MockRead("Connection: close\r\n"), |
| 2757 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2758 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2759 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2760 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2761 | }; |
| 2762 | |
| 2763 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2764 | // After restarting with a null identity, this is the |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2765 | // request we should be issuing -- the final header line contains a Type |
| 2766 | // 1 message. |
| 2767 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2768 | "Host: 172.22.68.17\r\n" |
| 2769 | "Connection: keep-alive\r\n" |
| 2770 | "Authorization: NTLM " |
| 2771 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2772 | |
| 2773 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2774 | // (the credentials for the origin server). The second request continues |
| 2775 | // on the same connection. |
| 2776 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2777 | "Host: 172.22.68.17\r\n" |
| 2778 | "Connection: keep-alive\r\n" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2779 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2780 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2781 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBVKW" |
| 2782 | "Yma5xzVAAAAAAAAAAAAAAAAAAAAACH+gWcm+YsP9Tqb9zCR3WAeZZX" |
| 2783 | "ahlhx5I=\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2784 | }; |
| 2785 | |
| 2786 | MockRead data_reads2[] = { |
| 2787 | // The origin server responds with a Type 2 message. |
| 2788 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2789 | MockRead("WWW-Authenticate: NTLM " |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2790 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCjGpMpPGlYKkAAAAAAAAAALo" |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2791 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2792 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2793 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2794 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2795 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2796 | "BtAAAAAAA=\r\n"), |
| 2797 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2798 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2799 | MockRead("You are not authorized to view this page\r\n"), |
| 2800 | |
| 2801 | // Lastly we get the desired content. |
| 2802 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2803 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 2804 | MockRead("Content-Length: 13\r\n\r\n"), |
| 2805 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2806 | MockRead(false, OK), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2807 | }; |
| 2808 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2809 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2810 | data_writes1, arraysize(data_writes1)); |
| 2811 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2812 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2813 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2814 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2815 | |
| 2816 | TestCompletionCallback callback1; |
| 2817 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2818 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2819 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2820 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2821 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2822 | |
| 2823 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2824 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2825 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2826 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2827 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2828 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2829 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2830 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2831 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2832 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2833 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2834 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2835 | ASSERT_FALSE(response == NULL); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2836 | |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2837 | // The password prompt info should have been set in |
| 2838 | // response->auth_challenge. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2839 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2840 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2841 | EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2842 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 2843 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 2844 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2845 | TestCompletionCallback callback3; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2846 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2847 | rv = trans->RestartWithAuth(kTestingNTLM, kTestingNTLM, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2848 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2849 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2850 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2851 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2852 | |
| 2853 | response = trans->GetResponseInfo(); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2854 | ASSERT_FALSE(response == NULL); |
| 2855 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2856 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2857 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 2858 | } |
| 2859 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2860 | // Enter a wrong password, and then the correct one. |
| 2861 | TEST_F(HttpNetworkTransactionTest, NTLMAuth2) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2862 | HttpRequestInfo request; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2863 | request.method = "GET"; |
| 2864 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 2865 | request.load_flags = 0; |
| 2866 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 2867 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom2, |
| 2868 | MockGetHostName); |
| 2869 | SessionDependencies session_deps; |
| 2870 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2871 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2872 | MockWrite data_writes1[] = { |
| 2873 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2874 | "Host: 172.22.68.17\r\n" |
| 2875 | "Connection: keep-alive\r\n\r\n"), |
| 2876 | }; |
| 2877 | |
| 2878 | MockRead data_reads1[] = { |
| 2879 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2880 | // Negotiate and NTLM are often requested together. However, we only want |
| 2881 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 2882 | // the header that requests Negotiate for this test. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2883 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2884 | MockRead("Connection: close\r\n"), |
| 2885 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2886 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2887 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2888 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2889 | }; |
| 2890 | |
| 2891 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2892 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2893 | // request we should be issuing -- the final header line contains a Type |
| 2894 | // 1 message. |
| 2895 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2896 | "Host: 172.22.68.17\r\n" |
| 2897 | "Connection: keep-alive\r\n" |
| 2898 | "Authorization: NTLM " |
| 2899 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2900 | |
| 2901 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2902 | // (the credentials for the origin server). The second request continues |
| 2903 | // on the same connection. |
| 2904 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2905 | "Host: 172.22.68.17\r\n" |
| 2906 | "Connection: keep-alive\r\n" |
| 2907 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2908 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2909 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwCWeY" |
| 2910 | "XnSZNwoQAAAAAAAAAAAAAAAAAAAADLa34/phTTKzNTWdub+uyFleOj" |
| 2911 | "4Ww7b7E=\r\n\r\n"), |
| 2912 | }; |
| 2913 | |
| 2914 | MockRead data_reads2[] = { |
| 2915 | // The origin server responds with a Type 2 message. |
| 2916 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2917 | MockRead("WWW-Authenticate: NTLM " |
| 2918 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCbVWUZezVGpAAAAAAAAAAALo" |
| 2919 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2920 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2921 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2922 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2923 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2924 | "BtAAAAAAA=\r\n"), |
| 2925 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2926 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2927 | MockRead("You are not authorized to view this page\r\n"), |
| 2928 | |
| 2929 | // Wrong password. |
| 2930 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2931 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2932 | MockRead("Connection: close\r\n"), |
| 2933 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2934 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2935 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2936 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2937 | }; |
| 2938 | |
| 2939 | MockWrite data_writes3[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2940 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2941 | // request we should be issuing -- the final header line contains a Type |
| 2942 | // 1 message. |
| 2943 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2944 | "Host: 172.22.68.17\r\n" |
| 2945 | "Connection: keep-alive\r\n" |
| 2946 | "Authorization: NTLM " |
| 2947 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2948 | |
| 2949 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2950 | // (the credentials for the origin server). The second request continues |
| 2951 | // on the same connection. |
| 2952 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2953 | "Host: 172.22.68.17\r\n" |
| 2954 | "Connection: keep-alive\r\n" |
| 2955 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2956 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2957 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBO54" |
| 2958 | "dFMVvTHwAAAAAAAAAAAAAAAAAAAACS7sT6Uzw7L0L//WUqlIaVWpbI" |
| 2959 | "+4MUm7c=\r\n\r\n"), |
| 2960 | }; |
| 2961 | |
| 2962 | MockRead data_reads3[] = { |
| 2963 | // The origin server responds with a Type 2 message. |
| 2964 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2965 | MockRead("WWW-Authenticate: NTLM " |
| 2966 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCL24VN8dgOR8AAAAAAAAAALo" |
| 2967 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2968 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2969 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2970 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2971 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2972 | "BtAAAAAAA=\r\n"), |
| 2973 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2974 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2975 | MockRead("You are not authorized to view this page\r\n"), |
| 2976 | |
| 2977 | // Lastly we get the desired content. |
| 2978 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2979 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 2980 | MockRead("Content-Length: 13\r\n\r\n"), |
| 2981 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2982 | MockRead(false, OK), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2983 | }; |
| 2984 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2985 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2986 | data_writes1, arraysize(data_writes1)); |
| 2987 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2988 | data_writes2, arraysize(data_writes2)); |
| 2989 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 2990 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2991 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2992 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2993 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2994 | |
| 2995 | TestCompletionCallback callback1; |
| 2996 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2997 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2998 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2999 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3000 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3001 | |
| 3002 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3003 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3004 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3005 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3006 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3007 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3008 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3009 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3010 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3011 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3012 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3013 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3014 | EXPECT_FALSE(response == NULL); |
| 3015 | |
| 3016 | // The password prompt info should have been set in response->auth_challenge. |
| 3017 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3018 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3019 | EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3020 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 3021 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 3022 | |
| 3023 | TestCompletionCallback callback3; |
| 3024 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3025 | // Enter the wrong password. |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3026 | rv = trans->RestartWithAuth(kTestingNTLM, kWrongPassword, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3027 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3028 | |
| 3029 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3030 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3031 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3032 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3033 | TestCompletionCallback callback4; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3034 | rv = trans->RestartWithAuth(string16(), string16(), &callback4); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3035 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3036 | rv = callback4.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3037 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3038 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3039 | |
| 3040 | response = trans->GetResponseInfo(); |
| 3041 | EXPECT_FALSE(response == NULL); |
| 3042 | |
| 3043 | // The password prompt info should have been set in response->auth_challenge. |
| 3044 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3045 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3046 | EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3047 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 3048 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 3049 | |
| 3050 | TestCompletionCallback callback5; |
| 3051 | |
| 3052 | // Now enter the right password. |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3053 | rv = trans->RestartWithAuth(kTestingNTLM, kTestingNTLM, &callback5); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3054 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3055 | |
| 3056 | rv = callback5.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3057 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3058 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3059 | response = trans->GetResponseInfo(); |
| 3060 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3061 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 3062 | } |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3063 | #endif // NTLM_PORTABLE |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3064 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3065 | // Test reading a server response which has only headers, and no body. |
| 3066 | // After some maximum number of bytes is consumed, the transaction should |
| 3067 | // fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
| 3068 | TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3069 | HttpRequestInfo request; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3070 | request.method = "GET"; |
| 3071 | request.url = GURL("http://www.google.com/"); |
| 3072 | request.load_flags = 0; |
| 3073 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3074 | SessionDependencies session_deps; |
| 3075 | scoped_ptr<HttpTransaction> trans( |
| 3076 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3077 | |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 3078 | // Respond with 300 kb of headers (we should fail after 256 kb). |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 3079 | std::string large_headers_string; |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 3080 | FillLargeHeadersString(&large_headers_string, 300 * 1024); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3081 | |
| 3082 | MockRead data_reads[] = { |
| 3083 | MockRead("HTTP/1.0 200 OK\r\n"), |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 3084 | MockRead(true, large_headers_string.data(), large_headers_string.size()), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3085 | MockRead("\r\nBODY"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3086 | MockRead(false, OK), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3087 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3088 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3089 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3090 | |
| 3091 | TestCompletionCallback callback; |
| 3092 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3093 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3094 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3095 | |
| 3096 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3097 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3098 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3099 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3100 | EXPECT_TRUE(response == NULL); |
| 3101 | } |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3102 | |
| 3103 | // Make sure that we don't try to reuse a TCPClientSocket when failing to |
| 3104 | // establish tunnel. |
| 3105 | // http://code.google.com/p/chromium/issues/detail?id=3772 |
| 3106 | TEST_F(HttpNetworkTransactionTest, DontRecycleTCPSocketForSSLTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3107 | HttpRequestInfo request; |
| 3108 | request.method = "GET"; |
| 3109 | request.url = GURL("https://www.google.com/"); |
| 3110 | request.load_flags = 0; |
| 3111 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3112 | // Configure against proxy server "myproxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 3113 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 3114 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3115 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3116 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3117 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3118 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3119 | // Since we have proxy, should try to establish tunnel. |
| 3120 | MockWrite data_writes1[] = { |
| 3121 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 3122 | "Host: www.google.com\r\n" |
| 3123 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3124 | }; |
| 3125 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 3126 | // The proxy responds to the connect with a 404, using a persistent |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3127 | // connection. Usually a proxy would return 501 (not implemented), |
| 3128 | // or 200 (tunnel established). |
| 3129 | MockRead data_reads1[] = { |
| 3130 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 3131 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3132 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3133 | }; |
| 3134 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3135 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3136 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3137 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3138 | |
| 3139 | TestCompletionCallback callback1; |
| 3140 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3141 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3142 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3143 | |
| 3144 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3145 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3146 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3147 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 3148 | EXPECT_TRUE(response == NULL); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3149 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3150 | // Empty the current queue. This is necessary because idle sockets are |
| 3151 | // added to the connection pool asynchronously with a PostTask. |
| 3152 | MessageLoop::current()->RunAllPending(); |
| 3153 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3154 | // We now check to make sure the TCPClientSocket was not added back to |
| 3155 | // the pool. |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 3156 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3157 | trans.reset(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3158 | MessageLoop::current()->RunAllPending(); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3159 | // Make sure that the socket didn't get recycled after calling the destructor. |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 3160 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3161 | } |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3162 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3163 | // Make sure that we recycle a socket after reading all of the response body. |
| 3164 | TEST_F(HttpNetworkTransactionTest, RecycleSocket) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3165 | HttpRequestInfo request; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3166 | request.method = "GET"; |
| 3167 | request.url = GURL("http://www.google.com/"); |
| 3168 | request.load_flags = 0; |
| 3169 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3170 | SessionDependencies session_deps; |
| 3171 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3172 | |
| 3173 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3174 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3175 | MockRead data_reads[] = { |
| 3176 | // A part of the response body is received with the response headers. |
| 3177 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 3178 | // The rest of the response body is received in two parts. |
| 3179 | MockRead("lo"), |
| 3180 | MockRead(" world"), |
| 3181 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3182 | MockRead(false, OK), |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3183 | }; |
| 3184 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3185 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3186 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3187 | |
| 3188 | TestCompletionCallback callback; |
| 3189 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3190 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3191 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3192 | |
| 3193 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3194 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3195 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3196 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3197 | EXPECT_TRUE(response != NULL); |
| 3198 | |
| 3199 | EXPECT_TRUE(response->headers != NULL); |
| 3200 | std::string status_line = response->headers->GetStatusLine(); |
| 3201 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 3202 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 3203 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3204 | |
| 3205 | std::string response_data; |
| 3206 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3207 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3208 | EXPECT_EQ("hello world", response_data); |
| 3209 | |
| 3210 | // Empty the current queue. This is necessary because idle sockets are |
| 3211 | // added to the connection pool asynchronously with a PostTask. |
| 3212 | MessageLoop::current()->RunAllPending(); |
| 3213 | |
| 3214 | // We now check to make sure the socket was added back to the pool. |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 3215 | EXPECT_EQ(1, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3216 | } |
| 3217 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3218 | // Make sure that we recycle a SSL socket after reading all of the response |
| 3219 | // body. |
| 3220 | TEST_F(HttpNetworkTransactionTest, RecycleSSLSocket) { |
| 3221 | SessionDependencies session_deps; |
| 3222 | HttpRequestInfo request; |
| 3223 | request.method = "GET"; |
| 3224 | request.url = GURL("https://www.google.com/"); |
| 3225 | request.load_flags = 0; |
| 3226 | |
| 3227 | MockWrite data_writes[] = { |
| 3228 | MockWrite("GET / HTTP/1.1\r\n" |
| 3229 | "Host: www.google.com\r\n" |
| 3230 | "Connection: keep-alive\r\n\r\n"), |
| 3231 | }; |
| 3232 | |
| 3233 | MockRead data_reads[] = { |
| 3234 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3235 | MockRead("Content-Length: 11\r\n\r\n"), |
| 3236 | MockRead("hello world"), |
| 3237 | MockRead(false, OK), |
| 3238 | }; |
| 3239 | |
| 3240 | SSLSocketDataProvider ssl(true, OK); |
| 3241 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 3242 | |
| 3243 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3244 | data_writes, arraysize(data_writes)); |
| 3245 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3246 | |
| 3247 | TestCompletionCallback callback; |
| 3248 | |
| 3249 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3250 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3251 | |
| 3252 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 3253 | |
| 3254 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3255 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3256 | |
| 3257 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3258 | ASSERT_TRUE(response != NULL); |
| 3259 | ASSERT_TRUE(response->headers != NULL); |
| 3260 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3261 | |
| 3262 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
| 3263 | |
| 3264 | std::string response_data; |
| 3265 | rv = ReadTransaction(trans.get(), &response_data); |
| 3266 | EXPECT_EQ(OK, rv); |
| 3267 | EXPECT_EQ("hello world", response_data); |
| 3268 | |
| 3269 | // Empty the current queue. This is necessary because idle sockets are |
| 3270 | // added to the connection pool asynchronously with a PostTask. |
| 3271 | MessageLoop::current()->RunAllPending(); |
| 3272 | |
| 3273 | // We now check to make sure the socket was added back to the pool. |
| 3274 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 3275 | } |
| 3276 | |
| 3277 | // Grab a SSL socket, use it, and put it back into the pool. Then, reuse it |
| 3278 | // from the pool and make sure that we recover okay. |
| 3279 | TEST_F(HttpNetworkTransactionTest, RecycleDeadSSLSocket) { |
| 3280 | SessionDependencies session_deps; |
| 3281 | HttpRequestInfo request; |
| 3282 | request.method = "GET"; |
| 3283 | request.url = GURL("https://www.google.com/"); |
| 3284 | request.load_flags = 0; |
| 3285 | |
| 3286 | MockWrite data_writes[] = { |
| 3287 | MockWrite("GET / HTTP/1.1\r\n" |
| 3288 | "Host: www.google.com\r\n" |
| 3289 | "Connection: keep-alive\r\n\r\n"), |
| 3290 | MockWrite("GET / HTTP/1.1\r\n" |
| 3291 | "Host: www.google.com\r\n" |
| 3292 | "Connection: keep-alive\r\n\r\n"), |
| 3293 | }; |
| 3294 | |
| 3295 | MockRead data_reads[] = { |
| 3296 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3297 | MockRead("Content-Length: 11\r\n\r\n"), |
| 3298 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 3299 | MockRead("hello world"), |
| 3300 | MockRead(true, 0, 0) // EOF |
| 3301 | }; |
| 3302 | |
| 3303 | SSLSocketDataProvider ssl(true, OK); |
| 3304 | SSLSocketDataProvider ssl2(true, OK); |
| 3305 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 3306 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 3307 | |
| 3308 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3309 | data_writes, arraysize(data_writes)); |
| 3310 | StaticSocketDataProvider data2(data_reads, arraysize(data_reads), |
| 3311 | data_writes, arraysize(data_writes)); |
| 3312 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3313 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3314 | |
| 3315 | TestCompletionCallback callback; |
| 3316 | |
| 3317 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3318 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3319 | |
| 3320 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 3321 | |
| 3322 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3323 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3324 | |
| 3325 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3326 | ASSERT_TRUE(response != NULL); |
| 3327 | ASSERT_TRUE(response->headers != NULL); |
| 3328 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3329 | |
| 3330 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
| 3331 | |
| 3332 | std::string response_data; |
| 3333 | rv = ReadTransaction(trans.get(), &response_data); |
| 3334 | EXPECT_EQ(OK, rv); |
| 3335 | EXPECT_EQ("hello world", response_data); |
| 3336 | |
| 3337 | // Empty the current queue. This is necessary because idle sockets are |
| 3338 | // added to the connection pool asynchronously with a PostTask. |
| 3339 | MessageLoop::current()->RunAllPending(); |
| 3340 | |
| 3341 | // We now check to make sure the socket was added back to the pool. |
| 3342 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 3343 | |
| 3344 | // Now start the second transaction, which should reuse the previous socket. |
| 3345 | |
| 3346 | trans.reset(new HttpNetworkTransaction(session)); |
| 3347 | |
| 3348 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 3349 | |
| 3350 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3351 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3352 | |
| 3353 | response = trans->GetResponseInfo(); |
| 3354 | ASSERT_TRUE(response != NULL); |
| 3355 | ASSERT_TRUE(response->headers != NULL); |
| 3356 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3357 | |
| 3358 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
| 3359 | |
| 3360 | rv = ReadTransaction(trans.get(), &response_data); |
| 3361 | EXPECT_EQ(OK, rv); |
| 3362 | EXPECT_EQ("hello world", response_data); |
| 3363 | |
| 3364 | // Empty the current queue. This is necessary because idle sockets are |
| 3365 | // added to the connection pool asynchronously with a PostTask. |
| 3366 | MessageLoop::current()->RunAllPending(); |
| 3367 | |
| 3368 | // We now check to make sure the socket was added back to the pool. |
| 3369 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 3370 | } |
| 3371 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3372 | // Make sure that we recycle a socket after a zero-length response. |
| 3373 | // http://crbug.com/9880 |
| 3374 | TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3375 | HttpRequestInfo request; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3376 | request.method = "GET"; |
| 3377 | request.url = GURL("http://www.google.com/csi?v=3&s=web&action=&" |
| 3378 | "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" |
| 3379 | "e=17259,18167,19592,19773,19981,20133,20173,20233&" |
| 3380 | "rt=prt.2642,ol.2649,xjs.2951"); |
| 3381 | request.load_flags = 0; |
| 3382 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3383 | SessionDependencies session_deps; |
| 3384 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3385 | |
| 3386 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3387 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3388 | MockRead data_reads[] = { |
| 3389 | MockRead("HTTP/1.1 204 No Content\r\n" |
| 3390 | "Content-Length: 0\r\n" |
| 3391 | "Content-Type: text/html\r\n\r\n"), |
| 3392 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3393 | MockRead(false, OK), |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3394 | }; |
| 3395 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3396 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3397 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3398 | |
| 3399 | TestCompletionCallback callback; |
| 3400 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3401 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3402 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3403 | |
| 3404 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3405 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3406 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3407 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3408 | EXPECT_TRUE(response != NULL); |
| 3409 | |
| 3410 | EXPECT_TRUE(response->headers != NULL); |
| 3411 | std::string status_line = response->headers->GetStatusLine(); |
| 3412 | EXPECT_EQ("HTTP/1.1 204 No Content", status_line); |
| 3413 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 3414 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3415 | |
| 3416 | std::string response_data; |
| 3417 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3418 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3419 | EXPECT_EQ("", response_data); |
| 3420 | |
| 3421 | // Empty the current queue. This is necessary because idle sockets are |
| 3422 | // added to the connection pool asynchronously with a PostTask. |
| 3423 | MessageLoop::current()->RunAllPending(); |
| 3424 | |
| 3425 | // We now check to make sure the socket was added back to the pool. |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 3426 | EXPECT_EQ(1, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3427 | } |
| 3428 | |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3429 | TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3430 | HttpRequestInfo request[2]; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3431 | // Transaction 1: a GET request that succeeds. The socket is recycled |
| 3432 | // after use. |
| 3433 | request[0].method = "GET"; |
| 3434 | request[0].url = GURL("http://www.google.com/"); |
| 3435 | request[0].load_flags = 0; |
| 3436 | // Transaction 2: a POST request. Reuses the socket kept alive from |
| 3437 | // transaction 1. The first attempts fails when writing the POST data. |
| 3438 | // This causes the transaction to retry with a new socket. The second |
| 3439 | // attempt succeeds. |
| 3440 | request[1].method = "POST"; |
| 3441 | request[1].url = GURL("http://www.google.com/login.cgi"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3442 | request[1].upload_data = new UploadData; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3443 | request[1].upload_data->AppendBytes("foo", 3); |
| 3444 | request[1].load_flags = 0; |
| 3445 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3446 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 3447 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3448 | |
| 3449 | // The first socket is used for transaction 1 and the first attempt of |
| 3450 | // transaction 2. |
| 3451 | |
| 3452 | // The response of transaction 1. |
| 3453 | MockRead data_reads1[] = { |
| 3454 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"), |
| 3455 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3456 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3457 | }; |
| 3458 | // The mock write results of transaction 1 and the first attempt of |
| 3459 | // transaction 2. |
| 3460 | MockWrite data_writes1[] = { |
| 3461 | MockWrite(false, 64), // GET |
| 3462 | MockWrite(false, 93), // POST |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3463 | MockWrite(false, ERR_CONNECTION_ABORTED), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3464 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3465 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3466 | data_writes1, arraysize(data_writes1)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3467 | |
| 3468 | // The second socket is used for the second attempt of transaction 2. |
| 3469 | |
| 3470 | // The response of transaction 2. |
| 3471 | MockRead data_reads2[] = { |
| 3472 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\n"), |
| 3473 | MockRead("welcome"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3474 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3475 | }; |
| 3476 | // The mock write results of the second attempt of transaction 2. |
| 3477 | MockWrite data_writes2[] = { |
| 3478 | MockWrite(false, 93), // POST |
| 3479 | MockWrite(false, 3), // POST data |
| 3480 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3481 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3482 | data_writes2, arraysize(data_writes2)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3483 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3484 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3485 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3486 | |
| 3487 | const char* kExpectedResponseData[] = { |
| 3488 | "hello world", "welcome" |
| 3489 | }; |
| 3490 | |
| 3491 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3492 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3493 | new HttpNetworkTransaction(session)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3494 | |
| 3495 | TestCompletionCallback callback; |
| 3496 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3497 | int rv = trans->Start(&request[i], &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3498 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3499 | |
| 3500 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3501 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3502 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3503 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3504 | EXPECT_TRUE(response != NULL); |
| 3505 | |
| 3506 | EXPECT_TRUE(response->headers != NULL); |
| 3507 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3508 | |
| 3509 | std::string response_data; |
| 3510 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3511 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3512 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 3513 | } |
| 3514 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3515 | |
| 3516 | // Test the request-challenge-retry sequence for basic auth when there is |
| 3517 | // an identity in the URL. The request should be sent as normal, but when |
| 3518 | // it fails the identity from the URL is used to answer the challenge. |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3519 | TEST_F(HttpNetworkTransactionTest, AuthIdentityInURL) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3520 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3521 | request.method = "GET"; |
| 3522 | // Note: the URL has a username:password in it. |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3523 | request.url = GURL("http://foo:b@[email protected]/"); |
| 3524 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3525 | SessionDependencies session_deps; |
| 3526 | scoped_ptr<HttpTransaction> trans( |
| 3527 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3528 | |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3529 | // The password contains an escaped character -- for this test to pass it |
| 3530 | // will need to be unescaped by HttpNetworkTransaction. |
| 3531 | EXPECT_EQ("b%40r", request.url.password()); |
| 3532 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3533 | request.load_flags = LOAD_NORMAL; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3534 | |
| 3535 | MockWrite data_writes1[] = { |
| 3536 | MockWrite("GET / HTTP/1.1\r\n" |
| 3537 | "Host: www.google.com\r\n" |
| 3538 | "Connection: keep-alive\r\n\r\n"), |
| 3539 | }; |
| 3540 | |
| 3541 | MockRead data_reads1[] = { |
| 3542 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3543 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3544 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3545 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3546 | }; |
| 3547 | |
| 3548 | // After the challenge above, the transaction will be restarted using the |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3549 | // identity from the url (foo, b@r) to answer the challenge. |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3550 | MockWrite data_writes2[] = { |
| 3551 | MockWrite("GET / HTTP/1.1\r\n" |
| 3552 | "Host: www.google.com\r\n" |
| 3553 | "Connection: keep-alive\r\n" |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3554 | "Authorization: Basic Zm9vOmJAcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3555 | }; |
| 3556 | |
| 3557 | MockRead data_reads2[] = { |
| 3558 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3559 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3560 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3561 | }; |
| 3562 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3563 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3564 | data_writes1, arraysize(data_writes1)); |
| 3565 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3566 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3567 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3568 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3569 | |
| 3570 | TestCompletionCallback callback1; |
| 3571 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3572 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3573 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3574 | |
| 3575 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3576 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3577 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3578 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3579 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3580 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3581 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3582 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3583 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3584 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3585 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3586 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3587 | EXPECT_FALSE(response == NULL); |
| 3588 | |
| 3589 | // There is no challenge info, since the identity in URL worked. |
| 3590 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3591 | |
| 3592 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3593 | |
| 3594 | // Empty the current queue. |
| 3595 | MessageLoop::current()->RunAllPending(); |
| 3596 | } |
| 3597 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3598 | // Test the request-challenge-retry sequence for basic auth when there is |
| 3599 | // an incorrect identity in the URL. The identity from the URL should be used |
| 3600 | // only once. |
| 3601 | TEST_F(HttpNetworkTransactionTest, WrongAuthIdentityInURL) { |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3602 | HttpRequestInfo request; |
| 3603 | request.method = "GET"; |
| 3604 | // Note: the URL has a username:password in it. The password "baz" is |
| 3605 | // wrong (should be "bar"). |
| 3606 | request.url = GURL("http://foo:[email protected]/"); |
| 3607 | |
| 3608 | request.load_flags = LOAD_NORMAL; |
| 3609 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3610 | SessionDependencies session_deps; |
| 3611 | scoped_ptr<HttpTransaction> trans( |
| 3612 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3613 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3614 | MockWrite data_writes1[] = { |
| 3615 | MockWrite("GET / HTTP/1.1\r\n" |
| 3616 | "Host: www.google.com\r\n" |
| 3617 | "Connection: keep-alive\r\n\r\n"), |
| 3618 | }; |
| 3619 | |
| 3620 | MockRead data_reads1[] = { |
| 3621 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3622 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3623 | MockRead("Content-Length: 10\r\n\r\n"), |
| 3624 | MockRead(false, ERR_FAILED), |
| 3625 | }; |
| 3626 | |
| 3627 | // After the challenge above, the transaction will be restarted using the |
| 3628 | // identity from the url (foo, baz) to answer the challenge. |
| 3629 | MockWrite data_writes2[] = { |
| 3630 | MockWrite("GET / HTTP/1.1\r\n" |
| 3631 | "Host: www.google.com\r\n" |
| 3632 | "Connection: keep-alive\r\n" |
| 3633 | "Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 3634 | }; |
| 3635 | |
| 3636 | MockRead data_reads2[] = { |
| 3637 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3638 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3639 | MockRead("Content-Length: 10\r\n\r\n"), |
| 3640 | MockRead(false, ERR_FAILED), |
| 3641 | }; |
| 3642 | |
| 3643 | // After the challenge above, the transaction will be restarted using the |
| 3644 | // identity supplied by the user (foo, bar) to answer the challenge. |
| 3645 | MockWrite data_writes3[] = { |
| 3646 | MockWrite("GET / HTTP/1.1\r\n" |
| 3647 | "Host: www.google.com\r\n" |
| 3648 | "Connection: keep-alive\r\n" |
| 3649 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3650 | }; |
| 3651 | |
| 3652 | MockRead data_reads3[] = { |
| 3653 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3654 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3655 | MockRead(false, OK), |
| 3656 | }; |
| 3657 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3658 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3659 | data_writes1, arraysize(data_writes1)); |
| 3660 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3661 | data_writes2, arraysize(data_writes2)); |
| 3662 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 3663 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3664 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3665 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3666 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3667 | |
| 3668 | TestCompletionCallback callback1; |
| 3669 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3670 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3671 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3672 | |
| 3673 | rv = callback1.WaitForResult(); |
| 3674 | EXPECT_EQ(OK, rv); |
| 3675 | |
| 3676 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3677 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3678 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3679 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3680 | rv = callback2.WaitForResult(); |
| 3681 | EXPECT_EQ(OK, rv); |
| 3682 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3683 | |
| 3684 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3685 | EXPECT_FALSE(response == NULL); |
| 3686 | // The password prompt info should have been set in response->auth_challenge. |
| 3687 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3688 | |
| 3689 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 3690 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 3691 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3692 | |
| 3693 | TestCompletionCallback callback3; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3694 | rv = trans->RestartWithAuth(kFoo, kBar, &callback3); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3695 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3696 | rv = callback3.WaitForResult(); |
| 3697 | EXPECT_EQ(OK, rv); |
| 3698 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3699 | |
| 3700 | response = trans->GetResponseInfo(); |
| 3701 | EXPECT_FALSE(response == NULL); |
| 3702 | |
| 3703 | // There is no challenge info, since the identity worked. |
| 3704 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3705 | |
| 3706 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3707 | |
| 3708 | // Empty the current queue. |
| 3709 | MessageLoop::current()->RunAllPending(); |
| 3710 | } |
| 3711 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3712 | // Test that previously tried username/passwords for a realm get re-used. |
| 3713 | TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3714 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 3715 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3716 | |
| 3717 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 3718 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3719 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3720 | request.method = "GET"; |
| 3721 | request.url = GURL("http://www.google.com/x/y/z"); |
| 3722 | request.load_flags = 0; |
| 3723 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3724 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3725 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3726 | MockWrite data_writes1[] = { |
| 3727 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3728 | "Host: www.google.com\r\n" |
| 3729 | "Connection: keep-alive\r\n\r\n"), |
| 3730 | }; |
| 3731 | |
| 3732 | MockRead data_reads1[] = { |
| 3733 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3734 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3735 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3736 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3737 | }; |
| 3738 | |
| 3739 | // Resend with authorization (username=foo, password=bar) |
| 3740 | MockWrite data_writes2[] = { |
| 3741 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3742 | "Host: www.google.com\r\n" |
| 3743 | "Connection: keep-alive\r\n" |
| 3744 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3745 | }; |
| 3746 | |
| 3747 | // Sever accepts the authorization. |
| 3748 | MockRead data_reads2[] = { |
| 3749 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3750 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3751 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3752 | }; |
| 3753 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3754 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3755 | data_writes1, arraysize(data_writes1)); |
| 3756 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3757 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3758 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3759 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3760 | |
| 3761 | TestCompletionCallback callback1; |
| 3762 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3763 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3764 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3765 | |
| 3766 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3767 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3768 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3769 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3770 | EXPECT_FALSE(response == NULL); |
| 3771 | |
| 3772 | // The password prompt info should have been set in |
| 3773 | // response->auth_challenge. |
| 3774 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3775 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3776 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3777 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 3778 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3779 | |
| 3780 | TestCompletionCallback callback2; |
| 3781 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3782 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3783 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3784 | |
| 3785 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3786 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3787 | |
| 3788 | response = trans->GetResponseInfo(); |
| 3789 | EXPECT_FALSE(response == NULL); |
| 3790 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3791 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3792 | } |
| 3793 | |
| 3794 | // ------------------------------------------------------------------------ |
| 3795 | |
| 3796 | // Transaction 2: authenticate (foo2, bar2) on MyRealm2 |
| 3797 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3798 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3799 | request.method = "GET"; |
| 3800 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 3801 | // protection space as MyRealm1. |
| 3802 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 3803 | request.load_flags = 0; |
| 3804 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3805 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3806 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3807 | MockWrite data_writes1[] = { |
| 3808 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3809 | "Host: www.google.com\r\n" |
| 3810 | "Connection: keep-alive\r\n" |
| 3811 | // Send preemptive authorization for MyRealm1 |
| 3812 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3813 | }; |
| 3814 | |
| 3815 | // The server didn't like the preemptive authorization, and |
| 3816 | // challenges us for a different realm (MyRealm2). |
| 3817 | MockRead data_reads1[] = { |
| 3818 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3819 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm2\"\r\n"), |
| 3820 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3821 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3822 | }; |
| 3823 | |
| 3824 | // Resend with authorization for MyRealm2 (username=foo2, password=bar2) |
| 3825 | MockWrite data_writes2[] = { |
| 3826 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3827 | "Host: www.google.com\r\n" |
| 3828 | "Connection: keep-alive\r\n" |
| 3829 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 3830 | }; |
| 3831 | |
| 3832 | // Sever accepts the authorization. |
| 3833 | MockRead data_reads2[] = { |
| 3834 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3835 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3836 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3837 | }; |
| 3838 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3839 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3840 | data_writes1, arraysize(data_writes1)); |
| 3841 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3842 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3843 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3844 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3845 | |
| 3846 | TestCompletionCallback callback1; |
| 3847 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3848 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3849 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3850 | |
| 3851 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3852 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3853 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3854 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3855 | EXPECT_FALSE(response == NULL); |
| 3856 | |
| 3857 | // The password prompt info should have been set in |
| 3858 | // response->auth_challenge. |
| 3859 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3860 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3861 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3862 | EXPECT_EQ(L"MyRealm2", response->auth_challenge->realm); |
| 3863 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3864 | |
| 3865 | TestCompletionCallback callback2; |
| 3866 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3867 | rv = trans->RestartWithAuth(kFoo2, kBar2, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3868 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3869 | |
| 3870 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3871 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3872 | |
| 3873 | response = trans->GetResponseInfo(); |
| 3874 | EXPECT_FALSE(response == NULL); |
| 3875 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3876 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3877 | } |
| 3878 | |
| 3879 | // ------------------------------------------------------------------------ |
| 3880 | |
| 3881 | // Transaction 3: Resend a request in MyRealm's protection space -- |
| 3882 | // succeed with preemptive authorization. |
| 3883 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3884 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3885 | request.method = "GET"; |
| 3886 | request.url = GURL("http://www.google.com/x/y/z2"); |
| 3887 | request.load_flags = 0; |
| 3888 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3889 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3890 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3891 | MockWrite data_writes1[] = { |
| 3892 | MockWrite("GET /x/y/z2 HTTP/1.1\r\n" |
| 3893 | "Host: www.google.com\r\n" |
| 3894 | "Connection: keep-alive\r\n" |
| 3895 | // The authorization for MyRealm1 gets sent preemptively |
| 3896 | // (since the url is in the same protection space) |
| 3897 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3898 | }; |
| 3899 | |
| 3900 | // Sever accepts the preemptive authorization |
| 3901 | MockRead data_reads1[] = { |
| 3902 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3903 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3904 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3905 | }; |
| 3906 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3907 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3908 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3909 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3910 | |
| 3911 | TestCompletionCallback callback1; |
| 3912 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3913 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3914 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3915 | |
| 3916 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3917 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3918 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3919 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3920 | EXPECT_FALSE(response == NULL); |
| 3921 | |
| 3922 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3923 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3924 | } |
| 3925 | |
| 3926 | // ------------------------------------------------------------------------ |
| 3927 | |
| 3928 | // Transaction 4: request another URL in MyRealm (however the |
| 3929 | // url is not known to belong to the protection space, so no pre-auth). |
| 3930 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3931 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3932 | request.method = "GET"; |
| 3933 | request.url = GURL("http://www.google.com/x/1"); |
| 3934 | request.load_flags = 0; |
| 3935 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 3936 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3937 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3938 | MockWrite data_writes1[] = { |
| 3939 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 3940 | "Host: www.google.com\r\n" |
| 3941 | "Connection: keep-alive\r\n\r\n"), |
| 3942 | }; |
| 3943 | |
| 3944 | MockRead data_reads1[] = { |
| 3945 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3946 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3947 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3948 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3949 | }; |
| 3950 | |
| 3951 | // Resend with authorization from MyRealm's cache. |
| 3952 | MockWrite data_writes2[] = { |
| 3953 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 3954 | "Host: www.google.com\r\n" |
| 3955 | "Connection: keep-alive\r\n" |
| 3956 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3957 | }; |
| 3958 | |
| 3959 | // Sever accepts the authorization. |
| 3960 | MockRead data_reads2[] = { |
| 3961 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3962 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3963 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3964 | }; |
| 3965 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3966 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3967 | data_writes1, arraysize(data_writes1)); |
| 3968 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3969 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3970 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3971 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3972 | |
| 3973 | TestCompletionCallback callback1; |
| 3974 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3975 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3976 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3977 | |
| 3978 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3979 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3980 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3981 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3982 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3983 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3984 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3985 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3986 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3987 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3988 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3989 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3990 | EXPECT_FALSE(response == NULL); |
| 3991 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3992 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3993 | } |
| 3994 | |
| 3995 | // ------------------------------------------------------------------------ |
| 3996 | |
| 3997 | // Transaction 5: request a URL in MyRealm, but the server rejects the |
| 3998 | // cached identity. Should invalidate and re-prompt. |
| 3999 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4000 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4001 | request.method = "GET"; |
| 4002 | request.url = GURL("http://www.google.com/p/q/t"); |
| 4003 | request.load_flags = 0; |
| 4004 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4005 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4006 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4007 | MockWrite data_writes1[] = { |
| 4008 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 4009 | "Host: www.google.com\r\n" |
| 4010 | "Connection: keep-alive\r\n\r\n"), |
| 4011 | }; |
| 4012 | |
| 4013 | MockRead data_reads1[] = { |
| 4014 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4015 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4016 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4017 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4018 | }; |
| 4019 | |
| 4020 | // Resend with authorization from cache for MyRealm. |
| 4021 | MockWrite data_writes2[] = { |
| 4022 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 4023 | "Host: www.google.com\r\n" |
| 4024 | "Connection: keep-alive\r\n" |
| 4025 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4026 | }; |
| 4027 | |
| 4028 | // Sever rejects the authorization. |
| 4029 | MockRead data_reads2[] = { |
| 4030 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4031 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4032 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4033 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4034 | }; |
| 4035 | |
| 4036 | // At this point we should prompt for new credentials for MyRealm. |
| 4037 | // Restart with username=foo3, password=foo4. |
| 4038 | MockWrite data_writes3[] = { |
| 4039 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 4040 | "Host: www.google.com\r\n" |
| 4041 | "Connection: keep-alive\r\n" |
| 4042 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 4043 | }; |
| 4044 | |
| 4045 | // Sever accepts the authorization. |
| 4046 | MockRead data_reads3[] = { |
| 4047 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4048 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4049 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4050 | }; |
| 4051 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4052 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4053 | data_writes1, arraysize(data_writes1)); |
| 4054 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 4055 | data_writes2, arraysize(data_writes2)); |
| 4056 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 4057 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4058 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4059 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 4060 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4061 | |
| 4062 | TestCompletionCallback callback1; |
| 4063 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4064 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4065 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4066 | |
| 4067 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4068 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4069 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4070 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 4071 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 4072 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4073 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4074 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4075 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4076 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 4077 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4078 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4079 | EXPECT_FALSE(response == NULL); |
| 4080 | |
| 4081 | // The password prompt info should have been set in |
| 4082 | // response->auth_challenge. |
| 4083 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 4084 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 4085 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4086 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 4087 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 4088 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4089 | TestCompletionCallback callback3; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4090 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 4091 | rv = trans->RestartWithAuth(kFoo3, kBar3, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4092 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4093 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4094 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4095 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4096 | |
| 4097 | response = trans->GetResponseInfo(); |
| 4098 | EXPECT_FALSE(response == NULL); |
| 4099 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4100 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4101 | } |
| 4102 | } |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4103 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4104 | // Tests that nonce count increments when multiple auth attempts |
| 4105 | // are started with the same nonce. |
| 4106 | TEST_F(HttpNetworkTransactionTest, DigestPreAuthNonceCount) { |
| 4107 | SessionDependencies session_deps; |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 4108 | HttpAuthHandlerDigest::Factory* digest_factory = |
| 4109 | new HttpAuthHandlerDigest::Factory(); |
| 4110 | HttpAuthHandlerDigest::FixedNonceGenerator* nonce_generator = |
| 4111 | new HttpAuthHandlerDigest::FixedNonceGenerator("0123456789abcdef"); |
| 4112 | digest_factory->set_nonce_generator(nonce_generator); |
| 4113 | session_deps.http_auth_handler_factory.reset(digest_factory); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4114 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4115 | |
| 4116 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 4117 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4118 | HttpRequestInfo request; |
| 4119 | request.method = "GET"; |
| 4120 | request.url = GURL("http://www.google.com/x/y/z"); |
| 4121 | request.load_flags = 0; |
| 4122 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4123 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4124 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4125 | MockWrite data_writes1[] = { |
| 4126 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 4127 | "Host: www.google.com\r\n" |
| 4128 | "Connection: keep-alive\r\n\r\n"), |
| 4129 | }; |
| 4130 | |
| 4131 | MockRead data_reads1[] = { |
| 4132 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4133 | MockRead("WWW-Authenticate: Digest realm=\"digestive\", nonce=\"OU812\", " |
| 4134 | "algorithm=MD5, qop=\"auth\"\r\n\r\n"), |
| 4135 | MockRead(false, OK), |
| 4136 | }; |
| 4137 | |
| 4138 | // Resend with authorization (username=foo, password=bar) |
| 4139 | MockWrite data_writes2[] = { |
| 4140 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 4141 | "Host: www.google.com\r\n" |
| 4142 | "Connection: keep-alive\r\n" |
| 4143 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 4144 | "nonce=\"OU812\", uri=\"/x/y/z\", algorithm=MD5, " |
| 4145 | "response=\"03ffbcd30add722589c1de345d7a927f\", qop=auth, " |
| 4146 | "nc=00000001, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 4147 | }; |
| 4148 | |
| 4149 | // Sever accepts the authorization. |
| 4150 | MockRead data_reads2[] = { |
| 4151 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4152 | MockRead(false, OK), |
| 4153 | }; |
| 4154 | |
| 4155 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4156 | data_writes1, arraysize(data_writes1)); |
| 4157 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 4158 | data_writes2, arraysize(data_writes2)); |
| 4159 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4160 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 4161 | |
| 4162 | TestCompletionCallback callback1; |
| 4163 | |
| 4164 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
| 4165 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4166 | |
| 4167 | rv = callback1.WaitForResult(); |
| 4168 | EXPECT_EQ(OK, rv); |
| 4169 | |
| 4170 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4171 | ASSERT_FALSE(response == NULL); |
| 4172 | |
| 4173 | // The password prompt info should have been set in |
| 4174 | // response->auth_challenge. |
| 4175 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 4176 | |
| 4177 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 4178 | EXPECT_EQ(L"digestive", response->auth_challenge->realm); |
| 4179 | EXPECT_EQ(L"digest", response->auth_challenge->scheme); |
| 4180 | |
| 4181 | TestCompletionCallback callback2; |
| 4182 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 4183 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4184 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4185 | |
| 4186 | rv = callback2.WaitForResult(); |
| 4187 | EXPECT_EQ(OK, rv); |
| 4188 | |
| 4189 | response = trans->GetResponseInfo(); |
| 4190 | ASSERT_FALSE(response == NULL); |
| 4191 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4192 | } |
| 4193 | |
| 4194 | // ------------------------------------------------------------------------ |
| 4195 | |
| 4196 | // Transaction 2: Request another resource in digestive's protection space. |
| 4197 | // This will preemptively add an Authorization header which should have an |
| 4198 | // "nc" value of 2 (as compared to 1 in the first use. |
| 4199 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4200 | HttpRequestInfo request; |
| 4201 | request.method = "GET"; |
| 4202 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 4203 | // protection space as digest. |
| 4204 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 4205 | request.load_flags = 0; |
| 4206 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4207 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4208 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4209 | MockWrite data_writes1[] = { |
| 4210 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 4211 | "Host: www.google.com\r\n" |
| 4212 | "Connection: keep-alive\r\n" |
| 4213 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 4214 | "nonce=\"OU812\", uri=\"/x/y/a/b\", algorithm=MD5, " |
| 4215 | "response=\"d6f9a2c07d1c5df7b89379dca1269b35\", qop=auth, " |
| 4216 | "nc=00000002, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 4217 | }; |
| 4218 | |
| 4219 | // Sever accepts the authorization. |
| 4220 | MockRead data_reads1[] = { |
| 4221 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4222 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4223 | MockRead(false, OK), |
| 4224 | }; |
| 4225 | |
| 4226 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4227 | data_writes1, arraysize(data_writes1)); |
| 4228 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4229 | |
| 4230 | TestCompletionCallback callback1; |
| 4231 | |
| 4232 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
| 4233 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4234 | |
| 4235 | rv = callback1.WaitForResult(); |
| 4236 | EXPECT_EQ(OK, rv); |
| 4237 | |
| 4238 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4239 | ASSERT_FALSE(response == NULL); |
| 4240 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4241 | } |
| 4242 | } |
| 4243 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4244 | // Test the ResetStateForRestart() private method. |
| 4245 | TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { |
| 4246 | // Create a transaction (the dependencies aren't important). |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4247 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4248 | scoped_ptr<HttpNetworkTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4249 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4250 | |
| 4251 | // Setup some state (which we expect ResetStateForRestart() will clear). |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4252 | trans->read_buf_ = new IOBuffer(15); |
| 4253 | trans->read_buf_len_ = 15; |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 4254 | trans->request_headers_.SetHeader("Authorization", "NTLM"); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4255 | |
| 4256 | // Setup state in response_ |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 4257 | HttpResponseInfo* response = &trans->response_; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4258 | response->auth_challenge = new AuthChallengeInfo(); |
| 4259 | response->ssl_info.cert_status = -15; |
| 4260 | response->response_time = base::Time::Now(); |
| 4261 | response->was_cached = true; // (Wouldn't ever actually be true...) |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4262 | |
| 4263 | { // Setup state for response_.vary_data |
| 4264 | HttpRequestInfo request; |
| 4265 | std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 4266 | std::replace(temp.begin(), temp.end(), '\n', '\0'); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4267 | scoped_refptr<HttpResponseHeaders> headers(new HttpResponseHeaders(temp)); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4268 | request.extra_headers.SetHeader("Foo", "1"); |
| 4269 | request.extra_headers.SetHeader("bar", "23"); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4270 | EXPECT_TRUE(response->vary_data.Init(request, *headers)); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4271 | } |
| 4272 | |
| 4273 | // Cause the above state to be reset. |
| 4274 | trans->ResetStateForRestart(); |
| 4275 | |
| 4276 | // Verify that the state that needed to be reset, has been reset. |
[email protected] | 9b6fee1 | 2009-09-29 18:13:07 | [diff] [blame] | 4277 | EXPECT_TRUE(trans->read_buf_.get() == NULL); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4278 | EXPECT_EQ(0, trans->read_buf_len_); |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 4279 | EXPECT_TRUE(trans->request_headers_.IsEmpty()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4280 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4281 | EXPECT_TRUE(response->headers.get() == NULL); |
[email protected] | 34f4094 | 2010-10-04 00:34:04 | [diff] [blame] | 4282 | EXPECT_FALSE(response->was_cached); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4283 | EXPECT_EQ(0, response->ssl_info.cert_status); |
| 4284 | EXPECT_FALSE(response->vary_data.is_valid()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4285 | } |
| 4286 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4287 | // Test HTTPS connections to a site with a bad certificate |
| 4288 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4289 | HttpRequestInfo request; |
| 4290 | request.method = "GET"; |
| 4291 | request.url = GURL("https://www.google.com/"); |
| 4292 | request.load_flags = 0; |
| 4293 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4294 | SessionDependencies session_deps; |
| 4295 | scoped_ptr<HttpTransaction> trans( |
| 4296 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4297 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4298 | MockWrite data_writes[] = { |
| 4299 | MockWrite("GET / HTTP/1.1\r\n" |
| 4300 | "Host: www.google.com\r\n" |
| 4301 | "Connection: keep-alive\r\n\r\n"), |
| 4302 | }; |
| 4303 | |
| 4304 | MockRead data_reads[] = { |
| 4305 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4306 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4307 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4308 | MockRead(false, OK), |
| 4309 | }; |
| 4310 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4311 | StaticSocketDataProvider ssl_bad_certificate; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4312 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4313 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4314 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4315 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4316 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4317 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4318 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4319 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4320 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4321 | |
| 4322 | TestCompletionCallback callback; |
| 4323 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4324 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4325 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4326 | |
| 4327 | rv = callback.WaitForResult(); |
| 4328 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4329 | |
| 4330 | rv = trans->RestartIgnoringLastError(&callback); |
| 4331 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4332 | |
| 4333 | rv = callback.WaitForResult(); |
| 4334 | EXPECT_EQ(OK, rv); |
| 4335 | |
| 4336 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4337 | |
| 4338 | EXPECT_FALSE(response == NULL); |
| 4339 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4340 | } |
| 4341 | |
| 4342 | // Test HTTPS connections to a site with a bad certificate, going through a |
| 4343 | // proxy |
| 4344 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaProxy) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 4345 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4346 | |
| 4347 | HttpRequestInfo request; |
| 4348 | request.method = "GET"; |
| 4349 | request.url = GURL("https://www.google.com/"); |
| 4350 | request.load_flags = 0; |
| 4351 | |
| 4352 | MockWrite proxy_writes[] = { |
| 4353 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 4354 | "Host: www.google.com\r\n" |
| 4355 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4356 | }; |
| 4357 | |
| 4358 | MockRead proxy_reads[] = { |
| 4359 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4360 | MockRead(false, OK) |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4361 | }; |
| 4362 | |
| 4363 | MockWrite data_writes[] = { |
| 4364 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 4365 | "Host: www.google.com\r\n" |
| 4366 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4367 | MockWrite("GET / HTTP/1.1\r\n" |
| 4368 | "Host: www.google.com\r\n" |
| 4369 | "Connection: keep-alive\r\n\r\n"), |
| 4370 | }; |
| 4371 | |
| 4372 | MockRead data_reads[] = { |
| 4373 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4374 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4375 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4376 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4377 | MockRead(false, OK), |
| 4378 | }; |
| 4379 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4380 | StaticSocketDataProvider ssl_bad_certificate( |
| 4381 | proxy_reads, arraysize(proxy_reads), |
| 4382 | proxy_writes, arraysize(proxy_writes)); |
| 4383 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4384 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4385 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4386 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4387 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4388 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4389 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4390 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4391 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4392 | |
| 4393 | TestCompletionCallback callback; |
| 4394 | |
| 4395 | for (int i = 0; i < 2; i++) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4396 | session_deps.socket_factory.ResetNextMockIndexes(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4397 | |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4398 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4399 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4400 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4401 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4402 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4403 | |
| 4404 | rv = callback.WaitForResult(); |
| 4405 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4406 | |
| 4407 | rv = trans->RestartIgnoringLastError(&callback); |
| 4408 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4409 | |
| 4410 | rv = callback.WaitForResult(); |
| 4411 | EXPECT_EQ(OK, rv); |
| 4412 | |
| 4413 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4414 | |
| 4415 | EXPECT_FALSE(response == NULL); |
| 4416 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4417 | } |
| 4418 | } |
| 4419 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4420 | |
| 4421 | // Test HTTPS connections to a site, going through an HTTPS proxy |
| 4422 | TEST_F(HttpNetworkTransactionTest, HTTPSViaHttpsProxy) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 4423 | SessionDependencies session_deps(ProxyService::CreateFixed("https://proxy:70")); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4424 | |
| 4425 | HttpRequestInfo request; |
| 4426 | request.method = "GET"; |
| 4427 | request.url = GURL("https://www.google.com/"); |
| 4428 | request.load_flags = 0; |
| 4429 | |
| 4430 | MockWrite data_writes[] = { |
| 4431 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4432 | "Host: www.google.com\r\n" |
| 4433 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4434 | MockWrite("GET / HTTP/1.1\r\n" |
| 4435 | "Host: www.google.com\r\n" |
| 4436 | "Connection: keep-alive\r\n\r\n"), |
| 4437 | }; |
| 4438 | |
| 4439 | MockRead data_reads[] = { |
| 4440 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4441 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 4442 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4443 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4444 | MockRead(false, OK), |
| 4445 | }; |
| 4446 | |
| 4447 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4448 | data_writes, arraysize(data_writes)); |
| 4449 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4450 | SSLSocketDataProvider tunnel_ssl(true, OK); // SSL through the tunnel |
| 4451 | |
| 4452 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4453 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4454 | session_deps.socket_factory.AddSSLSocketDataProvider(&tunnel_ssl); |
| 4455 | |
| 4456 | TestCompletionCallback callback; |
| 4457 | |
| 4458 | scoped_ptr<HttpTransaction> trans( |
| 4459 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4460 | |
| 4461 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4462 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4463 | |
| 4464 | rv = callback.WaitForResult(); |
| 4465 | EXPECT_EQ(OK, rv); |
| 4466 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4467 | |
| 4468 | ASSERT_FALSE(response == NULL); |
| 4469 | |
| 4470 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 4471 | EXPECT_EQ(200, response->headers->response_code()); |
| 4472 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4473 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4474 | } |
| 4475 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4476 | // Test an HTTPS Proxy's ability to redirect a CONNECT request |
| 4477 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaHttpsProxy) { |
| 4478 | SessionDependencies session_deps( |
| 4479 | ProxyService::CreateFixed("https://proxy:70")); |
| 4480 | |
| 4481 | HttpRequestInfo request; |
| 4482 | request.method = "GET"; |
| 4483 | request.url = GURL("https://www.google.com/"); |
| 4484 | request.load_flags = 0; |
| 4485 | |
| 4486 | MockWrite data_writes[] = { |
| 4487 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4488 | "Host: www.google.com\r\n" |
| 4489 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4490 | }; |
| 4491 | |
| 4492 | MockRead data_reads[] = { |
| 4493 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 4494 | MockRead("Location: http://login.example.com/\r\n"), |
| 4495 | MockRead("Content-Length: 0\r\n\r\n"), |
| 4496 | MockRead(false, OK), |
| 4497 | }; |
| 4498 | |
| 4499 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4500 | data_writes, arraysize(data_writes)); |
| 4501 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4502 | |
| 4503 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4504 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4505 | |
| 4506 | TestCompletionCallback callback; |
| 4507 | |
| 4508 | scoped_ptr<HttpTransaction> trans( |
| 4509 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4510 | |
| 4511 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4512 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4513 | |
| 4514 | rv = callback.WaitForResult(); |
| 4515 | EXPECT_EQ(OK, rv); |
| 4516 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4517 | |
| 4518 | ASSERT_FALSE(response == NULL); |
| 4519 | |
| 4520 | EXPECT_EQ(302, response->headers->response_code()); |
| 4521 | std::string url; |
| 4522 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 4523 | EXPECT_EQ("http://login.example.com/", url); |
| 4524 | } |
| 4525 | |
| 4526 | // Test an HTTPS (SPDY) Proxy's ability to redirect a CONNECT request |
| 4527 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaSpdyProxy) { |
| 4528 | SessionDependencies session_deps( |
| 4529 | ProxyService::CreateFixed("https://proxy:70")); |
| 4530 | |
| 4531 | HttpRequestInfo request; |
| 4532 | request.method = "GET"; |
| 4533 | request.url = GURL("https://www.google.com/"); |
| 4534 | request.load_flags = 0; |
| 4535 | |
| 4536 | scoped_ptr<spdy::SpdyFrame> conn(ConstructSpdyConnect(NULL, 0, 1)); |
| 4537 | scoped_ptr<spdy::SpdyFrame> goaway(ConstructSpdyRstStream(1, spdy::CANCEL)); |
| 4538 | MockWrite data_writes[] = { |
| 4539 | CreateMockWrite(*conn.get(), 0, false), |
| 4540 | }; |
| 4541 | |
| 4542 | static const char* const kExtraHeaders[] = { |
| 4543 | "location", |
| 4544 | "http://login.example.com/", |
| 4545 | }; |
| 4546 | scoped_ptr<spdy::SpdyFrame> resp( |
| 4547 | ConstructSpdySynReplyError("302 Redirect", kExtraHeaders, |
| 4548 | arraysize(kExtraHeaders)/2, 1)); |
| 4549 | MockRead data_reads[] = { |
| 4550 | CreateMockRead(*resp.get(), 1, false), |
| 4551 | MockRead(true, 0, 2), // EOF |
| 4552 | }; |
| 4553 | |
| 4554 | scoped_refptr<DelayedSocketData> data( |
| 4555 | new DelayedSocketData( |
| 4556 | 1, // wait for one write to finish before reading. |
| 4557 | data_reads, arraysize(data_reads), |
| 4558 | data_writes, arraysize(data_writes))); |
| 4559 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4560 | proxy_ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 4561 | proxy_ssl.next_proto = "spdy/2"; |
| 4562 | proxy_ssl.was_npn_negotiated = true; |
| 4563 | |
| 4564 | session_deps.socket_factory.AddSocketDataProvider(data.get()); |
| 4565 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4566 | |
| 4567 | TestCompletionCallback callback; |
| 4568 | |
| 4569 | scoped_ptr<HttpTransaction> trans( |
| 4570 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4571 | |
| 4572 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4573 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4574 | |
| 4575 | rv = callback.WaitForResult(); |
| 4576 | EXPECT_EQ(OK, rv); |
| 4577 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4578 | |
| 4579 | ASSERT_FALSE(response == NULL); |
| 4580 | |
| 4581 | EXPECT_EQ(302, response->headers->response_code()); |
| 4582 | std::string url; |
| 4583 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 4584 | EXPECT_EQ("http://login.example.com/", url); |
| 4585 | } |
| 4586 | |
| 4587 | // Test an HTTPS Proxy's ability to provide a response to a CONNECT request |
| 4588 | TEST_F(HttpNetworkTransactionTest, ErrorResponseTofHttpsConnectViaHttpsProxy) { |
| 4589 | SessionDependencies session_deps( |
| 4590 | ProxyService::CreateFixed("https://proxy:70")); |
| 4591 | |
| 4592 | HttpRequestInfo request; |
| 4593 | request.method = "GET"; |
| 4594 | request.url = GURL("https://www.google.com/"); |
| 4595 | request.load_flags = 0; |
| 4596 | |
| 4597 | MockWrite data_writes[] = { |
| 4598 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4599 | "Host: www.google.com\r\n" |
| 4600 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4601 | }; |
| 4602 | |
| 4603 | MockRead data_reads[] = { |
| 4604 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 4605 | MockRead("Content-Length: 23\r\n\r\n"), |
| 4606 | MockRead("The host does not exist"), |
| 4607 | MockRead(false, OK), |
| 4608 | }; |
| 4609 | |
| 4610 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4611 | data_writes, arraysize(data_writes)); |
| 4612 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4613 | |
| 4614 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4615 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4616 | |
| 4617 | TestCompletionCallback callback; |
| 4618 | |
| 4619 | scoped_ptr<HttpTransaction> trans( |
| 4620 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4621 | |
| 4622 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4623 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4624 | |
| 4625 | rv = callback.WaitForResult(); |
| 4626 | EXPECT_EQ(OK, rv); |
| 4627 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4628 | |
| 4629 | ASSERT_FALSE(response == NULL); |
| 4630 | |
| 4631 | EXPECT_EQ(404, response->headers->response_code()); |
| 4632 | EXPECT_EQ(23, response->headers->GetContentLength()); |
| 4633 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4634 | EXPECT_FALSE(response->ssl_info.is_valid()); |
| 4635 | |
| 4636 | std::string response_data; |
| 4637 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 4638 | EXPECT_EQ("The host does not exist", response_data); |
| 4639 | } |
| 4640 | |
| 4641 | // Test an HTTPS (SPDY) Proxy's ability to provide a response to a CONNECT |
| 4642 | // request |
| 4643 | TEST_F(HttpNetworkTransactionTest, ErrorResponseTofHttpsConnectViaSpdyProxy) { |
| 4644 | SessionDependencies session_deps( |
| 4645 | ProxyService::CreateFixed("https://proxy:70")); |
| 4646 | |
| 4647 | HttpRequestInfo request; |
| 4648 | request.method = "GET"; |
| 4649 | request.url = GURL("https://www.google.com/"); |
| 4650 | request.load_flags = 0; |
| 4651 | |
| 4652 | scoped_ptr<spdy::SpdyFrame> conn(ConstructSpdyConnect(NULL, 0, 1)); |
| 4653 | scoped_ptr<spdy::SpdyFrame> goaway(ConstructSpdyRstStream(1, spdy::CANCEL)); |
| 4654 | MockWrite data_writes[] = { |
| 4655 | CreateMockWrite(*conn.get(), 0, false), |
| 4656 | }; |
| 4657 | |
| 4658 | static const char* const kExtraHeaders[] = { |
| 4659 | "location", |
| 4660 | "http://login.example.com/", |
| 4661 | }; |
| 4662 | scoped_ptr<spdy::SpdyFrame> resp( |
| 4663 | ConstructSpdySynReplyError("404 Not Found", kExtraHeaders, |
| 4664 | arraysize(kExtraHeaders)/2, 1)); |
| 4665 | scoped_ptr<spdy::SpdyFrame> body( |
| 4666 | ConstructSpdyBodyFrame(1, "The host does not exist", 23, true)); |
| 4667 | MockRead data_reads[] = { |
| 4668 | CreateMockRead(*resp.get(), 1, false), |
| 4669 | CreateMockRead(*body.get(), 2, false), |
| 4670 | MockRead(true, 0, 3), // EOF |
| 4671 | }; |
| 4672 | |
| 4673 | scoped_refptr<DelayedSocketData> data( |
| 4674 | new DelayedSocketData( |
| 4675 | 1, // wait for one write to finish before reading. |
| 4676 | data_reads, arraysize(data_reads), |
| 4677 | data_writes, arraysize(data_writes))); |
| 4678 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4679 | proxy_ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 4680 | proxy_ssl.next_proto = "spdy/2"; |
| 4681 | proxy_ssl.was_npn_negotiated = true; |
| 4682 | |
| 4683 | session_deps.socket_factory.AddSocketDataProvider(data.get()); |
| 4684 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4685 | |
| 4686 | TestCompletionCallback callback; |
| 4687 | |
| 4688 | scoped_ptr<HttpTransaction> trans( |
| 4689 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4690 | |
| 4691 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4692 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4693 | |
| 4694 | rv = callback.WaitForResult(); |
| 4695 | EXPECT_EQ(OK, rv); |
| 4696 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4697 | |
| 4698 | ASSERT_FALSE(response == NULL); |
| 4699 | |
| 4700 | EXPECT_EQ(404, response->headers->response_code()); |
| 4701 | EXPECT_FALSE(response->ssl_info.is_valid()); |
| 4702 | |
| 4703 | std::string response_data; |
| 4704 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 4705 | EXPECT_EQ("The host does not exist", response_data); |
| 4706 | } |
| 4707 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4708 | // Test HTTPS connections to a site with a bad certificate, going through an |
| 4709 | // HTTPS proxy |
| 4710 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaHttpsProxy) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 4711 | SessionDependencies session_deps(ProxyService::CreateFixed("https://proxy:70")); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4712 | |
| 4713 | HttpRequestInfo request; |
| 4714 | request.method = "GET"; |
| 4715 | request.url = GURL("https://www.google.com/"); |
| 4716 | request.load_flags = 0; |
| 4717 | |
| 4718 | // Attempt to fetch the URL from a server with a bad cert |
| 4719 | MockWrite bad_cert_writes[] = { |
| 4720 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4721 | "Host: www.google.com\r\n" |
| 4722 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4723 | }; |
| 4724 | |
| 4725 | MockRead bad_cert_reads[] = { |
| 4726 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4727 | MockRead(false, OK) |
| 4728 | }; |
| 4729 | |
| 4730 | // Attempt to fetch the URL with a good cert |
| 4731 | MockWrite good_data_writes[] = { |
| 4732 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4733 | "Host: www.google.com\r\n" |
| 4734 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4735 | MockWrite("GET / HTTP/1.1\r\n" |
| 4736 | "Host: www.google.com\r\n" |
| 4737 | "Connection: keep-alive\r\n\r\n"), |
| 4738 | }; |
| 4739 | |
| 4740 | MockRead good_cert_reads[] = { |
| 4741 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4742 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4743 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4744 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4745 | MockRead(false, OK), |
| 4746 | }; |
| 4747 | |
| 4748 | StaticSocketDataProvider ssl_bad_certificate( |
| 4749 | bad_cert_reads, arraysize(bad_cert_reads), |
| 4750 | bad_cert_writes, arraysize(bad_cert_writes)); |
| 4751 | StaticSocketDataProvider data(good_cert_reads, arraysize(good_cert_reads), |
| 4752 | good_data_writes, arraysize(good_data_writes)); |
| 4753 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4754 | SSLSocketDataProvider ssl(true, OK); |
| 4755 | |
| 4756 | // SSL to the proxy, then CONNECT request, then SSL with bad certificate |
| 4757 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4758 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4759 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4760 | |
| 4761 | // SSL to the proxy, then CONNECT request, then valid SSL certificate |
| 4762 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4763 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4764 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4765 | |
| 4766 | TestCompletionCallback callback; |
| 4767 | |
| 4768 | scoped_ptr<HttpTransaction> trans( |
| 4769 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4770 | |
| 4771 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4772 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4773 | |
| 4774 | rv = callback.WaitForResult(); |
| 4775 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4776 | |
| 4777 | rv = trans->RestartIgnoringLastError(&callback); |
| 4778 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4779 | |
| 4780 | rv = callback.WaitForResult(); |
| 4781 | EXPECT_EQ(OK, rv); |
| 4782 | |
| 4783 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4784 | |
| 4785 | EXPECT_FALSE(response == NULL); |
| 4786 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4787 | } |
| 4788 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4789 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4790 | HttpRequestInfo request; |
| 4791 | request.method = "GET"; |
| 4792 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4793 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 4794 | "Chromium Ultra Awesome X Edition"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4795 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4796 | SessionDependencies session_deps; |
| 4797 | scoped_ptr<HttpTransaction> trans( |
| 4798 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4799 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4800 | MockWrite data_writes[] = { |
| 4801 | MockWrite("GET / HTTP/1.1\r\n" |
| 4802 | "Host: www.google.com\r\n" |
| 4803 | "Connection: keep-alive\r\n" |
| 4804 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4805 | }; |
| 4806 | |
| 4807 | // Lastly, the server responds with the actual content. |
| 4808 | MockRead data_reads[] = { |
| 4809 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4810 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4811 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4812 | MockRead(false, OK), |
| 4813 | }; |
| 4814 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4815 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4816 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4817 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4818 | |
| 4819 | TestCompletionCallback callback; |
| 4820 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4821 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4822 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4823 | |
| 4824 | rv = callback.WaitForResult(); |
| 4825 | EXPECT_EQ(OK, rv); |
| 4826 | } |
| 4827 | |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4828 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgentOverTunnel) { |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4829 | HttpRequestInfo request; |
| 4830 | request.method = "GET"; |
| 4831 | request.url = GURL("https://www.google.com/"); |
| 4832 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 4833 | "Chromium Ultra Awesome X Edition"); |
| 4834 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4835 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 4836 | scoped_ptr<HttpTransaction> trans( |
| 4837 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4838 | |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4839 | MockWrite data_writes[] = { |
| 4840 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4841 | "Host: www.google.com\r\n" |
| 4842 | "Proxy-Connection: keep-alive\r\n" |
| 4843 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4844 | }; |
| 4845 | MockRead data_reads[] = { |
| 4846 | // Return an error, so the transaction stops here (this test isn't |
| 4847 | // interested in the rest). |
| 4848 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4849 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4850 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 4851 | }; |
| 4852 | |
| 4853 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4854 | data_writes, arraysize(data_writes)); |
| 4855 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4856 | |
| 4857 | TestCompletionCallback callback; |
| 4858 | |
| 4859 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4860 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4861 | |
| 4862 | rv = callback.WaitForResult(); |
| 4863 | EXPECT_EQ(OK, rv); |
| 4864 | } |
| 4865 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4866 | TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4867 | HttpRequestInfo request; |
| 4868 | request.method = "GET"; |
| 4869 | request.url = GURL("http://www.google.com/"); |
| 4870 | request.load_flags = 0; |
| 4871 | request.referrer = GURL("http://the.previous.site.com/"); |
| 4872 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4873 | SessionDependencies session_deps; |
| 4874 | scoped_ptr<HttpTransaction> trans( |
| 4875 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4876 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4877 | MockWrite data_writes[] = { |
| 4878 | MockWrite("GET / HTTP/1.1\r\n" |
| 4879 | "Host: www.google.com\r\n" |
| 4880 | "Connection: keep-alive\r\n" |
| 4881 | "Referer: http://the.previous.site.com/\r\n\r\n"), |
| 4882 | }; |
| 4883 | |
| 4884 | // Lastly, the server responds with the actual content. |
| 4885 | MockRead data_reads[] = { |
| 4886 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4887 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4888 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4889 | MockRead(false, OK), |
| 4890 | }; |
| 4891 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4892 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4893 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4894 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4895 | |
| 4896 | TestCompletionCallback callback; |
| 4897 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4898 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4899 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4900 | |
| 4901 | rv = callback.WaitForResult(); |
| 4902 | EXPECT_EQ(OK, rv); |
| 4903 | } |
| 4904 | |
| 4905 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4906 | HttpRequestInfo request; |
| 4907 | request.method = "POST"; |
| 4908 | request.url = GURL("http://www.google.com/"); |
| 4909 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4910 | SessionDependencies session_deps; |
| 4911 | scoped_ptr<HttpTransaction> trans( |
| 4912 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4913 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4914 | MockWrite data_writes[] = { |
| 4915 | MockWrite("POST / HTTP/1.1\r\n" |
| 4916 | "Host: www.google.com\r\n" |
| 4917 | "Connection: keep-alive\r\n" |
| 4918 | "Content-Length: 0\r\n\r\n"), |
| 4919 | }; |
| 4920 | |
| 4921 | // Lastly, the server responds with the actual content. |
| 4922 | MockRead data_reads[] = { |
| 4923 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4924 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4925 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4926 | MockRead(false, OK), |
| 4927 | }; |
| 4928 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4929 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4930 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4931 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4932 | |
| 4933 | TestCompletionCallback callback; |
| 4934 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4935 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4936 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4937 | |
| 4938 | rv = callback.WaitForResult(); |
| 4939 | EXPECT_EQ(OK, rv); |
| 4940 | } |
| 4941 | |
| 4942 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4943 | HttpRequestInfo request; |
| 4944 | request.method = "PUT"; |
| 4945 | request.url = GURL("http://www.google.com/"); |
| 4946 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4947 | SessionDependencies session_deps; |
| 4948 | scoped_ptr<HttpTransaction> trans( |
| 4949 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4950 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4951 | MockWrite data_writes[] = { |
| 4952 | MockWrite("PUT / HTTP/1.1\r\n" |
| 4953 | "Host: www.google.com\r\n" |
| 4954 | "Connection: keep-alive\r\n" |
| 4955 | "Content-Length: 0\r\n\r\n"), |
| 4956 | }; |
| 4957 | |
| 4958 | // Lastly, the server responds with the actual content. |
| 4959 | MockRead data_reads[] = { |
| 4960 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4961 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4962 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4963 | MockRead(false, OK), |
| 4964 | }; |
| 4965 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4966 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4967 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4968 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4969 | |
| 4970 | TestCompletionCallback callback; |
| 4971 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4972 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4973 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4974 | |
| 4975 | rv = callback.WaitForResult(); |
| 4976 | EXPECT_EQ(OK, rv); |
| 4977 | } |
| 4978 | |
| 4979 | TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4980 | HttpRequestInfo request; |
| 4981 | request.method = "HEAD"; |
| 4982 | request.url = GURL("http://www.google.com/"); |
| 4983 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 4984 | SessionDependencies session_deps; |
| 4985 | scoped_ptr<HttpTransaction> trans( |
| 4986 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4987 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4988 | MockWrite data_writes[] = { |
| 4989 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 4990 | "Host: www.google.com\r\n" |
| 4991 | "Connection: keep-alive\r\n" |
| 4992 | "Content-Length: 0\r\n\r\n"), |
| 4993 | }; |
| 4994 | |
| 4995 | // Lastly, the server responds with the actual content. |
| 4996 | MockRead data_reads[] = { |
| 4997 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4998 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4999 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5000 | MockRead(false, OK), |
| 5001 | }; |
| 5002 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5003 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5004 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5005 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5006 | |
| 5007 | TestCompletionCallback callback; |
| 5008 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5009 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5010 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5011 | |
| 5012 | rv = callback.WaitForResult(); |
| 5013 | EXPECT_EQ(OK, rv); |
| 5014 | } |
| 5015 | |
| 5016 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5017 | HttpRequestInfo request; |
| 5018 | request.method = "GET"; |
| 5019 | request.url = GURL("http://www.google.com/"); |
| 5020 | request.load_flags = LOAD_BYPASS_CACHE; |
| 5021 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5022 | SessionDependencies session_deps; |
| 5023 | scoped_ptr<HttpTransaction> trans( |
| 5024 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5025 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5026 | MockWrite data_writes[] = { |
| 5027 | MockWrite("GET / HTTP/1.1\r\n" |
| 5028 | "Host: www.google.com\r\n" |
| 5029 | "Connection: keep-alive\r\n" |
| 5030 | "Pragma: no-cache\r\n" |
| 5031 | "Cache-Control: no-cache\r\n\r\n"), |
| 5032 | }; |
| 5033 | |
| 5034 | // Lastly, the server responds with the actual content. |
| 5035 | MockRead data_reads[] = { |
| 5036 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5037 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5038 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5039 | MockRead(false, OK), |
| 5040 | }; |
| 5041 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5042 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5043 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5044 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5045 | |
| 5046 | TestCompletionCallback callback; |
| 5047 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5048 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5049 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5050 | |
| 5051 | rv = callback.WaitForResult(); |
| 5052 | EXPECT_EQ(OK, rv); |
| 5053 | } |
| 5054 | |
| 5055 | TEST_F(HttpNetworkTransactionTest, |
| 5056 | BuildRequest_CacheControlValidateCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5057 | HttpRequestInfo request; |
| 5058 | request.method = "GET"; |
| 5059 | request.url = GURL("http://www.google.com/"); |
| 5060 | request.load_flags = LOAD_VALIDATE_CACHE; |
| 5061 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5062 | SessionDependencies session_deps; |
| 5063 | scoped_ptr<HttpTransaction> trans( |
| 5064 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5065 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5066 | MockWrite data_writes[] = { |
| 5067 | MockWrite("GET / HTTP/1.1\r\n" |
| 5068 | "Host: www.google.com\r\n" |
| 5069 | "Connection: keep-alive\r\n" |
| 5070 | "Cache-Control: max-age=0\r\n\r\n"), |
| 5071 | }; |
| 5072 | |
| 5073 | // Lastly, the server responds with the actual content. |
| 5074 | MockRead data_reads[] = { |
| 5075 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5076 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5077 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5078 | MockRead(false, OK), |
| 5079 | }; |
| 5080 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5081 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5082 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5083 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5084 | |
| 5085 | TestCompletionCallback callback; |
| 5086 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5087 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5088 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5089 | |
| 5090 | rv = callback.WaitForResult(); |
| 5091 | EXPECT_EQ(OK, rv); |
| 5092 | } |
| 5093 | |
| 5094 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5095 | HttpRequestInfo request; |
| 5096 | request.method = "GET"; |
| 5097 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5098 | request.extra_headers.SetHeader("FooHeader", "Bar"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5099 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5100 | SessionDependencies session_deps; |
| 5101 | scoped_ptr<HttpTransaction> trans( |
| 5102 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5103 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5104 | MockWrite data_writes[] = { |
| 5105 | MockWrite("GET / HTTP/1.1\r\n" |
| 5106 | "Host: www.google.com\r\n" |
| 5107 | "Connection: keep-alive\r\n" |
| 5108 | "FooHeader: Bar\r\n\r\n"), |
| 5109 | }; |
| 5110 | |
| 5111 | // Lastly, the server responds with the actual content. |
| 5112 | MockRead data_reads[] = { |
| 5113 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5114 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5115 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5116 | MockRead(false, OK), |
| 5117 | }; |
| 5118 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5119 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5120 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5121 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5122 | |
| 5123 | TestCompletionCallback callback; |
| 5124 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5125 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5126 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5127 | |
| 5128 | rv = callback.WaitForResult(); |
| 5129 | EXPECT_EQ(OK, rv); |
| 5130 | } |
| 5131 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5132 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeadersStripped) { |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5133 | HttpRequestInfo request; |
| 5134 | request.method = "GET"; |
| 5135 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5136 | request.extra_headers.SetHeader("referer", "www.foo.com"); |
| 5137 | request.extra_headers.SetHeader("hEllo", "Kitty"); |
| 5138 | request.extra_headers.SetHeader("FoO", "bar"); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5139 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5140 | SessionDependencies session_deps; |
| 5141 | scoped_ptr<HttpTransaction> trans( |
| 5142 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5143 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5144 | MockWrite data_writes[] = { |
| 5145 | MockWrite("GET / HTTP/1.1\r\n" |
| 5146 | "Host: www.google.com\r\n" |
| 5147 | "Connection: keep-alive\r\n" |
| 5148 | "hEllo: Kitty\r\n" |
| 5149 | "FoO: bar\r\n\r\n"), |
| 5150 | }; |
| 5151 | |
| 5152 | // Lastly, the server responds with the actual content. |
| 5153 | MockRead data_reads[] = { |
| 5154 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5155 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5156 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5157 | MockRead(false, OK), |
| 5158 | }; |
| 5159 | |
| 5160 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5161 | data_writes, arraysize(data_writes)); |
| 5162 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5163 | |
| 5164 | TestCompletionCallback callback; |
| 5165 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5166 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5167 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5168 | |
| 5169 | rv = callback.WaitForResult(); |
| 5170 | EXPECT_EQ(OK, rv); |
| 5171 | } |
| 5172 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5173 | TEST_F(HttpNetworkTransactionTest, SOCKS4_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5174 | HttpRequestInfo request; |
| 5175 | request.method = "GET"; |
| 5176 | request.url = GURL("http://www.google.com/"); |
| 5177 | request.load_flags = 0; |
| 5178 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5179 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5180 | ProxyService::CreateFixed("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5181 | |
| 5182 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5183 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5184 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5185 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 5186 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 5187 | |
| 5188 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5189 | MockWrite(true, write_buffer, arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5190 | MockWrite("GET / HTTP/1.1\r\n" |
| 5191 | "Host: www.google.com\r\n" |
| 5192 | "Connection: keep-alive\r\n\r\n") |
| 5193 | }; |
| 5194 | |
| 5195 | MockRead data_reads[] = { |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5196 | MockRead(true, read_buffer, arraysize(read_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5197 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5198 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5199 | MockRead("Payload"), |
| 5200 | MockRead(false, OK) |
| 5201 | }; |
| 5202 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5203 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5204 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5205 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5206 | |
| 5207 | TestCompletionCallback callback; |
| 5208 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5209 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5210 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5211 | |
| 5212 | rv = callback.WaitForResult(); |
| 5213 | EXPECT_EQ(OK, rv); |
| 5214 | |
| 5215 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5216 | EXPECT_FALSE(response == NULL); |
| 5217 | |
| 5218 | std::string response_text; |
| 5219 | rv = ReadTransaction(trans.get(), &response_text); |
| 5220 | EXPECT_EQ(OK, rv); |
| 5221 | EXPECT_EQ("Payload", response_text); |
| 5222 | } |
| 5223 | |
| 5224 | TEST_F(HttpNetworkTransactionTest, SOCKS4_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5225 | HttpRequestInfo request; |
| 5226 | request.method = "GET"; |
| 5227 | request.url = GURL("https://www.google.com/"); |
| 5228 | request.load_flags = 0; |
| 5229 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5230 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5231 | ProxyService::CreateFixed("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5232 | |
| 5233 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5234 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5235 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5236 | unsigned char write_buffer[] = { 0x04, 0x01, 0x01, 0xBB, 127, 0, 0, 1, 0 }; |
| 5237 | unsigned char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 5238 | |
| 5239 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5240 | MockWrite(true, reinterpret_cast<char*>(write_buffer), |
| 5241 | arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5242 | MockWrite("GET / HTTP/1.1\r\n" |
| 5243 | "Host: www.google.com\r\n" |
| 5244 | "Connection: keep-alive\r\n\r\n") |
| 5245 | }; |
| 5246 | |
| 5247 | MockRead data_reads[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5248 | MockWrite(true, reinterpret_cast<char*>(read_buffer), |
| 5249 | arraysize(read_buffer)), |
| 5250 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5251 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5252 | MockRead("Payload"), |
| 5253 | MockRead(false, OK) |
| 5254 | }; |
| 5255 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5256 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5257 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5258 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5259 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5260 | SSLSocketDataProvider ssl(true, OK); |
| 5261 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5262 | |
| 5263 | TestCompletionCallback callback; |
| 5264 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5265 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5266 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5267 | |
| 5268 | rv = callback.WaitForResult(); |
| 5269 | EXPECT_EQ(OK, rv); |
| 5270 | |
| 5271 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5272 | EXPECT_FALSE(response == NULL); |
| 5273 | |
| 5274 | std::string response_text; |
| 5275 | rv = ReadTransaction(trans.get(), &response_text); |
| 5276 | EXPECT_EQ(OK, rv); |
| 5277 | EXPECT_EQ("Payload", response_text); |
| 5278 | } |
| 5279 | |
| 5280 | TEST_F(HttpNetworkTransactionTest, SOCKS5_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5281 | HttpRequestInfo request; |
| 5282 | request.method = "GET"; |
| 5283 | request.url = GURL("http://www.google.com/"); |
| 5284 | request.load_flags = 0; |
| 5285 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5286 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5287 | ProxyService::CreateFixed("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5288 | |
| 5289 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5290 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5291 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5292 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 5293 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 5294 | const char kSOCKS5OkRequest[] = { |
| 5295 | 0x05, // Version |
| 5296 | 0x01, // Command (CONNECT) |
| 5297 | 0x00, // Reserved. |
| 5298 | 0x03, // Address type (DOMAINNAME). |
| 5299 | 0x0E, // Length of domain (14) |
| 5300 | // Domain string: |
| 5301 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 5302 | 0x00, 0x50, // 16-bit port (80) |
| 5303 | }; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5304 | const char kSOCKS5OkResponse[] = |
| 5305 | { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 5306 | |
| 5307 | MockWrite data_writes[] = { |
| 5308 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 5309 | MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)), |
| 5310 | MockWrite("GET / HTTP/1.1\r\n" |
| 5311 | "Host: www.google.com\r\n" |
| 5312 | "Connection: keep-alive\r\n\r\n") |
| 5313 | }; |
| 5314 | |
| 5315 | MockRead data_reads[] = { |
| 5316 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 5317 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
| 5318 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5319 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5320 | MockRead("Payload"), |
| 5321 | MockRead(false, OK) |
| 5322 | }; |
| 5323 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5324 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5325 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5326 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5327 | |
| 5328 | TestCompletionCallback callback; |
| 5329 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5330 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5331 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5332 | |
| 5333 | rv = callback.WaitForResult(); |
| 5334 | EXPECT_EQ(OK, rv); |
| 5335 | |
| 5336 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5337 | EXPECT_FALSE(response == NULL); |
| 5338 | |
| 5339 | std::string response_text; |
| 5340 | rv = ReadTransaction(trans.get(), &response_text); |
| 5341 | EXPECT_EQ(OK, rv); |
| 5342 | EXPECT_EQ("Payload", response_text); |
| 5343 | } |
| 5344 | |
| 5345 | TEST_F(HttpNetworkTransactionTest, SOCKS5_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5346 | HttpRequestInfo request; |
| 5347 | request.method = "GET"; |
| 5348 | request.url = GURL("https://www.google.com/"); |
| 5349 | request.load_flags = 0; |
| 5350 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5351 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5352 | ProxyService::CreateFixed("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5353 | |
| 5354 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5355 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5356 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5357 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 5358 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 5359 | const unsigned char kSOCKS5OkRequest[] = { |
| 5360 | 0x05, // Version |
| 5361 | 0x01, // Command (CONNECT) |
| 5362 | 0x00, // Reserved. |
| 5363 | 0x03, // Address type (DOMAINNAME). |
| 5364 | 0x0E, // Length of domain (14) |
| 5365 | // Domain string: |
| 5366 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 5367 | 0x01, 0xBB, // 16-bit port (443) |
| 5368 | }; |
| 5369 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5370 | const char kSOCKS5OkResponse[] = |
| 5371 | { 0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0x00, 0x00 }; |
| 5372 | |
| 5373 | MockWrite data_writes[] = { |
| 5374 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 5375 | MockWrite(true, reinterpret_cast<const char*>(kSOCKS5OkRequest), |
| 5376 | arraysize(kSOCKS5OkRequest)), |
| 5377 | MockWrite("GET / HTTP/1.1\r\n" |
| 5378 | "Host: www.google.com\r\n" |
| 5379 | "Connection: keep-alive\r\n\r\n") |
| 5380 | }; |
| 5381 | |
| 5382 | MockRead data_reads[] = { |
| 5383 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 5384 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5385 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5386 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5387 | MockRead("Payload"), |
| 5388 | MockRead(false, OK) |
| 5389 | }; |
| 5390 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5391 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5392 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5393 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5394 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5395 | SSLSocketDataProvider ssl(true, OK); |
| 5396 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5397 | |
| 5398 | TestCompletionCallback callback; |
| 5399 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5400 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5401 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5402 | |
| 5403 | rv = callback.WaitForResult(); |
| 5404 | EXPECT_EQ(OK, rv); |
| 5405 | |
| 5406 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5407 | EXPECT_FALSE(response == NULL); |
| 5408 | |
| 5409 | std::string response_text; |
| 5410 | rv = ReadTransaction(trans.get(), &response_text); |
| 5411 | EXPECT_EQ(OK, rv); |
| 5412 | EXPECT_EQ("Payload", response_text); |
| 5413 | } |
| 5414 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5415 | // Tests that for connection endpoints the group names are correctly set. |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5416 | |
| 5417 | struct GroupNameTest { |
| 5418 | std::string proxy_server; |
| 5419 | std::string url; |
| 5420 | std::string expected_group_name; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5421 | bool ssl; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5422 | }; |
| 5423 | |
| 5424 | scoped_refptr<HttpNetworkSession> SetupSessionForGroupNameTests( |
| 5425 | const std::string& proxy_server) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5426 | SessionDependencies session_deps(ProxyService::CreateFixed(proxy_server)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5427 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 5428 | |
| 5429 | HttpAlternateProtocols* alternate_protocols = |
| 5430 | session->mutable_alternate_protocols(); |
| 5431 | alternate_protocols->SetAlternateProtocolFor( |
| 5432 | HostPortPair("host.with.alternate", 80), 443, |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5433 | HttpAlternateProtocols::NPN_SPDY_2); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5434 | |
| 5435 | return session; |
| 5436 | } |
| 5437 | |
| 5438 | int GroupNameTransactionHelper( |
| 5439 | const std::string& url, |
| 5440 | const scoped_refptr<HttpNetworkSession>& session) { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5441 | HttpRequestInfo request; |
| 5442 | request.method = "GET"; |
| 5443 | request.url = GURL(url); |
| 5444 | request.load_flags = 0; |
| 5445 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5446 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5447 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5448 | TestCompletionCallback callback; |
| 5449 | |
| 5450 | // We do not complete this request, the dtor will clean the transaction up. |
| 5451 | return trans->Start(&request, &callback, BoundNetLog()); |
| 5452 | } |
| 5453 | |
| 5454 | TEST_F(HttpNetworkTransactionTest, GroupNameForDirectConnections) { |
| 5455 | const GroupNameTest tests[] = { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5456 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5457 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5458 | "http://www.google.com/direct", |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5459 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5460 | false, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5461 | }, |
| 5462 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5463 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5464 | "http://[2001:1418:13:1::25]/direct", |
| 5465 | "[2001:1418:13:1::25]:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5466 | false, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5467 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5468 | |
| 5469 | // SSL Tests |
| 5470 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5471 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5472 | "https://www.google.com/direct_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5473 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5474 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5475 | }, |
| 5476 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5477 | "", // unused |
| 5478 | "https://[2001:1418:13:1::25]/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5479 | "ssl/[2001:1418:13:1::25]:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5480 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5481 | }, |
| 5482 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5483 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5484 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5485 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5486 | true, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5487 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5488 | }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5489 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5490 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5491 | |
| 5492 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 5493 | scoped_refptr<HttpNetworkSession> session( |
| 5494 | SetupSessionForGroupNameTests(tests[i].proxy_server)); |
| 5495 | |
| 5496 | HttpNetworkSessionPeer peer(session); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5497 | CaptureGroupNameTCPSocketPool* tcp_conn_pool = |
| 5498 | new CaptureGroupNameTCPSocketPool(session); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5499 | peer.SetTCPSocketPool(tcp_conn_pool); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5500 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
| 5501 | new CaptureGroupNameSSLSocketPool(session.get()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5502 | peer.SetSSLSocketPool(ssl_conn_pool); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5503 | |
| 5504 | EXPECT_EQ(ERR_IO_PENDING, |
| 5505 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5506 | if (tests[i].ssl) |
| 5507 | EXPECT_EQ(tests[i].expected_group_name, |
| 5508 | ssl_conn_pool->last_group_name_received()); |
| 5509 | else |
| 5510 | EXPECT_EQ(tests[i].expected_group_name, |
| 5511 | tcp_conn_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5512 | } |
| 5513 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5514 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | TEST_F(HttpNetworkTransactionTest, GroupNameForHTTPProxyConnections) { |
| 5518 | const GroupNameTest tests[] = { |
| 5519 | { |
| 5520 | "http_proxy", |
| 5521 | "http://www.google.com/http_proxy_normal", |
| 5522 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5523 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5524 | }, |
| 5525 | |
| 5526 | // SSL Tests |
| 5527 | { |
| 5528 | "http_proxy", |
| 5529 | "https://www.google.com/http_connect_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5530 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5531 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5532 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 5533 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5534 | { |
| 5535 | "http_proxy", |
| 5536 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5537 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5538 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5539 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5540 | }; |
| 5541 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5542 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5543 | |
| 5544 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 5545 | scoped_refptr<HttpNetworkSession> session( |
| 5546 | SetupSessionForGroupNameTests(tests[i].proxy_server)); |
| 5547 | |
| 5548 | HttpNetworkSessionPeer peer(session); |
| 5549 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5550 | HostPortPair proxy_host("http_proxy", 80); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5551 | CaptureGroupNameHttpProxySocketPool* http_proxy_pool = |
| 5552 | new CaptureGroupNameHttpProxySocketPool(session); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5553 | peer.SetSocketPoolForHTTPProxy(proxy_host, http_proxy_pool); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5554 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
| 5555 | new CaptureGroupNameSSLSocketPool(session); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5556 | peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5557 | |
| 5558 | EXPECT_EQ(ERR_IO_PENDING, |
| 5559 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5560 | if (tests[i].ssl) |
| 5561 | EXPECT_EQ(tests[i].expected_group_name, |
| 5562 | ssl_conn_pool->last_group_name_received()); |
| 5563 | else |
| 5564 | EXPECT_EQ(tests[i].expected_group_name, |
| 5565 | http_proxy_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5566 | } |
| 5567 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5568 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5569 | } |
| 5570 | |
| 5571 | TEST_F(HttpNetworkTransactionTest, GroupNameForSOCKSConnections) { |
| 5572 | const GroupNameTest tests[] = { |
| 5573 | { |
| 5574 | "socks4://socks_proxy:1080", |
| 5575 | "http://www.google.com/socks4_direct", |
| 5576 | "socks4/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5577 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5578 | }, |
| 5579 | { |
| 5580 | "socks5://socks_proxy:1080", |
| 5581 | "http://www.google.com/socks5_direct", |
| 5582 | "socks5/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5583 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5584 | }, |
| 5585 | |
| 5586 | // SSL Tests |
| 5587 | { |
| 5588 | "socks4://socks_proxy:1080", |
| 5589 | "https://www.google.com/socks4_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5590 | "socks4/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5591 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5592 | }, |
| 5593 | { |
| 5594 | "socks5://socks_proxy:1080", |
| 5595 | "https://www.google.com/socks5_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5596 | "socks5/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5597 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5598 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 5599 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5600 | { |
| 5601 | "socks4://socks_proxy:1080", |
| 5602 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5603 | "socks4/ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5604 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5605 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5606 | }; |
| 5607 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5608 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5609 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5610 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5611 | scoped_refptr<HttpNetworkSession> session( |
| 5612 | SetupSessionForGroupNameTests(tests[i].proxy_server)); |
| 5613 | HttpNetworkSessionPeer peer(session); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5614 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5615 | HostPortPair proxy_host("socks_proxy", 1080); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5616 | CaptureGroupNameSOCKSSocketPool* socks_conn_pool = |
| 5617 | new CaptureGroupNameSOCKSSocketPool(session); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5618 | peer.SetSocketPoolForSOCKSProxy(proxy_host, socks_conn_pool); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5619 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
| 5620 | new CaptureGroupNameSSLSocketPool(session); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5621 | peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5622 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5623 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5624 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5625 | EXPECT_EQ(ERR_IO_PENDING, |
| 5626 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5627 | if (tests[i].ssl) |
| 5628 | EXPECT_EQ(tests[i].expected_group_name, |
| 5629 | ssl_conn_pool->last_group_name_received()); |
| 5630 | else |
| 5631 | EXPECT_EQ(tests[i].expected_group_name, |
| 5632 | socks_conn_pool->last_group_name_received()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5633 | } |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5634 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5635 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5636 | } |
| 5637 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5638 | TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5639 | HttpRequestInfo request; |
| 5640 | request.method = "GET"; |
| 5641 | request.url = GURL("http://www.google.com/"); |
| 5642 | |
[email protected] | 5c6a17e | 2009-06-10 00:54:54 | [diff] [blame] | 5643 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5644 | ProxyService::CreateFixed("myproxy:70;foobar:80")); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5645 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 5646 | // This simulates failure resolving all hostnames; that means we will fail |
| 5647 | // connecting to both proxies (myproxy:70 and foobar:80). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5648 | session_deps.host_resolver->rules()->AddSimulatedFailure("*"); |
| 5649 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5650 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5651 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5652 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5653 | TestCompletionCallback callback; |
| 5654 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5655 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5656 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5657 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5658 | rv = callback.WaitForResult(); |
[email protected] | f7fccee | 2010-09-16 20:53:01 | [diff] [blame] | 5659 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, rv); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5660 | } |
| 5661 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5662 | // Host resolution observer used by |
| 5663 | // HttpNetworkTransactionTest.ResolveMadeWithReferrer to check that host |
| 5664 | // resovle requests are issued with a referrer of |expected_referrer|. |
| 5665 | class ResolutionReferrerObserver : public HostResolver::Observer { |
| 5666 | public: |
| 5667 | explicit ResolutionReferrerObserver(const GURL& expected_referrer) |
| 5668 | : expected_referrer_(expected_referrer), |
| 5669 | called_start_with_referrer_(false), |
| 5670 | called_finish_with_referrer_(false) { |
| 5671 | } |
| 5672 | |
| 5673 | virtual void OnStartResolution(int id, |
| 5674 | const HostResolver::RequestInfo& info) { |
| 5675 | if (info.referrer() == expected_referrer_) |
| 5676 | called_start_with_referrer_ = true; |
| 5677 | } |
| 5678 | |
| 5679 | virtual void OnFinishResolutionWithStatus( |
| 5680 | int id, bool was_resolved, const HostResolver::RequestInfo& info ) { |
| 5681 | if (info.referrer() == expected_referrer_) |
| 5682 | called_finish_with_referrer_ = true; |
| 5683 | } |
| 5684 | |
[email protected] | eb255d3 | 2009-06-17 02:11:03 | [diff] [blame] | 5685 | virtual void OnCancelResolution(int id, |
| 5686 | const HostResolver::RequestInfo& info ) { |
| 5687 | FAIL() << "Should not be cancelling any requests!"; |
| 5688 | } |
| 5689 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5690 | bool did_complete_with_expected_referrer() const { |
| 5691 | return called_start_with_referrer_ && called_finish_with_referrer_; |
| 5692 | } |
| 5693 | |
| 5694 | private: |
| 5695 | GURL expected_referrer_; |
| 5696 | bool called_start_with_referrer_; |
| 5697 | bool called_finish_with_referrer_; |
| 5698 | |
| 5699 | DISALLOW_COPY_AND_ASSIGN(ResolutionReferrerObserver); |
| 5700 | }; |
| 5701 | |
| 5702 | // Make sure that when HostResolver::Resolve() is invoked, it passes through |
| 5703 | // the "referrer". This is depended on by the DNS prefetch observer. |
| 5704 | TEST_F(HttpNetworkTransactionTest, ResolveMadeWithReferrer) { |
| 5705 | GURL referrer = GURL("http://expected-referrer/"); |
| 5706 | EXPECT_TRUE(referrer.is_valid()); |
| 5707 | ResolutionReferrerObserver resolution_observer(referrer); |
| 5708 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5709 | // Issue a request, containing an HTTP referrer. |
| 5710 | HttpRequestInfo request; |
| 5711 | request.method = "GET"; |
| 5712 | request.referrer = referrer; |
| 5713 | request.url = GURL("http://www.google.com/"); |
| 5714 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5715 | SessionDependencies session_deps; |
| 5716 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5717 | CreateSession(&session_deps))); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5718 | |
| 5719 | // Attach an observer to watch the host resolutions being made. |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 5720 | session_deps.host_resolver->AddObserver(&resolution_observer); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5721 | |
| 5722 | // Connect up a mock socket which will fail when reading. |
| 5723 | MockRead data_reads[] = { |
| 5724 | MockRead(false, ERR_FAILED), |
| 5725 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5726 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5727 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5728 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5729 | // Run the request until it fails reading from the socket. |
| 5730 | TestCompletionCallback callback; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5731 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5732 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5733 | rv = callback.WaitForResult(); |
| 5734 | EXPECT_EQ(ERR_FAILED, rv); |
| 5735 | |
| 5736 | // Check that the host resolution observer saw |referrer|. |
| 5737 | EXPECT_TRUE(resolution_observer.did_complete_with_expected_referrer()); |
| 5738 | } |
| 5739 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 5740 | // Base test to make sure that when the load flags for a request specify to |
| 5741 | // bypass the cache, the DNS cache is not used. |
| 5742 | void BypassHostCacheOnRefreshHelper(int load_flags) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 5743 | // Issue a request, asking to bypass the cache(s). |
| 5744 | HttpRequestInfo request; |
| 5745 | request.method = "GET"; |
| 5746 | request.load_flags = load_flags; |
| 5747 | request.url = GURL("http://www.google.com/"); |
| 5748 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5749 | SessionDependencies session_deps; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5750 | |
[email protected] | a2c2fb9 | 2009-07-18 07:31:04 | [diff] [blame] | 5751 | // Select a host resolver that does caching. |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 5752 | session_deps.host_resolver.reset(new MockCachingHostResolver); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5753 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5754 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5755 | CreateSession(&session_deps))); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5756 | |
| 5757 | // Warm up the host cache so it has an entry for "www.google.com" (by doing |
| 5758 | // a synchronous lookup.) |
| 5759 | AddressList addrlist; |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 5760 | int rv = session_deps.host_resolver->Resolve( |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 5761 | HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5762 | NULL, NULL, BoundNetLog()); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5763 | EXPECT_EQ(OK, rv); |
| 5764 | |
| 5765 | // Verify that it was added to host cache, by doing a subsequent async lookup |
| 5766 | // and confirming it completes synchronously. |
| 5767 | TestCompletionCallback resolve_callback; |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 5768 | rv = session_deps.host_resolver->Resolve( |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 5769 | HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5770 | &resolve_callback, NULL, BoundNetLog()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5771 | ASSERT_EQ(OK, rv); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5772 | |
| 5773 | // Inject a failure the next time that "www.google.com" is resolved. This way |
| 5774 | // we can tell if the next lookup hit the cache, or the "network". |
| 5775 | // (cache --> success, "network" --> failure). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5776 | session_deps.host_resolver->rules()->AddSimulatedFailure("www.google.com"); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5777 | |
| 5778 | // Connect up a mock socket which will fail with ERR_UNEXPECTED during the |
| 5779 | // first read -- this won't be reached as the host resolution will fail first. |
| 5780 | MockRead data_reads[] = { MockRead(false, ERR_UNEXPECTED) }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5781 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5782 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5783 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5784 | // Run the request. |
| 5785 | TestCompletionCallback callback; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5786 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5787 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 5788 | rv = callback.WaitForResult(); |
| 5789 | |
| 5790 | // If we bypassed the cache, we would have gotten a failure while resolving |
| 5791 | // "www.google.com". |
| 5792 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 5793 | } |
| 5794 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 5795 | // There are multiple load flags that should trigger the host cache bypass. |
| 5796 | // Test each in isolation: |
| 5797 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh1) { |
| 5798 | BypassHostCacheOnRefreshHelper(LOAD_BYPASS_CACHE); |
| 5799 | } |
| 5800 | |
| 5801 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh2) { |
| 5802 | BypassHostCacheOnRefreshHelper(LOAD_VALIDATE_CACHE); |
| 5803 | } |
| 5804 | |
| 5805 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh3) { |
| 5806 | BypassHostCacheOnRefreshHelper(LOAD_DISABLE_CACHE); |
| 5807 | } |
| 5808 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5809 | // Make sure we can handle an error when writing the request. |
| 5810 | TEST_F(HttpNetworkTransactionTest, RequestWriteError) { |
| 5811 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 5812 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5813 | |
| 5814 | HttpRequestInfo request; |
| 5815 | request.method = "GET"; |
| 5816 | request.url = GURL("http://www.foo.com/"); |
| 5817 | request.load_flags = 0; |
| 5818 | |
| 5819 | MockWrite write_failure[] = { |
| 5820 | MockWrite(true, ERR_CONNECTION_RESET), |
| 5821 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5822 | StaticSocketDataProvider data(NULL, 0, |
| 5823 | write_failure, arraysize(write_failure)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5824 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5825 | |
| 5826 | TestCompletionCallback callback; |
| 5827 | |
| 5828 | scoped_ptr<HttpTransaction> trans( |
| 5829 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5830 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5831 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5832 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5833 | |
| 5834 | rv = callback.WaitForResult(); |
| 5835 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
| 5836 | } |
| 5837 | |
| 5838 | // Check that a connection closed after the start of the headers finishes ok. |
| 5839 | TEST_F(HttpNetworkTransactionTest, ConnectionClosedAfterStartOfHeaders) { |
| 5840 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 5841 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5842 | |
| 5843 | HttpRequestInfo request; |
| 5844 | request.method = "GET"; |
| 5845 | request.url = GURL("http://www.foo.com/"); |
| 5846 | request.load_flags = 0; |
| 5847 | |
| 5848 | MockRead data_reads[] = { |
| 5849 | MockRead("HTTP/1."), |
| 5850 | MockRead(false, OK), |
| 5851 | }; |
| 5852 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5853 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5854 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5855 | |
| 5856 | TestCompletionCallback callback; |
| 5857 | |
| 5858 | scoped_ptr<HttpTransaction> trans( |
| 5859 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5860 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5861 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5862 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5863 | |
| 5864 | rv = callback.WaitForResult(); |
| 5865 | EXPECT_EQ(OK, rv); |
| 5866 | |
| 5867 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5868 | EXPECT_TRUE(response != NULL); |
| 5869 | |
| 5870 | EXPECT_TRUE(response->headers != NULL); |
| 5871 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5872 | |
| 5873 | std::string response_data; |
| 5874 | rv = ReadTransaction(trans.get(), &response_data); |
| 5875 | EXPECT_EQ(OK, rv); |
| 5876 | EXPECT_EQ("", response_data); |
| 5877 | } |
| 5878 | |
| 5879 | // Make sure that a dropped connection while draining the body for auth |
| 5880 | // restart does the right thing. |
| 5881 | TEST_F(HttpNetworkTransactionTest, DrainResetOK) { |
| 5882 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 5883 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5884 | |
| 5885 | HttpRequestInfo request; |
| 5886 | request.method = "GET"; |
| 5887 | request.url = GURL("http://www.google.com/"); |
| 5888 | request.load_flags = 0; |
| 5889 | |
| 5890 | MockWrite data_writes1[] = { |
| 5891 | MockWrite("GET / HTTP/1.1\r\n" |
| 5892 | "Host: www.google.com\r\n" |
| 5893 | "Connection: keep-alive\r\n\r\n"), |
| 5894 | }; |
| 5895 | |
| 5896 | MockRead data_reads1[] = { |
| 5897 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 5898 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 5899 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5900 | MockRead("Content-Length: 14\r\n\r\n"), |
| 5901 | MockRead("Unauth"), |
| 5902 | MockRead(true, ERR_CONNECTION_RESET), |
| 5903 | }; |
| 5904 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5905 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 5906 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5907 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5908 | |
| 5909 | // After calling trans->RestartWithAuth(), this is the request we should |
| 5910 | // be issuing -- the final header line contains the credentials. |
| 5911 | MockWrite data_writes2[] = { |
| 5912 | MockWrite("GET / HTTP/1.1\r\n" |
| 5913 | "Host: www.google.com\r\n" |
| 5914 | "Connection: keep-alive\r\n" |
| 5915 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 5916 | }; |
| 5917 | |
| 5918 | // Lastly, the server responds with the actual content. |
| 5919 | MockRead data_reads2[] = { |
| 5920 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5921 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5922 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5923 | MockRead(false, OK), |
| 5924 | }; |
| 5925 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5926 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 5927 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5928 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5929 | |
| 5930 | TestCompletionCallback callback1; |
| 5931 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 5932 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5933 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5934 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5935 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5936 | |
| 5937 | rv = callback1.WaitForResult(); |
| 5938 | EXPECT_EQ(OK, rv); |
| 5939 | |
| 5940 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5941 | EXPECT_FALSE(response == NULL); |
| 5942 | |
| 5943 | // The password prompt info should have been set in response->auth_challenge. |
| 5944 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 5945 | |
| 5946 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5947 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 5948 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5949 | |
| 5950 | TestCompletionCallback callback2; |
| 5951 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5952 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5953 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5954 | |
| 5955 | rv = callback2.WaitForResult(); |
| 5956 | EXPECT_EQ(OK, rv); |
| 5957 | |
| 5958 | response = trans->GetResponseInfo(); |
| 5959 | EXPECT_FALSE(response == NULL); |
| 5960 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 5961 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 5962 | } |
| 5963 | |
| 5964 | // Test HTTPS connections going through a proxy that sends extra data. |
| 5965 | TEST_F(HttpNetworkTransactionTest, HTTPSViaProxyWithExtraData) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5966 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5967 | |
| 5968 | HttpRequestInfo request; |
| 5969 | request.method = "GET"; |
| 5970 | request.url = GURL("https://www.google.com/"); |
| 5971 | request.load_flags = 0; |
| 5972 | |
| 5973 | MockRead proxy_reads[] = { |
| 5974 | MockRead("HTTP/1.0 200 Connected\r\n\r\nExtra data"), |
| 5975 | MockRead(false, OK) |
| 5976 | }; |
| 5977 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5978 | StaticSocketDataProvider data(proxy_reads, arraysize(proxy_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5979 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5980 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5981 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5982 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5983 | |
| 5984 | TestCompletionCallback callback; |
| 5985 | |
| 5986 | session_deps.socket_factory.ResetNextMockIndexes(); |
| 5987 | |
| 5988 | scoped_ptr<HttpTransaction> trans( |
| 5989 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5990 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5991 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5992 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5993 | |
| 5994 | rv = callback.WaitForResult(); |
| 5995 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
| 5996 | } |
| 5997 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 5998 | TEST_F(HttpNetworkTransactionTest, LargeContentLengthThenClose) { |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 5999 | HttpRequestInfo request; |
| 6000 | request.method = "GET"; |
| 6001 | request.url = GURL("http://www.google.com/"); |
| 6002 | request.load_flags = 0; |
| 6003 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 6004 | SessionDependencies session_deps; |
| 6005 | scoped_ptr<HttpTransaction> trans( |
| 6006 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6007 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 6008 | MockRead data_reads[] = { |
| 6009 | MockRead("HTTP/1.0 200 OK\r\nContent-Length:6719476739\r\n\r\n"), |
| 6010 | MockRead(false, OK), |
| 6011 | }; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6012 | |
| 6013 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 6014 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6015 | |
| 6016 | TestCompletionCallback callback; |
| 6017 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6018 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6019 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6020 | |
| 6021 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6022 | |
| 6023 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6024 | EXPECT_TRUE(response != NULL); |
| 6025 | |
| 6026 | EXPECT_TRUE(response->headers != NULL); |
| 6027 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 6028 | |
| 6029 | std::string response_data; |
| 6030 | rv = ReadTransaction(trans.get(), &response_data); |
| 6031 | EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 6032 | } |
| 6033 | |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6034 | TEST_F(HttpNetworkTransactionTest, UploadFileSmallerThanLength) { |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6035 | HttpRequestInfo request; |
| 6036 | request.method = "POST"; |
| 6037 | request.url = GURL("http://www.google.com/upload"); |
| 6038 | request.upload_data = new UploadData; |
| 6039 | request.load_flags = 0; |
| 6040 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 6041 | SessionDependencies session_deps; |
| 6042 | scoped_ptr<HttpTransaction> trans( |
| 6043 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6044 | |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6045 | FilePath temp_file_path; |
| 6046 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
| 6047 | const uint64 kFakeSize = 100000; // file is actually blank |
| 6048 | |
| 6049 | std::vector<UploadData::Element> elements; |
| 6050 | UploadData::Element element; |
| 6051 | element.SetToFilePath(temp_file_path); |
| 6052 | element.SetContentLength(kFakeSize); |
| 6053 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 6054 | request.upload_data->SetElements(elements); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6055 | EXPECT_EQ(kFakeSize, request.upload_data->GetContentLength()); |
| 6056 | |
| 6057 | MockRead data_reads[] = { |
| 6058 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 6059 | MockRead("hello world"), |
| 6060 | MockRead(false, OK), |
| 6061 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 6062 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6063 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6064 | |
| 6065 | TestCompletionCallback callback; |
| 6066 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6067 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6068 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6069 | |
| 6070 | rv = callback.WaitForResult(); |
| 6071 | EXPECT_EQ(OK, rv); |
| 6072 | |
| 6073 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6074 | EXPECT_TRUE(response != NULL); |
| 6075 | |
| 6076 | EXPECT_TRUE(response->headers != NULL); |
| 6077 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 6078 | |
| 6079 | std::string response_data; |
| 6080 | rv = ReadTransaction(trans.get(), &response_data); |
| 6081 | EXPECT_EQ(OK, rv); |
| 6082 | EXPECT_EQ("hello world", response_data); |
| 6083 | |
| 6084 | file_util::Delete(temp_file_path, false); |
| 6085 | } |
| 6086 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6087 | TEST_F(HttpNetworkTransactionTest, UploadUnreadableFile) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 6088 | HttpRequestInfo request; |
| 6089 | request.method = "POST"; |
| 6090 | request.url = GURL("http://www.google.com/upload"); |
| 6091 | request.upload_data = new UploadData; |
| 6092 | request.load_flags = 0; |
| 6093 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6094 | // If we try to upload an unreadable file, the network stack should report |
| 6095 | // the file size as zero and upload zero bytes for that file. |
| 6096 | SessionDependencies session_deps; |
| 6097 | scoped_ptr<HttpTransaction> trans( |
| 6098 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6099 | |
| 6100 | FilePath temp_file; |
| 6101 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 6102 | std::string temp_file_content("Unreadable file."); |
| 6103 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_content.c_str(), |
| 6104 | temp_file_content.length())); |
| 6105 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 6106 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6107 | std::vector<UploadData::Element> elements; |
| 6108 | UploadData::Element element; |
| 6109 | element.SetToFilePath(temp_file); |
| 6110 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 6111 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6112 | |
| 6113 | MockRead data_reads[] = { |
| 6114 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 6115 | MockRead(false, OK), |
| 6116 | }; |
| 6117 | MockWrite data_writes[] = { |
| 6118 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 6119 | "Host: www.google.com\r\n" |
| 6120 | "Connection: keep-alive\r\n" |
| 6121 | "Content-Length: 0\r\n\r\n"), |
| 6122 | MockWrite(false, OK), |
| 6123 | }; |
| 6124 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 6125 | arraysize(data_writes)); |
| 6126 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6127 | |
| 6128 | TestCompletionCallback callback; |
| 6129 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6130 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6131 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6132 | |
| 6133 | rv = callback.WaitForResult(); |
| 6134 | EXPECT_EQ(OK, rv); |
| 6135 | |
| 6136 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6137 | EXPECT_TRUE(response != NULL); |
| 6138 | EXPECT_TRUE(response->headers != NULL); |
| 6139 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 6140 | |
| 6141 | file_util::Delete(temp_file, false); |
| 6142 | } |
| 6143 | |
| 6144 | TEST_F(HttpNetworkTransactionTest, UnreadableUploadFileAfterAuthRestart) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 6145 | HttpRequestInfo request; |
| 6146 | request.method = "POST"; |
| 6147 | request.url = GURL("http://www.google.com/upload"); |
| 6148 | request.upload_data = new UploadData; |
| 6149 | request.load_flags = 0; |
| 6150 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6151 | SessionDependencies session_deps; |
| 6152 | scoped_ptr<HttpTransaction> trans( |
| 6153 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6154 | |
| 6155 | FilePath temp_file; |
| 6156 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 6157 | std::string temp_file_contents("Unreadable file."); |
| 6158 | std::string unreadable_contents(temp_file_contents.length(), '\0'); |
| 6159 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_contents.c_str(), |
| 6160 | temp_file_contents.length())); |
| 6161 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6162 | std::vector<UploadData::Element> elements; |
| 6163 | UploadData::Element element; |
| 6164 | element.SetToFilePath(temp_file); |
| 6165 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 6166 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6167 | |
| 6168 | MockRead data_reads[] = { |
| 6169 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 6170 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 6171 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
| 6172 | |
| 6173 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 6174 | MockRead("Content-Length: 0\r\n\r\n"), |
| 6175 | MockRead(false, OK), |
| 6176 | }; |
| 6177 | MockWrite data_writes[] = { |
| 6178 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 6179 | "Host: www.google.com\r\n" |
| 6180 | "Connection: keep-alive\r\n" |
| 6181 | "Content-Length: 16\r\n\r\n"), |
| 6182 | MockWrite(false, temp_file_contents.c_str()), |
| 6183 | |
| 6184 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 6185 | "Host: www.google.com\r\n" |
| 6186 | "Connection: keep-alive\r\n" |
| 6187 | "Content-Length: 16\r\n" |
| 6188 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 6189 | MockWrite(false, unreadable_contents.c_str(), temp_file_contents.length()), |
| 6190 | MockWrite(false, OK), |
| 6191 | }; |
| 6192 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 6193 | arraysize(data_writes)); |
| 6194 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6195 | |
| 6196 | TestCompletionCallback callback1; |
| 6197 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6198 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6199 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6200 | |
| 6201 | rv = callback1.WaitForResult(); |
| 6202 | EXPECT_EQ(OK, rv); |
| 6203 | |
| 6204 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6205 | EXPECT_TRUE(response != NULL); |
| 6206 | EXPECT_TRUE(response->headers != NULL); |
| 6207 | EXPECT_EQ("HTTP/1.1 401 Unauthorized", response->headers->GetStatusLine()); |
| 6208 | |
| 6209 | // The password prompt info should have been set in response->auth_challenge. |
| 6210 | EXPECT_TRUE(response->auth_challenge.get() != NULL); |
| 6211 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 6212 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 6213 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 6214 | |
| 6215 | // Now make the file unreadable and try again. |
| 6216 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 6217 | |
| 6218 | TestCompletionCallback callback2; |
| 6219 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 6220 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6221 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6222 | |
| 6223 | rv = callback2.WaitForResult(); |
| 6224 | EXPECT_EQ(OK, rv); |
| 6225 | |
| 6226 | response = trans->GetResponseInfo(); |
| 6227 | EXPECT_TRUE(response != NULL); |
| 6228 | EXPECT_TRUE(response->headers != NULL); |
| 6229 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6230 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6231 | |
| 6232 | file_util::Delete(temp_file, false); |
| 6233 | } |
| 6234 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6235 | // Tests that changes to Auth realms are treated like auth rejections. |
| 6236 | TEST_F(HttpNetworkTransactionTest, ChangeAuthRealms) { |
| 6237 | SessionDependencies session_deps; |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6238 | |
| 6239 | HttpRequestInfo request; |
| 6240 | request.method = "GET"; |
| 6241 | request.url = GURL("http://www.google.com/"); |
| 6242 | request.load_flags = 0; |
| 6243 | |
| 6244 | // First transaction will request a resource and receive a Basic challenge |
| 6245 | // with realm="first_realm". |
| 6246 | MockWrite data_writes1[] = { |
| 6247 | MockWrite("GET / HTTP/1.1\r\n" |
| 6248 | "Host: www.google.com\r\n" |
| 6249 | "Connection: keep-alive\r\n" |
| 6250 | "\r\n"), |
| 6251 | }; |
| 6252 | MockRead data_reads1[] = { |
| 6253 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 6254 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 6255 | "\r\n"), |
| 6256 | }; |
| 6257 | |
| 6258 | // After calling trans->RestartWithAuth(), provide an Authentication header |
| 6259 | // for first_realm. The server will reject and provide a challenge with |
| 6260 | // second_realm. |
| 6261 | MockWrite data_writes2[] = { |
| 6262 | MockWrite("GET / HTTP/1.1\r\n" |
| 6263 | "Host: www.google.com\r\n" |
| 6264 | "Connection: keep-alive\r\n" |
| 6265 | "Authorization: Basic Zmlyc3Q6YmF6\r\n" |
| 6266 | "\r\n"), |
| 6267 | }; |
| 6268 | MockRead data_reads2[] = { |
| 6269 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 6270 | "WWW-Authenticate: Basic realm=\"second_realm\"\r\n" |
| 6271 | "\r\n"), |
| 6272 | }; |
| 6273 | |
| 6274 | // This again fails, and goes back to first_realm. Make sure that the |
| 6275 | // entry is removed from cache. |
| 6276 | MockWrite data_writes3[] = { |
| 6277 | MockWrite("GET / HTTP/1.1\r\n" |
| 6278 | "Host: www.google.com\r\n" |
| 6279 | "Connection: keep-alive\r\n" |
| 6280 | "Authorization: Basic c2Vjb25kOmZvdQ==\r\n" |
| 6281 | "\r\n"), |
| 6282 | }; |
| 6283 | MockRead data_reads3[] = { |
| 6284 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 6285 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 6286 | "\r\n"), |
| 6287 | }; |
| 6288 | |
| 6289 | // Try one last time (with the correct password) and get the resource. |
| 6290 | MockWrite data_writes4[] = { |
| 6291 | MockWrite("GET / HTTP/1.1\r\n" |
| 6292 | "Host: www.google.com\r\n" |
| 6293 | "Connection: keep-alive\r\n" |
| 6294 | "Authorization: Basic Zmlyc3Q6YmFy\r\n" |
| 6295 | "\r\n"), |
| 6296 | }; |
| 6297 | MockRead data_reads4[] = { |
| 6298 | MockRead("HTTP/1.1 200 OK\r\n" |
| 6299 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6300 | "Content-Length: 5\r\n" |
| 6301 | "\r\n" |
| 6302 | "hello"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6303 | }; |
| 6304 | |
| 6305 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 6306 | data_writes1, arraysize(data_writes1)); |
| 6307 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 6308 | data_writes2, arraysize(data_writes2)); |
| 6309 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 6310 | data_writes3, arraysize(data_writes3)); |
| 6311 | StaticSocketDataProvider data4(data_reads4, arraysize(data_reads4), |
| 6312 | data_writes4, arraysize(data_writes4)); |
| 6313 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 6314 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 6315 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 6316 | session_deps.socket_factory.AddSocketDataProvider(&data4); |
| 6317 | |
| 6318 | TestCompletionCallback callback1; |
| 6319 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6320 | scoped_ptr<HttpTransaction> trans( |
| 6321 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6322 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6323 | // Issue the first request with Authorize headers. There should be a |
| 6324 | // password prompt for first_realm waiting to be filled in after the |
| 6325 | // transaction completes. |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6326 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6327 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6328 | rv = callback1.WaitForResult(); |
| 6329 | EXPECT_EQ(OK, rv); |
| 6330 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6331 | ASSERT_FALSE(response == NULL); |
| 6332 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 6333 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 6334 | EXPECT_EQ(L"first_realm", response->auth_challenge->realm); |
| 6335 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 6336 | |
| 6337 | // Issue the second request with an incorrect password. There should be a |
| 6338 | // password prompt for second_realm waiting to be filled in after the |
| 6339 | // transaction completes. |
| 6340 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 6341 | rv = trans->RestartWithAuth(kFirst, kBaz, &callback2); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6342 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6343 | rv = callback2.WaitForResult(); |
| 6344 | EXPECT_EQ(OK, rv); |
| 6345 | response = trans->GetResponseInfo(); |
| 6346 | ASSERT_FALSE(response == NULL); |
| 6347 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 6348 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 6349 | EXPECT_EQ(L"second_realm", response->auth_challenge->realm); |
| 6350 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 6351 | |
| 6352 | // Issue the third request with another incorrect password. There should be |
| 6353 | // a password prompt for first_realm waiting to be filled in. If the password |
| 6354 | // prompt is not present, it indicates that the HttpAuthCacheEntry for |
| 6355 | // first_realm was not correctly removed. |
| 6356 | TestCompletionCallback callback3; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 6357 | rv = trans->RestartWithAuth(kSecond, kFou, &callback3); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6358 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6359 | rv = callback3.WaitForResult(); |
| 6360 | EXPECT_EQ(OK, rv); |
| 6361 | response = trans->GetResponseInfo(); |
| 6362 | ASSERT_FALSE(response == NULL); |
| 6363 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 6364 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 6365 | EXPECT_EQ(L"first_realm", response->auth_challenge->realm); |
| 6366 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 6367 | |
| 6368 | // Issue the fourth request with the correct password and username. |
| 6369 | TestCompletionCallback callback4; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 6370 | rv = trans->RestartWithAuth(kFirst, kBar, &callback4); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6371 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6372 | rv = callback4.WaitForResult(); |
| 6373 | EXPECT_EQ(OK, rv); |
| 6374 | response = trans->GetResponseInfo(); |
| 6375 | ASSERT_FALSE(response == NULL); |
| 6376 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6377 | } |
| 6378 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6379 | TEST_F(HttpNetworkTransactionTest, HonorAlternateProtocolHeader) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6380 | HttpStreamFactory::set_next_protos("needs_to_be_set_for_this_test"); |
| 6381 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6382 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6383 | SessionDependencies session_deps; |
| 6384 | |
| 6385 | MockRead data_reads[] = { |
| 6386 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6387 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6388 | MockRead("hello world"), |
| 6389 | MockRead(false, OK), |
| 6390 | }; |
| 6391 | |
| 6392 | HttpRequestInfo request; |
| 6393 | request.method = "GET"; |
| 6394 | request.url = GURL("http://www.google.com/"); |
| 6395 | request.load_flags = 0; |
| 6396 | |
| 6397 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 6398 | |
| 6399 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6400 | |
| 6401 | TestCompletionCallback callback; |
| 6402 | |
| 6403 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6404 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6405 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6406 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6407 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6408 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 6409 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6410 | const HttpAlternateProtocols& alternate_protocols = |
| 6411 | session->alternate_protocols(); |
| 6412 | EXPECT_FALSE( |
| 6413 | alternate_protocols.HasAlternateProtocolFor(http_host_port_pair)); |
| 6414 | |
| 6415 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6416 | |
| 6417 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6418 | ASSERT_TRUE(response != NULL); |
| 6419 | ASSERT_TRUE(response->headers != NULL); |
| 6420 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6421 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 6422 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 6423 | EXPECT_FALSE(response->was_alternate_protocol_available); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6424 | |
| 6425 | std::string response_data; |
| 6426 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6427 | EXPECT_EQ("hello world", response_data); |
| 6428 | |
| 6429 | ASSERT_TRUE(alternate_protocols.HasAlternateProtocolFor(http_host_port_pair)); |
| 6430 | const HttpAlternateProtocols::PortProtocolPair alternate = |
| 6431 | alternate_protocols.GetAlternateProtocolFor(http_host_port_pair); |
| 6432 | HttpAlternateProtocols::PortProtocolPair expected_alternate; |
| 6433 | expected_alternate.port = 443; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6434 | expected_alternate.protocol = HttpAlternateProtocols::NPN_SPDY_2; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6435 | EXPECT_TRUE(expected_alternate.Equals(alternate)); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6436 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6437 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6438 | HttpStreamFactory::set_next_protos(""); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6439 | } |
| 6440 | |
| 6441 | TEST_F(HttpNetworkTransactionTest, MarkBrokenAlternateProtocol) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6442 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6443 | SessionDependencies session_deps; |
| 6444 | |
| 6445 | HttpRequestInfo request; |
| 6446 | request.method = "GET"; |
| 6447 | request.url = GURL("http://www.google.com/"); |
| 6448 | request.load_flags = 0; |
| 6449 | |
| 6450 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6451 | StaticSocketDataProvider first_data; |
| 6452 | first_data.set_connect_data(mock_connect); |
| 6453 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6454 | |
| 6455 | MockRead data_reads[] = { |
| 6456 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6457 | MockRead("hello world"), |
| 6458 | MockRead(true, OK), |
| 6459 | }; |
| 6460 | StaticSocketDataProvider second_data( |
| 6461 | data_reads, arraysize(data_reads), NULL, 0); |
| 6462 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6463 | |
| 6464 | // TODO(willchan): Delete this extra data provider. It's necessary due to a |
| 6465 | // ClientSocketPoolBaseHelper bug that starts up too many ConnectJobs: |
| 6466 | // http://crbug.com/37454. |
| 6467 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6468 | |
| 6469 | TestCompletionCallback callback; |
| 6470 | |
| 6471 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6472 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 6473 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6474 | HttpAlternateProtocols* alternate_protocols = |
| 6475 | session->mutable_alternate_protocols(); |
| 6476 | alternate_protocols->SetAlternateProtocolFor( |
| 6477 | http_host_port_pair, 1234 /* port is ignored by MockConnect anyway */, |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6478 | HttpAlternateProtocols::NPN_SPDY_2); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6479 | |
| 6480 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6481 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6482 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6483 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6484 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6485 | |
| 6486 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6487 | ASSERT_TRUE(response != NULL); |
| 6488 | ASSERT_TRUE(response->headers != NULL); |
| 6489 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6490 | |
| 6491 | std::string response_data; |
| 6492 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6493 | EXPECT_EQ("hello world", response_data); |
| 6494 | |
| 6495 | ASSERT_TRUE( |
| 6496 | alternate_protocols->HasAlternateProtocolFor(http_host_port_pair)); |
| 6497 | const HttpAlternateProtocols::PortProtocolPair alternate = |
| 6498 | alternate_protocols->GetAlternateProtocolFor(http_host_port_pair); |
| 6499 | EXPECT_EQ(HttpAlternateProtocols::BROKEN, alternate.protocol); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6500 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6501 | } |
| 6502 | |
| 6503 | // TODO(willchan): Redo this test to use TLS/NPN=>SPDY. Currently, the code |
| 6504 | // says that it does SPDY, but it just does the TLS handshake, but the NPN |
| 6505 | // response does not indicate SPDY, so we just do standard HTTPS over the port. |
| 6506 | // We should add code such that we don't fallback to HTTPS, but fallback to HTTP |
| 6507 | // on the original port. |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6508 | // TEST_F(HttpNetworkTransactionTest, UseAlternateProtocol) { |
| 6509 | // SessionDependencies session_deps; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6510 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6511 | // HttpRequestInfo request; |
| 6512 | // request.method = "GET"; |
| 6513 | // request.url = GURL("http://www.google.com/"); |
| 6514 | // request.load_flags = 0; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6515 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6516 | // MockRead data_reads[] = { |
| 6517 | // MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6518 | // MockRead("hello world"), |
| 6519 | // MockRead(true, OK), |
| 6520 | // }; |
| 6521 | // StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 6522 | // session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6523 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6524 | // SSLSocketDataProvider ssl(true, OK); |
| 6525 | // session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6526 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6527 | // TestCompletionCallback callback; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6528 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6529 | // scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6530 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6531 | // HostPortPair http_host_port_pair; |
| 6532 | // http_host_port_pair.host = "www.google.com"; |
| 6533 | // http_host_port_pair.port = 80; |
| 6534 | // HttpAlternateProtocols* alternate_protocols = |
| 6535 | // session->mutable_alternate_protocols(); |
| 6536 | // alternate_protocols->SetAlternateProtocolFor( |
| 6537 | // http_host_port_pair, 1234 /* port is ignored */, |
[email protected] | 31e2c69e | 2010-04-15 18:06:06 | [diff] [blame] | 6538 | // HttpAlternateProtocols::NPN_SPDY_1); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6539 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6540 | // scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6541 | // |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6542 | // int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6543 | // EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6544 | // EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6545 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6546 | // const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6547 | // ASSERT_TRUE(response != NULL); |
| 6548 | // ASSERT_TRUE(response->headers != NULL); |
| 6549 | // EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6550 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6551 | // std::string response_data; |
| 6552 | // ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6553 | // EXPECT_EQ("hello world", response_data); |
| 6554 | // } |
| 6555 | |
| 6556 | TEST_F(HttpNetworkTransactionTest, FailNpnSpdyAndFallback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6557 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6558 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6559 | SessionDependencies session_deps; |
| 6560 | |
| 6561 | HttpRequestInfo request; |
| 6562 | request.method = "GET"; |
| 6563 | request.url = GURL("http://www.google.com/"); |
| 6564 | request.load_flags = 0; |
| 6565 | |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6566 | StaticSocketDataProvider first_tcp_connect; |
| 6567 | session_deps.socket_factory.AddSocketDataProvider(&first_tcp_connect); |
| 6568 | |
| 6569 | SSLSocketDataProvider ssl(true, OK); |
| 6570 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6571 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6572 | MockRead data_reads[] = { |
| 6573 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6574 | MockRead("hello world"), |
| 6575 | MockRead(true, OK), |
| 6576 | }; |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6577 | StaticSocketDataProvider fallback_data( |
| 6578 | data_reads, arraysize(data_reads), NULL, 0); |
| 6579 | session_deps.socket_factory.AddSocketDataProvider(&fallback_data); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6580 | |
| 6581 | TestCompletionCallback callback; |
| 6582 | |
| 6583 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6584 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 6585 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6586 | HttpAlternateProtocols* alternate_protocols = |
| 6587 | session->mutable_alternate_protocols(); |
| 6588 | alternate_protocols->SetAlternateProtocolFor( |
| 6589 | http_host_port_pair, 1234 /* port is ignored */, |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6590 | HttpAlternateProtocols::NPN_SPDY_2); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6591 | |
| 6592 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6593 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6594 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6595 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6596 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6597 | |
| 6598 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6599 | ASSERT_TRUE(response != NULL); |
| 6600 | ASSERT_TRUE(response->headers != NULL); |
| 6601 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6602 | |
| 6603 | std::string response_data; |
| 6604 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6605 | EXPECT_EQ("hello world", response_data); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6606 | HttpStreamFactory::set_next_protos(""); |
| 6607 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6608 | } |
| 6609 | |
| 6610 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6611 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6612 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6613 | SessionDependencies session_deps; |
| 6614 | |
| 6615 | HttpRequestInfo request; |
| 6616 | request.method = "GET"; |
| 6617 | request.url = GURL("http://www.google.com/"); |
| 6618 | request.load_flags = 0; |
| 6619 | |
| 6620 | MockRead data_reads[] = { |
| 6621 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6622 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6623 | MockRead("hello world"), |
| 6624 | MockRead(true, OK), |
| 6625 | }; |
| 6626 | |
| 6627 | StaticSocketDataProvider first_transaction( |
| 6628 | data_reads, arraysize(data_reads), NULL, 0); |
| 6629 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6630 | |
| 6631 | SSLSocketDataProvider ssl(true, OK); |
| 6632 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6633 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6634 | ssl.was_npn_negotiated = true; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6635 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6636 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6637 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6638 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6639 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6640 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6641 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6642 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6643 | CreateMockRead(*resp), |
| 6644 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6645 | MockRead(true, 0, 0), |
| 6646 | }; |
| 6647 | |
| 6648 | scoped_refptr<DelayedSocketData> spdy_data( |
| 6649 | new DelayedSocketData( |
| 6650 | 1, // wait for one write to finish before reading. |
| 6651 | spdy_reads, arraysize(spdy_reads), |
| 6652 | spdy_writes, arraysize(spdy_writes))); |
| 6653 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6654 | |
| 6655 | TestCompletionCallback callback; |
| 6656 | |
| 6657 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6658 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6659 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6660 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6661 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6662 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6663 | |
| 6664 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6665 | ASSERT_TRUE(response != NULL); |
| 6666 | ASSERT_TRUE(response->headers != NULL); |
| 6667 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6668 | |
| 6669 | std::string response_data; |
| 6670 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6671 | EXPECT_EQ("hello world", response_data); |
| 6672 | |
| 6673 | trans.reset(new HttpNetworkTransaction(session)); |
| 6674 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6675 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6676 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6677 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6678 | |
| 6679 | response = trans->GetResponseInfo(); |
| 6680 | ASSERT_TRUE(response != NULL); |
| 6681 | ASSERT_TRUE(response->headers != NULL); |
| 6682 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6683 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6684 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 6685 | EXPECT_TRUE(response->was_alternate_protocol_available); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6686 | |
| 6687 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6688 | EXPECT_EQ("hello!", response_data); |
| 6689 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6690 | HttpStreamFactory::set_next_protos(""); |
| 6691 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6692 | } |
| 6693 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6694 | class CapturingProxyResolver : public ProxyResolver { |
| 6695 | public: |
| 6696 | CapturingProxyResolver() : ProxyResolver(false /* expects_pac_bytes */) {} |
| 6697 | virtual ~CapturingProxyResolver() {} |
| 6698 | |
| 6699 | virtual int GetProxyForURL(const GURL& url, |
| 6700 | ProxyInfo* results, |
| 6701 | CompletionCallback* callback, |
| 6702 | RequestHandle* request, |
| 6703 | const BoundNetLog& net_log) { |
[email protected] | fae7669f | 2010-08-02 21:49:40 | [diff] [blame] | 6704 | ProxyServer proxy_server(ProxyServer::SCHEME_HTTP, |
| 6705 | HostPortPair("myproxy", 80)); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6706 | results->UseProxyServer(proxy_server); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6707 | resolved_.push_back(url); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6708 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6709 | } |
| 6710 | |
| 6711 | virtual void CancelRequest(RequestHandle request) { |
| 6712 | NOTREACHED(); |
| 6713 | } |
| 6714 | |
[email protected] | 1e60547 | 2010-12-16 21:41:40 | [diff] [blame] | 6715 | virtual void CancelSetPacScript() { |
| 6716 | NOTREACHED(); |
| 6717 | } |
| 6718 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 6719 | virtual int SetPacScript(const scoped_refptr<ProxyResolverScriptData>&, |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6720 | CompletionCallback* /*callback*/) { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6721 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6722 | } |
| 6723 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 6724 | const std::vector<GURL>& resolved() const { return resolved_; } |
| 6725 | |
| 6726 | private: |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6727 | std::vector<GURL> resolved_; |
| 6728 | |
| 6729 | DISALLOW_COPY_AND_ASSIGN(CapturingProxyResolver); |
| 6730 | }; |
| 6731 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6732 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForTunneledNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6733 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6734 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6735 | |
| 6736 | ProxyConfig proxy_config; |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6737 | proxy_config.set_auto_detect(true); |
| 6738 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 6739 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6740 | CapturingProxyResolver* capturing_proxy_resolver = |
| 6741 | new CapturingProxyResolver(); |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 6742 | SessionDependencies session_deps(new ProxyService( |
| 6743 | new ProxyConfigServiceFixed(proxy_config), capturing_proxy_resolver, |
| 6744 | NULL)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6745 | |
| 6746 | HttpRequestInfo request; |
| 6747 | request.method = "GET"; |
| 6748 | request.url = GURL("http://www.google.com/"); |
| 6749 | request.load_flags = 0; |
| 6750 | |
| 6751 | MockRead data_reads[] = { |
| 6752 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6753 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6754 | MockRead("hello world"), |
| 6755 | MockRead(true, OK), |
| 6756 | }; |
| 6757 | |
| 6758 | StaticSocketDataProvider first_transaction( |
| 6759 | data_reads, arraysize(data_reads), NULL, 0); |
| 6760 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6761 | |
| 6762 | SSLSocketDataProvider ssl(true, OK); |
| 6763 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6764 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6765 | ssl.was_npn_negotiated = true; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6766 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6767 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6768 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6769 | MockWrite spdy_writes[] = { |
| 6770 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 6771 | "Host: www.google.com\r\n" |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6772 | "Proxy-Connection: keep-alive\r\n\r\n"), // 0 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6773 | CreateMockWrite(*req) // 3 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6774 | }; |
| 6775 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6776 | const char kCONNECTResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 6777 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6778 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6779 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6780 | MockRead spdy_reads[] = { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6781 | MockRead(true, kCONNECTResponse, arraysize(kCONNECTResponse) - 1, 1), // 1 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6782 | CreateMockRead(*resp.get(), 4), // 2, 4 |
| 6783 | CreateMockRead(*data.get(), 4), // 5 |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6784 | MockRead(true, 0, 0, 4), // 6 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6785 | }; |
| 6786 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6787 | scoped_refptr<OrderedSocketData> spdy_data( |
| 6788 | new OrderedSocketData( |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6789 | spdy_reads, arraysize(spdy_reads), |
| 6790 | spdy_writes, arraysize(spdy_writes))); |
| 6791 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6792 | |
| 6793 | TestCompletionCallback callback; |
| 6794 | |
| 6795 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6796 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6797 | |
| 6798 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6799 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6800 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6801 | |
| 6802 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6803 | ASSERT_TRUE(response != NULL); |
| 6804 | ASSERT_TRUE(response->headers != NULL); |
| 6805 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6806 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 6807 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6808 | |
| 6809 | std::string response_data; |
| 6810 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6811 | EXPECT_EQ("hello world", response_data); |
| 6812 | |
| 6813 | trans.reset(new HttpNetworkTransaction(session)); |
| 6814 | |
| 6815 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6816 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6817 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6818 | |
| 6819 | response = trans->GetResponseInfo(); |
| 6820 | ASSERT_TRUE(response != NULL); |
| 6821 | ASSERT_TRUE(response->headers != NULL); |
| 6822 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6823 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6824 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6825 | |
| 6826 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6827 | EXPECT_EQ("hello!", response_data); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6828 | ASSERT_EQ(2u, capturing_proxy_resolver->resolved().size()); |
| 6829 | EXPECT_EQ("http://www.google.com/", |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6830 | capturing_proxy_resolver->resolved()[0].spec()); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6831 | EXPECT_EQ("https://www.google.com/", |
| 6832 | capturing_proxy_resolver->resolved()[1].spec()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6833 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6834 | HttpStreamFactory::set_next_protos(""); |
| 6835 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6836 | } |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6837 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6838 | TEST_F(HttpNetworkTransactionTest, |
| 6839 | UseAlternateProtocolForNpnSpdyWithExistingSpdySession) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6840 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6841 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6842 | SessionDependencies session_deps; |
| 6843 | |
| 6844 | HttpRequestInfo request; |
| 6845 | request.method = "GET"; |
| 6846 | request.url = GURL("http://www.google.com/"); |
| 6847 | request.load_flags = 0; |
| 6848 | |
| 6849 | MockRead data_reads[] = { |
| 6850 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6851 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6852 | MockRead("hello world"), |
| 6853 | MockRead(true, OK), |
| 6854 | }; |
| 6855 | |
| 6856 | StaticSocketDataProvider first_transaction( |
| 6857 | data_reads, arraysize(data_reads), NULL, 0); |
| 6858 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6859 | |
| 6860 | SSLSocketDataProvider ssl(true, OK); |
| 6861 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6862 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6863 | ssl.was_npn_negotiated = true; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6864 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6865 | // Make sure we use ssl for spdy here. |
| 6866 | SpdySession::SetSSLMode(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6867 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6868 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6869 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6870 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6871 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6872 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6873 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6874 | CreateMockRead(*resp), |
| 6875 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6876 | MockRead(true, 0, 0), |
| 6877 | }; |
| 6878 | |
| 6879 | scoped_refptr<DelayedSocketData> spdy_data( |
| 6880 | new DelayedSocketData( |
| 6881 | 1, // wait for one write to finish before reading. |
| 6882 | spdy_reads, arraysize(spdy_reads), |
| 6883 | spdy_writes, arraysize(spdy_writes))); |
| 6884 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6885 | |
| 6886 | TestCompletionCallback callback; |
| 6887 | |
| 6888 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6889 | |
| 6890 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6891 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6892 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6893 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6894 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6895 | |
| 6896 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6897 | ASSERT_TRUE(response != NULL); |
| 6898 | ASSERT_TRUE(response->headers != NULL); |
| 6899 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6900 | |
| 6901 | std::string response_data; |
| 6902 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6903 | EXPECT_EQ("hello world", response_data); |
| 6904 | |
| 6905 | // Set up an initial SpdySession in the pool to reuse. |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 6906 | HostPortPair host_port_pair("www.google.com", 443); |
| 6907 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6908 | scoped_refptr<SpdySession> spdy_session = |
[email protected] | f458033 | 2010-09-25 21:20:27 | [diff] [blame] | 6909 | session->spdy_session_pool()->Get(pair, session->mutable_spdy_settings(), |
| 6910 | BoundNetLog()); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 6911 | scoped_refptr<TCPSocketParams> tcp_params( |
| 6912 | new TCPSocketParams("www.google.com", 443, MEDIUM, GURL(), false)); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 6913 | |
| 6914 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 6915 | EXPECT_EQ(ERR_IO_PENDING, |
| 6916 | connection->Init(host_port_pair.ToString(),tcp_params, LOWEST, |
| 6917 | &callback, session->tcp_socket_pool(), |
| 6918 | BoundNetLog())); |
| 6919 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6920 | |
| 6921 | SSLConfig ssl_config; |
| 6922 | session->ssl_config_service()->GetSSLConfig(&ssl_config); |
| 6923 | ClientSocket* socket = connection->release_socket(); |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 6924 | socket = session->socket_factory()->CreateSSLClientSocket( |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 6925 | socket, HostPortPair("" , 443), ssl_config, NULL /* ssl_host_info */, |
| 6926 | session->cert_verifier()); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 6927 | connection->set_socket(socket); |
| 6928 | EXPECT_EQ(ERR_IO_PENDING, socket->Connect(&callback)); |
| 6929 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6930 | |
| 6931 | EXPECT_EQ(OK, spdy_session->InitializeWithSocket(connection.release(), |
| 6932 | true, OK)); |
| 6933 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6934 | trans.reset(new HttpNetworkTransaction(session)); |
| 6935 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6936 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6937 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6938 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6939 | |
| 6940 | response = trans->GetResponseInfo(); |
| 6941 | ASSERT_TRUE(response != NULL); |
| 6942 | ASSERT_TRUE(response->headers != NULL); |
| 6943 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6944 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6945 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 6946 | EXPECT_TRUE(response->was_alternate_protocol_available); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6947 | |
| 6948 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6949 | EXPECT_EQ("hello!", response_data); |
| 6950 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6951 | HttpStreamFactory::set_next_protos(""); |
| 6952 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6953 | } |
| 6954 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6955 | // GenerateAuthToken is a mighty big test. |
| 6956 | // It tests all permutation of GenerateAuthToken behavior: |
| 6957 | // - Synchronous and Asynchronous completion. |
| 6958 | // - OK or error on completion. |
| 6959 | // - Direct connection, non-authenticating proxy, and authenticating proxy. |
| 6960 | // - HTTP or HTTPS backend (to include proxy tunneling). |
| 6961 | // - Non-authenticating and authenticating backend. |
| 6962 | // |
| 6963 | // In all, there are 44 reasonable permuations (for example, if there are |
| 6964 | // problems generating an auth token for an authenticating proxy, we don't |
| 6965 | // need to test all permutations of the backend server). |
| 6966 | // |
| 6967 | // The test proceeds by going over each of the configuration cases, and |
| 6968 | // potentially running up to three rounds in each of the tests. The TestConfig |
| 6969 | // specifies both the configuration for the test as well as the expectations |
| 6970 | // for the results. |
| 6971 | TEST_F(HttpNetworkTransactionTest, GenerateAuthToken) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6972 | static const char kServer[] = "http://www.example.com"; |
| 6973 | static const char kSecureServer[] = "https://www.example.com"; |
| 6974 | static const char kProxy[] = "myproxy:70"; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6975 | const int kAuthErr = ERR_INVALID_AUTH_CREDENTIALS; |
| 6976 | |
| 6977 | enum AuthTiming { |
| 6978 | AUTH_NONE, |
| 6979 | AUTH_SYNC, |
| 6980 | AUTH_ASYNC, |
| 6981 | }; |
| 6982 | |
| 6983 | const MockWrite kGet( |
| 6984 | "GET / HTTP/1.1\r\n" |
| 6985 | "Host: www.example.com\r\n" |
| 6986 | "Connection: keep-alive\r\n\r\n"); |
| 6987 | const MockWrite kGetProxy( |
| 6988 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6989 | "Host: www.example.com\r\n" |
| 6990 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 6991 | const MockWrite kGetAuth( |
| 6992 | "GET / HTTP/1.1\r\n" |
| 6993 | "Host: www.example.com\r\n" |
| 6994 | "Connection: keep-alive\r\n" |
| 6995 | "Authorization: auth_token\r\n\r\n"); |
| 6996 | const MockWrite kGetProxyAuth( |
| 6997 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6998 | "Host: www.example.com\r\n" |
| 6999 | "Proxy-Connection: keep-alive\r\n" |
| 7000 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 7001 | const MockWrite kGetAuthThroughProxy( |
| 7002 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7003 | "Host: www.example.com\r\n" |
| 7004 | "Proxy-Connection: keep-alive\r\n" |
| 7005 | "Authorization: auth_token\r\n\r\n"); |
| 7006 | const MockWrite kGetAuthWithProxyAuth( |
| 7007 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7008 | "Host: www.example.com\r\n" |
| 7009 | "Proxy-Connection: keep-alive\r\n" |
| 7010 | "Proxy-Authorization: auth_token\r\n" |
| 7011 | "Authorization: auth_token\r\n\r\n"); |
| 7012 | const MockWrite kConnect( |
| 7013 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 7014 | "Host: www.example.com\r\n" |
| 7015 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 7016 | const MockWrite kConnectProxyAuth( |
| 7017 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 7018 | "Host: www.example.com\r\n" |
| 7019 | "Proxy-Connection: keep-alive\r\n" |
| 7020 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 7021 | |
| 7022 | const MockRead kSuccess( |
| 7023 | "HTTP/1.1 200 OK\r\n" |
| 7024 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7025 | "Content-Length: 3\r\n\r\n" |
| 7026 | "Yes"); |
| 7027 | const MockRead kFailure( |
| 7028 | "Should not be called."); |
| 7029 | const MockRead kServerChallenge( |
| 7030 | "HTTP/1.1 401 Unauthorized\r\n" |
| 7031 | "WWW-Authenticate: Mock realm=server\r\n" |
| 7032 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7033 | "Content-Length: 14\r\n\r\n" |
| 7034 | "Unauthorized\r\n"); |
| 7035 | const MockRead kProxyChallenge( |
| 7036 | "HTTP/1.1 407 Unauthorized\r\n" |
| 7037 | "Proxy-Authenticate: Mock realm=proxy\r\n" |
| 7038 | "Proxy-Connection: close\r\n" |
| 7039 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7040 | "Content-Length: 14\r\n\r\n" |
| 7041 | "Unauthorized\r\n"); |
| 7042 | const MockRead kProxyConnected( |
| 7043 | "HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 7044 | |
| 7045 | // NOTE(cbentzel): I wanted TestReadWriteRound to be a simple struct with |
| 7046 | // no constructors, but the C++ compiler on Windows warns about |
| 7047 | // unspecified data in compound literals. So, moved to using constructors, |
| 7048 | // and TestRound's created with the default constructor should not be used. |
| 7049 | struct TestRound { |
| 7050 | TestRound() |
| 7051 | : expected_rv(ERR_UNEXPECTED), |
| 7052 | extra_write(NULL), |
| 7053 | extra_read(NULL) { |
| 7054 | } |
| 7055 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 7056 | int expected_rv_arg) |
| 7057 | : write(write_arg), |
| 7058 | read(read_arg), |
| 7059 | expected_rv(expected_rv_arg), |
| 7060 | extra_write(NULL), |
| 7061 | extra_read(NULL) { |
| 7062 | } |
| 7063 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 7064 | int expected_rv_arg, const MockWrite* extra_write_arg, |
| 7065 | const MockWrite* extra_read_arg) |
| 7066 | : write(write_arg), |
| 7067 | read(read_arg), |
| 7068 | expected_rv(expected_rv_arg), |
| 7069 | extra_write(extra_write_arg), |
| 7070 | extra_read(extra_read_arg) { |
| 7071 | } |
| 7072 | MockWrite write; |
| 7073 | MockRead read; |
| 7074 | int expected_rv; |
| 7075 | const MockWrite* extra_write; |
| 7076 | const MockRead* extra_read; |
| 7077 | }; |
| 7078 | |
| 7079 | static const int kNoSSL = 500; |
| 7080 | |
| 7081 | struct TestConfig { |
| 7082 | const char* proxy_url; |
| 7083 | AuthTiming proxy_auth_timing; |
| 7084 | int proxy_auth_rv; |
| 7085 | const char* server_url; |
| 7086 | AuthTiming server_auth_timing; |
| 7087 | int server_auth_rv; |
| 7088 | int num_auth_rounds; |
| 7089 | int first_ssl_round; |
| 7090 | TestRound rounds[3]; |
| 7091 | } test_configs[] = { |
| 7092 | // Non-authenticating HTTP server with a direct connection. |
| 7093 | { NULL, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 7094 | { TestRound(kGet, kSuccess, OK)}}, |
| 7095 | // Authenticating HTTP server with a direct connection. |
| 7096 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 7097 | { TestRound(kGet, kServerChallenge, OK), |
| 7098 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7099 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 7100 | { TestRound(kGet, kServerChallenge, OK), |
| 7101 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7102 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 7103 | { TestRound(kGet, kServerChallenge, OK), |
| 7104 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7105 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 7106 | { TestRound(kGet, kServerChallenge, OK), |
| 7107 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7108 | // Non-authenticating HTTP server through a non-authenticating proxy. |
| 7109 | { kProxy, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 7110 | { TestRound(kGetProxy, kSuccess, OK)}}, |
| 7111 | // Authenticating HTTP server through a non-authenticating proxy. |
| 7112 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 7113 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7114 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 7115 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 7116 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7117 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 7118 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 7119 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7120 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 7121 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 7122 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7123 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 7124 | // Non-authenticating HTTP server through an authenticating proxy. |
| 7125 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7126 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7127 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 7128 | { kProxy, AUTH_SYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7129 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7130 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 7131 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7132 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7133 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 7134 | { kProxy, AUTH_ASYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7135 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7136 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 7137 | // Authenticating HTTP server through an authenticating proxy. |
| 7138 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 7139 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7140 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7141 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7142 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 7143 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7144 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7145 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7146 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 7147 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7148 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7149 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7150 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 7151 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7152 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7153 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7154 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 7155 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7156 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7157 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7158 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 7159 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7160 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7161 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7162 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 7163 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7164 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7165 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7166 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 7167 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7168 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7169 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7170 | // Non-authenticating HTTPS server with a direct connection. |
| 7171 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 7172 | { TestRound(kGet, kSuccess, OK)}}, |
| 7173 | // Authenticating HTTPS server with a direct connection. |
| 7174 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 7175 | { TestRound(kGet, kServerChallenge, OK), |
| 7176 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7177 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 7178 | { TestRound(kGet, kServerChallenge, OK), |
| 7179 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7180 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 7181 | { TestRound(kGet, kServerChallenge, OK), |
| 7182 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7183 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 7184 | { TestRound(kGet, kServerChallenge, OK), |
| 7185 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7186 | // Non-authenticating HTTPS server with a non-authenticating proxy. |
| 7187 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 7188 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 7189 | // Authenticating HTTPS server through a non-authenticating proxy. |
| 7190 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 7191 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7192 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7193 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 7194 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7195 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7196 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 7197 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7198 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7199 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 7200 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7201 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7202 | // Non-Authenticating HTTPS server through an authenticating proxy. |
| 7203 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 7204 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7205 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 7206 | { kProxy, AUTH_SYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7207 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7208 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 7209 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 7210 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7211 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 7212 | { kProxy, AUTH_ASYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7213 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7214 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 7215 | // Authenticating HTTPS server through an authenticating proxy. |
| 7216 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 7217 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7218 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7219 | &kGet, &kServerChallenge), |
| 7220 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7221 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 7222 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7223 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7224 | &kGet, &kServerChallenge), |
| 7225 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7226 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 7227 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7228 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7229 | &kGet, &kServerChallenge), |
| 7230 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7231 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 7232 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7233 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7234 | &kGet, &kServerChallenge), |
| 7235 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7236 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 7237 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7238 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7239 | &kGet, &kServerChallenge), |
| 7240 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7241 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 7242 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7243 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7244 | &kGet, &kServerChallenge), |
| 7245 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7246 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 7247 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7248 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7249 | &kGet, &kServerChallenge), |
| 7250 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7251 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 7252 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7253 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7254 | &kGet, &kServerChallenge), |
| 7255 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7256 | }; |
| 7257 | |
| 7258 | SessionDependencies session_deps; |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 7259 | HttpAuthHandlerMock::Factory* auth_factory( |
| 7260 | new HttpAuthHandlerMock::Factory()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7261 | session_deps.http_auth_handler_factory.reset(auth_factory); |
| 7262 | |
| 7263 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_configs); ++i) { |
| 7264 | const TestConfig& test_config = test_configs[i]; |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 7265 | |
| 7266 | // Set up authentication handlers as necessary. |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7267 | if (test_config.proxy_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 7268 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7269 | std::string auth_challenge = "Mock realm=proxy"; |
| 7270 | GURL origin(test_config.proxy_url); |
| 7271 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 7272 | auth_challenge.end()); |
| 7273 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_PROXY, |
| 7274 | origin, BoundNetLog()); |
| 7275 | auth_handler->SetGenerateExpectation( |
| 7276 | test_config.proxy_auth_timing == AUTH_ASYNC, |
| 7277 | test_config.proxy_auth_rv); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7278 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_PROXY); |
| 7279 | } |
| 7280 | if (test_config.server_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 7281 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7282 | std::string auth_challenge = "Mock realm=server"; |
| 7283 | GURL origin(test_config.server_url); |
| 7284 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 7285 | auth_challenge.end()); |
| 7286 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 7287 | origin, BoundNetLog()); |
| 7288 | auth_handler->SetGenerateExpectation( |
| 7289 | test_config.server_auth_timing == AUTH_ASYNC, |
| 7290 | test_config.server_auth_rv); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7291 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_SERVER); |
| 7292 | } |
| 7293 | if (test_config.proxy_url) { |
| 7294 | session_deps.proxy_service = |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 7295 | ProxyService::CreateFixed(test_config.proxy_url); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7296 | } else { |
[email protected] | ebeefff3 | 2010-09-15 05:10:02 | [diff] [blame] | 7297 | session_deps.proxy_service = ProxyService::CreateDirect(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7298 | } |
| 7299 | |
| 7300 | HttpRequestInfo request; |
| 7301 | request.method = "GET"; |
| 7302 | request.url = GURL(test_config.server_url); |
| 7303 | request.load_flags = 0; |
| 7304 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7305 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7306 | HttpNetworkTransaction trans(CreateSession(&session_deps)); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7307 | |
| 7308 | for (int round = 0; round < test_config.num_auth_rounds; ++round) { |
| 7309 | const TestRound& read_write_round = test_config.rounds[round]; |
| 7310 | |
| 7311 | // Set up expected reads and writes. |
| 7312 | MockRead reads[2]; |
| 7313 | reads[0] = read_write_round.read; |
| 7314 | size_t length_reads = 1; |
| 7315 | if (read_write_round.extra_read) { |
| 7316 | reads[1] = *read_write_round.extra_read; |
| 7317 | length_reads = 2; |
| 7318 | } |
| 7319 | |
| 7320 | MockWrite writes[2]; |
| 7321 | writes[0] = read_write_round.write; |
| 7322 | size_t length_writes = 1; |
| 7323 | if (read_write_round.extra_write) { |
| 7324 | writes[1] = *read_write_round.extra_write; |
| 7325 | length_writes = 2; |
| 7326 | } |
| 7327 | StaticSocketDataProvider data_provider( |
| 7328 | reads, length_reads, writes, length_writes); |
| 7329 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 7330 | |
| 7331 | // Add an SSL sequence if necessary. |
| 7332 | SSLSocketDataProvider ssl_socket_data_provider(false, OK); |
| 7333 | if (round >= test_config.first_ssl_round) |
| 7334 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7335 | &ssl_socket_data_provider); |
| 7336 | |
| 7337 | // Start or restart the transaction. |
| 7338 | TestCompletionCallback callback; |
| 7339 | int rv; |
| 7340 | if (round == 0) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7341 | rv = trans.Start(&request, &callback, BoundNetLog()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7342 | } else { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7343 | rv = trans.RestartWithAuth(kFoo, kBar, &callback); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7344 | } |
| 7345 | if (rv == ERR_IO_PENDING) |
| 7346 | rv = callback.WaitForResult(); |
| 7347 | |
| 7348 | // Compare results with expected data. |
| 7349 | EXPECT_EQ(read_write_round.expected_rv, rv); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7350 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7351 | if (read_write_round.expected_rv == OK) { |
| 7352 | EXPECT_FALSE(response == NULL); |
| 7353 | } else { |
| 7354 | EXPECT_TRUE(response == NULL); |
| 7355 | EXPECT_EQ(round + 1, test_config.num_auth_rounds); |
| 7356 | continue; |
| 7357 | } |
| 7358 | if (round + 1 < test_config.num_auth_rounds) { |
| 7359 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 7360 | } else { |
| 7361 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 7362 | } |
| 7363 | } |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 7364 | } |
| 7365 | } |
| 7366 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7367 | TEST_F(HttpNetworkTransactionTest, MultiRoundAuth) { |
| 7368 | // Do multi-round authentication and make sure it works correctly. |
| 7369 | SessionDependencies session_deps; |
| 7370 | HttpAuthHandlerMock::Factory* auth_factory( |
| 7371 | new HttpAuthHandlerMock::Factory()); |
| 7372 | session_deps.http_auth_handler_factory.reset(auth_factory); |
[email protected] | ebeefff3 | 2010-09-15 05:10:02 | [diff] [blame] | 7373 | session_deps.proxy_service = ProxyService::CreateDirect(); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7374 | session_deps.host_resolver->rules()->AddRule("www.example.com", "10.0.0.1"); |
| 7375 | session_deps.host_resolver->set_synchronous_mode(true); |
| 7376 | |
| 7377 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 7378 | auth_handler->set_connection_based(true); |
| 7379 | std::string auth_challenge = "Mock realm=server"; |
| 7380 | GURL origin("http://www.example.com"); |
| 7381 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 7382 | auth_challenge.end()); |
| 7383 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 7384 | origin, BoundNetLog()); |
| 7385 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_SERVER); |
| 7386 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7387 | int rv = OK; |
| 7388 | const HttpResponseInfo* response = NULL; |
| 7389 | HttpRequestInfo request; |
| 7390 | request.method = "GET"; |
| 7391 | request.url = origin; |
| 7392 | request.load_flags = 0; |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 7393 | |
| 7394 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7395 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7396 | TestCompletionCallback callback; |
| 7397 | |
| 7398 | const MockWrite kGet( |
| 7399 | "GET / HTTP/1.1\r\n" |
| 7400 | "Host: www.example.com\r\n" |
| 7401 | "Connection: keep-alive\r\n\r\n"); |
| 7402 | const MockWrite kGetAuth( |
| 7403 | "GET / HTTP/1.1\r\n" |
| 7404 | "Host: www.example.com\r\n" |
| 7405 | "Connection: keep-alive\r\n" |
| 7406 | "Authorization: auth_token\r\n\r\n"); |
| 7407 | |
| 7408 | const MockRead kServerChallenge( |
| 7409 | "HTTP/1.1 401 Unauthorized\r\n" |
| 7410 | "WWW-Authenticate: Mock realm=server\r\n" |
| 7411 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7412 | "Content-Length: 14\r\n\r\n" |
| 7413 | "Unauthorized\r\n"); |
| 7414 | const MockRead kSuccess( |
| 7415 | "HTTP/1.1 200 OK\r\n" |
| 7416 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7417 | "Content-Length: 3\r\n\r\n" |
| 7418 | "Yes"); |
| 7419 | |
| 7420 | MockWrite writes[] = { |
| 7421 | // First round |
| 7422 | kGet, |
| 7423 | // Second round |
| 7424 | kGetAuth, |
| 7425 | // Third round |
| 7426 | kGetAuth, |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7427 | // Fourth round |
| 7428 | kGetAuth |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7429 | }; |
| 7430 | MockRead reads[] = { |
| 7431 | // First round |
| 7432 | kServerChallenge, |
| 7433 | // Second round |
| 7434 | kServerChallenge, |
| 7435 | // Third round |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7436 | kServerChallenge, |
| 7437 | // Fourth round |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7438 | kSuccess, |
| 7439 | }; |
| 7440 | StaticSocketDataProvider data_provider(reads, arraysize(reads), |
| 7441 | writes, arraysize(writes)); |
| 7442 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 7443 | |
| 7444 | // First round |
| 7445 | auth_handler->SetGenerateExpectation(false, OK); |
| 7446 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 7447 | if (rv == ERR_IO_PENDING) |
| 7448 | rv = callback.WaitForResult(); |
| 7449 | EXPECT_EQ(OK, rv); |
| 7450 | response = trans->GetResponseInfo(); |
| 7451 | ASSERT_FALSE(response == NULL); |
| 7452 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 7453 | |
| 7454 | // Second round |
| 7455 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 7456 | rv = trans->RestartWithAuth(kFoo, kBar, &callback); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7457 | if (rv == ERR_IO_PENDING) |
| 7458 | rv = callback.WaitForResult(); |
| 7459 | EXPECT_EQ(OK, rv); |
| 7460 | response = trans->GetResponseInfo(); |
| 7461 | ASSERT_FALSE(response == NULL); |
| 7462 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 7463 | |
| 7464 | // Third round |
| 7465 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 7466 | rv = trans->RestartWithAuth(string16(), string16(), &callback); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7467 | if (rv == ERR_IO_PENDING) |
| 7468 | rv = callback.WaitForResult(); |
| 7469 | EXPECT_EQ(OK, rv); |
| 7470 | response = trans->GetResponseInfo(); |
| 7471 | ASSERT_FALSE(response == NULL); |
| 7472 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7473 | |
| 7474 | // Fourth round |
| 7475 | auth_handler->SetGenerateExpectation(false, OK); |
| 7476 | rv = trans->RestartWithAuth(string16(), string16(), &callback); |
| 7477 | if (rv == ERR_IO_PENDING) |
| 7478 | rv = callback.WaitForResult(); |
| 7479 | EXPECT_EQ(OK, rv); |
| 7480 | response = trans->GetResponseInfo(); |
| 7481 | ASSERT_FALSE(response == NULL); |
| 7482 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7483 | } |
| 7484 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7485 | class TLSDecompressionFailureSocketDataProvider : public SocketDataProvider { |
| 7486 | public: |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 7487 | explicit TLSDecompressionFailureSocketDataProvider(bool fail_all) |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7488 | : fail_all_(fail_all) { |
| 7489 | } |
| 7490 | |
| 7491 | virtual MockRead GetNextRead() { |
| 7492 | if (fail_all_) |
| 7493 | return MockRead(false /* async */, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
| 7494 | |
| 7495 | return MockRead(false /* async */, |
| 7496 | "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nok.\r\n"); |
| 7497 | } |
| 7498 | |
| 7499 | virtual MockWriteResult OnWrite(const std::string& data) { |
| 7500 | return MockWriteResult(false /* async */, data.size()); |
| 7501 | } |
| 7502 | |
| 7503 | void Reset() { |
| 7504 | } |
| 7505 | |
| 7506 | private: |
| 7507 | const bool fail_all_; |
| 7508 | }; |
| 7509 | |
| 7510 | // Test that we restart a connection when we see a decompression failure from |
| 7511 | // the peer during the handshake. (In the real world we'll restart with SSLv3 |
| 7512 | // and we won't offer DEFLATE in that case.) |
| 7513 | TEST_F(HttpNetworkTransactionTest, RestartAfterTLSDecompressionFailure) { |
| 7514 | HttpRequestInfo request; |
| 7515 | request.method = "GET"; |
| 7516 | request.url = GURL("https://tlsdecompressionfailure.example.com/"); |
| 7517 | request.load_flags = 0; |
| 7518 | |
| 7519 | SessionDependencies session_deps; |
| 7520 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 7521 | false /* fail all reads */); |
| 7522 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 7523 | SSLSocketDataProvider ssl_socket_data_provider1( |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 7524 | false, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7525 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 7526 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 7527 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 7528 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7529 | &ssl_socket_data_provider1); |
| 7530 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7531 | &ssl_socket_data_provider2); |
| 7532 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 7533 | // Work around http://crbug.com/37454 |
| 7534 | StaticSocketDataProvider bug37454_connection; |
| 7535 | bug37454_connection.set_connect_data(MockConnect(true, ERR_UNEXPECTED)); |
| 7536 | session_deps.socket_factory.AddSocketDataProvider(&bug37454_connection); |
| 7537 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7538 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7539 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7540 | TestCompletionCallback callback; |
| 7541 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 7542 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7543 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7544 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7545 | |
| 7546 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7547 | ASSERT_TRUE(response != NULL); |
| 7548 | ASSERT_TRUE(response->headers != NULL); |
| 7549 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7550 | |
| 7551 | std::string response_data; |
| 7552 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7553 | EXPECT_EQ("ok.", response_data); |
| 7554 | } |
| 7555 | |
| 7556 | // Test that we restart a connection if we get a decompression failure from the |
| 7557 | // peer while reading the first bytes from the connection. This occurs when the |
| 7558 | // peer cannot handle DEFLATE but we're using False Start, so we don't notice |
| 7559 | // in the handshake. |
| 7560 | TEST_F(HttpNetworkTransactionTest, |
| 7561 | RestartAfterTLSDecompressionFailureWithFalseStart) { |
| 7562 | HttpRequestInfo request; |
| 7563 | request.method = "GET"; |
| 7564 | request.url = GURL("https://tlsdecompressionfailure2.example.com/"); |
| 7565 | request.load_flags = 0; |
| 7566 | |
| 7567 | SessionDependencies session_deps; |
| 7568 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 7569 | true /* fail all reads */); |
| 7570 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 7571 | SSLSocketDataProvider ssl_socket_data_provider1(false, OK); |
| 7572 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 7573 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 7574 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 7575 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7576 | &ssl_socket_data_provider1); |
| 7577 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7578 | &ssl_socket_data_provider2); |
| 7579 | |
| 7580 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7581 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7582 | TestCompletionCallback callback; |
| 7583 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 7584 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7585 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7586 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7587 | |
| 7588 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7589 | ASSERT_TRUE(response != NULL); |
| 7590 | ASSERT_TRUE(response->headers != NULL); |
| 7591 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7592 | |
| 7593 | std::string response_data; |
| 7594 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7595 | EXPECT_EQ("ok.", response_data); |
| 7596 | } |
| 7597 | |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7598 | // This tests the case that a request is issued via http instead of spdy after |
| 7599 | // npn is negotiated. |
| 7600 | TEST_F(HttpNetworkTransactionTest, NpnWithHttpOverSSL) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7601 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 7602 | HttpStreamFactory::set_next_protos("\x08http/1.1\x07http1.1"); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7603 | SessionDependencies session_deps; |
| 7604 | HttpRequestInfo request; |
| 7605 | request.method = "GET"; |
| 7606 | request.url = GURL("https://www.google.com/"); |
| 7607 | request.load_flags = 0; |
| 7608 | |
| 7609 | MockWrite data_writes[] = { |
| 7610 | MockWrite("GET / HTTP/1.1\r\n" |
| 7611 | "Host: www.google.com\r\n" |
| 7612 | "Connection: keep-alive\r\n\r\n"), |
| 7613 | }; |
| 7614 | |
| 7615 | MockRead data_reads[] = { |
| 7616 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 7617 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7618 | MockRead("hello world"), |
| 7619 | MockRead(false, OK), |
| 7620 | }; |
| 7621 | |
| 7622 | SSLSocketDataProvider ssl(true, OK); |
| 7623 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 7624 | ssl.next_proto = "http/1.1"; |
| 7625 | |
| 7626 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7627 | |
| 7628 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 7629 | data_writes, arraysize(data_writes)); |
| 7630 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 7631 | |
| 7632 | TestCompletionCallback callback; |
| 7633 | |
| 7634 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7635 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7636 | |
| 7637 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 7638 | |
| 7639 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7640 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7641 | |
| 7642 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7643 | ASSERT_TRUE(response != NULL); |
| 7644 | ASSERT_TRUE(response->headers != NULL); |
| 7645 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7646 | |
| 7647 | std::string response_data; |
| 7648 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7649 | EXPECT_EQ("hello world", response_data); |
| 7650 | |
| 7651 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 7652 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 7653 | EXPECT_FALSE(response->was_alternate_protocol_available); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7654 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7655 | HttpStreamFactory::set_next_protos(""); |
| 7656 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7657 | } |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7658 | |
| 7659 | TEST_F(HttpNetworkTransactionTest, SpdyPostNPNServerHangup) { |
| 7660 | // Simulate the SSL handshake completing with an NPN negotiation |
| 7661 | // followed by an immediate server closing of the socket. |
| 7662 | // Fix crash: http://crbug.com/46369 |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7663 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 7664 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7665 | SessionDependencies session_deps; |
| 7666 | |
| 7667 | HttpRequestInfo request; |
| 7668 | request.method = "GET"; |
| 7669 | request.url = GURL("https://www.google.com/"); |
| 7670 | request.load_flags = 0; |
| 7671 | |
| 7672 | SSLSocketDataProvider ssl(true, OK); |
| 7673 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 7674 | ssl.next_proto = "spdy/2"; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7675 | ssl.was_npn_negotiated = true; |
| 7676 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7677 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7678 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7679 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7680 | |
| 7681 | MockRead spdy_reads[] = { |
| 7682 | MockRead(false, 0, 0) // Not async - return 0 immediately. |
| 7683 | }; |
| 7684 | |
| 7685 | scoped_refptr<DelayedSocketData> spdy_data( |
| 7686 | new DelayedSocketData( |
| 7687 | 0, // don't wait in this case, immediate hangup. |
| 7688 | spdy_reads, arraysize(spdy_reads), |
| 7689 | spdy_writes, arraysize(spdy_writes))); |
| 7690 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 7691 | |
| 7692 | TestCompletionCallback callback; |
| 7693 | |
| 7694 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7695 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 7696 | |
| 7697 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 7698 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7699 | EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult()); |
| 7700 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7701 | HttpStreamFactory::set_next_protos(""); |
| 7702 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7703 | } |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 7704 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7705 | TEST_F(HttpNetworkTransactionTest, SpdyAlternateProtocolThroughProxy) { |
| 7706 | // This test ensures that the URL passed into the proxy is upgraded |
| 7707 | // to https when doing an Alternate Protocol upgrade. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7708 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 7709 | HttpStreamFactory::set_next_protos( |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7710 | "\x08http/1.1\x07http1.1\x06spdy/2\x04spdy"); |
| 7711 | |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 7712 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7713 | HttpAuthHandlerMock::Factory* auth_factory = |
| 7714 | new HttpAuthHandlerMock::Factory(); |
| 7715 | HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
| 7716 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_PROXY); |
| 7717 | auth_factory->set_do_init_from_challenge(true); |
| 7718 | session_deps.http_auth_handler_factory.reset(auth_factory); |
| 7719 | |
| 7720 | HttpRequestInfo request; |
| 7721 | request.method = "GET"; |
| 7722 | request.url = GURL("http://www.google.com"); |
| 7723 | request.load_flags = 0; |
| 7724 | |
| 7725 | // First round goes unauthenticated through the proxy. |
| 7726 | MockWrite data_writes_1[] = { |
| 7727 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 7728 | "Host: www.google.com\r\n" |
| 7729 | "Proxy-Connection: keep-alive\r\n" |
| 7730 | "\r\n"), |
| 7731 | }; |
| 7732 | MockRead data_reads_1[] = { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7733 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7734 | MockRead("HTTP/1.1 200 OK\r\n" |
| 7735 | "Alternate-Protocol: 443:npn-spdy/2\r\n" |
| 7736 | "Proxy-Connection: close\r\n" |
| 7737 | "\r\n"), |
| 7738 | }; |
| 7739 | StaticSocketDataProvider data_1(data_reads_1, arraysize(data_reads_1), |
| 7740 | data_writes_1, arraysize(data_writes_1)); |
| 7741 | |
| 7742 | // Second round tries to tunnel to www.google.com due to the |
| 7743 | // Alternate-Protocol announcement in the first round. It fails due |
| 7744 | // to a proxy authentication challenge. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7745 | // After the failure, a tunnel is established to www.google.com using |
| 7746 | // Proxy-Authorization headers. There is then a SPDY request round. |
| 7747 | // |
| 7748 | // NOTE: Despite the "Proxy-Connection: Close", these are done on the |
| 7749 | // same MockTCPClientSocket since the underlying HttpNetworkClientSocket |
| 7750 | // does a Disconnect and Connect on the same socket, rather than trying |
| 7751 | // to obtain a new one. |
| 7752 | // |
| 7753 | // NOTE: Originally, the proxy response to the second CONNECT request |
| 7754 | // simply returned another 407 so the unit test could skip the SSL connection |
| 7755 | // establishment and SPDY framing issues. Alas, the |
| 7756 | // retry-http-when-alternate-protocol fails logic kicks in, which was more |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7757 | // complicated to set up expectations for than the SPDY session. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7758 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7759 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 7760 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 7761 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 7762 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7763 | MockWrite data_writes_2[] = { |
| 7764 | // First connection attempt without Proxy-Authorization. |
| 7765 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7766 | "Host: www.google.com\r\n" |
| 7767 | "Proxy-Connection: keep-alive\r\n" |
| 7768 | "\r\n"), |
| 7769 | |
| 7770 | // Second connection attempt with Proxy-Authorization. |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7771 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7772 | "Host: www.google.com\r\n" |
| 7773 | "Proxy-Connection: keep-alive\r\n" |
| 7774 | "Proxy-Authorization: auth_token\r\n" |
| 7775 | "\r\n"), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7776 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7777 | // SPDY request |
| 7778 | CreateMockWrite(*req), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7779 | }; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7780 | const char kRejectConnectResponse[] = ("HTTP/1.1 407 Unauthorized\r\n" |
| 7781 | "Proxy-Authenticate: Mock\r\n" |
| 7782 | "Proxy-Connection: close\r\n" |
| 7783 | "\r\n"); |
| 7784 | const char kAcceptConnectResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 7785 | MockRead data_reads_2[] = { |
| 7786 | // First connection attempt fails |
| 7787 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, 1), |
| 7788 | MockRead(true, kRejectConnectResponse, |
| 7789 | arraysize(kRejectConnectResponse) - 1, 1), |
| 7790 | |
| 7791 | // Second connection attempt passes |
| 7792 | MockRead(true, kAcceptConnectResponse, |
| 7793 | arraysize(kAcceptConnectResponse) -1, 4), |
| 7794 | |
| 7795 | // SPDY response |
| 7796 | CreateMockRead(*resp.get(), 6), |
| 7797 | CreateMockRead(*data.get(), 6), |
| 7798 | MockRead(true, 0, 0, 6), |
| 7799 | }; |
| 7800 | scoped_refptr<OrderedSocketData> data_2( |
| 7801 | new OrderedSocketData(data_reads_2, arraysize(data_reads_2), |
| 7802 | data_writes_2, arraysize(data_writes_2))); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7803 | |
| 7804 | SSLSocketDataProvider ssl(true, OK); |
| 7805 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 7806 | ssl.next_proto = "spdy/2"; |
| 7807 | ssl.was_npn_negotiated = true; |
| 7808 | |
| 7809 | session_deps.socket_factory.AddSocketDataProvider(&data_1); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7810 | session_deps.socket_factory.AddSocketDataProvider(data_2.get()); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7811 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7812 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7813 | |
| 7814 | // First round should work and provide the Alternate-Protocol state. |
| 7815 | TestCompletionCallback callback_1; |
| 7816 | scoped_ptr<HttpTransaction> trans_1(new HttpNetworkTransaction(session)); |
| 7817 | int rv = trans_1->Start(&request, &callback_1, BoundNetLog()); |
| 7818 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7819 | EXPECT_EQ(OK, callback_1.WaitForResult()); |
| 7820 | |
| 7821 | // Second round should attempt a tunnel connect and get an auth challenge. |
| 7822 | TestCompletionCallback callback_2; |
| 7823 | scoped_ptr<HttpTransaction> trans_2(new HttpNetworkTransaction(session)); |
| 7824 | rv = trans_2->Start(&request, &callback_2, BoundNetLog()); |
| 7825 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7826 | EXPECT_EQ(OK, callback_2.WaitForResult()); |
| 7827 | const HttpResponseInfo* response = trans_2->GetResponseInfo(); |
| 7828 | ASSERT_FALSE(response == NULL); |
| 7829 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 7830 | |
| 7831 | // Restart with auth. Tunnel should work and response received. |
| 7832 | TestCompletionCallback callback_3; |
| 7833 | rv = trans_2->RestartWithAuth(kFoo, kBar, &callback_3); |
| 7834 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7835 | EXPECT_EQ(OK, callback_3.WaitForResult()); |
| 7836 | |
| 7837 | // After all that work, these two lines (or actually, just the scheme) are |
| 7838 | // what this test is all about. Make sure it happens correctly. |
| 7839 | const GURL& request_url = auth_handler->request_url(); |
| 7840 | EXPECT_EQ("https", request_url.scheme()); |
| 7841 | EXPECT_EQ("www.google.com", request_url.host()); |
| 7842 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7843 | HttpStreamFactory::set_next_protos(""); |
| 7844 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 7845 | } |
| 7846 | |
| 7847 | // Test that if we cancel the transaction as the connection is completing, that |
| 7848 | // everything tears down correctly. |
| 7849 | TEST_F(HttpNetworkTransactionTest, SimpleCancel) { |
| 7850 | // Setup everything about the connection to complete synchronously, so that |
| 7851 | // after calling HttpNetworkTransaction::Start, the only thing we're waiting |
| 7852 | // for is the callback from the HttpStreamRequest. |
| 7853 | // Then cancel the transaction. |
| 7854 | // Verify that we don't crash. |
| 7855 | MockConnect mock_connect(false, OK); |
| 7856 | MockRead data_reads[] = { |
| 7857 | MockRead(false, "HTTP/1.0 200 OK\r\n\r\n"), |
| 7858 | MockRead(false, "hello world"), |
| 7859 | MockRead(false, OK), |
| 7860 | }; |
| 7861 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7862 | HttpRequestInfo request; |
| 7863 | request.method = "GET"; |
| 7864 | request.url = GURL("http://www.google.com/"); |
| 7865 | request.load_flags = 0; |
| 7866 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 7867 | SessionDependencies session_deps; |
| 7868 | session_deps.host_resolver->set_synchronous_mode(true); |
| 7869 | scoped_ptr<HttpTransaction> trans( |
| 7870 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 7871 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7872 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 7873 | data.set_connect_data(mock_connect); |
| 7874 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 7875 | |
| 7876 | TestCompletionCallback callback; |
| 7877 | |
| 7878 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7879 | int rv = trans->Start(&request, &callback, log.bound()); |
| 7880 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7881 | trans.reset(); // Cancel the transaction here. |
| 7882 | |
| 7883 | MessageLoop::current()->RunAllPending(); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7884 | } |
| 7885 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7886 | // Test a basic GET request through a proxy. |
| 7887 | TEST_F(HttpNetworkTransactionTest, ProxyGet) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 7888 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7889 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7890 | session_deps.net_log = log.bound().net_log(); |
| 7891 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7892 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7893 | HttpRequestInfo request; |
| 7894 | request.method = "GET"; |
| 7895 | request.url = GURL("http://www.google.com/"); |
| 7896 | |
| 7897 | MockWrite data_writes1[] = { |
| 7898 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 7899 | "Host: www.google.com\r\n" |
| 7900 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 7901 | }; |
| 7902 | |
| 7903 | MockRead data_reads1[] = { |
| 7904 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7905 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7906 | MockRead("Content-Length: 100\r\n\r\n"), |
| 7907 | MockRead(false, OK), |
| 7908 | }; |
| 7909 | |
| 7910 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 7911 | data_writes1, arraysize(data_writes1)); |
| 7912 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 7913 | |
| 7914 | TestCompletionCallback callback1; |
| 7915 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7916 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7917 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7918 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 7919 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7920 | |
| 7921 | rv = callback1.WaitForResult(); |
| 7922 | EXPECT_EQ(OK, rv); |
| 7923 | |
| 7924 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7925 | ASSERT_FALSE(response == NULL); |
| 7926 | |
| 7927 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 7928 | EXPECT_EQ(200, response->headers->response_code()); |
| 7929 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 7930 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 7931 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 7932 | } |
| 7933 | |
| 7934 | // Test a basic HTTPS GET request through a proxy. |
| 7935 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGet) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 7936 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7937 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7938 | session_deps.net_log = log.bound().net_log(); |
| 7939 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7940 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7941 | HttpRequestInfo request; |
| 7942 | request.method = "GET"; |
| 7943 | request.url = GURL("https://www.google.com/"); |
| 7944 | |
| 7945 | // Since we have proxy, should try to establish tunnel. |
| 7946 | MockWrite data_writes1[] = { |
| 7947 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7948 | "Host: www.google.com\r\n" |
| 7949 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 7950 | |
| 7951 | MockWrite("GET / HTTP/1.1\r\n" |
| 7952 | "Host: www.google.com\r\n" |
| 7953 | "Connection: keep-alive\r\n\r\n"), |
| 7954 | }; |
| 7955 | |
| 7956 | MockRead data_reads1[] = { |
| 7957 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 7958 | |
| 7959 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7960 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7961 | MockRead("Content-Length: 100\r\n\r\n"), |
| 7962 | MockRead(false, OK), |
| 7963 | }; |
| 7964 | |
| 7965 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 7966 | data_writes1, arraysize(data_writes1)); |
| 7967 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 7968 | SSLSocketDataProvider ssl(true, OK); |
| 7969 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7970 | |
| 7971 | TestCompletionCallback callback1; |
| 7972 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7973 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7974 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7975 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 7976 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7977 | |
| 7978 | rv = callback1.WaitForResult(); |
| 7979 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 7980 | net::CapturingNetLog::EntryList entries; |
| 7981 | log.GetEntries(&entries); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7982 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 7983 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7984 | NetLog::PHASE_NONE); |
| 7985 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 7986 | entries, pos, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7987 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 7988 | NetLog::PHASE_NONE); |
| 7989 | |
| 7990 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7991 | ASSERT_FALSE(response == NULL); |
| 7992 | |
| 7993 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 7994 | EXPECT_EQ(200, response->headers->response_code()); |
| 7995 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 7996 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 7997 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 7998 | } |
| 7999 | |
| 8000 | // Test a basic HTTPS GET request through a proxy, but the server hangs up |
| 8001 | // while establishing the tunnel. |
| 8002 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGetHangup) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 8003 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8004 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8005 | session_deps.net_log = log.bound().net_log(); |
| 8006 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8007 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8008 | HttpRequestInfo request; |
| 8009 | request.method = "GET"; |
| 8010 | request.url = GURL("https://www.google.com/"); |
| 8011 | |
| 8012 | // Since we have proxy, should try to establish tunnel. |
| 8013 | MockWrite data_writes1[] = { |
| 8014 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 8015 | "Host: www.google.com\r\n" |
| 8016 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 8017 | |
| 8018 | MockWrite("GET / HTTP/1.1\r\n" |
| 8019 | "Host: www.google.com\r\n" |
| 8020 | "Connection: keep-alive\r\n\r\n"), |
| 8021 | }; |
| 8022 | |
| 8023 | MockRead data_reads1[] = { |
| 8024 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 8025 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 8026 | MockRead(true, 0, 0), // EOF |
| 8027 | }; |
| 8028 | |
| 8029 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 8030 | data_writes1, arraysize(data_writes1)); |
| 8031 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8032 | SSLSocketDataProvider ssl(true, OK); |
| 8033 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8034 | |
| 8035 | TestCompletionCallback callback1; |
| 8036 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 8037 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 8038 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8039 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 8040 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8041 | |
| 8042 | rv = callback1.WaitForResult(); |
| 8043 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8044 | net::CapturingNetLog::EntryList entries; |
| 8045 | log.GetEntries(&entries); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8046 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8047 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8048 | NetLog::PHASE_NONE); |
| 8049 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8050 | entries, pos, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8051 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 8052 | NetLog::PHASE_NONE); |
| 8053 | } |
| 8054 | |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8055 | // Test for crbug.com/55424. |
| 8056 | TEST_F(HttpNetworkTransactionTest, PreconnectWithExistingSpdySession) { |
| 8057 | SessionDependencies session_deps; |
| 8058 | |
| 8059 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet( |
| 8060 | "https://www.google.com", false, 1, LOWEST)); |
| 8061 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
| 8062 | |
| 8063 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 8064 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 8065 | MockRead spdy_reads[] = { |
| 8066 | CreateMockRead(*resp), |
| 8067 | CreateMockRead(*data), |
| 8068 | MockRead(true, 0, 0), |
| 8069 | }; |
| 8070 | |
| 8071 | scoped_refptr<DelayedSocketData> spdy_data( |
| 8072 | new DelayedSocketData( |
| 8073 | 1, // wait for one write to finish before reading. |
| 8074 | spdy_reads, arraysize(spdy_reads), |
| 8075 | spdy_writes, arraysize(spdy_writes))); |
| 8076 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 8077 | |
| 8078 | SSLSocketDataProvider ssl(true, OK); |
| 8079 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 8080 | ssl.next_proto = "spdy/2"; |
| 8081 | ssl.was_npn_negotiated = true; |
| 8082 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8083 | |
| 8084 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8085 | |
| 8086 | // Set up an initial SpdySession in the pool to reuse. |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 8087 | HostPortPair host_port_pair("www.google.com", 443); |
| 8088 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8089 | scoped_refptr<SpdySession> spdy_session = |
[email protected] | f458033 | 2010-09-25 21:20:27 | [diff] [blame] | 8090 | session->spdy_session_pool()->Get(pair, session->mutable_spdy_settings(), |
| 8091 | BoundNetLog()); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 8092 | scoped_refptr<TCPSocketParams> tcp_params( |
| 8093 | new TCPSocketParams("www.google.com", 443, MEDIUM, GURL(), false)); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 8094 | TestCompletionCallback callback; |
| 8095 | |
| 8096 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 8097 | EXPECT_EQ(ERR_IO_PENDING, |
| 8098 | connection->Init(host_port_pair.ToString(), tcp_params, LOWEST, |
| 8099 | &callback, session->tcp_socket_pool(), |
| 8100 | BoundNetLog())); |
| 8101 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 8102 | spdy_session->InitializeWithSocket(connection.release(), false, OK); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8103 | |
| 8104 | HttpRequestInfo request; |
| 8105 | request.method = "GET"; |
| 8106 | request.url = GURL("https://www.google.com/"); |
| 8107 | request.load_flags = 0; |
| 8108 | |
| 8109 | // This is the important line that marks this as a preconnect. |
| 8110 | request.motivation = HttpRequestInfo::PRECONNECT_MOTIVATED; |
| 8111 | |
| 8112 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8113 | |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8114 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 8115 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8116 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 8117 | } |
| 8118 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8119 | // Given a net error, cause that error to be returned from the first Write() |
| 8120 | // call and verify that the HttpTransaction fails with that error. |
| 8121 | static void CheckErrorIsPassedBack(int error, bool async) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 8122 | net::HttpRequestInfo request_info; |
| 8123 | request_info.url = GURL("https://www.example.com/"); |
| 8124 | request_info.method = "GET"; |
| 8125 | request_info.load_flags = net::LOAD_NORMAL; |
| 8126 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8127 | SessionDependencies session_deps; |
| 8128 | |
| 8129 | SSLSocketDataProvider ssl_data(async, OK); |
| 8130 | net::MockWrite data_writes[] = { |
| 8131 | net::MockWrite(async, error), |
| 8132 | }; |
| 8133 | net::StaticSocketDataProvider data(NULL, 0, |
| 8134 | data_writes, arraysize(data_writes)); |
| 8135 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 8136 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data); |
| 8137 | |
| 8138 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8139 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8140 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8141 | TestCompletionCallback callback; |
| 8142 | int rv = trans->Start(&request_info, &callback, net::BoundNetLog()); |
| 8143 | if (rv == net::ERR_IO_PENDING) |
| 8144 | rv = callback.WaitForResult(); |
| 8145 | ASSERT_EQ(error, rv); |
| 8146 | } |
| 8147 | |
| 8148 | TEST_F(HttpNetworkTransactionTest, SSLWriteCertError) { |
| 8149 | // Just check a grab bag of cert errors. |
| 8150 | static const int kErrors[] = { |
| 8151 | ERR_CERT_COMMON_NAME_INVALID, |
| 8152 | ERR_CERT_AUTHORITY_INVALID, |
| 8153 | ERR_CERT_DATE_INVALID, |
| 8154 | }; |
| 8155 | for (size_t i = 0; i < arraysize(kErrors); i++) { |
| 8156 | CheckErrorIsPassedBack(kErrors[i], false /* not async */); |
| 8157 | CheckErrorIsPassedBack(kErrors[i], true /* async */); |
| 8158 | } |
| 8159 | } |
| 8160 | |
| 8161 | // Test that the transaction is restarted in the event of an NPN misprediction. |
| 8162 | TEST_F(HttpNetworkTransactionTest, NPNMispredict) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 8163 | net::HttpRequestInfo request_info; |
| 8164 | request_info.url = GURL("https://www.example.com/"); |
| 8165 | request_info.method = "GET"; |
| 8166 | request_info.load_flags = net::LOAD_NORMAL; |
| 8167 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8168 | SessionDependencies session_deps; |
| 8169 | |
| 8170 | SSLSocketDataProvider ssl_data1(true /* async */, OK); |
| 8171 | SSLSocketDataProvider ssl_data2(true /* async */, OK); |
| 8172 | |
| 8173 | net::MockWrite data1_writes[] = { |
| 8174 | net::MockWrite(true /* async */, ERR_SSL_SNAP_START_NPN_MISPREDICTION), |
| 8175 | }; |
| 8176 | net::MockRead data2_reads[] = { |
| 8177 | net::MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 8178 | net::MockRead("hello world"), |
| 8179 | net::MockRead(false, net::OK), |
| 8180 | }; |
| 8181 | net::MockWrite data2_writes[] = { |
| 8182 | net::MockWrite("GET / HTTP/1.1\r\n" |
| 8183 | "Host: www.example.com\r\n" |
| 8184 | "Connection: keep-alive\r\n\r\n"), |
| 8185 | }; |
| 8186 | net::StaticSocketDataProvider data1( |
| 8187 | NULL, 0, data1_writes, arraysize(data1_writes)); |
| 8188 | net::StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), |
| 8189 | data2_writes, arraysize(data2_writes)); |
| 8190 | |
| 8191 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8192 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8193 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8194 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8195 | |
| 8196 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8197 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8198 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8199 | TestCompletionCallback callback; |
| 8200 | int rv = trans->Start(&request_info, &callback, net::BoundNetLog()); |
| 8201 | if (rv == net::ERR_IO_PENDING) |
| 8202 | rv = callback.WaitForResult(); |
| 8203 | ASSERT_EQ(OK, rv); |
| 8204 | |
| 8205 | std::string contents; |
| 8206 | rv = ReadTransaction(trans.get(), &contents); |
| 8207 | EXPECT_EQ(net::OK, rv); |
| 8208 | EXPECT_EQ("hello world", contents); |
| 8209 | } |
| 8210 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8211 | // Ensure that a client certificate is removed from the SSL client auth |
| 8212 | // cache when: |
| 8213 | // 1) No proxy is involved. |
| 8214 | // 2) TLS False Start is disabled. |
| 8215 | // 3) The initial TLS handshake requests a client certificate. |
| 8216 | // 4) The client supplies an invalid/unacceptable certificate. |
| 8217 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_NoFalseStart) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 8218 | net::HttpRequestInfo request_info; |
| 8219 | request_info.url = GURL("https://www.example.com/"); |
| 8220 | request_info.method = "GET"; |
| 8221 | request_info.load_flags = net::LOAD_NORMAL; |
| 8222 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8223 | SessionDependencies session_deps; |
| 8224 | |
| 8225 | scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); |
| 8226 | cert_request->host_and_port = "www.example.com:443"; |
| 8227 | |
| 8228 | // [ssl_]data1 contains the data for the first SSL handshake. When a |
| 8229 | // CertificateRequest is received for the first time, the handshake will |
| 8230 | // be aborted to allow the caller to provide a certificate. |
| 8231 | SSLSocketDataProvider ssl_data1(true /* async */, |
| 8232 | net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 8233 | ssl_data1.cert_request_info = cert_request.get(); |
| 8234 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8235 | net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| 8236 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8237 | |
| 8238 | // [ssl_]data2 contains the data for the second SSL handshake. When TLS |
| 8239 | // False Start is not being used, the result of the SSL handshake will be |
| 8240 | // returned as part of the SSLClientSocket::Connect() call. This test |
| 8241 | // matches the result of a server sending a handshake_failure alert, |
| 8242 | // rather than a Finished message, because it requires a client |
| 8243 | // certificate and none was supplied. |
| 8244 | SSLSocketDataProvider ssl_data2(true /* async */, |
| 8245 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8246 | ssl_data2.cert_request_info = cert_request.get(); |
| 8247 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8248 | net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); |
| 8249 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8250 | |
| 8251 | // [ssl_]data3 contains the data for the third SSL handshake. When a |
| 8252 | // connection to a server fails during an SSL handshake, |
| 8253 | // HttpNetworkTransaction will attempt to fallback to SSLv3 if the initial |
| 8254 | // connection was attempted with TLSv1. This is transparent to the caller |
| 8255 | // of the HttpNetworkTransaction. Because this test failure is due to |
| 8256 | // requiring a client certificate, this fallback handshake should also |
| 8257 | // fail. |
| 8258 | SSLSocketDataProvider ssl_data3(true /* async */, |
| 8259 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8260 | ssl_data3.cert_request_info = cert_request.get(); |
| 8261 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data3); |
| 8262 | net::StaticSocketDataProvider data3(NULL, 0, NULL, 0); |
| 8263 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 8264 | |
| 8265 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8266 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8267 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8268 | // Begin the SSL handshake with the peer. This consumes ssl_data1. |
| 8269 | TestCompletionCallback callback; |
| 8270 | int rv = trans->Start(&request_info, &callback, net::BoundNetLog()); |
| 8271 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8272 | |
| 8273 | // Complete the SSL handshake, which should abort due to requiring a |
| 8274 | // client certificate. |
| 8275 | rv = callback.WaitForResult(); |
| 8276 | ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 8277 | |
| 8278 | // Indicate that no certificate should be supplied. From the perspective |
| 8279 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 8280 | // certificate, so this is the same as supply a |
| 8281 | // legitimate-but-unacceptable certificate. |
| 8282 | rv = trans->RestartWithCertificate(NULL, &callback); |
| 8283 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8284 | |
| 8285 | // Ensure the certificate was added to the client auth cache before |
| 8286 | // allowing the connection to continue restarting. |
| 8287 | scoped_refptr<X509Certificate> client_cert; |
| 8288 | ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8289 | &client_cert)); |
| 8290 | ASSERT_EQ(NULL, client_cert.get()); |
| 8291 | |
| 8292 | // Restart the handshake. This will consume ssl_data2, which fails, and |
| 8293 | // then consume ssl_data3, which should also fail. The result code is |
| 8294 | // checked against what ssl_data3 should return. |
| 8295 | rv = callback.WaitForResult(); |
| 8296 | ASSERT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); |
| 8297 | |
| 8298 | // Ensure that the client certificate is removed from the cache on a |
| 8299 | // handshake failure. |
| 8300 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8301 | &client_cert)); |
| 8302 | } |
| 8303 | |
| 8304 | // Ensure that a client certificate is removed from the SSL client auth |
| 8305 | // cache when: |
| 8306 | // 1) No proxy is involved. |
| 8307 | // 2) TLS False Start is enabled. |
| 8308 | // 3) The initial TLS handshake requests a client certificate. |
| 8309 | // 4) The client supplies an invalid/unacceptable certificate. |
| 8310 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_FalseStart) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame^] | 8311 | net::HttpRequestInfo request_info; |
| 8312 | request_info.url = GURL("https://www.example.com/"); |
| 8313 | request_info.method = "GET"; |
| 8314 | request_info.load_flags = net::LOAD_NORMAL; |
| 8315 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8316 | SessionDependencies session_deps; |
| 8317 | |
| 8318 | scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); |
| 8319 | cert_request->host_and_port = "www.example.com:443"; |
| 8320 | |
| 8321 | // When TLS False Start is used, SSLClientSocket::Connect() calls will |
| 8322 | // return successfully after reading up to the peer's Certificate message. |
| 8323 | // This is to allow the caller to call SSLClientSocket::Write(), which can |
| 8324 | // enqueue application data to be sent in the same packet as the |
| 8325 | // ChangeCipherSpec and Finished messages. |
| 8326 | // The actual handshake will be finished when SSLClientSocket::Read() is |
| 8327 | // called, which expects to process the peer's ChangeCipherSpec and |
| 8328 | // Finished messages. If there was an error negotiating with the peer, |
| 8329 | // such as due to the peer requiring a client certificate when none was |
| 8330 | // supplied, the alert sent by the peer won't be processed until Read() is |
| 8331 | // called. |
| 8332 | |
| 8333 | // Like the non-False Start case, when a client certificate is requested by |
| 8334 | // the peer, the handshake is aborted during the Connect() call. |
| 8335 | // [ssl_]data1 represents the initial SSL handshake with the peer. |
| 8336 | SSLSocketDataProvider ssl_data1(true /* async */, |
| 8337 | net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 8338 | ssl_data1.cert_request_info = cert_request.get(); |
| 8339 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8340 | net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| 8341 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8342 | |
| 8343 | // When a client certificate is supplied, Connect() will not be aborted |
| 8344 | // when the peer requests the certificate. Instead, the handshake will |
| 8345 | // artificially succeed, allowing the caller to write the HTTP request to |
| 8346 | // the socket. The handshake messages are not processed until Read() is |
| 8347 | // called, which then detects that the handshake was aborted, due to the |
| 8348 | // peer sending a handshake_failure because it requires a client |
| 8349 | // certificate. |
| 8350 | SSLSocketDataProvider ssl_data2(true /* async */, net::OK); |
| 8351 | ssl_data2.cert_request_info = cert_request.get(); |
| 8352 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8353 | net::MockRead data2_reads[] = { |
| 8354 | net::MockRead(true /* async */, net::ERR_SSL_PROTOCOL_ERROR), |
| 8355 | }; |
| 8356 | net::StaticSocketDataProvider data2( |
| 8357 | data2_reads, arraysize(data2_reads), NULL, 0); |
| 8358 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8359 | |
| 8360 | // As described in ClientAuthCertCache_Direct_NoFalseStart, [ssl_]data3 is |
| 8361 | // the data for the SSL handshake once the TLSv1 connection falls back to |
| 8362 | // SSLv3. It has the same behaviour as [ssl_]data2. |
| 8363 | SSLSocketDataProvider ssl_data3(true /* async */, net::OK); |
| 8364 | ssl_data3.cert_request_info = cert_request.get(); |
| 8365 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data3); |
| 8366 | net::StaticSocketDataProvider data3( |
| 8367 | data2_reads, arraysize(data2_reads), NULL, 0); |
| 8368 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 8369 | |
| 8370 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8371 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8372 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8373 | // Begin the initial SSL handshake. |
| 8374 | TestCompletionCallback callback; |
| 8375 | int rv = trans->Start(&request_info, &callback, net::BoundNetLog()); |
| 8376 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8377 | |
| 8378 | // Complete the SSL handshake, which should abort due to requiring a |
| 8379 | // client certificate. |
| 8380 | rv = callback.WaitForResult(); |
| 8381 | ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 8382 | |
| 8383 | // Indicate that no certificate should be supplied. From the perspective |
| 8384 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 8385 | // certificate, so this is the same as supply a |
| 8386 | // legitimate-but-unacceptable certificate. |
| 8387 | rv = trans->RestartWithCertificate(NULL, &callback); |
| 8388 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8389 | |
| 8390 | // Ensure the certificate was added to the client auth cache before |
| 8391 | // allowing the connection to continue restarting. |
| 8392 | scoped_refptr<X509Certificate> client_cert; |
| 8393 | ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8394 | &client_cert)); |
| 8395 | ASSERT_EQ(NULL, client_cert.get()); |
| 8396 | |
| 8397 | |
| 8398 | // Restart the handshake. This will consume ssl_data2, which fails, and |
| 8399 | // then consume ssl_data3, which should also fail. The result code is |
| 8400 | // checked against what ssl_data3 should return. |
| 8401 | rv = callback.WaitForResult(); |
| 8402 | ASSERT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); |
| 8403 | |
| 8404 | // Ensure that the client certificate is removed from the cache on a |
| 8405 | // handshake failure. |
| 8406 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8407 | &client_cert)); |
| 8408 | } |
| 8409 | |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 8410 | // Ensure that a client certificate is removed from the SSL client auth |
| 8411 | // cache when: |
| 8412 | // 1) An HTTPS proxy is involved. |
| 8413 | // 3) The HTTPS proxy requests a client certificate. |
| 8414 | // 4) The client supplies an invalid/unacceptable certificate for the |
| 8415 | // proxy. |
| 8416 | // The test is repeated twice, first for connecting to an HTTPS endpoint, |
| 8417 | // then for connecting to an HTTP endpoint. |
| 8418 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Proxy_Fail) { |
| 8419 | SessionDependencies session_deps( |
| 8420 | ProxyService::CreateFixed("https://proxy:70")); |
| 8421 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8422 | session_deps.net_log = log.bound().net_log(); |
| 8423 | |
| 8424 | scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); |
| 8425 | cert_request->host_and_port = "proxy:70"; |
| 8426 | |
| 8427 | // See ClientAuthCertCache_Direct_NoFalseStart for the explanation of |
| 8428 | // [ssl_]data[1-3]. Rather than represending the endpoint |
| 8429 | // (www.example.com:443), they represent failures with the HTTPS proxy |
| 8430 | // (proxy:70). |
| 8431 | SSLSocketDataProvider ssl_data1(true /* async */, |
| 8432 | net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 8433 | ssl_data1.cert_request_info = cert_request.get(); |
| 8434 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8435 | net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| 8436 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8437 | |
| 8438 | SSLSocketDataProvider ssl_data2(true /* async */, |
| 8439 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8440 | ssl_data2.cert_request_info = cert_request.get(); |
| 8441 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8442 | net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); |
| 8443 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8444 | |
| 8445 | SSLSocketDataProvider ssl_data3(true /* async */, |
| 8446 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8447 | ssl_data3.cert_request_info = cert_request.get(); |
| 8448 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data3); |
| 8449 | net::StaticSocketDataProvider data3(NULL, 0, NULL, 0); |
| 8450 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 8451 | |
| 8452 | net::HttpRequestInfo requests[2]; |
| 8453 | requests[0].url = GURL("https://www.example.com/"); |
| 8454 | requests[0].method = "GET"; |
| 8455 | requests[0].load_flags = net::LOAD_NORMAL; |
| 8456 | |
| 8457 | requests[1].url = GURL("http://www.example.com/"); |
| 8458 | requests[1].method = "GET"; |
| 8459 | requests[1].load_flags = net::LOAD_NORMAL; |
| 8460 | |
| 8461 | for (size_t i = 0; i < arraysize(requests); ++i) { |
| 8462 | session_deps.socket_factory.ResetNextMockIndexes(); |
| 8463 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8464 | scoped_ptr<HttpNetworkTransaction> trans( |
| 8465 | new HttpNetworkTransaction(session)); |
| 8466 | |
| 8467 | // Begin the SSL handshake with the proxy. |
| 8468 | TestCompletionCallback callback; |
| 8469 | int rv = trans->Start(&requests[i], &callback, net::BoundNetLog()); |
| 8470 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8471 | |
| 8472 | // Complete the SSL handshake, which should abort due to requiring a |
| 8473 | // client certificate. |
| 8474 | rv = callback.WaitForResult(); |
| 8475 | ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 8476 | |
| 8477 | // Indicate that no certificate should be supplied. From the perspective |
| 8478 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 8479 | // certificate, so this is the same as supply a |
| 8480 | // legitimate-but-unacceptable certificate. |
| 8481 | rv = trans->RestartWithCertificate(NULL, &callback); |
| 8482 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8483 | |
| 8484 | // Ensure the certificate was added to the client auth cache before |
| 8485 | // allowing the connection to continue restarting. |
| 8486 | scoped_refptr<X509Certificate> client_cert; |
| 8487 | ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("proxy:70", |
| 8488 | &client_cert)); |
| 8489 | ASSERT_EQ(NULL, client_cert.get()); |
| 8490 | // Ensure the certificate was NOT cached for the endpoint. This only |
| 8491 | // applies to HTTPS requests, but is fine to check for HTTP requests. |
| 8492 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8493 | &client_cert)); |
| 8494 | |
| 8495 | // Restart the handshake. This will consume ssl_data2, which fails, and |
| 8496 | // then consume ssl_data3, which should also fail. The result code is |
| 8497 | // checked against what ssl_data3 should return. |
| 8498 | rv = callback.WaitForResult(); |
| 8499 | ASSERT_EQ(net::ERR_PROXY_CONNECTION_FAILED, rv); |
| 8500 | |
| 8501 | // Now that the new handshake has failed, ensure that the client |
| 8502 | // certificate was removed from the client auth cache. |
| 8503 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("proxy:70", |
| 8504 | &client_cert)); |
| 8505 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8506 | &client_cert)); |
| 8507 | } |
| 8508 | } |
| 8509 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 8510 | } // namespace net |