OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/http/http_network_transaction.h" | |
6 | |
7 #include <math.h> // ceil | |
8 #include <stdarg.h> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/compiler_specific.h" | |
14 #include "base/file_util.h" | |
15 #include "base/files/file_path.h" | |
16 #include "base/json/json_writer.h" | |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "base/strings/string_util.h" | |
19 #include "base/strings/utf_string_conversions.h" | |
20 #include "base/test/test_file_util.h" | |
21 #include "net/base/auth.h" | |
22 #include "net/base/capturing_net_log.h" | |
23 #include "net/base/completion_callback.h" | |
24 #include "net/base/load_timing_info.h" | |
25 #include "net/base/load_timing_info_test_util.h" | |
26 #include "net/base/net_log.h" | |
27 #include "net/base/net_log_unittest.h" | |
28 #include "net/base/request_priority.h" | |
29 #include "net/base/test_completion_callback.h" | |
30 #include "net/base/test_data_directory.h" | |
31 #include "net/base/upload_bytes_element_reader.h" | |
32 #include "net/base/upload_data_stream.h" | |
33 #include "net/base/upload_file_element_reader.h" | |
34 #include "net/cert/mock_cert_verifier.h" | |
35 #include "net/dns/host_cache.h" | |
36 #include "net/dns/mock_host_resolver.h" | |
37 #include "net/http/http_auth_handler_digest.h" | |
38 #include "net/http/http_auth_handler_mock.h" | |
39 #include "net/http/http_auth_handler_ntlm.h" | |
40 #include "net/http/http_basic_stream.h" | |
41 #include "net/http/http_network_session.h" | |
42 #include "net/http/http_network_session_peer.h" | |
43 #include "net/http/http_server_properties_impl.h" | |
44 #include "net/http/http_stream.h" | |
45 #include "net/http/http_stream_factory.h" | |
46 #include "net/http/http_transaction_unittest.h" | |
47 #include "net/proxy/proxy_config_service_fixed.h" | |
48 #include "net/proxy/proxy_resolver.h" | |
49 #include "net/proxy/proxy_service.h" | |
50 #include "net/socket/client_socket_factory.h" | |
51 #include "net/socket/client_socket_pool_manager.h" | |
52 #include "net/socket/mock_client_socket_pool_manager.h" | |
53 #include "net/socket/next_proto.h" | |
54 #include "net/socket/socket_test_util.h" | |
55 #include "net/socket/ssl_client_socket.h" | |
56 #include "net/spdy/spdy_framer.h" | |
57 #include "net/spdy/spdy_session.h" | |
58 #include "net/spdy/spdy_session_pool.h" | |
59 #include "net/spdy/spdy_test_util_spdy2.h" | |
60 #include "net/ssl/ssl_cert_request_info.h" | |
61 #include "net/ssl/ssl_config_service_defaults.h" | |
62 #include "net/ssl/ssl_info.h" | |
63 #include "net/test/cert_test_util.h" | |
64 #include "testing/gtest/include/gtest/gtest.h" | |
65 #include "testing/platform_test.h" | |
66 | |
67 using namespace net::test_spdy2; | |
68 | |
69 //----------------------------------------------------------------------------- | |
70 | |
71 namespace { | |
72 | |
73 const base::string16 kBar(ASCIIToUTF16("bar")); | |
74 const base::string16 kBar2(ASCIIToUTF16("bar2")); | |
75 const base::string16 kBar3(ASCIIToUTF16("bar3")); | |
76 const base::string16 kBaz(ASCIIToUTF16("baz")); | |
77 const base::string16 kFirst(ASCIIToUTF16("first")); | |
78 const base::string16 kFoo(ASCIIToUTF16("foo")); | |
79 const base::string16 kFoo2(ASCIIToUTF16("foo2")); | |
80 const base::string16 kFoo3(ASCIIToUTF16("foo3")); | |
81 const base::string16 kFou(ASCIIToUTF16("fou")); | |
82 const base::string16 kSecond(ASCIIToUTF16("second")); | |
83 const base::string16 kTestingNTLM(ASCIIToUTF16("testing-ntlm")); | |
84 const base::string16 kWrongPassword(ASCIIToUTF16("wrongpassword")); | |
85 | |
86 // MakeNextProtos is a utility function that returns a vector of std::strings | |
87 // from its arguments. Don't forget to terminate the argument list with a NULL. | |
88 std::vector<std::string> MakeNextProtos(const char* a, ...) { | |
89 std::vector<std::string> ret; | |
90 ret.push_back(a); | |
91 | |
92 va_list args; | |
93 va_start(args, a); | |
94 | |
95 for (;;) { | |
96 const char* value = va_arg(args, const char*); | |
97 if (value == NULL) | |
98 break; | |
99 ret.push_back(value); | |
100 } | |
101 va_end(args); | |
102 | |
103 return ret; | |
104 } | |
105 | |
106 // SpdyNextProtos returns a vector of NPN protocol strings for negotiating | |
107 // SPDY. | |
108 std::vector<std::string> SpdyNextProtos() { | |
109 return MakeNextProtos("http/1.1", "spdy/2", NULL); | |
110 } | |
111 | |
112 int GetIdleSocketCountInTransportSocketPool(net::HttpNetworkSession* session) { | |
113 return session->GetTransportSocketPool( | |
114 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); | |
115 } | |
116 | |
117 int GetIdleSocketCountInSSLSocketPool(net::HttpNetworkSession* session) { | |
118 return session->GetSSLSocketPool( | |
119 net::HttpNetworkSession::NORMAL_SOCKET_POOL)->IdleSocketCount(); | |
120 } | |
121 | |
122 // Takes in a Value created from a NetLogHttpResponseParameter, and returns | |
123 // a JSONified list of headers as a single string. Uses single quotes instead | |
124 // of double quotes for easier comparison. Returns false on failure. | |
125 bool GetHeaders(base::DictionaryValue* params, std::string* headers) { | |
126 if (!params) | |
127 return false; | |
128 base::ListValue* header_list; | |
129 if (!params->GetList("headers", &header_list)) | |
130 return false; | |
131 std::string double_quote_headers; | |
132 base::JSONWriter::Write(header_list, &double_quote_headers); | |
133 ReplaceChars(double_quote_headers, "\"", "'", headers); | |
134 return true; | |
135 } | |
136 | |
137 // Tests LoadTimingInfo in the case a socket is reused and no PAC script is | |
138 // used. | |
139 void TestLoadTimingReused(const net::LoadTimingInfo& load_timing_info) { | |
140 EXPECT_TRUE(load_timing_info.socket_reused); | |
141 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | |
142 | |
143 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); | |
144 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); | |
145 | |
146 net::ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); | |
147 EXPECT_FALSE(load_timing_info.send_start.is_null()); | |
148 | |
149 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); | |
150 | |
151 // Set at a higher level. | |
152 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); | |
153 EXPECT_TRUE(load_timing_info.request_start.is_null()); | |
154 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | |
155 } | |
156 | |
157 // Tests LoadTimingInfo in the case a new socket is used and no PAC script is | |
158 // used. | |
159 void TestLoadTimingNotReused(const net::LoadTimingInfo& load_timing_info, | |
160 int connect_timing_flags) { | |
161 EXPECT_FALSE(load_timing_info.socket_reused); | |
162 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | |
163 | |
164 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); | |
165 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); | |
166 | |
167 net::ExpectConnectTimingHasTimes(load_timing_info.connect_timing, | |
168 connect_timing_flags); | |
169 EXPECT_LE(load_timing_info.connect_timing.connect_end, | |
170 load_timing_info.send_start); | |
171 | |
172 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); | |
173 | |
174 // Set at a higher level. | |
175 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); | |
176 EXPECT_TRUE(load_timing_info.request_start.is_null()); | |
177 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | |
178 } | |
179 | |
180 // Tests LoadTimingInfo in the case a socket is reused and a PAC script is | |
181 // used. | |
182 void TestLoadTimingReusedWithPac(const net::LoadTimingInfo& load_timing_info) { | |
183 EXPECT_TRUE(load_timing_info.socket_reused); | |
184 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | |
185 | |
186 net::ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); | |
187 | |
188 EXPECT_FALSE(load_timing_info.proxy_resolve_start.is_null()); | |
189 EXPECT_LE(load_timing_info.proxy_resolve_start, | |
190 load_timing_info.proxy_resolve_end); | |
191 EXPECT_LE(load_timing_info.proxy_resolve_end, | |
192 load_timing_info.send_start); | |
193 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); | |
194 | |
195 // Set at a higher level. | |
196 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); | |
197 EXPECT_TRUE(load_timing_info.request_start.is_null()); | |
198 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | |
199 } | |
200 | |
201 // Tests LoadTimingInfo in the case a new socket is used and a PAC script is | |
202 // used. | |
203 void TestLoadTimingNotReusedWithPac(const net::LoadTimingInfo& load_timing_info, | |
204 int connect_timing_flags) { | |
205 EXPECT_FALSE(load_timing_info.socket_reused); | |
206 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | |
207 | |
208 EXPECT_FALSE(load_timing_info.proxy_resolve_start.is_null()); | |
209 EXPECT_LE(load_timing_info.proxy_resolve_start, | |
210 load_timing_info.proxy_resolve_end); | |
211 EXPECT_LE(load_timing_info.proxy_resolve_end, | |
212 load_timing_info.connect_timing.connect_start); | |
213 net::ExpectConnectTimingHasTimes(load_timing_info.connect_timing, | |
214 connect_timing_flags); | |
215 EXPECT_LE(load_timing_info.connect_timing.connect_end, | |
216 load_timing_info.send_start); | |
217 | |
218 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); | |
219 | |
220 // Set at a higher level. | |
221 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); | |
222 EXPECT_TRUE(load_timing_info.request_start.is_null()); | |
223 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | |
224 } | |
225 | |
226 } // namespace | |
227 | |
228 namespace net { | |
229 | |
230 namespace { | |
231 | |
232 HttpNetworkSession* CreateSession(SpdySessionDependencies* session_deps) { | |
233 return SpdySessionDependencies::SpdyCreateSession(session_deps); | |
234 } | |
235 | |
236 } // namespace | |
237 | |
238 class HttpNetworkTransactionSpdy2Test : public PlatformTest { | |
239 public: | |
240 virtual ~HttpNetworkTransactionSpdy2Test() { | |
241 // Important to restore the per-pool limit first, since the pool limit must | |
242 // always be greater than group limit, and the tests reduce both limits. | |
243 ClientSocketPoolManager::set_max_sockets_per_pool( | |
244 HttpNetworkSession::NORMAL_SOCKET_POOL, old_max_pool_sockets_); | |
245 ClientSocketPoolManager::set_max_sockets_per_group( | |
246 HttpNetworkSession::NORMAL_SOCKET_POOL, old_max_group_sockets_); | |
247 } | |
248 | |
249 protected: | |
250 HttpNetworkTransactionSpdy2Test() | |
251 : spdy_util_(kProtoSPDY2), | |
252 session_deps_(kProtoSPDY2), | |
253 old_max_group_sockets_(ClientSocketPoolManager::max_sockets_per_group( | |
254 HttpNetworkSession::NORMAL_SOCKET_POOL)), | |
255 old_max_pool_sockets_(ClientSocketPoolManager::max_sockets_per_pool( | |
256 HttpNetworkSession::NORMAL_SOCKET_POOL)) { | |
257 } | |
258 | |
259 struct SimpleGetHelperResult { | |
260 int rv; | |
261 std::string status_line; | |
262 std::string response_data; | |
263 LoadTimingInfo load_timing_info; | |
264 }; | |
265 | |
266 virtual void SetUp() { | |
267 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
268 base::MessageLoop::current()->RunUntilIdle(); | |
269 } | |
270 | |
271 virtual void TearDown() { | |
272 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
273 base::MessageLoop::current()->RunUntilIdle(); | |
274 // Empty the current queue. | |
275 base::MessageLoop::current()->RunUntilIdle(); | |
276 PlatformTest::TearDown(); | |
277 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); | |
278 base::MessageLoop::current()->RunUntilIdle(); | |
279 HttpStreamFactory::set_use_alternate_protocols(false); | |
280 HttpStreamFactory::SetNextProtos(std::vector<std::string>()); | |
281 } | |
282 | |
283 // Either |write_failure| specifies a write failure or |read_failure| | |
284 // specifies a read failure when using a reused socket. In either case, the | |
285 // failure should cause the network transaction to resend the request, and the | |
286 // other argument should be NULL. | |
287 void KeepAliveConnectionResendRequestTest(const MockWrite* write_failure, | |
288 const MockRead* read_failure); | |
289 | |
290 SimpleGetHelperResult SimpleGetHelperForData(StaticSocketDataProvider* data[], | |
291 size_t data_count) { | |
292 SimpleGetHelperResult out; | |
293 | |
294 HttpRequestInfo request; | |
295 request.method = "GET"; | |
296 request.url = GURL("http://www.google.com/"); | |
297 request.load_flags = 0; | |
298 | |
299 CapturingBoundNetLog log; | |
300 session_deps_.net_log = log.bound().net_log(); | |
301 scoped_ptr<HttpTransaction> trans( | |
302 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
303 CreateSession(&session_deps_))); | |
304 | |
305 for (size_t i = 0; i < data_count; ++i) { | |
306 session_deps_.socket_factory->AddSocketDataProvider(data[i]); | |
307 } | |
308 | |
309 TestCompletionCallback callback; | |
310 | |
311 EXPECT_TRUE(log.bound().IsLoggingAllEvents()); | |
312 int rv = trans->Start(&request, callback.callback(), log.bound()); | |
313 EXPECT_EQ(ERR_IO_PENDING, rv); | |
314 | |
315 out.rv = callback.WaitForResult(); | |
316 | |
317 // Even in the failure cases that use this function, connections are always | |
318 // successfully established before the error. | |
319 EXPECT_TRUE(trans->GetLoadTimingInfo(&out.load_timing_info)); | |
320 TestLoadTimingNotReused(out.load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); | |
321 | |
322 if (out.rv != OK) | |
323 return out; | |
324 | |
325 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
326 // Can't use ASSERT_* inside helper functions like this, so | |
327 // return an error. | |
328 if (response == NULL || response->headers.get() == NULL) { | |
329 out.rv = ERR_UNEXPECTED; | |
330 return out; | |
331 } | |
332 out.status_line = response->headers->GetStatusLine(); | |
333 | |
334 EXPECT_EQ("127.0.0.1", response->socket_address.host()); | |
335 EXPECT_EQ(80, response->socket_address.port()); | |
336 | |
337 rv = ReadTransaction(trans.get(), &out.response_data); | |
338 EXPECT_EQ(OK, rv); | |
339 | |
340 net::CapturingNetLog::CapturedEntryList entries; | |
341 log.GetEntries(&entries); | |
342 size_t pos = ExpectLogContainsSomewhere( | |
343 entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, | |
344 NetLog::PHASE_NONE); | |
345 ExpectLogContainsSomewhere( | |
346 entries, pos, | |
347 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, | |
348 NetLog::PHASE_NONE); | |
349 | |
350 std::string line; | |
351 EXPECT_TRUE(entries[pos].GetStringValue("line", &line)); | |
352 EXPECT_EQ("GET / HTTP/1.1\r\n", line); | |
353 | |
354 std::string headers; | |
355 EXPECT_TRUE(GetHeaders(entries[pos].params.get(), &headers)); | |
356 EXPECT_EQ("['Host: www.google.com','Connection: keep-alive']", headers); | |
357 | |
358 return out; | |
359 } | |
360 | |
361 SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[], | |
362 size_t reads_count) { | |
363 StaticSocketDataProvider reads(data_reads, reads_count, NULL, 0); | |
364 StaticSocketDataProvider* data[] = { &reads }; | |
365 return SimpleGetHelperForData(data, 1); | |
366 } | |
367 | |
368 void ConnectStatusHelperWithExpectedStatus(const MockRead& status, | |
369 int expected_status); | |
370 | |
371 void ConnectStatusHelper(const MockRead& status); | |
372 | |
373 void BypassHostCacheOnRefreshHelper(int load_flags); | |
374 | |
375 void CheckErrorIsPassedBack(int error, IoMode mode); | |
376 | |
377 SpdyTestUtil spdy_util_; | |
378 SpdySessionDependencies session_deps_; | |
379 | |
380 // Original socket limits. Some tests set these. Safest to always restore | |
381 // them once each test has been run. | |
382 int old_max_group_sockets_; | |
383 int old_max_pool_sockets_; | |
384 }; | |
385 | |
386 namespace { | |
387 | |
388 // Fill |str| with a long header list that consumes >= |size| bytes. | |
389 void FillLargeHeadersString(std::string* str, int size) { | |
390 const char* row = | |
391 "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; | |
392 const int sizeof_row = strlen(row); | |
393 const int num_rows = static_cast<int>( | |
394 ceil(static_cast<float>(size) / sizeof_row)); | |
395 const int sizeof_data = num_rows * sizeof_row; | |
396 DCHECK(sizeof_data >= size); | |
397 str->reserve(sizeof_data); | |
398 | |
399 for (int i = 0; i < num_rows; ++i) | |
400 str->append(row, sizeof_row); | |
401 } | |
402 | |
403 // Alternative functions that eliminate randomness and dependency on the local | |
404 // host name so that the generated NTLM messages are reproducible. | |
405 void MockGenerateRandom1(uint8* output, size_t n) { | |
406 static const uint8 bytes[] = { | |
407 0x55, 0x29, 0x66, 0x26, 0x6b, 0x9c, 0x73, 0x54 | |
408 }; | |
409 static size_t current_byte = 0; | |
410 for (size_t i = 0; i < n; ++i) { | |
411 output[i] = bytes[current_byte++]; | |
412 current_byte %= arraysize(bytes); | |
413 } | |
414 } | |
415 | |
416 void MockGenerateRandom2(uint8* output, size_t n) { | |
417 static const uint8 bytes[] = { | |
418 0x96, 0x79, 0x85, 0xe7, 0x49, 0x93, 0x70, 0xa1, | |
419 0x4e, 0xe7, 0x87, 0x45, 0x31, 0x5b, 0xd3, 0x1f | |
420 }; | |
421 static size_t current_byte = 0; | |
422 for (size_t i = 0; i < n; ++i) { | |
423 output[i] = bytes[current_byte++]; | |
424 current_byte %= arraysize(bytes); | |
425 } | |
426 } | |
427 | |
428 std::string MockGetHostName() { | |
429 return "WTC-WIN7"; | |
430 } | |
431 | |
432 template<typename ParentPool> | |
433 class CaptureGroupNameSocketPool : public ParentPool { | |
434 public: | |
435 CaptureGroupNameSocketPool(HostResolver* host_resolver, | |
436 CertVerifier* cert_verifier); | |
437 | |
438 const std::string last_group_name_received() const { | |
439 return last_group_name_; | |
440 } | |
441 | |
442 virtual int RequestSocket(const std::string& group_name, | |
443 const void* socket_params, | |
444 RequestPriority priority, | |
445 ClientSocketHandle* handle, | |
446 const CompletionCallback& callback, | |
447 const BoundNetLog& net_log) { | |
448 last_group_name_ = group_name; | |
449 return ERR_IO_PENDING; | |
450 } | |
451 virtual void CancelRequest(const std::string& group_name, | |
452 ClientSocketHandle* handle) {} | |
453 virtual void ReleaseSocket(const std::string& group_name, | |
454 StreamSocket* socket, | |
455 int id) {} | |
456 virtual void CloseIdleSockets() {} | |
457 virtual int IdleSocketCount() const { | |
458 return 0; | |
459 } | |
460 virtual int IdleSocketCountInGroup(const std::string& group_name) const { | |
461 return 0; | |
462 } | |
463 virtual LoadState GetLoadState(const std::string& group_name, | |
464 const ClientSocketHandle* handle) const { | |
465 return LOAD_STATE_IDLE; | |
466 } | |
467 virtual base::TimeDelta ConnectionTimeout() const { | |
468 return base::TimeDelta(); | |
469 } | |
470 | |
471 private: | |
472 std::string last_group_name_; | |
473 }; | |
474 | |
475 typedef CaptureGroupNameSocketPool<TransportClientSocketPool> | |
476 CaptureGroupNameTransportSocketPool; | |
477 typedef CaptureGroupNameSocketPool<HttpProxyClientSocketPool> | |
478 CaptureGroupNameHttpProxySocketPool; | |
479 typedef CaptureGroupNameSocketPool<SOCKSClientSocketPool> | |
480 CaptureGroupNameSOCKSSocketPool; | |
481 typedef CaptureGroupNameSocketPool<SSLClientSocketPool> | |
482 CaptureGroupNameSSLSocketPool; | |
483 | |
484 template<typename ParentPool> | |
485 CaptureGroupNameSocketPool<ParentPool>::CaptureGroupNameSocketPool( | |
486 HostResolver* host_resolver, | |
487 CertVerifier* /* cert_verifier */) | |
488 : ParentPool(0, 0, NULL, host_resolver, NULL, NULL) {} | |
489 | |
490 template<> | |
491 CaptureGroupNameHttpProxySocketPool::CaptureGroupNameSocketPool( | |
492 HostResolver* host_resolver, | |
493 CertVerifier* /* cert_verifier */) | |
494 : HttpProxyClientSocketPool(0, 0, NULL, host_resolver, NULL, NULL, NULL) {} | |
495 | |
496 template <> | |
497 CaptureGroupNameSSLSocketPool::CaptureGroupNameSocketPool( | |
498 HostResolver* host_resolver, | |
499 CertVerifier* cert_verifier) | |
500 : SSLClientSocketPool(0, | |
501 0, | |
502 NULL, | |
503 host_resolver, | |
504 cert_verifier, | |
505 NULL, | |
506 NULL, | |
507 std::string(), | |
508 NULL, | |
509 NULL, | |
510 NULL, | |
511 NULL, | |
512 NULL, | |
513 NULL) {} | |
514 | |
515 //----------------------------------------------------------------------------- | |
516 | |
517 // This is the expected return from a current server advertising SPDY. | |
518 static const char kAlternateProtocolHttpHeader[] = | |
519 "Alternate-Protocol: 443:npn-spdy/2\r\n\r\n"; | |
520 | |
521 // Helper functions for validating that AuthChallengeInfo's are correctly | |
522 // configured for common cases. | |
523 bool CheckBasicServerAuth(const AuthChallengeInfo* auth_challenge) { | |
524 if (!auth_challenge) | |
525 return false; | |
526 EXPECT_FALSE(auth_challenge->is_proxy); | |
527 EXPECT_EQ("www.google.com:80", auth_challenge->challenger.ToString()); | |
528 EXPECT_EQ("MyRealm1", auth_challenge->realm); | |
529 EXPECT_EQ("basic", auth_challenge->scheme); | |
530 return true; | |
531 } | |
532 | |
533 bool CheckBasicProxyAuth(const AuthChallengeInfo* auth_challenge) { | |
534 if (!auth_challenge) | |
535 return false; | |
536 EXPECT_TRUE(auth_challenge->is_proxy); | |
537 EXPECT_EQ("myproxy:70", auth_challenge->challenger.ToString()); | |
538 EXPECT_EQ("MyRealm1", auth_challenge->realm); | |
539 EXPECT_EQ("basic", auth_challenge->scheme); | |
540 return true; | |
541 } | |
542 | |
543 bool CheckDigestServerAuth(const AuthChallengeInfo* auth_challenge) { | |
544 if (!auth_challenge) | |
545 return false; | |
546 EXPECT_FALSE(auth_challenge->is_proxy); | |
547 EXPECT_EQ("www.google.com:80", auth_challenge->challenger.ToString()); | |
548 EXPECT_EQ("digestive", auth_challenge->realm); | |
549 EXPECT_EQ("digest", auth_challenge->scheme); | |
550 return true; | |
551 } | |
552 | |
553 bool CheckNTLMServerAuth(const AuthChallengeInfo* auth_challenge) { | |
554 if (!auth_challenge) | |
555 return false; | |
556 EXPECT_FALSE(auth_challenge->is_proxy); | |
557 EXPECT_EQ("172.22.68.17:80", auth_challenge->challenger.ToString()); | |
558 EXPECT_EQ(std::string(), auth_challenge->realm); | |
559 EXPECT_EQ("ntlm", auth_challenge->scheme); | |
560 return true; | |
561 } | |
562 | |
563 } // namespace | |
564 | |
565 TEST_F(HttpNetworkTransactionSpdy2Test, Basic) { | |
566 scoped_ptr<HttpTransaction> trans( | |
567 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
568 CreateSession(&session_deps_))); | |
569 } | |
570 | |
571 TEST_F(HttpNetworkTransactionSpdy2Test, SimpleGET) { | |
572 MockRead data_reads[] = { | |
573 MockRead("HTTP/1.0 200 OK\r\n\r\n"), | |
574 MockRead("hello world"), | |
575 MockRead(SYNCHRONOUS, OK), | |
576 }; | |
577 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
578 arraysize(data_reads)); | |
579 EXPECT_EQ(OK, out.rv); | |
580 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); | |
581 EXPECT_EQ("hello world", out.response_data); | |
582 } | |
583 | |
584 // Response with no status line. | |
585 TEST_F(HttpNetworkTransactionSpdy2Test, SimpleGETNoHeaders) { | |
586 MockRead data_reads[] = { | |
587 MockRead("hello world"), | |
588 MockRead(SYNCHRONOUS, OK), | |
589 }; | |
590 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
591 arraysize(data_reads)); | |
592 EXPECT_EQ(OK, out.rv); | |
593 EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); | |
594 EXPECT_EQ("hello world", out.response_data); | |
595 } | |
596 | |
597 // Allow up to 4 bytes of junk to precede status line. | |
598 TEST_F(HttpNetworkTransactionSpdy2Test, StatusLineJunk3Bytes) { | |
599 MockRead data_reads[] = { | |
600 MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), | |
601 MockRead(SYNCHRONOUS, OK), | |
602 }; | |
603 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
604 arraysize(data_reads)); | |
605 EXPECT_EQ(OK, out.rv); | |
606 EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); | |
607 EXPECT_EQ("DATA", out.response_data); | |
608 } | |
609 | |
610 // Allow up to 4 bytes of junk to precede status line. | |
611 TEST_F(HttpNetworkTransactionSpdy2Test, StatusLineJunk4Bytes) { | |
612 MockRead data_reads[] = { | |
613 MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), | |
614 MockRead(SYNCHRONOUS, OK), | |
615 }; | |
616 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
617 arraysize(data_reads)); | |
618 EXPECT_EQ(OK, out.rv); | |
619 EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); | |
620 EXPECT_EQ("DATA", out.response_data); | |
621 } | |
622 | |
623 // Beyond 4 bytes of slop and it should fail to find a status line. | |
624 TEST_F(HttpNetworkTransactionSpdy2Test, StatusLineJunk5Bytes) { | |
625 MockRead data_reads[] = { | |
626 MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"), | |
627 MockRead(SYNCHRONOUS, OK), | |
628 }; | |
629 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
630 arraysize(data_reads)); | |
631 EXPECT_EQ(OK, out.rv); | |
632 EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); | |
633 EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data); | |
634 } | |
635 | |
636 // Same as StatusLineJunk4Bytes, except the read chunks are smaller. | |
637 TEST_F(HttpNetworkTransactionSpdy2Test, StatusLineJunk4Bytes_Slow) { | |
638 MockRead data_reads[] = { | |
639 MockRead("\n"), | |
640 MockRead("\n"), | |
641 MockRead("Q"), | |
642 MockRead("J"), | |
643 MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), | |
644 MockRead(SYNCHRONOUS, OK), | |
645 }; | |
646 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
647 arraysize(data_reads)); | |
648 EXPECT_EQ(OK, out.rv); | |
649 EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); | |
650 EXPECT_EQ("DATA", out.response_data); | |
651 } | |
652 | |
653 // Close the connection before enough bytes to have a status line. | |
654 TEST_F(HttpNetworkTransactionSpdy2Test, StatusLinePartial) { | |
655 MockRead data_reads[] = { | |
656 MockRead("HTT"), | |
657 MockRead(SYNCHRONOUS, OK), | |
658 }; | |
659 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
660 arraysize(data_reads)); | |
661 EXPECT_EQ(OK, out.rv); | |
662 EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); | |
663 EXPECT_EQ("HTT", out.response_data); | |
664 } | |
665 | |
666 // Simulate a 204 response, lacking a Content-Length header, sent over a | |
667 // persistent connection. The response should still terminate since a 204 | |
668 // cannot have a response body. | |
669 TEST_F(HttpNetworkTransactionSpdy2Test, StopsReading204) { | |
670 MockRead data_reads[] = { | |
671 MockRead("HTTP/1.1 204 No Content\r\n\r\n"), | |
672 MockRead("junk"), // Should not be read!! | |
673 MockRead(SYNCHRONOUS, OK), | |
674 }; | |
675 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
676 arraysize(data_reads)); | |
677 EXPECT_EQ(OK, out.rv); | |
678 EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line); | |
679 EXPECT_EQ("", out.response_data); | |
680 } | |
681 | |
682 // A simple request using chunked encoding with some extra data after. | |
683 // (Like might be seen in a pipelined response.) | |
684 TEST_F(HttpNetworkTransactionSpdy2Test, ChunkedEncoding) { | |
685 MockRead data_reads[] = { | |
686 MockRead("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"), | |
687 MockRead("5\r\nHello\r\n"), | |
688 MockRead("1\r\n"), | |
689 MockRead(" \r\n"), | |
690 MockRead("5\r\nworld\r\n"), | |
691 MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), | |
692 MockRead(SYNCHRONOUS, OK), | |
693 }; | |
694 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
695 arraysize(data_reads)); | |
696 EXPECT_EQ(OK, out.rv); | |
697 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); | |
698 EXPECT_EQ("Hello world", out.response_data); | |
699 } | |
700 | |
701 // Next tests deal with http://crbug.com/56344. | |
702 | |
703 TEST_F(HttpNetworkTransactionSpdy2Test, | |
704 MultipleContentLengthHeadersNoTransferEncoding) { | |
705 MockRead data_reads[] = { | |
706 MockRead("HTTP/1.1 200 OK\r\n"), | |
707 MockRead("Content-Length: 10\r\n"), | |
708 MockRead("Content-Length: 5\r\n\r\n"), | |
709 }; | |
710 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
711 arraysize(data_reads)); | |
712 EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, out.rv); | |
713 } | |
714 | |
715 TEST_F(HttpNetworkTransactionSpdy2Test, | |
716 DuplicateContentLengthHeadersNoTransferEncoding) { | |
717 MockRead data_reads[] = { | |
718 MockRead("HTTP/1.1 200 OK\r\n"), | |
719 MockRead("Content-Length: 5\r\n"), | |
720 MockRead("Content-Length: 5\r\n\r\n"), | |
721 MockRead("Hello"), | |
722 }; | |
723 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
724 arraysize(data_reads)); | |
725 EXPECT_EQ(OK, out.rv); | |
726 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); | |
727 EXPECT_EQ("Hello", out.response_data); | |
728 } | |
729 | |
730 TEST_F(HttpNetworkTransactionSpdy2Test, | |
731 ComplexContentLengthHeadersNoTransferEncoding) { | |
732 // More than 2 dupes. | |
733 { | |
734 MockRead data_reads[] = { | |
735 MockRead("HTTP/1.1 200 OK\r\n"), | |
736 MockRead("Content-Length: 5\r\n"), | |
737 MockRead("Content-Length: 5\r\n"), | |
738 MockRead("Content-Length: 5\r\n\r\n"), | |
739 MockRead("Hello"), | |
740 }; | |
741 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
742 arraysize(data_reads)); | |
743 EXPECT_EQ(OK, out.rv); | |
744 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); | |
745 EXPECT_EQ("Hello", out.response_data); | |
746 } | |
747 // HTTP/1.0 | |
748 { | |
749 MockRead data_reads[] = { | |
750 MockRead("HTTP/1.0 200 OK\r\n"), | |
751 MockRead("Content-Length: 5\r\n"), | |
752 MockRead("Content-Length: 5\r\n"), | |
753 MockRead("Content-Length: 5\r\n\r\n"), | |
754 MockRead("Hello"), | |
755 }; | |
756 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
757 arraysize(data_reads)); | |
758 EXPECT_EQ(OK, out.rv); | |
759 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); | |
760 EXPECT_EQ("Hello", out.response_data); | |
761 } | |
762 // 2 dupes and one mismatched. | |
763 { | |
764 MockRead data_reads[] = { | |
765 MockRead("HTTP/1.1 200 OK\r\n"), | |
766 MockRead("Content-Length: 10\r\n"), | |
767 MockRead("Content-Length: 10\r\n"), | |
768 MockRead("Content-Length: 5\r\n\r\n"), | |
769 }; | |
770 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
771 arraysize(data_reads)); | |
772 EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, out.rv); | |
773 } | |
774 } | |
775 | |
776 TEST_F(HttpNetworkTransactionSpdy2Test, | |
777 MultipleContentLengthHeadersTransferEncoding) { | |
778 MockRead data_reads[] = { | |
779 MockRead("HTTP/1.1 200 OK\r\n"), | |
780 MockRead("Content-Length: 666\r\n"), | |
781 MockRead("Content-Length: 1337\r\n"), | |
782 MockRead("Transfer-Encoding: chunked\r\n\r\n"), | |
783 MockRead("5\r\nHello\r\n"), | |
784 MockRead("1\r\n"), | |
785 MockRead(" \r\n"), | |
786 MockRead("5\r\nworld\r\n"), | |
787 MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), | |
788 MockRead(SYNCHRONOUS, OK), | |
789 }; | |
790 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
791 arraysize(data_reads)); | |
792 EXPECT_EQ(OK, out.rv); | |
793 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); | |
794 EXPECT_EQ("Hello world", out.response_data); | |
795 } | |
796 | |
797 // Next tests deal with http://crbug.com/98895. | |
798 | |
799 // Checks that a single Content-Disposition header results in no error. | |
800 TEST_F(HttpNetworkTransactionSpdy2Test, SingleContentDispositionHeader) { | |
801 MockRead data_reads[] = { | |
802 MockRead("HTTP/1.1 200 OK\r\n"), | |
803 MockRead("Content-Disposition: attachment;filename=\"salutations.txt\"r\n"), | |
804 MockRead("Content-Length: 5\r\n\r\n"), | |
805 MockRead("Hello"), | |
806 }; | |
807 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
808 arraysize(data_reads)); | |
809 EXPECT_EQ(OK, out.rv); | |
810 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); | |
811 EXPECT_EQ("Hello", out.response_data); | |
812 } | |
813 | |
814 // Checks that two identical Content-Disposition headers result in no error. | |
815 TEST_F(HttpNetworkTransactionSpdy2Test, | |
816 TwoIdenticalContentDispositionHeaders) { | |
817 MockRead data_reads[] = { | |
818 MockRead("HTTP/1.1 200 OK\r\n"), | |
819 MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), | |
820 MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), | |
821 MockRead("Content-Length: 5\r\n\r\n"), | |
822 MockRead("Hello"), | |
823 }; | |
824 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
825 arraysize(data_reads)); | |
826 EXPECT_EQ(OK, out.rv); | |
827 EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); | |
828 EXPECT_EQ("Hello", out.response_data); | |
829 } | |
830 | |
831 // Checks that two distinct Content-Disposition headers result in an error. | |
832 TEST_F(HttpNetworkTransactionSpdy2Test, TwoDistinctContentDispositionHeaders) { | |
833 MockRead data_reads[] = { | |
834 MockRead("HTTP/1.1 200 OK\r\n"), | |
835 MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), | |
836 MockRead("Content-Disposition: attachment;filename=\"hi.txt\"r\n"), | |
837 MockRead("Content-Length: 5\r\n\r\n"), | |
838 MockRead("Hello"), | |
839 }; | |
840 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
841 arraysize(data_reads)); | |
842 EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION, out.rv); | |
843 } | |
844 | |
845 // Checks that two identical Location headers result in no error. | |
846 // Also tests Location header behavior. | |
847 TEST_F(HttpNetworkTransactionSpdy2Test, TwoIdenticalLocationHeaders) { | |
848 MockRead data_reads[] = { | |
849 MockRead("HTTP/1.1 302 Redirect\r\n"), | |
850 MockRead("Location: http://good.com/\r\n"), | |
851 MockRead("Location: http://good.com/\r\n"), | |
852 MockRead("Content-Length: 0\r\n\r\n"), | |
853 MockRead(SYNCHRONOUS, OK), | |
854 }; | |
855 | |
856 HttpRequestInfo request; | |
857 request.method = "GET"; | |
858 request.url = GURL("http://redirect.com/"); | |
859 request.load_flags = 0; | |
860 | |
861 scoped_ptr<HttpTransaction> trans( | |
862 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
863 CreateSession(&session_deps_))); | |
864 | |
865 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
866 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
867 | |
868 TestCompletionCallback callback; | |
869 | |
870 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
871 EXPECT_EQ(ERR_IO_PENDING, rv); | |
872 | |
873 EXPECT_EQ(OK, callback.WaitForResult()); | |
874 | |
875 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
876 ASSERT_TRUE(response != NULL && response->headers.get() != NULL); | |
877 EXPECT_EQ("HTTP/1.1 302 Redirect", response->headers->GetStatusLine()); | |
878 std::string url; | |
879 EXPECT_TRUE(response->headers->IsRedirect(&url)); | |
880 EXPECT_EQ("http://good.com/", url); | |
881 } | |
882 | |
883 // Checks that two distinct Location headers result in an error. | |
884 TEST_F(HttpNetworkTransactionSpdy2Test, TwoDistinctLocationHeaders) { | |
885 MockRead data_reads[] = { | |
886 MockRead("HTTP/1.1 302 Redirect\r\n"), | |
887 MockRead("Location: http://good.com/\r\n"), | |
888 MockRead("Location: http://evil.com/\r\n"), | |
889 MockRead("Content-Length: 0\r\n\r\n"), | |
890 MockRead(SYNCHRONOUS, OK), | |
891 }; | |
892 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
893 arraysize(data_reads)); | |
894 EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION, out.rv); | |
895 } | |
896 | |
897 // Do a request using the HEAD method. Verify that we don't try to read the | |
898 // message body (since HEAD has none). | |
899 TEST_F(HttpNetworkTransactionSpdy2Test, Head) { | |
900 HttpRequestInfo request; | |
901 request.method = "HEAD"; | |
902 request.url = GURL("http://www.google.com/"); | |
903 request.load_flags = 0; | |
904 | |
905 scoped_ptr<HttpTransaction> trans( | |
906 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
907 CreateSession(&session_deps_))); | |
908 | |
909 MockWrite data_writes1[] = { | |
910 MockWrite("HEAD / HTTP/1.1\r\n" | |
911 "Host: www.google.com\r\n" | |
912 "Connection: keep-alive\r\n" | |
913 "Content-Length: 0\r\n\r\n"), | |
914 }; | |
915 MockRead data_reads1[] = { | |
916 MockRead("HTTP/1.1 404 Not Found\r\n"), | |
917 MockRead("Server: Blah\r\n"), | |
918 MockRead("Content-Length: 1234\r\n\r\n"), | |
919 | |
920 // No response body because the test stops reading here. | |
921 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. | |
922 }; | |
923 | |
924 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
925 data_writes1, arraysize(data_writes1)); | |
926 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
927 | |
928 TestCompletionCallback callback1; | |
929 | |
930 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
931 EXPECT_EQ(ERR_IO_PENDING, rv); | |
932 | |
933 rv = callback1.WaitForResult(); | |
934 EXPECT_EQ(OK, rv); | |
935 | |
936 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
937 ASSERT_TRUE(response != NULL); | |
938 | |
939 // Check that the headers got parsed. | |
940 EXPECT_TRUE(response->headers.get() != NULL); | |
941 EXPECT_EQ(1234, response->headers->GetContentLength()); | |
942 EXPECT_EQ("HTTP/1.1 404 Not Found", response->headers->GetStatusLine()); | |
943 | |
944 std::string server_header; | |
945 void* iter = NULL; | |
946 bool has_server_header = response->headers->EnumerateHeader( | |
947 &iter, "Server", &server_header); | |
948 EXPECT_TRUE(has_server_header); | |
949 EXPECT_EQ("Blah", server_header); | |
950 | |
951 // Reading should give EOF right away, since there is no message body | |
952 // (despite non-zero content-length). | |
953 std::string response_data; | |
954 rv = ReadTransaction(trans.get(), &response_data); | |
955 EXPECT_EQ(OK, rv); | |
956 EXPECT_EQ("", response_data); | |
957 } | |
958 | |
959 TEST_F(HttpNetworkTransactionSpdy2Test, ReuseConnection) { | |
960 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
961 | |
962 MockRead data_reads[] = { | |
963 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), | |
964 MockRead("hello"), | |
965 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), | |
966 MockRead("world"), | |
967 MockRead(SYNCHRONOUS, OK), | |
968 }; | |
969 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
970 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
971 | |
972 const char* const kExpectedResponseData[] = { | |
973 "hello", "world" | |
974 }; | |
975 | |
976 for (int i = 0; i < 2; ++i) { | |
977 HttpRequestInfo request; | |
978 request.method = "GET"; | |
979 request.url = GURL("http://www.google.com/"); | |
980 request.load_flags = 0; | |
981 | |
982 scoped_ptr<HttpTransaction> trans( | |
983 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
984 | |
985 TestCompletionCallback callback; | |
986 | |
987 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
988 EXPECT_EQ(ERR_IO_PENDING, rv); | |
989 | |
990 rv = callback.WaitForResult(); | |
991 EXPECT_EQ(OK, rv); | |
992 | |
993 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
994 ASSERT_TRUE(response != NULL); | |
995 | |
996 EXPECT_TRUE(response->headers.get() != NULL); | |
997 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
998 | |
999 std::string response_data; | |
1000 rv = ReadTransaction(trans.get(), &response_data); | |
1001 EXPECT_EQ(OK, rv); | |
1002 EXPECT_EQ(kExpectedResponseData[i], response_data); | |
1003 } | |
1004 } | |
1005 | |
1006 TEST_F(HttpNetworkTransactionSpdy2Test, Ignores100) { | |
1007 ScopedVector<UploadElementReader> element_readers; | |
1008 element_readers.push_back(new UploadBytesElementReader("foo", 3)); | |
1009 UploadDataStream upload_data_stream(&element_readers, 0); | |
1010 | |
1011 HttpRequestInfo request; | |
1012 request.method = "POST"; | |
1013 request.url = GURL("http://www.foo.com/"); | |
1014 request.upload_data_stream = &upload_data_stream; | |
1015 request.load_flags = 0; | |
1016 | |
1017 scoped_ptr<HttpTransaction> trans( | |
1018 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
1019 CreateSession(&session_deps_))); | |
1020 | |
1021 MockRead data_reads[] = { | |
1022 MockRead("HTTP/1.0 100 Continue\r\n\r\n"), | |
1023 MockRead("HTTP/1.0 200 OK\r\n\r\n"), | |
1024 MockRead("hello world"), | |
1025 MockRead(SYNCHRONOUS, OK), | |
1026 }; | |
1027 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
1028 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
1029 | |
1030 TestCompletionCallback callback; | |
1031 | |
1032 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1033 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1034 | |
1035 rv = callback.WaitForResult(); | |
1036 EXPECT_EQ(OK, rv); | |
1037 | |
1038 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1039 ASSERT_TRUE(response != NULL); | |
1040 | |
1041 EXPECT_TRUE(response->headers.get() != NULL); | |
1042 EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); | |
1043 | |
1044 std::string response_data; | |
1045 rv = ReadTransaction(trans.get(), &response_data); | |
1046 EXPECT_EQ(OK, rv); | |
1047 EXPECT_EQ("hello world", response_data); | |
1048 } | |
1049 | |
1050 // This test is almost the same as Ignores100 above, but the response contains | |
1051 // a 102 instead of a 100. Also, instead of HTTP/1.0 the response is | |
1052 // HTTP/1.1 and the two status headers are read in one read. | |
1053 TEST_F(HttpNetworkTransactionSpdy2Test, Ignores1xx) { | |
1054 HttpRequestInfo request; | |
1055 request.method = "GET"; | |
1056 request.url = GURL("http://www.foo.com/"); | |
1057 request.load_flags = 0; | |
1058 | |
1059 scoped_ptr<HttpTransaction> trans( | |
1060 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
1061 CreateSession(&session_deps_))); | |
1062 | |
1063 MockRead data_reads[] = { | |
1064 MockRead("HTTP/1.1 102 Unspecified status code\r\n\r\n" | |
1065 "HTTP/1.1 200 OK\r\n\r\n"), | |
1066 MockRead("hello world"), | |
1067 MockRead(SYNCHRONOUS, OK), | |
1068 }; | |
1069 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
1070 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
1071 | |
1072 TestCompletionCallback callback; | |
1073 | |
1074 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1075 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1076 | |
1077 rv = callback.WaitForResult(); | |
1078 EXPECT_EQ(OK, rv); | |
1079 | |
1080 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1081 ASSERT_TRUE(response != NULL); | |
1082 | |
1083 EXPECT_TRUE(response->headers.get() != NULL); | |
1084 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
1085 | |
1086 std::string response_data; | |
1087 rv = ReadTransaction(trans.get(), &response_data); | |
1088 EXPECT_EQ(OK, rv); | |
1089 EXPECT_EQ("hello world", response_data); | |
1090 } | |
1091 | |
1092 TEST_F(HttpNetworkTransactionSpdy2Test, Incomplete100ThenEOF) { | |
1093 HttpRequestInfo request; | |
1094 request.method = "POST"; | |
1095 request.url = GURL("http://www.foo.com/"); | |
1096 request.load_flags = 0; | |
1097 | |
1098 scoped_ptr<HttpTransaction> trans( | |
1099 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
1100 CreateSession(&session_deps_))); | |
1101 | |
1102 MockRead data_reads[] = { | |
1103 MockRead(SYNCHRONOUS, "HTTP/1.0 100 Continue\r\n"), | |
1104 MockRead(ASYNC, 0), | |
1105 }; | |
1106 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
1107 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
1108 | |
1109 TestCompletionCallback callback; | |
1110 | |
1111 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1112 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1113 | |
1114 rv = callback.WaitForResult(); | |
1115 EXPECT_EQ(OK, rv); | |
1116 | |
1117 std::string response_data; | |
1118 rv = ReadTransaction(trans.get(), &response_data); | |
1119 EXPECT_EQ(OK, rv); | |
1120 EXPECT_EQ("", response_data); | |
1121 } | |
1122 | |
1123 TEST_F(HttpNetworkTransactionSpdy2Test, EmptyResponse) { | |
1124 HttpRequestInfo request; | |
1125 request.method = "POST"; | |
1126 request.url = GURL("http://www.foo.com/"); | |
1127 request.load_flags = 0; | |
1128 | |
1129 scoped_ptr<HttpTransaction> trans( | |
1130 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
1131 CreateSession(&session_deps_))); | |
1132 | |
1133 MockRead data_reads[] = { | |
1134 MockRead(ASYNC, 0), | |
1135 }; | |
1136 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
1137 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
1138 | |
1139 TestCompletionCallback callback; | |
1140 | |
1141 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1142 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1143 | |
1144 rv = callback.WaitForResult(); | |
1145 EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); | |
1146 } | |
1147 | |
1148 void HttpNetworkTransactionSpdy2Test::KeepAliveConnectionResendRequestTest( | |
1149 const MockWrite* write_failure, | |
1150 const MockRead* read_failure) { | |
1151 HttpRequestInfo request; | |
1152 request.method = "GET"; | |
1153 request.url = GURL("http://www.foo.com/"); | |
1154 request.load_flags = 0; | |
1155 | |
1156 CapturingNetLog net_log; | |
1157 session_deps_.net_log = &net_log; | |
1158 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
1159 | |
1160 // Written data for successfully sending both requests. | |
1161 MockWrite data1_writes[] = { | |
1162 MockWrite("GET / HTTP/1.1\r\n" | |
1163 "Host: www.foo.com\r\n" | |
1164 "Connection: keep-alive\r\n\r\n"), | |
1165 MockWrite("GET / HTTP/1.1\r\n" | |
1166 "Host: www.foo.com\r\n" | |
1167 "Connection: keep-alive\r\n\r\n") | |
1168 }; | |
1169 | |
1170 // Read results for the first request. | |
1171 MockRead data1_reads[] = { | |
1172 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), | |
1173 MockRead("hello"), | |
1174 MockRead(ASYNC, OK), | |
1175 }; | |
1176 | |
1177 if (write_failure) { | |
1178 ASSERT_TRUE(!read_failure); | |
1179 data1_writes[1] = *write_failure; | |
1180 } else { | |
1181 ASSERT_TRUE(read_failure); | |
1182 data1_reads[2] = *read_failure; | |
1183 } | |
1184 | |
1185 StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), | |
1186 data1_writes, arraysize(data1_writes)); | |
1187 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
1188 | |
1189 MockRead data2_reads[] = { | |
1190 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), | |
1191 MockRead("world"), | |
1192 MockRead(ASYNC, OK), | |
1193 }; | |
1194 StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); | |
1195 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
1196 | |
1197 const char* kExpectedResponseData[] = { | |
1198 "hello", "world" | |
1199 }; | |
1200 | |
1201 uint32 first_socket_log_id = NetLog::Source::kInvalidId; | |
1202 for (int i = 0; i < 2; ++i) { | |
1203 TestCompletionCallback callback; | |
1204 | |
1205 scoped_ptr<HttpTransaction> trans( | |
1206 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
1207 | |
1208 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1209 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1210 | |
1211 rv = callback.WaitForResult(); | |
1212 EXPECT_EQ(OK, rv); | |
1213 | |
1214 LoadTimingInfo load_timing_info; | |
1215 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
1216 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); | |
1217 if (i == 0) { | |
1218 first_socket_log_id = load_timing_info.socket_log_id; | |
1219 } else { | |
1220 // The second request should be using a new socket. | |
1221 EXPECT_NE(first_socket_log_id, load_timing_info.socket_log_id); | |
1222 } | |
1223 | |
1224 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1225 ASSERT_TRUE(response != NULL); | |
1226 | |
1227 EXPECT_TRUE(response->headers.get() != NULL); | |
1228 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
1229 | |
1230 std::string response_data; | |
1231 rv = ReadTransaction(trans.get(), &response_data); | |
1232 EXPECT_EQ(OK, rv); | |
1233 EXPECT_EQ(kExpectedResponseData[i], response_data); | |
1234 } | |
1235 } | |
1236 | |
1237 TEST_F(HttpNetworkTransactionSpdy2Test, | |
1238 KeepAliveConnectionNotConnectedOnWrite) { | |
1239 MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); | |
1240 KeepAliveConnectionResendRequestTest(&write_failure, NULL); | |
1241 } | |
1242 | |
1243 TEST_F(HttpNetworkTransactionSpdy2Test, KeepAliveConnectionReset) { | |
1244 MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); | |
1245 KeepAliveConnectionResendRequestTest(NULL, &read_failure); | |
1246 } | |
1247 | |
1248 TEST_F(HttpNetworkTransactionSpdy2Test, KeepAliveConnectionEOF) { | |
1249 MockRead read_failure(SYNCHRONOUS, OK); // EOF | |
1250 KeepAliveConnectionResendRequestTest(NULL, &read_failure); | |
1251 } | |
1252 | |
1253 TEST_F(HttpNetworkTransactionSpdy2Test, NonKeepAliveConnectionReset) { | |
1254 HttpRequestInfo request; | |
1255 request.method = "GET"; | |
1256 request.url = GURL("http://www.google.com/"); | |
1257 request.load_flags = 0; | |
1258 | |
1259 scoped_ptr<HttpTransaction> trans( | |
1260 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
1261 CreateSession(&session_deps_))); | |
1262 | |
1263 MockRead data_reads[] = { | |
1264 MockRead(ASYNC, ERR_CONNECTION_RESET), | |
1265 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used | |
1266 MockRead("hello world"), | |
1267 MockRead(SYNCHRONOUS, OK), | |
1268 }; | |
1269 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
1270 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
1271 | |
1272 TestCompletionCallback callback; | |
1273 | |
1274 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1275 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1276 | |
1277 rv = callback.WaitForResult(); | |
1278 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | |
1279 | |
1280 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1281 EXPECT_TRUE(response == NULL); | |
1282 } | |
1283 | |
1284 // What do various browsers do when the server closes a non-keepalive | |
1285 // connection without sending any response header or body? | |
1286 // | |
1287 // IE7: error page | |
1288 // Safari 3.1.2 (Windows): error page | |
1289 // Firefox 3.0.1: blank page | |
1290 // Opera 9.52: after five attempts, blank page | |
1291 // Us with WinHTTP: error page (ERR_INVALID_RESPONSE) | |
1292 // Us: error page (EMPTY_RESPONSE) | |
1293 TEST_F(HttpNetworkTransactionSpdy2Test, NonKeepAliveConnectionEOF) { | |
1294 MockRead data_reads[] = { | |
1295 MockRead(SYNCHRONOUS, OK), // EOF | |
1296 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used | |
1297 MockRead("hello world"), | |
1298 MockRead(SYNCHRONOUS, OK), | |
1299 }; | |
1300 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | |
1301 arraysize(data_reads)); | |
1302 EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); | |
1303 } | |
1304 | |
1305 // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression | |
1306 // tests. There was a bug causing HttpNetworkTransaction to hang in the | |
1307 // destructor in such situations. | |
1308 // See http://crbug.com/154712 and http://crbug.com/156609. | |
1309 TEST_F(HttpNetworkTransactionSpdy2Test, KeepAliveEarlyClose) { | |
1310 HttpRequestInfo request; | |
1311 request.method = "GET"; | |
1312 request.url = GURL("http://www.google.com/"); | |
1313 request.load_flags = 0; | |
1314 | |
1315 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
1316 scoped_ptr<HttpTransaction> trans( | |
1317 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
1318 | |
1319 MockRead data_reads[] = { | |
1320 MockRead("HTTP/1.0 200 OK\r\n"), | |
1321 MockRead("Connection: keep-alive\r\n"), | |
1322 MockRead("Content-Length: 100\r\n\r\n"), | |
1323 MockRead("hello"), | |
1324 MockRead(SYNCHRONOUS, 0), | |
1325 }; | |
1326 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
1327 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
1328 | |
1329 TestCompletionCallback callback; | |
1330 | |
1331 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1332 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1333 | |
1334 rv = callback.WaitForResult(); | |
1335 EXPECT_EQ(OK, rv); | |
1336 | |
1337 scoped_refptr<IOBufferWithSize> io_buf(new IOBufferWithSize(100)); | |
1338 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); | |
1339 if (rv == ERR_IO_PENDING) | |
1340 rv = callback.WaitForResult(); | |
1341 EXPECT_EQ(5, rv); | |
1342 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); | |
1343 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, rv); | |
1344 | |
1345 trans.reset(); | |
1346 base::MessageLoop::current()->RunUntilIdle(); | |
1347 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); | |
1348 } | |
1349 | |
1350 TEST_F(HttpNetworkTransactionSpdy2Test, KeepAliveEarlyClose2) { | |
1351 HttpRequestInfo request; | |
1352 request.method = "GET"; | |
1353 request.url = GURL("http://www.google.com/"); | |
1354 request.load_flags = 0; | |
1355 | |
1356 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
1357 scoped_ptr<HttpTransaction> trans( | |
1358 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
1359 | |
1360 MockRead data_reads[] = { | |
1361 MockRead("HTTP/1.0 200 OK\r\n"), | |
1362 MockRead("Connection: keep-alive\r\n"), | |
1363 MockRead("Content-Length: 100\r\n\r\n"), | |
1364 MockRead(SYNCHRONOUS, 0), | |
1365 }; | |
1366 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
1367 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
1368 | |
1369 TestCompletionCallback callback; | |
1370 | |
1371 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1372 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1373 | |
1374 rv = callback.WaitForResult(); | |
1375 EXPECT_EQ(OK, rv); | |
1376 | |
1377 scoped_refptr<IOBufferWithSize> io_buf(new IOBufferWithSize(100)); | |
1378 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); | |
1379 if (rv == ERR_IO_PENDING) | |
1380 rv = callback.WaitForResult(); | |
1381 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, rv); | |
1382 | |
1383 trans.reset(); | |
1384 base::MessageLoop::current()->RunUntilIdle(); | |
1385 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); | |
1386 } | |
1387 | |
1388 // Test that we correctly reuse a keep-alive connection after not explicitly | |
1389 // reading the body. | |
1390 TEST_F(HttpNetworkTransactionSpdy2Test, KeepAliveAfterUnreadBody) { | |
1391 HttpRequestInfo request; | |
1392 request.method = "GET"; | |
1393 request.url = GURL("http://www.foo.com/"); | |
1394 request.load_flags = 0; | |
1395 | |
1396 CapturingNetLog net_log; | |
1397 session_deps_.net_log = &net_log; | |
1398 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
1399 | |
1400 // Note that because all these reads happen in the same | |
1401 // StaticSocketDataProvider, it shows that the same socket is being reused for | |
1402 // all transactions. | |
1403 MockRead data1_reads[] = { | |
1404 MockRead("HTTP/1.1 204 No Content\r\n\r\n"), | |
1405 MockRead("HTTP/1.1 205 Reset Content\r\n\r\n"), | |
1406 MockRead("HTTP/1.1 304 Not Modified\r\n\r\n"), | |
1407 MockRead("HTTP/1.1 302 Found\r\n" | |
1408 "Content-Length: 0\r\n\r\n"), | |
1409 MockRead("HTTP/1.1 302 Found\r\n" | |
1410 "Content-Length: 5\r\n\r\n" | |
1411 "hello"), | |
1412 MockRead("HTTP/1.1 301 Moved Permanently\r\n" | |
1413 "Content-Length: 0\r\n\r\n"), | |
1414 MockRead("HTTP/1.1 301 Moved Permanently\r\n" | |
1415 "Content-Length: 5\r\n\r\n" | |
1416 "hello"), | |
1417 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), | |
1418 MockRead("hello"), | |
1419 }; | |
1420 StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); | |
1421 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
1422 | |
1423 MockRead data2_reads[] = { | |
1424 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. | |
1425 }; | |
1426 StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); | |
1427 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
1428 | |
1429 const int kNumUnreadBodies = arraysize(data1_reads) - 2; | |
1430 std::string response_lines[kNumUnreadBodies]; | |
1431 | |
1432 uint32 first_socket_log_id = NetLog::Source::kInvalidId; | |
1433 for (size_t i = 0; i < arraysize(data1_reads) - 2; ++i) { | |
1434 TestCompletionCallback callback; | |
1435 | |
1436 scoped_ptr<HttpTransaction> trans( | |
1437 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
1438 | |
1439 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1440 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1441 | |
1442 rv = callback.WaitForResult(); | |
1443 EXPECT_EQ(OK, rv); | |
1444 | |
1445 LoadTimingInfo load_timing_info; | |
1446 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
1447 if (i == 0) { | |
1448 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); | |
1449 first_socket_log_id = load_timing_info.socket_log_id; | |
1450 } else { | |
1451 TestLoadTimingReused(load_timing_info); | |
1452 EXPECT_EQ(first_socket_log_id, load_timing_info.socket_log_id); | |
1453 } | |
1454 | |
1455 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1456 ASSERT_TRUE(response != NULL); | |
1457 | |
1458 ASSERT_TRUE(response->headers.get() != NULL); | |
1459 response_lines[i] = response->headers->GetStatusLine(); | |
1460 | |
1461 // We intentionally don't read the response bodies. | |
1462 } | |
1463 | |
1464 const char* const kStatusLines[] = { | |
1465 "HTTP/1.1 204 No Content", | |
1466 "HTTP/1.1 205 Reset Content", | |
1467 "HTTP/1.1 304 Not Modified", | |
1468 "HTTP/1.1 302 Found", | |
1469 "HTTP/1.1 302 Found", | |
1470 "HTTP/1.1 301 Moved Permanently", | |
1471 "HTTP/1.1 301 Moved Permanently", | |
1472 }; | |
1473 | |
1474 COMPILE_ASSERT(kNumUnreadBodies == arraysize(kStatusLines), | |
1475 forgot_to_update_kStatusLines); | |
1476 | |
1477 for (int i = 0; i < kNumUnreadBodies; ++i) | |
1478 EXPECT_EQ(kStatusLines[i], response_lines[i]); | |
1479 | |
1480 TestCompletionCallback callback; | |
1481 scoped_ptr<HttpTransaction> trans( | |
1482 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
1483 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1484 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1485 rv = callback.WaitForResult(); | |
1486 EXPECT_EQ(OK, rv); | |
1487 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1488 ASSERT_TRUE(response != NULL); | |
1489 ASSERT_TRUE(response->headers.get() != NULL); | |
1490 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
1491 std::string response_data; | |
1492 rv = ReadTransaction(trans.get(), &response_data); | |
1493 EXPECT_EQ(OK, rv); | |
1494 EXPECT_EQ("hello", response_data); | |
1495 } | |
1496 | |
1497 // Test the request-challenge-retry sequence for basic auth. | |
1498 // (basic auth is the easiest to mock, because it has no randomness). | |
1499 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuth) { | |
1500 HttpRequestInfo request; | |
1501 request.method = "GET"; | |
1502 request.url = GURL("http://www.google.com/"); | |
1503 request.load_flags = 0; | |
1504 | |
1505 CapturingNetLog log; | |
1506 session_deps_.net_log = &log; | |
1507 scoped_ptr<HttpTransaction> trans( | |
1508 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
1509 CreateSession(&session_deps_))); | |
1510 | |
1511 MockWrite data_writes1[] = { | |
1512 MockWrite("GET / HTTP/1.1\r\n" | |
1513 "Host: www.google.com\r\n" | |
1514 "Connection: keep-alive\r\n\r\n"), | |
1515 }; | |
1516 | |
1517 MockRead data_reads1[] = { | |
1518 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
1519 // Give a couple authenticate options (only the middle one is actually | |
1520 // supported). | |
1521 MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. | |
1522 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
1523 MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), | |
1524 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1525 // Large content-length -- won't matter, as connection will be reset. | |
1526 MockRead("Content-Length: 10000\r\n\r\n"), | |
1527 MockRead(SYNCHRONOUS, ERR_FAILED), | |
1528 }; | |
1529 | |
1530 // After calling trans->RestartWithAuth(), this is the request we should | |
1531 // be issuing -- the final header line contains the credentials. | |
1532 MockWrite data_writes2[] = { | |
1533 MockWrite("GET / HTTP/1.1\r\n" | |
1534 "Host: www.google.com\r\n" | |
1535 "Connection: keep-alive\r\n" | |
1536 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
1537 }; | |
1538 | |
1539 // Lastly, the server responds with the actual content. | |
1540 MockRead data_reads2[] = { | |
1541 MockRead("HTTP/1.0 200 OK\r\n"), | |
1542 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1543 MockRead("Content-Length: 100\r\n\r\n"), | |
1544 MockRead(SYNCHRONOUS, OK), | |
1545 }; | |
1546 | |
1547 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
1548 data_writes1, arraysize(data_writes1)); | |
1549 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
1550 data_writes2, arraysize(data_writes2)); | |
1551 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
1552 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
1553 | |
1554 TestCompletionCallback callback1; | |
1555 | |
1556 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
1557 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1558 | |
1559 rv = callback1.WaitForResult(); | |
1560 EXPECT_EQ(OK, rv); | |
1561 | |
1562 LoadTimingInfo load_timing_info1; | |
1563 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info1)); | |
1564 TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_DNS_TIMES); | |
1565 | |
1566 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1567 ASSERT_TRUE(response != NULL); | |
1568 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
1569 | |
1570 TestCompletionCallback callback2; | |
1571 | |
1572 rv = trans->RestartWithAuth( | |
1573 AuthCredentials(kFoo, kBar), callback2.callback()); | |
1574 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1575 | |
1576 rv = callback2.WaitForResult(); | |
1577 EXPECT_EQ(OK, rv); | |
1578 | |
1579 LoadTimingInfo load_timing_info2; | |
1580 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info2)); | |
1581 TestLoadTimingNotReused(load_timing_info2, CONNECT_TIMING_HAS_DNS_TIMES); | |
1582 // The load timing after restart should have a new socket ID, and times after | |
1583 // those of the first load timing. | |
1584 EXPECT_LE(load_timing_info1.receive_headers_end, | |
1585 load_timing_info2.connect_timing.connect_start); | |
1586 EXPECT_NE(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); | |
1587 | |
1588 response = trans->GetResponseInfo(); | |
1589 ASSERT_TRUE(response != NULL); | |
1590 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
1591 EXPECT_EQ(100, response->headers->GetContentLength()); | |
1592 } | |
1593 | |
1594 TEST_F(HttpNetworkTransactionSpdy2Test, DoNotSendAuth) { | |
1595 HttpRequestInfo request; | |
1596 request.method = "GET"; | |
1597 request.url = GURL("http://www.google.com/"); | |
1598 request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; | |
1599 | |
1600 scoped_ptr<HttpTransaction> trans( | |
1601 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
1602 CreateSession(&session_deps_))); | |
1603 | |
1604 MockWrite data_writes[] = { | |
1605 MockWrite("GET / HTTP/1.1\r\n" | |
1606 "Host: www.google.com\r\n" | |
1607 "Connection: keep-alive\r\n\r\n"), | |
1608 }; | |
1609 | |
1610 MockRead data_reads[] = { | |
1611 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
1612 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
1613 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1614 // Large content-length -- won't matter, as connection will be reset. | |
1615 MockRead("Content-Length: 10000\r\n\r\n"), | |
1616 MockRead(SYNCHRONOUS, ERR_FAILED), | |
1617 }; | |
1618 | |
1619 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
1620 data_writes, arraysize(data_writes)); | |
1621 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
1622 TestCompletionCallback callback; | |
1623 | |
1624 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
1625 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1626 | |
1627 rv = callback.WaitForResult(); | |
1628 EXPECT_EQ(0, rv); | |
1629 | |
1630 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1631 ASSERT_TRUE(response != NULL); | |
1632 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
1633 } | |
1634 | |
1635 // Test the request-challenge-retry sequence for basic auth, over a keep-alive | |
1636 // connection. | |
1637 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthKeepAlive) { | |
1638 HttpRequestInfo request; | |
1639 request.method = "GET"; | |
1640 request.url = GURL("http://www.google.com/"); | |
1641 request.load_flags = 0; | |
1642 | |
1643 CapturingNetLog log; | |
1644 session_deps_.net_log = &log; | |
1645 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
1646 | |
1647 MockWrite data_writes1[] = { | |
1648 MockWrite("GET / HTTP/1.1\r\n" | |
1649 "Host: www.google.com\r\n" | |
1650 "Connection: keep-alive\r\n\r\n"), | |
1651 | |
1652 // After calling trans->RestartWithAuth(), this is the request we should | |
1653 // be issuing -- the final header line contains the credentials. | |
1654 MockWrite("GET / HTTP/1.1\r\n" | |
1655 "Host: www.google.com\r\n" | |
1656 "Connection: keep-alive\r\n" | |
1657 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
1658 }; | |
1659 | |
1660 MockRead data_reads1[] = { | |
1661 MockRead("HTTP/1.1 401 Unauthorized\r\n"), | |
1662 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
1663 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1664 MockRead("Content-Length: 14\r\n\r\n"), | |
1665 MockRead("Unauthorized\r\n"), | |
1666 | |
1667 // Lastly, the server responds with the actual content. | |
1668 MockRead("HTTP/1.1 200 OK\r\n"), | |
1669 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1670 MockRead("Content-Length: 5\r\n\r\n"), | |
1671 MockRead("Hello"), | |
1672 }; | |
1673 | |
1674 // If there is a regression where we disconnect a Keep-Alive | |
1675 // connection during an auth roundtrip, we'll end up reading this. | |
1676 MockRead data_reads2[] = { | |
1677 MockRead(SYNCHRONOUS, ERR_FAILED), | |
1678 }; | |
1679 | |
1680 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
1681 data_writes1, arraysize(data_writes1)); | |
1682 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
1683 NULL, 0); | |
1684 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
1685 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
1686 | |
1687 TestCompletionCallback callback1; | |
1688 | |
1689 scoped_ptr<HttpTransaction> trans( | |
1690 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
1691 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
1692 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1693 | |
1694 rv = callback1.WaitForResult(); | |
1695 EXPECT_EQ(OK, rv); | |
1696 | |
1697 LoadTimingInfo load_timing_info1; | |
1698 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info1)); | |
1699 TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_DNS_TIMES); | |
1700 | |
1701 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1702 ASSERT_TRUE(response != NULL); | |
1703 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
1704 | |
1705 TestCompletionCallback callback2; | |
1706 | |
1707 rv = trans->RestartWithAuth( | |
1708 AuthCredentials(kFoo, kBar), callback2.callback()); | |
1709 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1710 | |
1711 rv = callback2.WaitForResult(); | |
1712 EXPECT_EQ(OK, rv); | |
1713 | |
1714 LoadTimingInfo load_timing_info2; | |
1715 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info2)); | |
1716 TestLoadTimingReused(load_timing_info2); | |
1717 // The load timing after restart should have the same socket ID, and times | |
1718 // those of the first load timing. | |
1719 EXPECT_LE(load_timing_info1.receive_headers_end, | |
1720 load_timing_info2.send_start); | |
1721 EXPECT_EQ(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); | |
1722 | |
1723 response = trans->GetResponseInfo(); | |
1724 ASSERT_TRUE(response != NULL); | |
1725 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
1726 EXPECT_EQ(5, response->headers->GetContentLength()); | |
1727 } | |
1728 | |
1729 // Test the request-challenge-retry sequence for basic auth, over a keep-alive | |
1730 // connection and with no response body to drain. | |
1731 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthKeepAliveNoBody) { | |
1732 HttpRequestInfo request; | |
1733 request.method = "GET"; | |
1734 request.url = GURL("http://www.google.com/"); | |
1735 request.load_flags = 0; | |
1736 | |
1737 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
1738 | |
1739 MockWrite data_writes1[] = { | |
1740 MockWrite("GET / HTTP/1.1\r\n" | |
1741 "Host: www.google.com\r\n" | |
1742 "Connection: keep-alive\r\n\r\n"), | |
1743 | |
1744 // After calling trans->RestartWithAuth(), this is the request we should | |
1745 // be issuing -- the final header line contains the credentials. | |
1746 MockWrite("GET / HTTP/1.1\r\n" | |
1747 "Host: www.google.com\r\n" | |
1748 "Connection: keep-alive\r\n" | |
1749 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
1750 }; | |
1751 | |
1752 MockRead data_reads1[] = { | |
1753 MockRead("HTTP/1.1 401 Unauthorized\r\n"), | |
1754 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
1755 MockRead("Content-Length: 0\r\n\r\n"), // No response body. | |
1756 | |
1757 // Lastly, the server responds with the actual content. | |
1758 MockRead("HTTP/1.1 200 OK\r\n"), | |
1759 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1760 MockRead("Content-Length: 5\r\n\r\n"), | |
1761 MockRead("hello"), | |
1762 }; | |
1763 | |
1764 // An incorrect reconnect would cause this to be read. | |
1765 MockRead data_reads2[] = { | |
1766 MockRead(SYNCHRONOUS, ERR_FAILED), | |
1767 }; | |
1768 | |
1769 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
1770 data_writes1, arraysize(data_writes1)); | |
1771 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
1772 NULL, 0); | |
1773 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
1774 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
1775 | |
1776 TestCompletionCallback callback1; | |
1777 | |
1778 scoped_ptr<HttpTransaction> trans( | |
1779 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
1780 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
1781 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1782 | |
1783 rv = callback1.WaitForResult(); | |
1784 EXPECT_EQ(OK, rv); | |
1785 | |
1786 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1787 ASSERT_TRUE(response != NULL); | |
1788 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
1789 | |
1790 TestCompletionCallback callback2; | |
1791 | |
1792 rv = trans->RestartWithAuth( | |
1793 AuthCredentials(kFoo, kBar), callback2.callback()); | |
1794 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1795 | |
1796 rv = callback2.WaitForResult(); | |
1797 EXPECT_EQ(OK, rv); | |
1798 | |
1799 response = trans->GetResponseInfo(); | |
1800 ASSERT_TRUE(response != NULL); | |
1801 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
1802 EXPECT_EQ(5, response->headers->GetContentLength()); | |
1803 } | |
1804 | |
1805 // Test the request-challenge-retry sequence for basic auth, over a keep-alive | |
1806 // connection and with a large response body to drain. | |
1807 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthKeepAliveLargeBody) { | |
1808 HttpRequestInfo request; | |
1809 request.method = "GET"; | |
1810 request.url = GURL("http://www.google.com/"); | |
1811 request.load_flags = 0; | |
1812 | |
1813 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
1814 | |
1815 MockWrite data_writes1[] = { | |
1816 MockWrite("GET / HTTP/1.1\r\n" | |
1817 "Host: www.google.com\r\n" | |
1818 "Connection: keep-alive\r\n\r\n"), | |
1819 | |
1820 // After calling trans->RestartWithAuth(), this is the request we should | |
1821 // be issuing -- the final header line contains the credentials. | |
1822 MockWrite("GET / HTTP/1.1\r\n" | |
1823 "Host: www.google.com\r\n" | |
1824 "Connection: keep-alive\r\n" | |
1825 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
1826 }; | |
1827 | |
1828 // Respond with 5 kb of response body. | |
1829 std::string large_body_string("Unauthorized"); | |
1830 large_body_string.append(5 * 1024, ' '); | |
1831 large_body_string.append("\r\n"); | |
1832 | |
1833 MockRead data_reads1[] = { | |
1834 MockRead("HTTP/1.1 401 Unauthorized\r\n"), | |
1835 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
1836 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1837 // 5134 = 12 + 5 * 1024 + 2 | |
1838 MockRead("Content-Length: 5134\r\n\r\n"), | |
1839 MockRead(ASYNC, large_body_string.data(), large_body_string.size()), | |
1840 | |
1841 // Lastly, the server responds with the actual content. | |
1842 MockRead("HTTP/1.1 200 OK\r\n"), | |
1843 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1844 MockRead("Content-Length: 5\r\n\r\n"), | |
1845 MockRead("hello"), | |
1846 }; | |
1847 | |
1848 // An incorrect reconnect would cause this to be read. | |
1849 MockRead data_reads2[] = { | |
1850 MockRead(SYNCHRONOUS, ERR_FAILED), | |
1851 }; | |
1852 | |
1853 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
1854 data_writes1, arraysize(data_writes1)); | |
1855 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
1856 NULL, 0); | |
1857 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
1858 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
1859 | |
1860 TestCompletionCallback callback1; | |
1861 | |
1862 scoped_ptr<HttpTransaction> trans( | |
1863 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
1864 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
1865 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1866 | |
1867 rv = callback1.WaitForResult(); | |
1868 EXPECT_EQ(OK, rv); | |
1869 | |
1870 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1871 ASSERT_TRUE(response != NULL); | |
1872 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
1873 | |
1874 TestCompletionCallback callback2; | |
1875 | |
1876 rv = trans->RestartWithAuth( | |
1877 AuthCredentials(kFoo, kBar), callback2.callback()); | |
1878 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1879 | |
1880 rv = callback2.WaitForResult(); | |
1881 EXPECT_EQ(OK, rv); | |
1882 | |
1883 response = trans->GetResponseInfo(); | |
1884 ASSERT_TRUE(response != NULL); | |
1885 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
1886 EXPECT_EQ(5, response->headers->GetContentLength()); | |
1887 } | |
1888 | |
1889 // Test the request-challenge-retry sequence for basic auth, over a keep-alive | |
1890 // connection, but the server gets impatient and closes the connection. | |
1891 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthKeepAliveImpatientServer) { | |
1892 HttpRequestInfo request; | |
1893 request.method = "GET"; | |
1894 request.url = GURL("http://www.google.com/"); | |
1895 request.load_flags = 0; | |
1896 | |
1897 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
1898 | |
1899 MockWrite data_writes1[] = { | |
1900 MockWrite("GET / HTTP/1.1\r\n" | |
1901 "Host: www.google.com\r\n" | |
1902 "Connection: keep-alive\r\n\r\n"), | |
1903 // This simulates the seemingly successful write to a closed connection | |
1904 // if the bug is not fixed. | |
1905 MockWrite("GET / HTTP/1.1\r\n" | |
1906 "Host: www.google.com\r\n" | |
1907 "Connection: keep-alive\r\n" | |
1908 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
1909 }; | |
1910 | |
1911 MockRead data_reads1[] = { | |
1912 MockRead("HTTP/1.1 401 Unauthorized\r\n"), | |
1913 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
1914 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1915 MockRead("Content-Length: 14\r\n\r\n"), | |
1916 // Tell MockTCPClientSocket to simulate the server closing the connection. | |
1917 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), | |
1918 MockRead("Unauthorized\r\n"), | |
1919 MockRead(SYNCHRONOUS, OK), // The server closes the connection. | |
1920 }; | |
1921 | |
1922 // After calling trans->RestartWithAuth(), this is the request we should | |
1923 // be issuing -- the final header line contains the credentials. | |
1924 MockWrite data_writes2[] = { | |
1925 MockWrite("GET / HTTP/1.1\r\n" | |
1926 "Host: www.google.com\r\n" | |
1927 "Connection: keep-alive\r\n" | |
1928 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
1929 }; | |
1930 | |
1931 // Lastly, the server responds with the actual content. | |
1932 MockRead data_reads2[] = { | |
1933 MockRead("HTTP/1.1 200 OK\r\n"), | |
1934 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
1935 MockRead("Content-Length: 5\r\n\r\n"), | |
1936 MockRead("hello"), | |
1937 }; | |
1938 | |
1939 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
1940 data_writes1, arraysize(data_writes1)); | |
1941 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
1942 data_writes2, arraysize(data_writes2)); | |
1943 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
1944 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
1945 | |
1946 TestCompletionCallback callback1; | |
1947 | |
1948 scoped_ptr<HttpTransaction> trans( | |
1949 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
1950 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
1951 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1952 | |
1953 rv = callback1.WaitForResult(); | |
1954 EXPECT_EQ(OK, rv); | |
1955 | |
1956 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
1957 ASSERT_TRUE(response != NULL); | |
1958 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
1959 | |
1960 TestCompletionCallback callback2; | |
1961 | |
1962 rv = trans->RestartWithAuth( | |
1963 AuthCredentials(kFoo, kBar), callback2.callback()); | |
1964 EXPECT_EQ(ERR_IO_PENDING, rv); | |
1965 | |
1966 rv = callback2.WaitForResult(); | |
1967 EXPECT_EQ(OK, rv); | |
1968 | |
1969 response = trans->GetResponseInfo(); | |
1970 ASSERT_TRUE(response != NULL); | |
1971 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
1972 EXPECT_EQ(5, response->headers->GetContentLength()); | |
1973 } | |
1974 | |
1975 // Test the request-challenge-retry sequence for basic auth, over a connection | |
1976 // that requires a restart when setting up an SSL tunnel. | |
1977 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthProxyNoKeepAlive) { | |
1978 HttpRequestInfo request; | |
1979 request.method = "GET"; | |
1980 request.url = GURL("https://www.google.com/"); | |
1981 // when the no authentication data flag is set. | |
1982 request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; | |
1983 | |
1984 // Configure against proxy server "myproxy:70". | |
1985 session_deps_.proxy_service.reset( | |
1986 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70")); | |
1987 CapturingBoundNetLog log; | |
1988 session_deps_.net_log = log.bound().net_log(); | |
1989 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
1990 | |
1991 // Since we have proxy, should try to establish tunnel. | |
1992 MockWrite data_writes1[] = { | |
1993 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
1994 "Host: www.google.com\r\n" | |
1995 "Proxy-Connection: keep-alive\r\n\r\n"), | |
1996 | |
1997 // After calling trans->RestartWithAuth(), this is the request we should | |
1998 // be issuing -- the final header line contains the credentials. | |
1999 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
2000 "Host: www.google.com\r\n" | |
2001 "Proxy-Connection: keep-alive\r\n" | |
2002 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
2003 | |
2004 MockWrite("GET / HTTP/1.1\r\n" | |
2005 "Host: www.google.com\r\n" | |
2006 "Connection: keep-alive\r\n\r\n"), | |
2007 }; | |
2008 | |
2009 // The proxy responds to the connect with a 407, using a persistent | |
2010 // connection. | |
2011 MockRead data_reads1[] = { | |
2012 // No credentials. | |
2013 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), | |
2014 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
2015 MockRead("Proxy-Connection: close\r\n\r\n"), | |
2016 | |
2017 MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), | |
2018 | |
2019 MockRead("HTTP/1.1 200 OK\r\n"), | |
2020 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
2021 MockRead("Content-Length: 5\r\n\r\n"), | |
2022 MockRead(SYNCHRONOUS, "hello"), | |
2023 }; | |
2024 | |
2025 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
2026 data_writes1, arraysize(data_writes1)); | |
2027 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
2028 SSLSocketDataProvider ssl(ASYNC, OK); | |
2029 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2030 | |
2031 TestCompletionCallback callback1; | |
2032 | |
2033 scoped_ptr<HttpTransaction> trans( | |
2034 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2035 | |
2036 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
2037 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2038 | |
2039 rv = callback1.WaitForResult(); | |
2040 EXPECT_EQ(OK, rv); | |
2041 net::CapturingNetLog::CapturedEntryList entries; | |
2042 log.GetEntries(&entries); | |
2043 size_t pos = ExpectLogContainsSomewhere( | |
2044 entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, | |
2045 NetLog::PHASE_NONE); | |
2046 ExpectLogContainsSomewhere( | |
2047 entries, pos, | |
2048 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, | |
2049 NetLog::PHASE_NONE); | |
2050 | |
2051 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
2052 ASSERT_TRUE(response != NULL); | |
2053 ASSERT_FALSE(response->headers.get() == NULL); | |
2054 EXPECT_EQ(407, response->headers->response_code()); | |
2055 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
2056 EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); | |
2057 | |
2058 LoadTimingInfo load_timing_info; | |
2059 // CONNECT requests and responses are handled at the connect job level, so | |
2060 // the transaction does not yet have a connection. | |
2061 EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); | |
2062 | |
2063 TestCompletionCallback callback2; | |
2064 | |
2065 rv = trans->RestartWithAuth( | |
2066 AuthCredentials(kFoo, kBar), callback2.callback()); | |
2067 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2068 | |
2069 rv = callback2.WaitForResult(); | |
2070 EXPECT_EQ(OK, rv); | |
2071 | |
2072 response = trans->GetResponseInfo(); | |
2073 ASSERT_TRUE(response != NULL); | |
2074 | |
2075 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
2076 EXPECT_EQ(200, response->headers->response_code()); | |
2077 EXPECT_EQ(5, response->headers->GetContentLength()); | |
2078 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
2079 | |
2080 // The password prompt info should not be set. | |
2081 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
2082 | |
2083 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
2084 TestLoadTimingNotReusedWithPac(load_timing_info, | |
2085 CONNECT_TIMING_HAS_SSL_TIMES); | |
2086 | |
2087 trans.reset(); | |
2088 session->CloseAllConnections(); | |
2089 } | |
2090 | |
2091 // Test the request-challenge-retry sequence for basic auth, over a keep-alive | |
2092 // proxy connection, when setting up an SSL tunnel. | |
2093 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthProxyKeepAlive) { | |
2094 HttpRequestInfo request; | |
2095 request.method = "GET"; | |
2096 request.url = GURL("https://www.google.com/"); | |
2097 // Ensure that proxy authentication is attempted even | |
2098 // when the no authentication data flag is set. | |
2099 request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; | |
2100 | |
2101 // Configure against proxy server "myproxy:70". | |
2102 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
2103 CapturingBoundNetLog log; | |
2104 session_deps_.net_log = log.bound().net_log(); | |
2105 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2106 | |
2107 scoped_ptr<HttpTransaction> trans( | |
2108 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2109 | |
2110 // Since we have proxy, should try to establish tunnel. | |
2111 MockWrite data_writes1[] = { | |
2112 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
2113 "Host: www.google.com\r\n" | |
2114 "Proxy-Connection: keep-alive\r\n\r\n"), | |
2115 | |
2116 // After calling trans->RestartWithAuth(), this is the request we should | |
2117 // be issuing -- the final header line contains the credentials. | |
2118 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
2119 "Host: www.google.com\r\n" | |
2120 "Proxy-Connection: keep-alive\r\n" | |
2121 "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), | |
2122 }; | |
2123 | |
2124 // The proxy responds to the connect with a 407, using a persistent | |
2125 // connection. | |
2126 MockRead data_reads1[] = { | |
2127 // No credentials. | |
2128 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), | |
2129 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
2130 MockRead("Content-Length: 10\r\n\r\n"), | |
2131 MockRead("0123456789"), | |
2132 | |
2133 // Wrong credentials (wrong password). | |
2134 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), | |
2135 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
2136 MockRead("Content-Length: 10\r\n\r\n"), | |
2137 // No response body because the test stops reading here. | |
2138 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. | |
2139 }; | |
2140 | |
2141 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
2142 data_writes1, arraysize(data_writes1)); | |
2143 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
2144 | |
2145 TestCompletionCallback callback1; | |
2146 | |
2147 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
2148 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2149 | |
2150 rv = callback1.WaitForResult(); | |
2151 EXPECT_EQ(OK, rv); | |
2152 net::CapturingNetLog::CapturedEntryList entries; | |
2153 log.GetEntries(&entries); | |
2154 size_t pos = ExpectLogContainsSomewhere( | |
2155 entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, | |
2156 NetLog::PHASE_NONE); | |
2157 ExpectLogContainsSomewhere( | |
2158 entries, pos, | |
2159 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, | |
2160 NetLog::PHASE_NONE); | |
2161 | |
2162 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
2163 ASSERT_TRUE(response != NULL); | |
2164 ASSERT_FALSE(response->headers.get() == NULL); | |
2165 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
2166 EXPECT_EQ(407, response->headers->response_code()); | |
2167 EXPECT_EQ(10, response->headers->GetContentLength()); | |
2168 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
2169 EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); | |
2170 | |
2171 TestCompletionCallback callback2; | |
2172 | |
2173 // Wrong password (should be "bar"). | |
2174 rv = trans->RestartWithAuth( | |
2175 AuthCredentials(kFoo, kBaz), callback2.callback()); | |
2176 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2177 | |
2178 rv = callback2.WaitForResult(); | |
2179 EXPECT_EQ(OK, rv); | |
2180 | |
2181 response = trans->GetResponseInfo(); | |
2182 ASSERT_TRUE(response != NULL); | |
2183 ASSERT_FALSE(response->headers.get() == NULL); | |
2184 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
2185 EXPECT_EQ(407, response->headers->response_code()); | |
2186 EXPECT_EQ(10, response->headers->GetContentLength()); | |
2187 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
2188 EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); | |
2189 | |
2190 // Flush the idle socket before the NetLog and HttpNetworkTransaction go | |
2191 // out of scope. | |
2192 session->CloseAllConnections(); | |
2193 } | |
2194 | |
2195 // Test that we don't read the response body when we fail to establish a tunnel, | |
2196 // even if the user cancels the proxy's auth attempt. | |
2197 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthProxyCancelTunnel) { | |
2198 HttpRequestInfo request; | |
2199 request.method = "GET"; | |
2200 request.url = GURL("https://www.google.com/"); | |
2201 request.load_flags = 0; | |
2202 | |
2203 // Configure against proxy server "myproxy:70". | |
2204 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
2205 | |
2206 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2207 | |
2208 scoped_ptr<HttpTransaction> trans( | |
2209 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2210 | |
2211 // Since we have proxy, should try to establish tunnel. | |
2212 MockWrite data_writes[] = { | |
2213 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
2214 "Host: www.google.com\r\n" | |
2215 "Proxy-Connection: keep-alive\r\n\r\n"), | |
2216 }; | |
2217 | |
2218 // The proxy responds to the connect with a 407. | |
2219 MockRead data_reads[] = { | |
2220 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), | |
2221 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
2222 MockRead("Content-Length: 10\r\n\r\n"), | |
2223 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. | |
2224 }; | |
2225 | |
2226 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
2227 data_writes, arraysize(data_writes)); | |
2228 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
2229 | |
2230 TestCompletionCallback callback; | |
2231 | |
2232 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
2233 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2234 | |
2235 rv = callback.WaitForResult(); | |
2236 EXPECT_EQ(OK, rv); | |
2237 | |
2238 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
2239 ASSERT_TRUE(response != NULL); | |
2240 | |
2241 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
2242 EXPECT_EQ(407, response->headers->response_code()); | |
2243 EXPECT_EQ(10, response->headers->GetContentLength()); | |
2244 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
2245 | |
2246 std::string response_data; | |
2247 rv = ReadTransaction(trans.get(), &response_data); | |
2248 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); | |
2249 | |
2250 // Flush the idle socket before the HttpNetworkTransaction goes out of scope. | |
2251 session->CloseAllConnections(); | |
2252 } | |
2253 | |
2254 // Test when a server (non-proxy) returns a 407 (proxy-authenticate). | |
2255 // The request should fail with ERR_UNEXPECTED_PROXY_AUTH. | |
2256 TEST_F(HttpNetworkTransactionSpdy2Test, UnexpectedProxyAuth) { | |
2257 HttpRequestInfo request; | |
2258 request.method = "GET"; | |
2259 request.url = GURL("http://www.google.com/"); | |
2260 request.load_flags = 0; | |
2261 | |
2262 // We are using a DIRECT connection (i.e. no proxy) for this session. | |
2263 scoped_ptr<HttpTransaction> trans( | |
2264 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
2265 CreateSession(&session_deps_))); | |
2266 | |
2267 MockWrite data_writes1[] = { | |
2268 MockWrite("GET / HTTP/1.1\r\n" | |
2269 "Host: www.google.com\r\n" | |
2270 "Connection: keep-alive\r\n\r\n"), | |
2271 }; | |
2272 | |
2273 MockRead data_reads1[] = { | |
2274 MockRead("HTTP/1.0 407 Proxy Auth required\r\n"), | |
2275 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
2276 // Large content-length -- won't matter, as connection will be reset. | |
2277 MockRead("Content-Length: 10000\r\n\r\n"), | |
2278 MockRead(SYNCHRONOUS, ERR_FAILED), | |
2279 }; | |
2280 | |
2281 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
2282 data_writes1, arraysize(data_writes1)); | |
2283 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
2284 | |
2285 TestCompletionCallback callback; | |
2286 | |
2287 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
2288 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2289 | |
2290 rv = callback.WaitForResult(); | |
2291 EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); | |
2292 } | |
2293 | |
2294 // Tests when an HTTPS server (non-proxy) returns a 407 (proxy-authentication) | |
2295 // through a non-authenticating proxy. The request should fail with | |
2296 // ERR_UNEXPECTED_PROXY_AUTH. | |
2297 // Note that it is impossible to detect if an HTTP server returns a 407 through | |
2298 // a non-authenticating proxy - there is nothing to indicate whether the | |
2299 // response came from the proxy or the server, so it is treated as if the proxy | |
2300 // issued the challenge. | |
2301 TEST_F(HttpNetworkTransactionSpdy2Test, | |
2302 HttpsServerRequestsProxyAuthThroughProxy) { | |
2303 HttpRequestInfo request; | |
2304 request.method = "GET"; | |
2305 request.url = GURL("https://www.google.com/"); | |
2306 | |
2307 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
2308 CapturingBoundNetLog log; | |
2309 session_deps_.net_log = log.bound().net_log(); | |
2310 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2311 | |
2312 // Since we have proxy, should try to establish tunnel. | |
2313 MockWrite data_writes1[] = { | |
2314 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
2315 "Host: www.google.com\r\n" | |
2316 "Proxy-Connection: keep-alive\r\n\r\n"), | |
2317 | |
2318 MockWrite("GET / HTTP/1.1\r\n" | |
2319 "Host: www.google.com\r\n" | |
2320 "Connection: keep-alive\r\n\r\n"), | |
2321 }; | |
2322 | |
2323 MockRead data_reads1[] = { | |
2324 MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), | |
2325 | |
2326 MockRead("HTTP/1.1 407 Unauthorized\r\n"), | |
2327 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
2328 MockRead("\r\n"), | |
2329 MockRead(SYNCHRONOUS, OK), | |
2330 }; | |
2331 | |
2332 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
2333 data_writes1, arraysize(data_writes1)); | |
2334 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
2335 SSLSocketDataProvider ssl(ASYNC, OK); | |
2336 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2337 | |
2338 TestCompletionCallback callback1; | |
2339 | |
2340 scoped_ptr<HttpTransaction> trans( | |
2341 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2342 | |
2343 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
2344 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2345 | |
2346 rv = callback1.WaitForResult(); | |
2347 EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); | |
2348 net::CapturingNetLog::CapturedEntryList entries; | |
2349 log.GetEntries(&entries); | |
2350 size_t pos = ExpectLogContainsSomewhere( | |
2351 entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, | |
2352 NetLog::PHASE_NONE); | |
2353 ExpectLogContainsSomewhere( | |
2354 entries, pos, | |
2355 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, | |
2356 NetLog::PHASE_NONE); | |
2357 } | |
2358 | |
2359 // Test the load timing for HTTPS requests with an HTTP proxy. | |
2360 TEST_F(HttpNetworkTransactionSpdy2Test, HttpProxyLoadTimingNoPacTwoRequests) { | |
2361 HttpRequestInfo request1; | |
2362 request1.method = "GET"; | |
2363 request1.url = GURL("https://www.google.com/1"); | |
2364 | |
2365 HttpRequestInfo request2; | |
2366 request2.method = "GET"; | |
2367 request2.url = GURL("https://www.google.com/2"); | |
2368 | |
2369 // Configure against proxy server "myproxy:70". | |
2370 session_deps_.proxy_service.reset( | |
2371 ProxyService::CreateFixed("PROXY myproxy:70")); | |
2372 CapturingBoundNetLog log; | |
2373 session_deps_.net_log = log.bound().net_log(); | |
2374 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2375 | |
2376 // Since we have proxy, should try to establish tunnel. | |
2377 MockWrite data_writes1[] = { | |
2378 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
2379 "Host: www.google.com\r\n" | |
2380 "Proxy-Connection: keep-alive\r\n\r\n"), | |
2381 | |
2382 MockWrite("GET /1 HTTP/1.1\r\n" | |
2383 "Host: www.google.com\r\n" | |
2384 "Connection: keep-alive\r\n\r\n"), | |
2385 | |
2386 MockWrite("GET /2 HTTP/1.1\r\n" | |
2387 "Host: www.google.com\r\n" | |
2388 "Connection: keep-alive\r\n\r\n"), | |
2389 }; | |
2390 | |
2391 // The proxy responds to the connect with a 407, using a persistent | |
2392 // connection. | |
2393 MockRead data_reads1[] = { | |
2394 MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), | |
2395 | |
2396 MockRead("HTTP/1.1 200 OK\r\n"), | |
2397 MockRead("Content-Length: 1\r\n\r\n"), | |
2398 MockRead(SYNCHRONOUS, "1"), | |
2399 | |
2400 MockRead("HTTP/1.1 200 OK\r\n"), | |
2401 MockRead("Content-Length: 2\r\n\r\n"), | |
2402 MockRead(SYNCHRONOUS, "22"), | |
2403 }; | |
2404 | |
2405 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
2406 data_writes1, arraysize(data_writes1)); | |
2407 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
2408 SSLSocketDataProvider ssl(ASYNC, OK); | |
2409 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2410 | |
2411 TestCompletionCallback callback1; | |
2412 scoped_ptr<HttpTransaction> trans1( | |
2413 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2414 | |
2415 int rv = trans1->Start(&request1, callback1.callback(), log.bound()); | |
2416 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2417 | |
2418 rv = callback1.WaitForResult(); | |
2419 EXPECT_EQ(OK, rv); | |
2420 | |
2421 const HttpResponseInfo* response1 = trans1->GetResponseInfo(); | |
2422 ASSERT_TRUE(response1 != NULL); | |
2423 ASSERT_TRUE(response1->headers.get() != NULL); | |
2424 EXPECT_EQ(1, response1->headers->GetContentLength()); | |
2425 | |
2426 LoadTimingInfo load_timing_info1; | |
2427 EXPECT_TRUE(trans1->GetLoadTimingInfo(&load_timing_info1)); | |
2428 TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_SSL_TIMES); | |
2429 | |
2430 trans1.reset(); | |
2431 | |
2432 TestCompletionCallback callback2; | |
2433 scoped_ptr<HttpTransaction> trans2( | |
2434 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2435 | |
2436 rv = trans2->Start(&request2, callback2.callback(), log.bound()); | |
2437 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2438 | |
2439 rv = callback2.WaitForResult(); | |
2440 EXPECT_EQ(OK, rv); | |
2441 | |
2442 const HttpResponseInfo* response2 = trans2->GetResponseInfo(); | |
2443 ASSERT_TRUE(response2 != NULL); | |
2444 ASSERT_TRUE(response2->headers.get() != NULL); | |
2445 EXPECT_EQ(2, response2->headers->GetContentLength()); | |
2446 | |
2447 LoadTimingInfo load_timing_info2; | |
2448 EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); | |
2449 TestLoadTimingReused(load_timing_info2); | |
2450 | |
2451 EXPECT_EQ(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); | |
2452 | |
2453 trans2.reset(); | |
2454 session->CloseAllConnections(); | |
2455 } | |
2456 | |
2457 // Test the load timing for HTTPS requests with an HTTP proxy and a PAC script. | |
2458 TEST_F(HttpNetworkTransactionSpdy2Test, HttpProxyLoadTimingWithPacTwoRequests) { | |
2459 HttpRequestInfo request1; | |
2460 request1.method = "GET"; | |
2461 request1.url = GURL("https://www.google.com/1"); | |
2462 | |
2463 HttpRequestInfo request2; | |
2464 request2.method = "GET"; | |
2465 request2.url = GURL("https://www.google.com/2"); | |
2466 | |
2467 // Configure against proxy server "myproxy:70". | |
2468 session_deps_.proxy_service.reset( | |
2469 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70")); | |
2470 CapturingBoundNetLog log; | |
2471 session_deps_.net_log = log.bound().net_log(); | |
2472 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2473 | |
2474 // Since we have proxy, should try to establish tunnel. | |
2475 MockWrite data_writes1[] = { | |
2476 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
2477 "Host: www.google.com\r\n" | |
2478 "Proxy-Connection: keep-alive\r\n\r\n"), | |
2479 | |
2480 MockWrite("GET /1 HTTP/1.1\r\n" | |
2481 "Host: www.google.com\r\n" | |
2482 "Connection: keep-alive\r\n\r\n"), | |
2483 | |
2484 MockWrite("GET /2 HTTP/1.1\r\n" | |
2485 "Host: www.google.com\r\n" | |
2486 "Connection: keep-alive\r\n\r\n"), | |
2487 }; | |
2488 | |
2489 // The proxy responds to the connect with a 407, using a persistent | |
2490 // connection. | |
2491 MockRead data_reads1[] = { | |
2492 MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), | |
2493 | |
2494 MockRead("HTTP/1.1 200 OK\r\n"), | |
2495 MockRead("Content-Length: 1\r\n\r\n"), | |
2496 MockRead(SYNCHRONOUS, "1"), | |
2497 | |
2498 MockRead("HTTP/1.1 200 OK\r\n"), | |
2499 MockRead("Content-Length: 2\r\n\r\n"), | |
2500 MockRead(SYNCHRONOUS, "22"), | |
2501 }; | |
2502 | |
2503 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
2504 data_writes1, arraysize(data_writes1)); | |
2505 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
2506 SSLSocketDataProvider ssl(ASYNC, OK); | |
2507 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2508 | |
2509 TestCompletionCallback callback1; | |
2510 scoped_ptr<HttpTransaction> trans1( | |
2511 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2512 | |
2513 int rv = trans1->Start(&request1, callback1.callback(), log.bound()); | |
2514 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2515 | |
2516 rv = callback1.WaitForResult(); | |
2517 EXPECT_EQ(OK, rv); | |
2518 | |
2519 const HttpResponseInfo* response1 = trans1->GetResponseInfo(); | |
2520 ASSERT_TRUE(response1 != NULL); | |
2521 ASSERT_TRUE(response1->headers.get() != NULL); | |
2522 EXPECT_EQ(1, response1->headers->GetContentLength()); | |
2523 | |
2524 LoadTimingInfo load_timing_info1; | |
2525 EXPECT_TRUE(trans1->GetLoadTimingInfo(&load_timing_info1)); | |
2526 TestLoadTimingNotReusedWithPac(load_timing_info1, | |
2527 CONNECT_TIMING_HAS_SSL_TIMES); | |
2528 | |
2529 trans1.reset(); | |
2530 | |
2531 TestCompletionCallback callback2; | |
2532 scoped_ptr<HttpTransaction> trans2( | |
2533 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2534 | |
2535 rv = trans2->Start(&request2, callback2.callback(), log.bound()); | |
2536 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2537 | |
2538 rv = callback2.WaitForResult(); | |
2539 EXPECT_EQ(OK, rv); | |
2540 | |
2541 const HttpResponseInfo* response2 = trans2->GetResponseInfo(); | |
2542 ASSERT_TRUE(response2 != NULL); | |
2543 ASSERT_TRUE(response2->headers.get() != NULL); | |
2544 EXPECT_EQ(2, response2->headers->GetContentLength()); | |
2545 | |
2546 LoadTimingInfo load_timing_info2; | |
2547 EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); | |
2548 TestLoadTimingReusedWithPac(load_timing_info2); | |
2549 | |
2550 EXPECT_EQ(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); | |
2551 | |
2552 trans2.reset(); | |
2553 session->CloseAllConnections(); | |
2554 } | |
2555 | |
2556 // Test a simple get through an HTTPS Proxy. | |
2557 TEST_F(HttpNetworkTransactionSpdy2Test, HttpsProxyGet) { | |
2558 HttpRequestInfo request; | |
2559 request.method = "GET"; | |
2560 request.url = GURL("http://www.google.com/"); | |
2561 | |
2562 // Configure against https proxy server "proxy:70". | |
2563 session_deps_.proxy_service.reset(ProxyService::CreateFixed( | |
2564 "https://proxy:70")); | |
2565 CapturingBoundNetLog log; | |
2566 session_deps_.net_log = log.bound().net_log(); | |
2567 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2568 | |
2569 // Since we have proxy, should use full url | |
2570 MockWrite data_writes1[] = { | |
2571 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | |
2572 "Host: www.google.com\r\n" | |
2573 "Proxy-Connection: keep-alive\r\n\r\n"), | |
2574 }; | |
2575 | |
2576 MockRead data_reads1[] = { | |
2577 MockRead("HTTP/1.1 200 OK\r\n"), | |
2578 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
2579 MockRead("Content-Length: 100\r\n\r\n"), | |
2580 MockRead(SYNCHRONOUS, OK), | |
2581 }; | |
2582 | |
2583 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
2584 data_writes1, arraysize(data_writes1)); | |
2585 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
2586 SSLSocketDataProvider ssl(ASYNC, OK); | |
2587 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2588 | |
2589 TestCompletionCallback callback1; | |
2590 | |
2591 scoped_ptr<HttpTransaction> trans( | |
2592 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2593 | |
2594 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
2595 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2596 | |
2597 rv = callback1.WaitForResult(); | |
2598 EXPECT_EQ(OK, rv); | |
2599 | |
2600 LoadTimingInfo load_timing_info; | |
2601 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
2602 TestLoadTimingNotReused(load_timing_info, | |
2603 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
2604 | |
2605 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
2606 ASSERT_TRUE(response != NULL); | |
2607 | |
2608 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
2609 EXPECT_EQ(200, response->headers->response_code()); | |
2610 EXPECT_EQ(100, response->headers->GetContentLength()); | |
2611 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
2612 | |
2613 // The password prompt info should not be set. | |
2614 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
2615 } | |
2616 | |
2617 // Test a SPDY get through an HTTPS Proxy. | |
2618 TEST_F(HttpNetworkTransactionSpdy2Test, HttpsProxySpdyGet) { | |
2619 HttpRequestInfo request; | |
2620 request.method = "GET"; | |
2621 request.url = GURL("http://www.google.com/"); | |
2622 request.load_flags = 0; | |
2623 | |
2624 // Configure against https proxy server "proxy:70". | |
2625 session_deps_.proxy_service.reset(ProxyService::CreateFixed( | |
2626 "https://proxy:70")); | |
2627 CapturingBoundNetLog log; | |
2628 session_deps_.net_log = log.bound().net_log(); | |
2629 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2630 | |
2631 // fetch http://www.google.com/ via SPDY | |
2632 scoped_ptr<SpdyFrame> req( | |
2633 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, false)); | |
2634 MockWrite spdy_writes[] = { CreateMockWrite(*req) }; | |
2635 | |
2636 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
2637 scoped_ptr<SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); | |
2638 MockRead spdy_reads[] = { | |
2639 CreateMockRead(*resp), | |
2640 CreateMockRead(*data), | |
2641 MockRead(ASYNC, 0, 0), | |
2642 }; | |
2643 | |
2644 DelayedSocketData spdy_data( | |
2645 1, // wait for one write to finish before reading. | |
2646 spdy_reads, arraysize(spdy_reads), | |
2647 spdy_writes, arraysize(spdy_writes)); | |
2648 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
2649 | |
2650 SSLSocketDataProvider ssl(ASYNC, OK); | |
2651 ssl.SetNextProto(kProtoSPDY2); | |
2652 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2653 | |
2654 TestCompletionCallback callback1; | |
2655 | |
2656 scoped_ptr<HttpTransaction> trans( | |
2657 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2658 | |
2659 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
2660 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2661 | |
2662 rv = callback1.WaitForResult(); | |
2663 EXPECT_EQ(OK, rv); | |
2664 | |
2665 LoadTimingInfo load_timing_info; | |
2666 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
2667 TestLoadTimingNotReused(load_timing_info, | |
2668 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
2669 | |
2670 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
2671 ASSERT_TRUE(response != NULL); | |
2672 ASSERT_TRUE(response->headers.get() != NULL); | |
2673 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
2674 | |
2675 std::string response_data; | |
2676 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
2677 EXPECT_EQ(kUploadData, response_data); | |
2678 } | |
2679 | |
2680 // Test a SPDY get through an HTTPS Proxy. | |
2681 TEST_F(HttpNetworkTransactionSpdy2Test, HttpsProxySpdyGetWithProxyAuth) { | |
2682 HttpRequestInfo request; | |
2683 request.method = "GET"; | |
2684 request.url = GURL("http://www.google.com/"); | |
2685 request.load_flags = 0; | |
2686 | |
2687 // Configure against https proxy server "myproxy:70". | |
2688 session_deps_.proxy_service.reset( | |
2689 ProxyService::CreateFixed("https://myproxy:70")); | |
2690 CapturingBoundNetLog log; | |
2691 session_deps_.net_log = log.bound().net_log(); | |
2692 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2693 | |
2694 // The first request will be a bare GET, the second request will be a | |
2695 // GET with a Proxy-Authorization header. | |
2696 scoped_ptr<SpdyFrame> req_get( | |
2697 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, false)); | |
2698 const char* const kExtraAuthorizationHeaders[] = { | |
2699 "proxy-authorization", "Basic Zm9vOmJhcg==" | |
2700 }; | |
2701 scoped_ptr<SpdyFrame> req_get_authorization( | |
2702 spdy_util_.ConstructSpdyGet(kExtraAuthorizationHeaders, | |
2703 arraysize(kExtraAuthorizationHeaders) / 2, | |
2704 false, | |
2705 3, | |
2706 LOWEST, | |
2707 false)); | |
2708 MockWrite spdy_writes[] = { | |
2709 CreateMockWrite(*req_get, 1), | |
2710 CreateMockWrite(*req_get_authorization, 4), | |
2711 }; | |
2712 | |
2713 // The first response is a 407 proxy authentication challenge, and the second | |
2714 // response will be a 200 response since the second request includes a valid | |
2715 // Authorization header. | |
2716 const char* const kExtraAuthenticationHeaders[] = { | |
2717 "proxy-authenticate", "Basic realm=\"MyRealm1\"" | |
2718 }; | |
2719 scoped_ptr<SpdyFrame> resp_authentication( | |
2720 ConstructSpdySynReplyError( | |
2721 "407 Proxy Authentication Required", | |
2722 kExtraAuthenticationHeaders, arraysize(kExtraAuthenticationHeaders)/2, | |
2723 1)); | |
2724 scoped_ptr<SpdyFrame> body_authentication( | |
2725 ConstructSpdyBodyFrame(1, true)); | |
2726 scoped_ptr<SpdyFrame> resp_data(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
2727 scoped_ptr<SpdyFrame> body_data(ConstructSpdyBodyFrame(3, true)); | |
2728 MockRead spdy_reads[] = { | |
2729 CreateMockRead(*resp_authentication, 2), | |
2730 CreateMockRead(*body_authentication, 3), | |
2731 CreateMockRead(*resp_data, 5), | |
2732 CreateMockRead(*body_data, 6), | |
2733 MockRead(ASYNC, 0, 7), | |
2734 }; | |
2735 | |
2736 OrderedSocketData data( | |
2737 spdy_reads, arraysize(spdy_reads), | |
2738 spdy_writes, arraysize(spdy_writes)); | |
2739 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
2740 | |
2741 SSLSocketDataProvider ssl(ASYNC, OK); | |
2742 ssl.SetNextProto(kProtoSPDY2); | |
2743 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2744 | |
2745 TestCompletionCallback callback1; | |
2746 | |
2747 scoped_ptr<HttpTransaction> trans( | |
2748 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2749 | |
2750 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
2751 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2752 | |
2753 rv = callback1.WaitForResult(); | |
2754 EXPECT_EQ(OK, rv); | |
2755 | |
2756 const HttpResponseInfo* const response = trans->GetResponseInfo(); | |
2757 | |
2758 ASSERT_TRUE(response != NULL); | |
2759 ASSERT_TRUE(response->headers.get() != NULL); | |
2760 EXPECT_EQ(407, response->headers->response_code()); | |
2761 EXPECT_TRUE(response->was_fetched_via_spdy); | |
2762 EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); | |
2763 | |
2764 TestCompletionCallback callback2; | |
2765 | |
2766 rv = trans->RestartWithAuth( | |
2767 AuthCredentials(kFoo, kBar), callback2.callback()); | |
2768 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2769 | |
2770 rv = callback2.WaitForResult(); | |
2771 EXPECT_EQ(OK, rv); | |
2772 | |
2773 const HttpResponseInfo* const response_restart = trans->GetResponseInfo(); | |
2774 | |
2775 ASSERT_TRUE(response_restart != NULL); | |
2776 ASSERT_TRUE(response_restart->headers.get() != NULL); | |
2777 EXPECT_EQ(200, response_restart->headers->response_code()); | |
2778 // The password prompt info should not be set. | |
2779 EXPECT_TRUE(response_restart->auth_challenge.get() == NULL); | |
2780 } | |
2781 | |
2782 // Test a SPDY CONNECT through an HTTPS Proxy to an HTTPS (non-SPDY) Server. | |
2783 TEST_F(HttpNetworkTransactionSpdy2Test, HttpsProxySpdyConnectHttps) { | |
2784 HttpRequestInfo request; | |
2785 request.method = "GET"; | |
2786 request.url = GURL("https://www.google.com/"); | |
2787 request.load_flags = 0; | |
2788 | |
2789 // Configure against https proxy server "proxy:70". | |
2790 session_deps_.proxy_service.reset(ProxyService::CreateFixed( | |
2791 "https://proxy:70")); | |
2792 CapturingBoundNetLog log; | |
2793 session_deps_.net_log = log.bound().net_log(); | |
2794 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2795 | |
2796 scoped_ptr<HttpTransaction> trans( | |
2797 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2798 | |
2799 // CONNECT to www.google.com:443 via SPDY | |
2800 scoped_ptr<SpdyFrame> connect(spdy_util_.ConstructSpdyConnect(NULL, 0, 1)); | |
2801 // fetch https://www.google.com/ via HTTP | |
2802 | |
2803 const char get[] = "GET / HTTP/1.1\r\n" | |
2804 "Host: www.google.com\r\n" | |
2805 "Connection: keep-alive\r\n\r\n"; | |
2806 scoped_ptr<SpdyFrame> wrapped_get( | |
2807 ConstructSpdyBodyFrame(1, get, strlen(get), false)); | |
2808 scoped_ptr<SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
2809 const char resp[] = "HTTP/1.1 200 OK\r\n" | |
2810 "Content-Length: 10\r\n\r\n"; | |
2811 scoped_ptr<SpdyFrame> wrapped_get_resp( | |
2812 ConstructSpdyBodyFrame(1, resp, strlen(resp), false)); | |
2813 scoped_ptr<SpdyFrame> wrapped_body( | |
2814 ConstructSpdyBodyFrame(1, "1234567890", 10, false)); | |
2815 scoped_ptr<SpdyFrame> window_update( | |
2816 spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp->size())); | |
2817 | |
2818 MockWrite spdy_writes[] = { | |
2819 CreateMockWrite(*connect, 1), | |
2820 CreateMockWrite(*wrapped_get, 3), | |
2821 CreateMockWrite(*window_update, 5), | |
2822 }; | |
2823 | |
2824 MockRead spdy_reads[] = { | |
2825 CreateMockRead(*conn_resp, 2, ASYNC), | |
2826 CreateMockRead(*wrapped_get_resp, 4, ASYNC), | |
2827 CreateMockRead(*wrapped_body, 6, ASYNC), | |
2828 CreateMockRead(*wrapped_body, 7, ASYNC), | |
2829 MockRead(ASYNC, 0, 8), | |
2830 }; | |
2831 | |
2832 OrderedSocketData spdy_data( | |
2833 spdy_reads, arraysize(spdy_reads), | |
2834 spdy_writes, arraysize(spdy_writes)); | |
2835 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
2836 | |
2837 SSLSocketDataProvider ssl(ASYNC, OK); | |
2838 ssl.SetNextProto(kProtoSPDY2); | |
2839 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2840 SSLSocketDataProvider ssl2(ASYNC, OK); | |
2841 ssl2.was_npn_negotiated = false; | |
2842 ssl2.protocol_negotiated = kProtoUnknown; | |
2843 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); | |
2844 | |
2845 TestCompletionCallback callback1; | |
2846 | |
2847 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
2848 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2849 | |
2850 rv = callback1.WaitForResult(); | |
2851 EXPECT_EQ(OK, rv); | |
2852 | |
2853 LoadTimingInfo load_timing_info; | |
2854 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
2855 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); | |
2856 | |
2857 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
2858 ASSERT_TRUE(response != NULL); | |
2859 ASSERT_TRUE(response->headers.get() != NULL); | |
2860 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
2861 | |
2862 std::string response_data; | |
2863 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
2864 EXPECT_EQ("1234567890", response_data); | |
2865 } | |
2866 | |
2867 // Test a SPDY CONNECT through an HTTPS Proxy to a SPDY server. | |
2868 TEST_F(HttpNetworkTransactionSpdy2Test, HttpsProxySpdyConnectSpdy) { | |
2869 HttpRequestInfo request; | |
2870 request.method = "GET"; | |
2871 request.url = GURL("https://www.google.com/"); | |
2872 request.load_flags = 0; | |
2873 | |
2874 // Configure against https proxy server "proxy:70". | |
2875 session_deps_.proxy_service.reset(ProxyService::CreateFixed( | |
2876 "https://proxy:70")); | |
2877 CapturingBoundNetLog log; | |
2878 session_deps_.net_log = log.bound().net_log(); | |
2879 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2880 | |
2881 scoped_ptr<HttpTransaction> trans( | |
2882 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2883 | |
2884 // CONNECT to www.google.com:443 via SPDY | |
2885 scoped_ptr<SpdyFrame> connect(spdy_util_.ConstructSpdyConnect(NULL, 0, 1)); | |
2886 // fetch https://www.google.com/ via SPDY | |
2887 const char* const kMyUrl = "https://www.google.com/"; | |
2888 scoped_ptr<SpdyFrame> get( | |
2889 spdy_util_.ConstructSpdyGet(kMyUrl, false, 1, LOWEST)); | |
2890 scoped_ptr<SpdyFrame> wrapped_get(ConstructWrappedSpdyFrame(get, 1)); | |
2891 scoped_ptr<SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
2892 scoped_ptr<SpdyFrame> get_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
2893 scoped_ptr<SpdyFrame> wrapped_get_resp( | |
2894 ConstructWrappedSpdyFrame(get_resp, 1)); | |
2895 scoped_ptr<SpdyFrame> body(ConstructSpdyBodyFrame(1, true)); | |
2896 scoped_ptr<SpdyFrame> wrapped_body(ConstructWrappedSpdyFrame(body, 1)); | |
2897 scoped_ptr<SpdyFrame> window_update_get_resp( | |
2898 spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp->size())); | |
2899 scoped_ptr<SpdyFrame> window_update_body( | |
2900 spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_body->size())); | |
2901 | |
2902 MockWrite spdy_writes[] = { | |
2903 CreateMockWrite(*connect, 1), | |
2904 CreateMockWrite(*wrapped_get, 3), | |
2905 CreateMockWrite(*window_update_get_resp, 5), | |
2906 CreateMockWrite(*window_update_body, 7), | |
2907 }; | |
2908 | |
2909 MockRead spdy_reads[] = { | |
2910 CreateMockRead(*conn_resp, 2, ASYNC), | |
2911 CreateMockRead(*wrapped_get_resp, 4, ASYNC), | |
2912 CreateMockRead(*wrapped_body, 6, ASYNC), | |
2913 MockRead(ASYNC, 0, 8), | |
2914 }; | |
2915 | |
2916 OrderedSocketData spdy_data( | |
2917 spdy_reads, arraysize(spdy_reads), | |
2918 spdy_writes, arraysize(spdy_writes)); | |
2919 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
2920 | |
2921 SSLSocketDataProvider ssl(ASYNC, OK); | |
2922 ssl.SetNextProto(kProtoSPDY2); | |
2923 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2924 SSLSocketDataProvider ssl2(ASYNC, OK); | |
2925 ssl2.SetNextProto(kProtoSPDY2); | |
2926 ssl2.protocol_negotiated = kProtoSPDY2; | |
2927 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); | |
2928 | |
2929 TestCompletionCallback callback1; | |
2930 | |
2931 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
2932 EXPECT_EQ(ERR_IO_PENDING, rv); | |
2933 | |
2934 rv = callback1.WaitForResult(); | |
2935 EXPECT_EQ(OK, rv); | |
2936 | |
2937 LoadTimingInfo load_timing_info; | |
2938 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
2939 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); | |
2940 | |
2941 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
2942 ASSERT_TRUE(response != NULL); | |
2943 ASSERT_TRUE(response->headers.get() != NULL); | |
2944 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
2945 | |
2946 std::string response_data; | |
2947 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
2948 EXPECT_EQ(kUploadData, response_data); | |
2949 } | |
2950 | |
2951 // Test a SPDY CONNECT failure through an HTTPS Proxy. | |
2952 TEST_F(HttpNetworkTransactionSpdy2Test, HttpsProxySpdyConnectFailure) { | |
2953 HttpRequestInfo request; | |
2954 request.method = "GET"; | |
2955 request.url = GURL("https://www.google.com/"); | |
2956 request.load_flags = 0; | |
2957 | |
2958 // Configure against https proxy server "proxy:70". | |
2959 session_deps_.proxy_service.reset(ProxyService::CreateFixed( | |
2960 "https://proxy:70")); | |
2961 CapturingBoundNetLog log; | |
2962 session_deps_.net_log = log.bound().net_log(); | |
2963 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
2964 | |
2965 scoped_ptr<HttpTransaction> trans( | |
2966 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
2967 | |
2968 // CONNECT to www.google.com:443 via SPDY | |
2969 scoped_ptr<SpdyFrame> connect(spdy_util_.ConstructSpdyConnect(NULL, 0, 1)); | |
2970 scoped_ptr<SpdyFrame> get( | |
2971 spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL)); | |
2972 | |
2973 MockWrite spdy_writes[] = { | |
2974 CreateMockWrite(*connect, 1), | |
2975 CreateMockWrite(*get, 3), | |
2976 }; | |
2977 | |
2978 scoped_ptr<SpdyFrame> resp(ConstructSpdySynReplyError(1)); | |
2979 scoped_ptr<SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); | |
2980 MockRead spdy_reads[] = { | |
2981 CreateMockRead(*resp, 2, ASYNC), | |
2982 MockRead(ASYNC, 0, 4), | |
2983 }; | |
2984 | |
2985 OrderedSocketData spdy_data( | |
2986 spdy_reads, arraysize(spdy_reads), | |
2987 spdy_writes, arraysize(spdy_writes)); | |
2988 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
2989 | |
2990 SSLSocketDataProvider ssl(ASYNC, OK); | |
2991 ssl.SetNextProto(kProtoSPDY2); | |
2992 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
2993 SSLSocketDataProvider ssl2(ASYNC, OK); | |
2994 ssl2.SetNextProto(kProtoSPDY2); | |
2995 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); | |
2996 | |
2997 TestCompletionCallback callback1; | |
2998 | |
2999 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
3000 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3001 | |
3002 rv = callback1.WaitForResult(); | |
3003 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); | |
3004 | |
3005 // TODO(ttuttle): Anything else to check here? | |
3006 } | |
3007 | |
3008 // Test load timing in the case of two HTTPS (non-SPDY) requests through a SPDY | |
3009 // HTTPS Proxy to different servers. | |
3010 TEST_F(HttpNetworkTransactionSpdy2Test, | |
3011 HttpsProxySpdyConnectHttpsLoadTimingTwoRequestsTwoServers) { | |
3012 // Configure against https proxy server "proxy:70". | |
3013 session_deps_.proxy_service.reset(ProxyService::CreateFixed( | |
3014 "https://proxy:70")); | |
3015 CapturingBoundNetLog log; | |
3016 session_deps_.net_log = log.bound().net_log(); | |
3017 scoped_refptr<HttpNetworkSession> session( | |
3018 SpdySessionDependencies::SpdyCreateSessionDeterministic(&session_deps_)); | |
3019 | |
3020 HttpRequestInfo request1; | |
3021 request1.method = "GET"; | |
3022 request1.url = GURL("https://www.google.com/"); | |
3023 request1.load_flags = 0; | |
3024 | |
3025 HttpRequestInfo request2; | |
3026 request2.method = "GET"; | |
3027 request2.url = GURL("https://news.google.com/"); | |
3028 request2.load_flags = 0; | |
3029 | |
3030 // CONNECT to www.google.com:443 via SPDY. | |
3031 scoped_ptr<SpdyFrame> connect1(spdy_util_.ConstructSpdyConnect(NULL, 0, 1)); | |
3032 scoped_ptr<SpdyFrame> conn_resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
3033 | |
3034 // Fetch https://www.google.com/ via HTTP. | |
3035 const char get1[] = "GET / HTTP/1.1\r\n" | |
3036 "Host: www.google.com\r\n" | |
3037 "Connection: keep-alive\r\n\r\n"; | |
3038 scoped_ptr<SpdyFrame> wrapped_get1( | |
3039 ConstructSpdyBodyFrame(1, get1, strlen(get1), false)); | |
3040 const char resp1[] = "HTTP/1.1 200 OK\r\n" | |
3041 "Content-Length: 1\r\n\r\n"; | |
3042 scoped_ptr<SpdyFrame> wrapped_get_resp1( | |
3043 ConstructSpdyBodyFrame(1, resp1, strlen(resp1), false)); | |
3044 scoped_ptr<SpdyFrame> wrapped_body1(ConstructSpdyBodyFrame(1, "1", 1, false)); | |
3045 scoped_ptr<SpdyFrame> window_update( | |
3046 spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp1->size())); | |
3047 | |
3048 // CONNECT to news.google.com:443 via SPDY. | |
3049 const char* const kConnectHeaders2[] = { | |
3050 "method", "CONNECT", | |
3051 "url", "news.google.com:443", | |
3052 "host", "news.google.com", | |
3053 "version", "HTTP/1.1", | |
3054 }; | |
3055 scoped_ptr<SpdyFrame> connect2( | |
3056 spdy_util_.ConstructSpdyControlFrame(NULL, | |
3057 0, | |
3058 /*compressed*/ false, | |
3059 3, | |
3060 LOWEST, | |
3061 SYN_STREAM, | |
3062 CONTROL_FLAG_NONE, | |
3063 kConnectHeaders2, | |
3064 arraysize(kConnectHeaders2), | |
3065 0)); | |
3066 scoped_ptr<SpdyFrame> conn_resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
3067 | |
3068 // Fetch https://news.google.com/ via HTTP. | |
3069 const char get2[] = "GET / HTTP/1.1\r\n" | |
3070 "Host: news.google.com\r\n" | |
3071 "Connection: keep-alive\r\n\r\n"; | |
3072 scoped_ptr<SpdyFrame> wrapped_get2( | |
3073 ConstructSpdyBodyFrame(3, get2, strlen(get2), false)); | |
3074 const char resp2[] = "HTTP/1.1 200 OK\r\n" | |
3075 "Content-Length: 2\r\n\r\n"; | |
3076 scoped_ptr<SpdyFrame> wrapped_get_resp2( | |
3077 ConstructSpdyBodyFrame(3, resp2, strlen(resp2), false)); | |
3078 scoped_ptr<SpdyFrame> wrapped_body2( | |
3079 ConstructSpdyBodyFrame(3, "22", 2, false)); | |
3080 | |
3081 MockWrite spdy_writes[] = { | |
3082 CreateMockWrite(*connect1, 0), | |
3083 CreateMockWrite(*wrapped_get1, 2), | |
3084 CreateMockWrite(*connect2, 5), | |
3085 CreateMockWrite(*wrapped_get2, 7), | |
3086 }; | |
3087 | |
3088 MockRead spdy_reads[] = { | |
3089 CreateMockRead(*conn_resp1, 1, ASYNC), | |
3090 CreateMockRead(*wrapped_get_resp1, 3, ASYNC), | |
3091 CreateMockRead(*wrapped_body1, 4, ASYNC), | |
3092 CreateMockRead(*conn_resp2, 6, ASYNC), | |
3093 CreateMockRead(*wrapped_get_resp2, 8, ASYNC), | |
3094 CreateMockRead(*wrapped_body2, 9, ASYNC), | |
3095 MockRead(ASYNC, 0, 10), | |
3096 }; | |
3097 | |
3098 DeterministicSocketData spdy_data( | |
3099 spdy_reads, arraysize(spdy_reads), | |
3100 spdy_writes, arraysize(spdy_writes)); | |
3101 session_deps_.deterministic_socket_factory->AddSocketDataProvider(&spdy_data); | |
3102 | |
3103 SSLSocketDataProvider ssl(ASYNC, OK); | |
3104 ssl.SetNextProto(kProtoSPDY2); | |
3105 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl); | |
3106 SSLSocketDataProvider ssl2(ASYNC, OK); | |
3107 ssl2.was_npn_negotiated = false; | |
3108 ssl2.protocol_negotiated = kProtoUnknown; | |
3109 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl2); | |
3110 SSLSocketDataProvider ssl3(ASYNC, OK); | |
3111 ssl3.was_npn_negotiated = false; | |
3112 ssl3.protocol_negotiated = kProtoUnknown; | |
3113 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl3); | |
3114 | |
3115 TestCompletionCallback callback; | |
3116 | |
3117 scoped_ptr<HttpTransaction> trans( | |
3118 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
3119 int rv = trans->Start(&request1, callback.callback(), BoundNetLog()); | |
3120 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3121 // The first connect and request, each of their responses, and the body. | |
3122 spdy_data.RunFor(5); | |
3123 | |
3124 rv = callback.WaitForResult(); | |
3125 EXPECT_EQ(OK, rv); | |
3126 | |
3127 LoadTimingInfo load_timing_info; | |
3128 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
3129 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); | |
3130 | |
3131 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
3132 ASSERT_TRUE(response != NULL); | |
3133 ASSERT_TRUE(response->headers.get() != NULL); | |
3134 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
3135 | |
3136 std::string response_data; | |
3137 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
3138 EXPECT_EQ(1, trans->Read(buf.get(), 256, callback.callback())); | |
3139 | |
3140 scoped_ptr<HttpTransaction> trans2( | |
3141 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
3142 rv = trans2->Start(&request2, callback.callback(), BoundNetLog()); | |
3143 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3144 | |
3145 // The second connect and request, each of their responses, and the body. | |
3146 spdy_data.RunFor(5); | |
3147 rv = callback.WaitForResult(); | |
3148 EXPECT_EQ(OK, rv); | |
3149 | |
3150 LoadTimingInfo load_timing_info2; | |
3151 EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); | |
3152 // Even though the SPDY connection is reused, a new tunnelled connection has | |
3153 // to be created, so the socket's load timing looks like a fresh connection. | |
3154 TestLoadTimingNotReused(load_timing_info2, CONNECT_TIMING_HAS_SSL_TIMES); | |
3155 | |
3156 // The requests should have different IDs, since they each are using their own | |
3157 // separate stream. | |
3158 EXPECT_NE(load_timing_info.socket_log_id, load_timing_info2.socket_log_id); | |
3159 | |
3160 EXPECT_EQ(2, trans2->Read(buf.get(), 256, callback.callback())); | |
3161 } | |
3162 | |
3163 // Test load timing in the case of two HTTPS (non-SPDY) requests through a SPDY | |
3164 // HTTPS Proxy to the same server. | |
3165 TEST_F(HttpNetworkTransactionSpdy2Test, | |
3166 HttpsProxySpdyConnectHttpsLoadTimingTwoRequestsSameServer) { | |
3167 // Configure against https proxy server "proxy:70". | |
3168 session_deps_.proxy_service.reset(ProxyService::CreateFixed( | |
3169 "https://proxy:70")); | |
3170 CapturingBoundNetLog log; | |
3171 session_deps_.net_log = log.bound().net_log(); | |
3172 scoped_refptr<HttpNetworkSession> session( | |
3173 SpdySessionDependencies::SpdyCreateSessionDeterministic(&session_deps_)); | |
3174 | |
3175 HttpRequestInfo request1; | |
3176 request1.method = "GET"; | |
3177 request1.url = GURL("https://www.google.com/"); | |
3178 request1.load_flags = 0; | |
3179 | |
3180 HttpRequestInfo request2; | |
3181 request2.method = "GET"; | |
3182 request2.url = GURL("https://www.google.com/2"); | |
3183 request2.load_flags = 0; | |
3184 | |
3185 // CONNECT to www.google.com:443 via SPDY. | |
3186 scoped_ptr<SpdyFrame> connect1(spdy_util_.ConstructSpdyConnect(NULL, 0, 1)); | |
3187 scoped_ptr<SpdyFrame> conn_resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
3188 | |
3189 // Fetch https://www.google.com/ via HTTP. | |
3190 const char get1[] = "GET / HTTP/1.1\r\n" | |
3191 "Host: www.google.com\r\n" | |
3192 "Connection: keep-alive\r\n\r\n"; | |
3193 scoped_ptr<SpdyFrame> wrapped_get1( | |
3194 ConstructSpdyBodyFrame(1, get1, strlen(get1), false)); | |
3195 const char resp1[] = "HTTP/1.1 200 OK\r\n" | |
3196 "Content-Length: 1\r\n\r\n"; | |
3197 scoped_ptr<SpdyFrame> wrapped_get_resp1( | |
3198 ConstructSpdyBodyFrame(1, resp1, strlen(resp1), false)); | |
3199 scoped_ptr<SpdyFrame> wrapped_body1(ConstructSpdyBodyFrame(1, "1", 1, false)); | |
3200 scoped_ptr<SpdyFrame> window_update( | |
3201 spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp1->size())); | |
3202 | |
3203 // Fetch https://www.google.com/2 via HTTP. | |
3204 const char get2[] = "GET /2 HTTP/1.1\r\n" | |
3205 "Host: www.google.com\r\n" | |
3206 "Connection: keep-alive\r\n\r\n"; | |
3207 scoped_ptr<SpdyFrame> wrapped_get2( | |
3208 ConstructSpdyBodyFrame(1, get2, strlen(get2), false)); | |
3209 const char resp2[] = "HTTP/1.1 200 OK\r\n" | |
3210 "Content-Length: 2\r\n\r\n"; | |
3211 scoped_ptr<SpdyFrame> wrapped_get_resp2( | |
3212 ConstructSpdyBodyFrame(1, resp2, strlen(resp2), false)); | |
3213 scoped_ptr<SpdyFrame> wrapped_body2( | |
3214 ConstructSpdyBodyFrame(1, "22", 2, false)); | |
3215 | |
3216 MockWrite spdy_writes[] = { | |
3217 CreateMockWrite(*connect1, 0), | |
3218 CreateMockWrite(*wrapped_get1, 2), | |
3219 CreateMockWrite(*wrapped_get2, 5), | |
3220 }; | |
3221 | |
3222 MockRead spdy_reads[] = { | |
3223 CreateMockRead(*conn_resp1, 1, ASYNC), | |
3224 CreateMockRead(*wrapped_get_resp1, 3, ASYNC), | |
3225 CreateMockRead(*wrapped_body1, 4, ASYNC), | |
3226 CreateMockRead(*wrapped_get_resp2, 6, ASYNC), | |
3227 CreateMockRead(*wrapped_body2, 7, ASYNC), | |
3228 MockRead(ASYNC, 0, 8), | |
3229 }; | |
3230 | |
3231 DeterministicSocketData spdy_data( | |
3232 spdy_reads, arraysize(spdy_reads), | |
3233 spdy_writes, arraysize(spdy_writes)); | |
3234 session_deps_.deterministic_socket_factory->AddSocketDataProvider(&spdy_data); | |
3235 | |
3236 SSLSocketDataProvider ssl(ASYNC, OK); | |
3237 ssl.SetNextProto(kProtoSPDY2); | |
3238 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl); | |
3239 SSLSocketDataProvider ssl2(ASYNC, OK); | |
3240 ssl2.was_npn_negotiated = false; | |
3241 ssl2.protocol_negotiated = kProtoUnknown; | |
3242 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl2); | |
3243 | |
3244 TestCompletionCallback callback; | |
3245 | |
3246 scoped_ptr<HttpTransaction> trans( | |
3247 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
3248 int rv = trans->Start(&request1, callback.callback(), BoundNetLog()); | |
3249 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3250 // The first connect and request, each of their responses, and the body. | |
3251 spdy_data.RunFor(5); | |
3252 | |
3253 rv = callback.WaitForResult(); | |
3254 EXPECT_EQ(OK, rv); | |
3255 | |
3256 LoadTimingInfo load_timing_info; | |
3257 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
3258 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); | |
3259 | |
3260 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
3261 ASSERT_TRUE(response != NULL); | |
3262 ASSERT_TRUE(response->headers.get() != NULL); | |
3263 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
3264 | |
3265 std::string response_data; | |
3266 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
3267 EXPECT_EQ(1, trans->Read(buf.get(), 256, callback.callback())); | |
3268 trans.reset(); | |
3269 | |
3270 scoped_ptr<HttpTransaction> trans2( | |
3271 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
3272 rv = trans2->Start(&request2, callback.callback(), BoundNetLog()); | |
3273 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3274 | |
3275 // The second request, response, and body. There should not be a second | |
3276 // connect. | |
3277 spdy_data.RunFor(3); | |
3278 rv = callback.WaitForResult(); | |
3279 EXPECT_EQ(OK, rv); | |
3280 | |
3281 LoadTimingInfo load_timing_info2; | |
3282 EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); | |
3283 TestLoadTimingReused(load_timing_info2); | |
3284 | |
3285 // The requests should have the same ID. | |
3286 EXPECT_EQ(load_timing_info.socket_log_id, load_timing_info2.socket_log_id); | |
3287 | |
3288 EXPECT_EQ(2, trans2->Read(buf.get(), 256, callback.callback())); | |
3289 } | |
3290 | |
3291 // Test load timing in the case of of two HTTP requests through a SPDY HTTPS | |
3292 // Proxy to different servers. | |
3293 TEST_F(HttpNetworkTransactionSpdy2Test, | |
3294 HttpsProxySpdyLoadTimingTwoHttpRequests) { | |
3295 // Configure against https proxy server "proxy:70". | |
3296 session_deps_.proxy_service.reset(ProxyService::CreateFixed( | |
3297 "https://proxy:70")); | |
3298 CapturingBoundNetLog log; | |
3299 session_deps_.net_log = log.bound().net_log(); | |
3300 scoped_refptr<HttpNetworkSession> session( | |
3301 SpdySessionDependencies::SpdyCreateSessionDeterministic(&session_deps_)); | |
3302 | |
3303 HttpRequestInfo request1; | |
3304 request1.method = "GET"; | |
3305 request1.url = GURL("http://www.google.com/"); | |
3306 request1.load_flags = 0; | |
3307 | |
3308 HttpRequestInfo request2; | |
3309 request2.method = "GET"; | |
3310 request2.url = GURL("http://news.google.com/"); | |
3311 request2.load_flags = 0; | |
3312 | |
3313 // http://www.google.com/ | |
3314 const char* const headers1[] = { | |
3315 "method", "GET", | |
3316 "url", "http://www.google.com/", | |
3317 "host", "www.google.com", | |
3318 "scheme", "http", | |
3319 "version", "HTTP/1.1" | |
3320 }; | |
3321 scoped_ptr<SpdyFrame> get1(spdy_util_.ConstructSpdyControlFrame( | |
3322 NULL, 0, false, 1, LOWEST, SYN_STREAM, CONTROL_FLAG_FIN, | |
3323 headers1, arraysize(headers1), 0)); | |
3324 scoped_ptr<SpdyFrame> get_resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
3325 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, "1", 1, true)); | |
3326 | |
3327 // http://news.google.com/ | |
3328 const char* const headers2[] = { | |
3329 "method", "GET", | |
3330 "url", "http://news.google.com/", | |
3331 "host", "news.google.com", | |
3332 "scheme", "http", | |
3333 "version", "HTTP/1.1" | |
3334 }; | |
3335 scoped_ptr<SpdyFrame> get2(spdy_util_.ConstructSpdyControlFrame( | |
3336 NULL, 0, false, 3, LOWEST, SYN_STREAM, CONTROL_FLAG_FIN, | |
3337 headers2, arraysize(headers2), 0)); | |
3338 scoped_ptr<SpdyFrame> get_resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
3339 scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, "22", 2, true)); | |
3340 | |
3341 MockWrite spdy_writes[] = { | |
3342 CreateMockWrite(*get1, 0), | |
3343 CreateMockWrite(*get2, 3), | |
3344 }; | |
3345 | |
3346 MockRead spdy_reads[] = { | |
3347 CreateMockRead(*get_resp1, 1, ASYNC), | |
3348 CreateMockRead(*body1, 2, ASYNC), | |
3349 CreateMockRead(*get_resp2, 4, ASYNC), | |
3350 CreateMockRead(*body2, 5, ASYNC), | |
3351 MockRead(ASYNC, 0, 6), | |
3352 }; | |
3353 | |
3354 DeterministicSocketData spdy_data( | |
3355 spdy_reads, arraysize(spdy_reads), | |
3356 spdy_writes, arraysize(spdy_writes)); | |
3357 session_deps_.deterministic_socket_factory->AddSocketDataProvider(&spdy_data); | |
3358 | |
3359 SSLSocketDataProvider ssl(ASYNC, OK); | |
3360 ssl.SetNextProto(kProtoSPDY2); | |
3361 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl); | |
3362 | |
3363 TestCompletionCallback callback; | |
3364 | |
3365 scoped_ptr<HttpTransaction> trans( | |
3366 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
3367 int rv = trans->Start(&request1, callback.callback(), BoundNetLog()); | |
3368 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3369 spdy_data.RunFor(2); | |
3370 | |
3371 rv = callback.WaitForResult(); | |
3372 EXPECT_EQ(OK, rv); | |
3373 | |
3374 LoadTimingInfo load_timing_info; | |
3375 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
3376 TestLoadTimingNotReused(load_timing_info, | |
3377 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
3378 | |
3379 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
3380 ASSERT_TRUE(response != NULL); | |
3381 ASSERT_TRUE(response->headers.get() != NULL); | |
3382 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
3383 | |
3384 std::string response_data; | |
3385 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
3386 EXPECT_EQ(ERR_IO_PENDING, trans->Read(buf.get(), 256, callback.callback())); | |
3387 spdy_data.RunFor(1); | |
3388 EXPECT_EQ(1, callback.WaitForResult()); | |
3389 // Delete the first request, so the second one can reuse the socket. | |
3390 trans.reset(); | |
3391 | |
3392 scoped_ptr<HttpTransaction> trans2( | |
3393 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
3394 rv = trans2->Start(&request2, callback.callback(), BoundNetLog()); | |
3395 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3396 | |
3397 spdy_data.RunFor(2); | |
3398 rv = callback.WaitForResult(); | |
3399 EXPECT_EQ(OK, rv); | |
3400 | |
3401 LoadTimingInfo load_timing_info2; | |
3402 EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); | |
3403 TestLoadTimingReused(load_timing_info2); | |
3404 | |
3405 // The requests should have the same ID. | |
3406 EXPECT_EQ(load_timing_info.socket_log_id, load_timing_info2.socket_log_id); | |
3407 | |
3408 EXPECT_EQ(ERR_IO_PENDING, trans2->Read(buf.get(), 256, callback.callback())); | |
3409 spdy_data.RunFor(1); | |
3410 EXPECT_EQ(2, callback.WaitForResult()); | |
3411 } | |
3412 | |
3413 // Test the challenge-response-retry sequence through an HTTPS Proxy | |
3414 TEST_F(HttpNetworkTransactionSpdy2Test, HttpsProxyAuthRetry) { | |
3415 HttpRequestInfo request; | |
3416 request.method = "GET"; | |
3417 request.url = GURL("http://www.google.com/"); | |
3418 // when the no authentication data flag is set. | |
3419 request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; | |
3420 | |
3421 // Configure against https proxy server "myproxy:70". | |
3422 session_deps_.proxy_service.reset( | |
3423 ProxyService::CreateFixed("https://myproxy:70")); | |
3424 CapturingBoundNetLog log; | |
3425 session_deps_.net_log = log.bound().net_log(); | |
3426 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
3427 | |
3428 // Since we have proxy, should use full url | |
3429 MockWrite data_writes1[] = { | |
3430 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | |
3431 "Host: www.google.com\r\n" | |
3432 "Proxy-Connection: keep-alive\r\n\r\n"), | |
3433 | |
3434 // After calling trans->RestartWithAuth(), this is the request we should | |
3435 // be issuing -- the final header line contains the credentials. | |
3436 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | |
3437 "Host: www.google.com\r\n" | |
3438 "Proxy-Connection: keep-alive\r\n" | |
3439 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
3440 }; | |
3441 | |
3442 // The proxy responds to the GET with a 407, using a persistent | |
3443 // connection. | |
3444 MockRead data_reads1[] = { | |
3445 // No credentials. | |
3446 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), | |
3447 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
3448 MockRead("Proxy-Connection: keep-alive\r\n"), | |
3449 MockRead("Content-Length: 0\r\n\r\n"), | |
3450 | |
3451 MockRead("HTTP/1.1 200 OK\r\n"), | |
3452 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
3453 MockRead("Content-Length: 100\r\n\r\n"), | |
3454 MockRead(SYNCHRONOUS, OK), | |
3455 }; | |
3456 | |
3457 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
3458 data_writes1, arraysize(data_writes1)); | |
3459 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
3460 SSLSocketDataProvider ssl(ASYNC, OK); | |
3461 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
3462 | |
3463 TestCompletionCallback callback1; | |
3464 | |
3465 scoped_ptr<HttpTransaction> trans( | |
3466 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
3467 | |
3468 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
3469 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3470 | |
3471 rv = callback1.WaitForResult(); | |
3472 EXPECT_EQ(OK, rv); | |
3473 | |
3474 LoadTimingInfo load_timing_info; | |
3475 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
3476 TestLoadTimingNotReused(load_timing_info, | |
3477 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
3478 | |
3479 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
3480 ASSERT_TRUE(response != NULL); | |
3481 ASSERT_FALSE(response->headers.get() == NULL); | |
3482 EXPECT_EQ(407, response->headers->response_code()); | |
3483 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
3484 EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); | |
3485 | |
3486 TestCompletionCallback callback2; | |
3487 | |
3488 rv = trans->RestartWithAuth( | |
3489 AuthCredentials(kFoo, kBar), callback2.callback()); | |
3490 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3491 | |
3492 rv = callback2.WaitForResult(); | |
3493 EXPECT_EQ(OK, rv); | |
3494 | |
3495 load_timing_info = LoadTimingInfo(); | |
3496 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
3497 // Retrying with HTTP AUTH is considered to be reusing a socket. | |
3498 TestLoadTimingReused(load_timing_info); | |
3499 | |
3500 response = trans->GetResponseInfo(); | |
3501 ASSERT_TRUE(response != NULL); | |
3502 | |
3503 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
3504 EXPECT_EQ(200, response->headers->response_code()); | |
3505 EXPECT_EQ(100, response->headers->GetContentLength()); | |
3506 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
3507 | |
3508 // The password prompt info should not be set. | |
3509 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
3510 } | |
3511 | |
3512 void HttpNetworkTransactionSpdy2Test::ConnectStatusHelperWithExpectedStatus( | |
3513 const MockRead& status, int expected_status) { | |
3514 HttpRequestInfo request; | |
3515 request.method = "GET"; | |
3516 request.url = GURL("https://www.google.com/"); | |
3517 request.load_flags = 0; | |
3518 | |
3519 // Configure against proxy server "myproxy:70". | |
3520 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
3521 | |
3522 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
3523 | |
3524 // Since we have proxy, should try to establish tunnel. | |
3525 MockWrite data_writes[] = { | |
3526 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
3527 "Host: www.google.com\r\n" | |
3528 "Proxy-Connection: keep-alive\r\n\r\n"), | |
3529 }; | |
3530 | |
3531 MockRead data_reads[] = { | |
3532 status, | |
3533 MockRead("Content-Length: 10\r\n\r\n"), | |
3534 // No response body because the test stops reading here. | |
3535 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. | |
3536 }; | |
3537 | |
3538 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
3539 data_writes, arraysize(data_writes)); | |
3540 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
3541 | |
3542 TestCompletionCallback callback; | |
3543 | |
3544 scoped_ptr<HttpTransaction> trans( | |
3545 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
3546 | |
3547 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
3548 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3549 | |
3550 rv = callback.WaitForResult(); | |
3551 EXPECT_EQ(expected_status, rv); | |
3552 } | |
3553 | |
3554 void HttpNetworkTransactionSpdy2Test::ConnectStatusHelper( | |
3555 const MockRead& status) { | |
3556 ConnectStatusHelperWithExpectedStatus( | |
3557 status, ERR_TUNNEL_CONNECTION_FAILED); | |
3558 } | |
3559 | |
3560 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus100) { | |
3561 ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n")); | |
3562 } | |
3563 | |
3564 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus101) { | |
3565 ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n")); | |
3566 } | |
3567 | |
3568 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus201) { | |
3569 ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n")); | |
3570 } | |
3571 | |
3572 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus202) { | |
3573 ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n")); | |
3574 } | |
3575 | |
3576 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus203) { | |
3577 ConnectStatusHelper( | |
3578 MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n")); | |
3579 } | |
3580 | |
3581 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus204) { | |
3582 ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n")); | |
3583 } | |
3584 | |
3585 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus205) { | |
3586 ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n")); | |
3587 } | |
3588 | |
3589 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus206) { | |
3590 ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n")); | |
3591 } | |
3592 | |
3593 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus300) { | |
3594 ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n")); | |
3595 } | |
3596 | |
3597 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus301) { | |
3598 ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n")); | |
3599 } | |
3600 | |
3601 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus302) { | |
3602 ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n")); | |
3603 } | |
3604 | |
3605 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus303) { | |
3606 ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n")); | |
3607 } | |
3608 | |
3609 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus304) { | |
3610 ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n")); | |
3611 } | |
3612 | |
3613 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus305) { | |
3614 ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n")); | |
3615 } | |
3616 | |
3617 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus306) { | |
3618 ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n")); | |
3619 } | |
3620 | |
3621 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus307) { | |
3622 ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n")); | |
3623 } | |
3624 | |
3625 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus400) { | |
3626 ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n")); | |
3627 } | |
3628 | |
3629 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus401) { | |
3630 ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n")); | |
3631 } | |
3632 | |
3633 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus402) { | |
3634 ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n")); | |
3635 } | |
3636 | |
3637 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus403) { | |
3638 ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n")); | |
3639 } | |
3640 | |
3641 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus404) { | |
3642 ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n")); | |
3643 } | |
3644 | |
3645 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus405) { | |
3646 ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n")); | |
3647 } | |
3648 | |
3649 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus406) { | |
3650 ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n")); | |
3651 } | |
3652 | |
3653 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus407) { | |
3654 ConnectStatusHelperWithExpectedStatus( | |
3655 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), | |
3656 ERR_PROXY_AUTH_UNSUPPORTED); | |
3657 } | |
3658 | |
3659 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus408) { | |
3660 ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n")); | |
3661 } | |
3662 | |
3663 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus409) { | |
3664 ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n")); | |
3665 } | |
3666 | |
3667 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus410) { | |
3668 ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n")); | |
3669 } | |
3670 | |
3671 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus411) { | |
3672 ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n")); | |
3673 } | |
3674 | |
3675 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus412) { | |
3676 ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n")); | |
3677 } | |
3678 | |
3679 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus413) { | |
3680 ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n")); | |
3681 } | |
3682 | |
3683 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus414) { | |
3684 ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n")); | |
3685 } | |
3686 | |
3687 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus415) { | |
3688 ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n")); | |
3689 } | |
3690 | |
3691 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus416) { | |
3692 ConnectStatusHelper( | |
3693 MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n")); | |
3694 } | |
3695 | |
3696 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus417) { | |
3697 ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n")); | |
3698 } | |
3699 | |
3700 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus500) { | |
3701 ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n")); | |
3702 } | |
3703 | |
3704 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus501) { | |
3705 ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n")); | |
3706 } | |
3707 | |
3708 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus502) { | |
3709 ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n")); | |
3710 } | |
3711 | |
3712 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus503) { | |
3713 ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n")); | |
3714 } | |
3715 | |
3716 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus504) { | |
3717 ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n")); | |
3718 } | |
3719 | |
3720 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectStatus505) { | |
3721 ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n")); | |
3722 } | |
3723 | |
3724 // Test the flow when both the proxy server AND origin server require | |
3725 // authentication. Again, this uses basic auth for both since that is | |
3726 // the simplest to mock. | |
3727 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthProxyThenServer) { | |
3728 HttpRequestInfo request; | |
3729 request.method = "GET"; | |
3730 request.url = GURL("http://www.google.com/"); | |
3731 request.load_flags = 0; | |
3732 | |
3733 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
3734 | |
3735 // Configure against proxy server "myproxy:70". | |
3736 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
3737 CreateSession(&session_deps_))); | |
3738 | |
3739 MockWrite data_writes1[] = { | |
3740 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | |
3741 "Host: www.google.com\r\n" | |
3742 "Proxy-Connection: keep-alive\r\n\r\n"), | |
3743 }; | |
3744 | |
3745 MockRead data_reads1[] = { | |
3746 MockRead("HTTP/1.0 407 Unauthorized\r\n"), | |
3747 // Give a couple authenticate options (only the middle one is actually | |
3748 // supported). | |
3749 MockRead("Proxy-Authenticate: Basic invalid\r\n"), // Malformed. | |
3750 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
3751 MockRead("Proxy-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), | |
3752 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
3753 // Large content-length -- won't matter, as connection will be reset. | |
3754 MockRead("Content-Length: 10000\r\n\r\n"), | |
3755 MockRead(SYNCHRONOUS, ERR_FAILED), | |
3756 }; | |
3757 | |
3758 // After calling trans->RestartWithAuth() the first time, this is the | |
3759 // request we should be issuing -- the final header line contains the | |
3760 // proxy's credentials. | |
3761 MockWrite data_writes2[] = { | |
3762 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | |
3763 "Host: www.google.com\r\n" | |
3764 "Proxy-Connection: keep-alive\r\n" | |
3765 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
3766 }; | |
3767 | |
3768 // Now the proxy server lets the request pass through to origin server. | |
3769 // The origin server responds with a 401. | |
3770 MockRead data_reads2[] = { | |
3771 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
3772 // Note: We are using the same realm-name as the proxy server. This is | |
3773 // completely valid, as realms are unique across hosts. | |
3774 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
3775 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
3776 MockRead("Content-Length: 2000\r\n\r\n"), | |
3777 MockRead(SYNCHRONOUS, ERR_FAILED), // Won't be reached. | |
3778 }; | |
3779 | |
3780 // After calling trans->RestartWithAuth() the second time, we should send | |
3781 // the credentials for both the proxy and origin server. | |
3782 MockWrite data_writes3[] = { | |
3783 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | |
3784 "Host: www.google.com\r\n" | |
3785 "Proxy-Connection: keep-alive\r\n" | |
3786 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" | |
3787 "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), | |
3788 }; | |
3789 | |
3790 // Lastly we get the desired content. | |
3791 MockRead data_reads3[] = { | |
3792 MockRead("HTTP/1.0 200 OK\r\n"), | |
3793 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
3794 MockRead("Content-Length: 100\r\n\r\n"), | |
3795 MockRead(SYNCHRONOUS, OK), | |
3796 }; | |
3797 | |
3798 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
3799 data_writes1, arraysize(data_writes1)); | |
3800 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
3801 data_writes2, arraysize(data_writes2)); | |
3802 StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), | |
3803 data_writes3, arraysize(data_writes3)); | |
3804 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
3805 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
3806 session_deps_.socket_factory->AddSocketDataProvider(&data3); | |
3807 | |
3808 TestCompletionCallback callback1; | |
3809 | |
3810 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
3811 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3812 | |
3813 rv = callback1.WaitForResult(); | |
3814 EXPECT_EQ(OK, rv); | |
3815 | |
3816 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
3817 ASSERT_TRUE(response != NULL); | |
3818 EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); | |
3819 | |
3820 TestCompletionCallback callback2; | |
3821 | |
3822 rv = trans->RestartWithAuth( | |
3823 AuthCredentials(kFoo, kBar), callback2.callback()); | |
3824 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3825 | |
3826 rv = callback2.WaitForResult(); | |
3827 EXPECT_EQ(OK, rv); | |
3828 | |
3829 response = trans->GetResponseInfo(); | |
3830 ASSERT_TRUE(response != NULL); | |
3831 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
3832 | |
3833 TestCompletionCallback callback3; | |
3834 | |
3835 rv = trans->RestartWithAuth( | |
3836 AuthCredentials(kFoo2, kBar2), callback3.callback()); | |
3837 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3838 | |
3839 rv = callback3.WaitForResult(); | |
3840 EXPECT_EQ(OK, rv); | |
3841 | |
3842 response = trans->GetResponseInfo(); | |
3843 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
3844 EXPECT_EQ(100, response->headers->GetContentLength()); | |
3845 } | |
3846 | |
3847 // For the NTLM implementation using SSPI, we skip the NTLM tests since we | |
3848 // can't hook into its internals to cause it to generate predictable NTLM | |
3849 // authorization headers. | |
3850 #if defined(NTLM_PORTABLE) | |
3851 // The NTLM authentication unit tests were generated by capturing the HTTP | |
3852 // requests and responses using Fiddler 2 and inspecting the generated random | |
3853 // bytes in the debugger. | |
3854 | |
3855 // Enter the correct password and authenticate successfully. | |
3856 TEST_F(HttpNetworkTransactionSpdy2Test, NTLMAuth1) { | |
3857 HttpRequestInfo request; | |
3858 request.method = "GET"; | |
3859 request.url = GURL("http://172.22.68.17/kids/login.aspx"); | |
3860 request.load_flags = 0; | |
3861 | |
3862 HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1, | |
3863 MockGetHostName); | |
3864 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
3865 | |
3866 MockWrite data_writes1[] = { | |
3867 MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" | |
3868 "Host: 172.22.68.17\r\n" | |
3869 "Connection: keep-alive\r\n\r\n"), | |
3870 }; | |
3871 | |
3872 MockRead data_reads1[] = { | |
3873 MockRead("HTTP/1.1 401 Access Denied\r\n"), | |
3874 // Negotiate and NTLM are often requested together. However, we only want | |
3875 // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip | |
3876 // the header that requests Negotiate for this test. | |
3877 MockRead("WWW-Authenticate: NTLM\r\n"), | |
3878 MockRead("Connection: close\r\n"), | |
3879 MockRead("Content-Length: 42\r\n"), | |
3880 MockRead("Content-Type: text/html\r\n\r\n"), | |
3881 // Missing content -- won't matter, as connection will be reset. | |
3882 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), | |
3883 }; | |
3884 | |
3885 MockWrite data_writes2[] = { | |
3886 // After restarting with a null identity, this is the | |
3887 // request we should be issuing -- the final header line contains a Type | |
3888 // 1 message. | |
3889 MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" | |
3890 "Host: 172.22.68.17\r\n" | |
3891 "Connection: keep-alive\r\n" | |
3892 "Authorization: NTLM " | |
3893 "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), | |
3894 | |
3895 // After calling trans->RestartWithAuth(), we should send a Type 3 message | |
3896 // (the credentials for the origin server). The second request continues | |
3897 // on the same connection. | |
3898 MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" | |
3899 "Host: 172.22.68.17\r\n" | |
3900 "Connection: keep-alive\r\n" | |
3901 "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" | |
3902 "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" | |
3903 "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBVKW" | |
3904 "Yma5xzVAAAAAAAAAAAAAAAAAAAAACH+gWcm+YsP9Tqb9zCR3WAeZZX" | |
3905 "ahlhx5I=\r\n\r\n"), | |
3906 }; | |
3907 | |
3908 MockRead data_reads2[] = { | |
3909 // The origin server responds with a Type 2 message. | |
3910 MockRead("HTTP/1.1 401 Access Denied\r\n"), | |
3911 MockRead("WWW-Authenticate: NTLM " | |
3912 "TlRMTVNTUAACAAAADAAMADgAAAAFgokCjGpMpPGlYKkAAAAAAAAAALo" | |
3913 "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" | |
3914 "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" | |
3915 "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" | |
3916 "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" | |
3917 "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" | |
3918 "BtAAAAAAA=\r\n"), | |
3919 MockRead("Content-Length: 42\r\n"), | |
3920 MockRead("Content-Type: text/html\r\n\r\n"), | |
3921 MockRead("You are not authorized to view this page\r\n"), | |
3922 | |
3923 // Lastly we get the desired content. | |
3924 MockRead("HTTP/1.1 200 OK\r\n"), | |
3925 MockRead("Content-Type: text/html; charset=utf-8\r\n"), | |
3926 MockRead("Content-Length: 13\r\n\r\n"), | |
3927 MockRead("Please Login\r\n"), | |
3928 MockRead(SYNCHRONOUS, OK), | |
3929 }; | |
3930 | |
3931 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
3932 data_writes1, arraysize(data_writes1)); | |
3933 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
3934 data_writes2, arraysize(data_writes2)); | |
3935 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
3936 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
3937 | |
3938 TestCompletionCallback callback1; | |
3939 | |
3940 scoped_ptr<HttpTransaction> trans( | |
3941 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
3942 | |
3943 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
3944 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3945 | |
3946 rv = callback1.WaitForResult(); | |
3947 EXPECT_EQ(OK, rv); | |
3948 | |
3949 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); | |
3950 | |
3951 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
3952 ASSERT_FALSE(response == NULL); | |
3953 EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge.get())); | |
3954 | |
3955 TestCompletionCallback callback2; | |
3956 | |
3957 rv = trans->RestartWithAuth(AuthCredentials(kTestingNTLM, kTestingNTLM), | |
3958 callback2.callback()); | |
3959 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3960 | |
3961 rv = callback2.WaitForResult(); | |
3962 EXPECT_EQ(OK, rv); | |
3963 | |
3964 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); | |
3965 | |
3966 response = trans->GetResponseInfo(); | |
3967 ASSERT_TRUE(response != NULL); | |
3968 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
3969 | |
3970 TestCompletionCallback callback3; | |
3971 | |
3972 rv = trans->RestartWithAuth(AuthCredentials(), callback3.callback()); | |
3973 EXPECT_EQ(ERR_IO_PENDING, rv); | |
3974 | |
3975 rv = callback3.WaitForResult(); | |
3976 EXPECT_EQ(OK, rv); | |
3977 | |
3978 response = trans->GetResponseInfo(); | |
3979 ASSERT_TRUE(response != NULL); | |
3980 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
3981 EXPECT_EQ(13, response->headers->GetContentLength()); | |
3982 } | |
3983 | |
3984 // Enter a wrong password, and then the correct one. | |
3985 TEST_F(HttpNetworkTransactionSpdy2Test, NTLMAuth2) { | |
3986 HttpRequestInfo request; | |
3987 request.method = "GET"; | |
3988 request.url = GURL("http://172.22.68.17/kids/login.aspx"); | |
3989 request.load_flags = 0; | |
3990 | |
3991 HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom2, | |
3992 MockGetHostName); | |
3993 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
3994 | |
3995 MockWrite data_writes1[] = { | |
3996 MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" | |
3997 "Host: 172.22.68.17\r\n" | |
3998 "Connection: keep-alive\r\n\r\n"), | |
3999 }; | |
4000 | |
4001 MockRead data_reads1[] = { | |
4002 MockRead("HTTP/1.1 401 Access Denied\r\n"), | |
4003 // Negotiate and NTLM are often requested together. However, we only want | |
4004 // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip | |
4005 // the header that requests Negotiate for this test. | |
4006 MockRead("WWW-Authenticate: NTLM\r\n"), | |
4007 MockRead("Connection: close\r\n"), | |
4008 MockRead("Content-Length: 42\r\n"), | |
4009 MockRead("Content-Type: text/html\r\n\r\n"), | |
4010 // Missing content -- won't matter, as connection will be reset. | |
4011 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), | |
4012 }; | |
4013 | |
4014 MockWrite data_writes2[] = { | |
4015 // After restarting with a null identity, this is the | |
4016 // request we should be issuing -- the final header line contains a Type | |
4017 // 1 message. | |
4018 MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" | |
4019 "Host: 172.22.68.17\r\n" | |
4020 "Connection: keep-alive\r\n" | |
4021 "Authorization: NTLM " | |
4022 "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), | |
4023 | |
4024 // After calling trans->RestartWithAuth(), we should send a Type 3 message | |
4025 // (the credentials for the origin server). The second request continues | |
4026 // on the same connection. | |
4027 MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" | |
4028 "Host: 172.22.68.17\r\n" | |
4029 "Connection: keep-alive\r\n" | |
4030 "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" | |
4031 "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" | |
4032 "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwCWeY" | |
4033 "XnSZNwoQAAAAAAAAAAAAAAAAAAAADLa34/phTTKzNTWdub+uyFleOj" | |
4034 "4Ww7b7E=\r\n\r\n"), | |
4035 }; | |
4036 | |
4037 MockRead data_reads2[] = { | |
4038 // The origin server responds with a Type 2 message. | |
4039 MockRead("HTTP/1.1 401 Access Denied\r\n"), | |
4040 MockRead("WWW-Authenticate: NTLM " | |
4041 "TlRMTVNTUAACAAAADAAMADgAAAAFgokCbVWUZezVGpAAAAAAAAAAALo" | |
4042 "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" | |
4043 "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" | |
4044 "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" | |
4045 "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" | |
4046 "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" | |
4047 "BtAAAAAAA=\r\n"), | |
4048 MockRead("Content-Length: 42\r\n"), | |
4049 MockRead("Content-Type: text/html\r\n\r\n"), | |
4050 MockRead("You are not authorized to view this page\r\n"), | |
4051 | |
4052 // Wrong password. | |
4053 MockRead("HTTP/1.1 401 Access Denied\r\n"), | |
4054 MockRead("WWW-Authenticate: NTLM\r\n"), | |
4055 MockRead("Connection: close\r\n"), | |
4056 MockRead("Content-Length: 42\r\n"), | |
4057 MockRead("Content-Type: text/html\r\n\r\n"), | |
4058 // Missing content -- won't matter, as connection will be reset. | |
4059 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), | |
4060 }; | |
4061 | |
4062 MockWrite data_writes3[] = { | |
4063 // After restarting with a null identity, this is the | |
4064 // request we should be issuing -- the final header line contains a Type | |
4065 // 1 message. | |
4066 MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" | |
4067 "Host: 172.22.68.17\r\n" | |
4068 "Connection: keep-alive\r\n" | |
4069 "Authorization: NTLM " | |
4070 "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), | |
4071 | |
4072 // After calling trans->RestartWithAuth(), we should send a Type 3 message | |
4073 // (the credentials for the origin server). The second request continues | |
4074 // on the same connection. | |
4075 MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" | |
4076 "Host: 172.22.68.17\r\n" | |
4077 "Connection: keep-alive\r\n" | |
4078 "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" | |
4079 "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" | |
4080 "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBO54" | |
4081 "dFMVvTHwAAAAAAAAAAAAAAAAAAAACS7sT6Uzw7L0L//WUqlIaVWpbI" | |
4082 "+4MUm7c=\r\n\r\n"), | |
4083 }; | |
4084 | |
4085 MockRead data_reads3[] = { | |
4086 // The origin server responds with a Type 2 message. | |
4087 MockRead("HTTP/1.1 401 Access Denied\r\n"), | |
4088 MockRead("WWW-Authenticate: NTLM " | |
4089 "TlRMTVNTUAACAAAADAAMADgAAAAFgokCL24VN8dgOR8AAAAAAAAAALo" | |
4090 "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" | |
4091 "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" | |
4092 "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" | |
4093 "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" | |
4094 "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" | |
4095 "BtAAAAAAA=\r\n"), | |
4096 MockRead("Content-Length: 42\r\n"), | |
4097 MockRead("Content-Type: text/html\r\n\r\n"), | |
4098 MockRead("You are not authorized to view this page\r\n"), | |
4099 | |
4100 // Lastly we get the desired content. | |
4101 MockRead("HTTP/1.1 200 OK\r\n"), | |
4102 MockRead("Content-Type: text/html; charset=utf-8\r\n"), | |
4103 MockRead("Content-Length: 13\r\n\r\n"), | |
4104 MockRead("Please Login\r\n"), | |
4105 MockRead(SYNCHRONOUS, OK), | |
4106 }; | |
4107 | |
4108 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
4109 data_writes1, arraysize(data_writes1)); | |
4110 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
4111 data_writes2, arraysize(data_writes2)); | |
4112 StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), | |
4113 data_writes3, arraysize(data_writes3)); | |
4114 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
4115 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
4116 session_deps_.socket_factory->AddSocketDataProvider(&data3); | |
4117 | |
4118 TestCompletionCallback callback1; | |
4119 | |
4120 scoped_ptr<HttpTransaction> trans( | |
4121 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4122 | |
4123 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
4124 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4125 | |
4126 rv = callback1.WaitForResult(); | |
4127 EXPECT_EQ(OK, rv); | |
4128 | |
4129 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); | |
4130 | |
4131 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4132 ASSERT_TRUE(response != NULL); | |
4133 EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge.get())); | |
4134 | |
4135 TestCompletionCallback callback2; | |
4136 | |
4137 // Enter the wrong password. | |
4138 rv = trans->RestartWithAuth(AuthCredentials(kTestingNTLM, kWrongPassword), | |
4139 callback2.callback()); | |
4140 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4141 | |
4142 rv = callback2.WaitForResult(); | |
4143 EXPECT_EQ(OK, rv); | |
4144 | |
4145 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); | |
4146 TestCompletionCallback callback3; | |
4147 rv = trans->RestartWithAuth(AuthCredentials(), callback3.callback()); | |
4148 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4149 rv = callback3.WaitForResult(); | |
4150 EXPECT_EQ(OK, rv); | |
4151 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); | |
4152 | |
4153 response = trans->GetResponseInfo(); | |
4154 ASSERT_FALSE(response == NULL); | |
4155 EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge.get())); | |
4156 | |
4157 TestCompletionCallback callback4; | |
4158 | |
4159 // Now enter the right password. | |
4160 rv = trans->RestartWithAuth(AuthCredentials(kTestingNTLM, kTestingNTLM), | |
4161 callback4.callback()); | |
4162 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4163 | |
4164 rv = callback4.WaitForResult(); | |
4165 EXPECT_EQ(OK, rv); | |
4166 | |
4167 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); | |
4168 | |
4169 TestCompletionCallback callback5; | |
4170 | |
4171 // One more roundtrip | |
4172 rv = trans->RestartWithAuth(AuthCredentials(), callback5.callback()); | |
4173 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4174 | |
4175 rv = callback5.WaitForResult(); | |
4176 EXPECT_EQ(OK, rv); | |
4177 | |
4178 response = trans->GetResponseInfo(); | |
4179 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
4180 EXPECT_EQ(13, response->headers->GetContentLength()); | |
4181 } | |
4182 #endif // NTLM_PORTABLE | |
4183 | |
4184 // Test reading a server response which has only headers, and no body. | |
4185 // After some maximum number of bytes is consumed, the transaction should | |
4186 // fail with ERR_RESPONSE_HEADERS_TOO_BIG. | |
4187 TEST_F(HttpNetworkTransactionSpdy2Test, LargeHeadersNoBody) { | |
4188 HttpRequestInfo request; | |
4189 request.method = "GET"; | |
4190 request.url = GURL("http://www.google.com/"); | |
4191 request.load_flags = 0; | |
4192 | |
4193 scoped_ptr<HttpTransaction> trans( | |
4194 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
4195 CreateSession(&session_deps_))); | |
4196 | |
4197 // Respond with 300 kb of headers (we should fail after 256 kb). | |
4198 std::string large_headers_string; | |
4199 FillLargeHeadersString(&large_headers_string, 300 * 1024); | |
4200 | |
4201 MockRead data_reads[] = { | |
4202 MockRead("HTTP/1.0 200 OK\r\n"), | |
4203 MockRead(ASYNC, large_headers_string.data(), large_headers_string.size()), | |
4204 MockRead("\r\nBODY"), | |
4205 MockRead(SYNCHRONOUS, OK), | |
4206 }; | |
4207 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
4208 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
4209 | |
4210 TestCompletionCallback callback; | |
4211 | |
4212 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
4213 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4214 | |
4215 rv = callback.WaitForResult(); | |
4216 EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, rv); | |
4217 | |
4218 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4219 EXPECT_TRUE(response == NULL); | |
4220 } | |
4221 | |
4222 // Make sure that we don't try to reuse a TCPClientSocket when failing to | |
4223 // establish tunnel. | |
4224 // http://code.google.com/p/chromium/issues/detail?id=3772 | |
4225 TEST_F(HttpNetworkTransactionSpdy2Test, | |
4226 DontRecycleTransportSocketForSSLTunnel) { | |
4227 HttpRequestInfo request; | |
4228 request.method = "GET"; | |
4229 request.url = GURL("https://www.google.com/"); | |
4230 request.load_flags = 0; | |
4231 | |
4232 // Configure against proxy server "myproxy:70". | |
4233 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
4234 | |
4235 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
4236 | |
4237 scoped_ptr<HttpTransaction> trans( | |
4238 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4239 | |
4240 // Since we have proxy, should try to establish tunnel. | |
4241 MockWrite data_writes1[] = { | |
4242 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
4243 "Host: www.google.com\r\n" | |
4244 "Proxy-Connection: keep-alive\r\n\r\n"), | |
4245 }; | |
4246 | |
4247 // The proxy responds to the connect with a 404, using a persistent | |
4248 // connection. Usually a proxy would return 501 (not implemented), | |
4249 // or 200 (tunnel established). | |
4250 MockRead data_reads1[] = { | |
4251 MockRead("HTTP/1.1 404 Not Found\r\n"), | |
4252 MockRead("Content-Length: 10\r\n\r\n"), | |
4253 MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. | |
4254 }; | |
4255 | |
4256 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
4257 data_writes1, arraysize(data_writes1)); | |
4258 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
4259 | |
4260 TestCompletionCallback callback1; | |
4261 | |
4262 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
4263 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4264 | |
4265 rv = callback1.WaitForResult(); | |
4266 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); | |
4267 | |
4268 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4269 EXPECT_TRUE(response == NULL); | |
4270 | |
4271 // Empty the current queue. This is necessary because idle sockets are | |
4272 // added to the connection pool asynchronously with a PostTask. | |
4273 base::MessageLoop::current()->RunUntilIdle(); | |
4274 | |
4275 // We now check to make sure the TCPClientSocket was not added back to | |
4276 // the pool. | |
4277 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); | |
4278 trans.reset(); | |
4279 base::MessageLoop::current()->RunUntilIdle(); | |
4280 // Make sure that the socket didn't get recycled after calling the destructor. | |
4281 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); | |
4282 } | |
4283 | |
4284 // Make sure that we recycle a socket after reading all of the response body. | |
4285 TEST_F(HttpNetworkTransactionSpdy2Test, RecycleSocket) { | |
4286 HttpRequestInfo request; | |
4287 request.method = "GET"; | |
4288 request.url = GURL("http://www.google.com/"); | |
4289 request.load_flags = 0; | |
4290 | |
4291 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
4292 | |
4293 scoped_ptr<HttpTransaction> trans( | |
4294 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4295 | |
4296 MockRead data_reads[] = { | |
4297 // A part of the response body is received with the response headers. | |
4298 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), | |
4299 // The rest of the response body is received in two parts. | |
4300 MockRead("lo"), | |
4301 MockRead(" world"), | |
4302 MockRead("junk"), // Should not be read!! | |
4303 MockRead(SYNCHRONOUS, OK), | |
4304 }; | |
4305 | |
4306 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
4307 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
4308 | |
4309 TestCompletionCallback callback; | |
4310 | |
4311 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
4312 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4313 | |
4314 rv = callback.WaitForResult(); | |
4315 EXPECT_EQ(OK, rv); | |
4316 | |
4317 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4318 ASSERT_TRUE(response != NULL); | |
4319 | |
4320 EXPECT_TRUE(response->headers.get() != NULL); | |
4321 std::string status_line = response->headers->GetStatusLine(); | |
4322 EXPECT_EQ("HTTP/1.1 200 OK", status_line); | |
4323 | |
4324 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); | |
4325 | |
4326 std::string response_data; | |
4327 rv = ReadTransaction(trans.get(), &response_data); | |
4328 EXPECT_EQ(OK, rv); | |
4329 EXPECT_EQ("hello world", response_data); | |
4330 | |
4331 // Empty the current queue. This is necessary because idle sockets are | |
4332 // added to the connection pool asynchronously with a PostTask. | |
4333 base::MessageLoop::current()->RunUntilIdle(); | |
4334 | |
4335 // We now check to make sure the socket was added back to the pool. | |
4336 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); | |
4337 } | |
4338 | |
4339 // Make sure that we recycle a SSL socket after reading all of the response | |
4340 // body. | |
4341 TEST_F(HttpNetworkTransactionSpdy2Test, RecycleSSLSocket) { | |
4342 HttpRequestInfo request; | |
4343 request.method = "GET"; | |
4344 request.url = GURL("https://www.google.com/"); | |
4345 request.load_flags = 0; | |
4346 | |
4347 MockWrite data_writes[] = { | |
4348 MockWrite("GET / HTTP/1.1\r\n" | |
4349 "Host: www.google.com\r\n" | |
4350 "Connection: keep-alive\r\n\r\n"), | |
4351 }; | |
4352 | |
4353 MockRead data_reads[] = { | |
4354 MockRead("HTTP/1.1 200 OK\r\n"), | |
4355 MockRead("Content-Length: 11\r\n\r\n"), | |
4356 MockRead("hello world"), | |
4357 MockRead(SYNCHRONOUS, OK), | |
4358 }; | |
4359 | |
4360 SSLSocketDataProvider ssl(ASYNC, OK); | |
4361 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
4362 | |
4363 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
4364 data_writes, arraysize(data_writes)); | |
4365 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
4366 | |
4367 TestCompletionCallback callback; | |
4368 | |
4369 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
4370 scoped_ptr<HttpTransaction> trans( | |
4371 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4372 | |
4373 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
4374 | |
4375 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4376 EXPECT_EQ(OK, callback.WaitForResult()); | |
4377 | |
4378 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4379 ASSERT_TRUE(response != NULL); | |
4380 ASSERT_TRUE(response->headers.get() != NULL); | |
4381 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
4382 | |
4383 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); | |
4384 | |
4385 std::string response_data; | |
4386 rv = ReadTransaction(trans.get(), &response_data); | |
4387 EXPECT_EQ(OK, rv); | |
4388 EXPECT_EQ("hello world", response_data); | |
4389 | |
4390 // Empty the current queue. This is necessary because idle sockets are | |
4391 // added to the connection pool asynchronously with a PostTask. | |
4392 base::MessageLoop::current()->RunUntilIdle(); | |
4393 | |
4394 // We now check to make sure the socket was added back to the pool. | |
4395 EXPECT_EQ(1, GetIdleSocketCountInSSLSocketPool(session.get())); | |
4396 } | |
4397 | |
4398 // Grab a SSL socket, use it, and put it back into the pool. Then, reuse it | |
4399 // from the pool and make sure that we recover okay. | |
4400 TEST_F(HttpNetworkTransactionSpdy2Test, RecycleDeadSSLSocket) { | |
4401 HttpRequestInfo request; | |
4402 request.method = "GET"; | |
4403 request.url = GURL("https://www.google.com/"); | |
4404 request.load_flags = 0; | |
4405 | |
4406 MockWrite data_writes[] = { | |
4407 MockWrite("GET / HTTP/1.1\r\n" | |
4408 "Host: www.google.com\r\n" | |
4409 "Connection: keep-alive\r\n\r\n"), | |
4410 MockWrite("GET / HTTP/1.1\r\n" | |
4411 "Host: www.google.com\r\n" | |
4412 "Connection: keep-alive\r\n\r\n"), | |
4413 }; | |
4414 | |
4415 MockRead data_reads[] = { | |
4416 MockRead("HTTP/1.1 200 OK\r\n"), | |
4417 MockRead("Content-Length: 11\r\n\r\n"), | |
4418 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), | |
4419 MockRead("hello world"), | |
4420 MockRead(ASYNC, 0, 0) // EOF | |
4421 }; | |
4422 | |
4423 SSLSocketDataProvider ssl(ASYNC, OK); | |
4424 SSLSocketDataProvider ssl2(ASYNC, OK); | |
4425 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
4426 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); | |
4427 | |
4428 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
4429 data_writes, arraysize(data_writes)); | |
4430 StaticSocketDataProvider data2(data_reads, arraysize(data_reads), | |
4431 data_writes, arraysize(data_writes)); | |
4432 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
4433 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
4434 | |
4435 TestCompletionCallback callback; | |
4436 | |
4437 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
4438 scoped_ptr<HttpTransaction> trans( | |
4439 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4440 | |
4441 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
4442 | |
4443 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4444 EXPECT_EQ(OK, callback.WaitForResult()); | |
4445 | |
4446 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4447 ASSERT_TRUE(response != NULL); | |
4448 ASSERT_TRUE(response->headers.get() != NULL); | |
4449 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
4450 | |
4451 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); | |
4452 | |
4453 std::string response_data; | |
4454 rv = ReadTransaction(trans.get(), &response_data); | |
4455 EXPECT_EQ(OK, rv); | |
4456 EXPECT_EQ("hello world", response_data); | |
4457 | |
4458 // Empty the current queue. This is necessary because idle sockets are | |
4459 // added to the connection pool asynchronously with a PostTask. | |
4460 base::MessageLoop::current()->RunUntilIdle(); | |
4461 | |
4462 // We now check to make sure the socket was added back to the pool. | |
4463 EXPECT_EQ(1, GetIdleSocketCountInSSLSocketPool(session.get())); | |
4464 | |
4465 // Now start the second transaction, which should reuse the previous socket. | |
4466 | |
4467 trans.reset(new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4468 | |
4469 rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
4470 | |
4471 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4472 EXPECT_EQ(OK, callback.WaitForResult()); | |
4473 | |
4474 response = trans->GetResponseInfo(); | |
4475 ASSERT_TRUE(response != NULL); | |
4476 ASSERT_TRUE(response->headers.get() != NULL); | |
4477 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
4478 | |
4479 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); | |
4480 | |
4481 rv = ReadTransaction(trans.get(), &response_data); | |
4482 EXPECT_EQ(OK, rv); | |
4483 EXPECT_EQ("hello world", response_data); | |
4484 | |
4485 // Empty the current queue. This is necessary because idle sockets are | |
4486 // added to the connection pool asynchronously with a PostTask. | |
4487 base::MessageLoop::current()->RunUntilIdle(); | |
4488 | |
4489 // We now check to make sure the socket was added back to the pool. | |
4490 EXPECT_EQ(1, GetIdleSocketCountInSSLSocketPool(session.get())); | |
4491 } | |
4492 | |
4493 // Make sure that we recycle a socket after a zero-length response. | |
4494 // http://crbug.com/9880 | |
4495 TEST_F(HttpNetworkTransactionSpdy2Test, RecycleSocketAfterZeroContentLength) { | |
4496 HttpRequestInfo request; | |
4497 request.method = "GET"; | |
4498 request.url = GURL("http://www.google.com/csi?v=3&s=web&action=&" | |
4499 "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" | |
4500 "e=17259,18167,19592,19773,19981,20133,20173,20233&" | |
4501 "rt=prt.2642,ol.2649,xjs.2951"); | |
4502 request.load_flags = 0; | |
4503 | |
4504 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
4505 | |
4506 scoped_ptr<HttpTransaction> trans( | |
4507 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4508 | |
4509 MockRead data_reads[] = { | |
4510 MockRead("HTTP/1.1 204 No Content\r\n" | |
4511 "Content-Length: 0\r\n" | |
4512 "Content-Type: text/html\r\n\r\n"), | |
4513 MockRead("junk"), // Should not be read!! | |
4514 MockRead(SYNCHRONOUS, OK), | |
4515 }; | |
4516 | |
4517 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
4518 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
4519 | |
4520 TestCompletionCallback callback; | |
4521 | |
4522 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
4523 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4524 | |
4525 rv = callback.WaitForResult(); | |
4526 EXPECT_EQ(OK, rv); | |
4527 | |
4528 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4529 ASSERT_TRUE(response != NULL); | |
4530 | |
4531 EXPECT_TRUE(response->headers.get() != NULL); | |
4532 std::string status_line = response->headers->GetStatusLine(); | |
4533 EXPECT_EQ("HTTP/1.1 204 No Content", status_line); | |
4534 | |
4535 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); | |
4536 | |
4537 std::string response_data; | |
4538 rv = ReadTransaction(trans.get(), &response_data); | |
4539 EXPECT_EQ(OK, rv); | |
4540 EXPECT_EQ("", response_data); | |
4541 | |
4542 // Empty the current queue. This is necessary because idle sockets are | |
4543 // added to the connection pool asynchronously with a PostTask. | |
4544 base::MessageLoop::current()->RunUntilIdle(); | |
4545 | |
4546 // We now check to make sure the socket was added back to the pool. | |
4547 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); | |
4548 } | |
4549 | |
4550 TEST_F(HttpNetworkTransactionSpdy2Test, ResendRequestOnWriteBodyError) { | |
4551 ScopedVector<UploadElementReader> element_readers; | |
4552 element_readers.push_back(new UploadBytesElementReader("foo", 3)); | |
4553 UploadDataStream upload_data_stream(&element_readers, 0); | |
4554 | |
4555 HttpRequestInfo request[2]; | |
4556 // Transaction 1: a GET request that succeeds. The socket is recycled | |
4557 // after use. | |
4558 request[0].method = "GET"; | |
4559 request[0].url = GURL("http://www.google.com/"); | |
4560 request[0].load_flags = 0; | |
4561 // Transaction 2: a POST request. Reuses the socket kept alive from | |
4562 // transaction 1. The first attempts fails when writing the POST data. | |
4563 // This causes the transaction to retry with a new socket. The second | |
4564 // attempt succeeds. | |
4565 request[1].method = "POST"; | |
4566 request[1].url = GURL("http://www.google.com/login.cgi"); | |
4567 request[1].upload_data_stream = &upload_data_stream; | |
4568 request[1].load_flags = 0; | |
4569 | |
4570 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
4571 | |
4572 // The first socket is used for transaction 1 and the first attempt of | |
4573 // transaction 2. | |
4574 | |
4575 // The response of transaction 1. | |
4576 MockRead data_reads1[] = { | |
4577 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"), | |
4578 MockRead("hello world"), | |
4579 MockRead(SYNCHRONOUS, OK), | |
4580 }; | |
4581 // The mock write results of transaction 1 and the first attempt of | |
4582 // transaction 2. | |
4583 MockWrite data_writes1[] = { | |
4584 MockWrite(SYNCHRONOUS, 64), // GET | |
4585 MockWrite(SYNCHRONOUS, 93), // POST | |
4586 MockWrite(SYNCHRONOUS, ERR_CONNECTION_ABORTED), // POST data | |
4587 }; | |
4588 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
4589 data_writes1, arraysize(data_writes1)); | |
4590 | |
4591 // The second socket is used for the second attempt of transaction 2. | |
4592 | |
4593 // The response of transaction 2. | |
4594 MockRead data_reads2[] = { | |
4595 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\n"), | |
4596 MockRead("welcome"), | |
4597 MockRead(SYNCHRONOUS, OK), | |
4598 }; | |
4599 // The mock write results of the second attempt of transaction 2. | |
4600 MockWrite data_writes2[] = { | |
4601 MockWrite(SYNCHRONOUS, 93), // POST | |
4602 MockWrite(SYNCHRONOUS, 3), // POST data | |
4603 }; | |
4604 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
4605 data_writes2, arraysize(data_writes2)); | |
4606 | |
4607 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
4608 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
4609 | |
4610 const char* kExpectedResponseData[] = { | |
4611 "hello world", "welcome" | |
4612 }; | |
4613 | |
4614 for (int i = 0; i < 2; ++i) { | |
4615 scoped_ptr<HttpTransaction> trans( | |
4616 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4617 | |
4618 TestCompletionCallback callback; | |
4619 | |
4620 int rv = trans->Start(&request[i], callback.callback(), BoundNetLog()); | |
4621 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4622 | |
4623 rv = callback.WaitForResult(); | |
4624 EXPECT_EQ(OK, rv); | |
4625 | |
4626 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4627 ASSERT_TRUE(response != NULL); | |
4628 | |
4629 EXPECT_TRUE(response->headers.get() != NULL); | |
4630 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
4631 | |
4632 std::string response_data; | |
4633 rv = ReadTransaction(trans.get(), &response_data); | |
4634 EXPECT_EQ(OK, rv); | |
4635 EXPECT_EQ(kExpectedResponseData[i], response_data); | |
4636 } | |
4637 } | |
4638 | |
4639 // Test the request-challenge-retry sequence for basic auth when there is | |
4640 // an identity in the URL. The request should be sent as normal, but when | |
4641 // it fails the identity from the URL is used to answer the challenge. | |
4642 TEST_F(HttpNetworkTransactionSpdy2Test, AuthIdentityInURL) { | |
4643 HttpRequestInfo request; | |
4644 request.method = "GET"; | |
4645 request.url = GURL("http://foo:b@r@www.google.com/"); | |
4646 request.load_flags = LOAD_NORMAL; | |
4647 | |
4648 scoped_ptr<HttpTransaction> trans( | |
4649 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
4650 CreateSession(&session_deps_))); | |
4651 | |
4652 // The password contains an escaped character -- for this test to pass it | |
4653 // will need to be unescaped by HttpNetworkTransaction. | |
4654 EXPECT_EQ("b%40r", request.url.password()); | |
4655 | |
4656 MockWrite data_writes1[] = { | |
4657 MockWrite("GET / HTTP/1.1\r\n" | |
4658 "Host: www.google.com\r\n" | |
4659 "Connection: keep-alive\r\n\r\n"), | |
4660 }; | |
4661 | |
4662 MockRead data_reads1[] = { | |
4663 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
4664 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
4665 MockRead("Content-Length: 10\r\n\r\n"), | |
4666 MockRead(SYNCHRONOUS, ERR_FAILED), | |
4667 }; | |
4668 | |
4669 // After the challenge above, the transaction will be restarted using the | |
4670 // identity from the url (foo, b@r) to answer the challenge. | |
4671 MockWrite data_writes2[] = { | |
4672 MockWrite("GET / HTTP/1.1\r\n" | |
4673 "Host: www.google.com\r\n" | |
4674 "Connection: keep-alive\r\n" | |
4675 "Authorization: Basic Zm9vOmJAcg==\r\n\r\n"), | |
4676 }; | |
4677 | |
4678 MockRead data_reads2[] = { | |
4679 MockRead("HTTP/1.0 200 OK\r\n"), | |
4680 MockRead("Content-Length: 100\r\n\r\n"), | |
4681 MockRead(SYNCHRONOUS, OK), | |
4682 }; | |
4683 | |
4684 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
4685 data_writes1, arraysize(data_writes1)); | |
4686 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
4687 data_writes2, arraysize(data_writes2)); | |
4688 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
4689 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
4690 | |
4691 TestCompletionCallback callback1; | |
4692 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
4693 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4694 rv = callback1.WaitForResult(); | |
4695 EXPECT_EQ(OK, rv); | |
4696 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); | |
4697 | |
4698 TestCompletionCallback callback2; | |
4699 rv = trans->RestartWithAuth(AuthCredentials(), callback2.callback()); | |
4700 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4701 rv = callback2.WaitForResult(); | |
4702 EXPECT_EQ(OK, rv); | |
4703 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); | |
4704 | |
4705 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4706 ASSERT_TRUE(response != NULL); | |
4707 | |
4708 // There is no challenge info, since the identity in URL worked. | |
4709 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
4710 | |
4711 EXPECT_EQ(100, response->headers->GetContentLength()); | |
4712 | |
4713 // Empty the current queue. | |
4714 base::MessageLoop::current()->RunUntilIdle(); | |
4715 } | |
4716 | |
4717 // Test the request-challenge-retry sequence for basic auth when there is an | |
4718 // incorrect identity in the URL. The identity from the URL should be used only | |
4719 // once. | |
4720 TEST_F(HttpNetworkTransactionSpdy2Test, WrongAuthIdentityInURL) { | |
4721 HttpRequestInfo request; | |
4722 request.method = "GET"; | |
4723 // Note: the URL has a username:password in it. The password "baz" is | |
4724 // wrong (should be "bar"). | |
4725 request.url = GURL("http://foo:baz@www.google.com/"); | |
4726 | |
4727 request.load_flags = LOAD_NORMAL; | |
4728 | |
4729 scoped_ptr<HttpTransaction> trans( | |
4730 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
4731 CreateSession(&session_deps_))); | |
4732 | |
4733 MockWrite data_writes1[] = { | |
4734 MockWrite("GET / HTTP/1.1\r\n" | |
4735 "Host: www.google.com\r\n" | |
4736 "Connection: keep-alive\r\n\r\n"), | |
4737 }; | |
4738 | |
4739 MockRead data_reads1[] = { | |
4740 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
4741 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
4742 MockRead("Content-Length: 10\r\n\r\n"), | |
4743 MockRead(SYNCHRONOUS, ERR_FAILED), | |
4744 }; | |
4745 | |
4746 // After the challenge above, the transaction will be restarted using the | |
4747 // identity from the url (foo, baz) to answer the challenge. | |
4748 MockWrite data_writes2[] = { | |
4749 MockWrite("GET / HTTP/1.1\r\n" | |
4750 "Host: www.google.com\r\n" | |
4751 "Connection: keep-alive\r\n" | |
4752 "Authorization: Basic Zm9vOmJheg==\r\n\r\n"), | |
4753 }; | |
4754 | |
4755 MockRead data_reads2[] = { | |
4756 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
4757 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
4758 MockRead("Content-Length: 10\r\n\r\n"), | |
4759 MockRead(SYNCHRONOUS, ERR_FAILED), | |
4760 }; | |
4761 | |
4762 // After the challenge above, the transaction will be restarted using the | |
4763 // identity supplied by the user (foo, bar) to answer the challenge. | |
4764 MockWrite data_writes3[] = { | |
4765 MockWrite("GET / HTTP/1.1\r\n" | |
4766 "Host: www.google.com\r\n" | |
4767 "Connection: keep-alive\r\n" | |
4768 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
4769 }; | |
4770 | |
4771 MockRead data_reads3[] = { | |
4772 MockRead("HTTP/1.0 200 OK\r\n"), | |
4773 MockRead("Content-Length: 100\r\n\r\n"), | |
4774 MockRead(SYNCHRONOUS, OK), | |
4775 }; | |
4776 | |
4777 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
4778 data_writes1, arraysize(data_writes1)); | |
4779 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
4780 data_writes2, arraysize(data_writes2)); | |
4781 StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), | |
4782 data_writes3, arraysize(data_writes3)); | |
4783 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
4784 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
4785 session_deps_.socket_factory->AddSocketDataProvider(&data3); | |
4786 | |
4787 TestCompletionCallback callback1; | |
4788 | |
4789 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
4790 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4791 | |
4792 rv = callback1.WaitForResult(); | |
4793 EXPECT_EQ(OK, rv); | |
4794 | |
4795 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); | |
4796 TestCompletionCallback callback2; | |
4797 rv = trans->RestartWithAuth(AuthCredentials(), callback2.callback()); | |
4798 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4799 rv = callback2.WaitForResult(); | |
4800 EXPECT_EQ(OK, rv); | |
4801 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); | |
4802 | |
4803 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4804 ASSERT_TRUE(response != NULL); | |
4805 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
4806 | |
4807 TestCompletionCallback callback3; | |
4808 rv = trans->RestartWithAuth( | |
4809 AuthCredentials(kFoo, kBar), callback3.callback()); | |
4810 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4811 rv = callback3.WaitForResult(); | |
4812 EXPECT_EQ(OK, rv); | |
4813 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); | |
4814 | |
4815 response = trans->GetResponseInfo(); | |
4816 ASSERT_TRUE(response != NULL); | |
4817 | |
4818 // There is no challenge info, since the identity worked. | |
4819 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
4820 | |
4821 EXPECT_EQ(100, response->headers->GetContentLength()); | |
4822 | |
4823 // Empty the current queue. | |
4824 base::MessageLoop::current()->RunUntilIdle(); | |
4825 } | |
4826 | |
4827 // Test that previously tried username/passwords for a realm get re-used. | |
4828 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthCacheAndPreauth) { | |
4829 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
4830 | |
4831 // Transaction 1: authenticate (foo, bar) on MyRealm1 | |
4832 { | |
4833 HttpRequestInfo request; | |
4834 request.method = "GET"; | |
4835 request.url = GURL("http://www.google.com/x/y/z"); | |
4836 request.load_flags = 0; | |
4837 | |
4838 scoped_ptr<HttpTransaction> trans( | |
4839 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4840 | |
4841 MockWrite data_writes1[] = { | |
4842 MockWrite("GET /x/y/z HTTP/1.1\r\n" | |
4843 "Host: www.google.com\r\n" | |
4844 "Connection: keep-alive\r\n\r\n"), | |
4845 }; | |
4846 | |
4847 MockRead data_reads1[] = { | |
4848 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
4849 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
4850 MockRead("Content-Length: 10000\r\n\r\n"), | |
4851 MockRead(SYNCHRONOUS, ERR_FAILED), | |
4852 }; | |
4853 | |
4854 // Resend with authorization (username=foo, password=bar) | |
4855 MockWrite data_writes2[] = { | |
4856 MockWrite("GET /x/y/z HTTP/1.1\r\n" | |
4857 "Host: www.google.com\r\n" | |
4858 "Connection: keep-alive\r\n" | |
4859 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
4860 }; | |
4861 | |
4862 // Sever accepts the authorization. | |
4863 MockRead data_reads2[] = { | |
4864 MockRead("HTTP/1.0 200 OK\r\n"), | |
4865 MockRead("Content-Length: 100\r\n\r\n"), | |
4866 MockRead(SYNCHRONOUS, OK), | |
4867 }; | |
4868 | |
4869 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
4870 data_writes1, arraysize(data_writes1)); | |
4871 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
4872 data_writes2, arraysize(data_writes2)); | |
4873 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
4874 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
4875 | |
4876 TestCompletionCallback callback1; | |
4877 | |
4878 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
4879 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4880 | |
4881 rv = callback1.WaitForResult(); | |
4882 EXPECT_EQ(OK, rv); | |
4883 | |
4884 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4885 ASSERT_TRUE(response != NULL); | |
4886 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
4887 | |
4888 TestCompletionCallback callback2; | |
4889 | |
4890 rv = trans->RestartWithAuth( | |
4891 AuthCredentials(kFoo, kBar), callback2.callback()); | |
4892 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4893 | |
4894 rv = callback2.WaitForResult(); | |
4895 EXPECT_EQ(OK, rv); | |
4896 | |
4897 response = trans->GetResponseInfo(); | |
4898 ASSERT_TRUE(response != NULL); | |
4899 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
4900 EXPECT_EQ(100, response->headers->GetContentLength()); | |
4901 } | |
4902 | |
4903 // ------------------------------------------------------------------------ | |
4904 | |
4905 // Transaction 2: authenticate (foo2, bar2) on MyRealm2 | |
4906 { | |
4907 HttpRequestInfo request; | |
4908 request.method = "GET"; | |
4909 // Note that Transaction 1 was at /x/y/z, so this is in the same | |
4910 // protection space as MyRealm1. | |
4911 request.url = GURL("http://www.google.com/x/y/a/b"); | |
4912 request.load_flags = 0; | |
4913 | |
4914 scoped_ptr<HttpTransaction> trans( | |
4915 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
4916 | |
4917 MockWrite data_writes1[] = { | |
4918 MockWrite("GET /x/y/a/b HTTP/1.1\r\n" | |
4919 "Host: www.google.com\r\n" | |
4920 "Connection: keep-alive\r\n" | |
4921 // Send preemptive authorization for MyRealm1 | |
4922 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
4923 }; | |
4924 | |
4925 // The server didn't like the preemptive authorization, and | |
4926 // challenges us for a different realm (MyRealm2). | |
4927 MockRead data_reads1[] = { | |
4928 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
4929 MockRead("WWW-Authenticate: Basic realm=\"MyRealm2\"\r\n"), | |
4930 MockRead("Content-Length: 10000\r\n\r\n"), | |
4931 MockRead(SYNCHRONOUS, ERR_FAILED), | |
4932 }; | |
4933 | |
4934 // Resend with authorization for MyRealm2 (username=foo2, password=bar2) | |
4935 MockWrite data_writes2[] = { | |
4936 MockWrite("GET /x/y/a/b HTTP/1.1\r\n" | |
4937 "Host: www.google.com\r\n" | |
4938 "Connection: keep-alive\r\n" | |
4939 "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), | |
4940 }; | |
4941 | |
4942 // Sever accepts the authorization. | |
4943 MockRead data_reads2[] = { | |
4944 MockRead("HTTP/1.0 200 OK\r\n"), | |
4945 MockRead("Content-Length: 100\r\n\r\n"), | |
4946 MockRead(SYNCHRONOUS, OK), | |
4947 }; | |
4948 | |
4949 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
4950 data_writes1, arraysize(data_writes1)); | |
4951 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
4952 data_writes2, arraysize(data_writes2)); | |
4953 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
4954 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
4955 | |
4956 TestCompletionCallback callback1; | |
4957 | |
4958 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
4959 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4960 | |
4961 rv = callback1.WaitForResult(); | |
4962 EXPECT_EQ(OK, rv); | |
4963 | |
4964 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
4965 ASSERT_TRUE(response != NULL); | |
4966 ASSERT_TRUE(response->auth_challenge.get()); | |
4967 EXPECT_FALSE(response->auth_challenge->is_proxy); | |
4968 EXPECT_EQ("www.google.com:80", | |
4969 response->auth_challenge->challenger.ToString()); | |
4970 EXPECT_EQ("MyRealm2", response->auth_challenge->realm); | |
4971 EXPECT_EQ("basic", response->auth_challenge->scheme); | |
4972 | |
4973 TestCompletionCallback callback2; | |
4974 | |
4975 rv = trans->RestartWithAuth( | |
4976 AuthCredentials(kFoo2, kBar2), callback2.callback()); | |
4977 EXPECT_EQ(ERR_IO_PENDING, rv); | |
4978 | |
4979 rv = callback2.WaitForResult(); | |
4980 EXPECT_EQ(OK, rv); | |
4981 | |
4982 response = trans->GetResponseInfo(); | |
4983 ASSERT_TRUE(response != NULL); | |
4984 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
4985 EXPECT_EQ(100, response->headers->GetContentLength()); | |
4986 } | |
4987 | |
4988 // ------------------------------------------------------------------------ | |
4989 | |
4990 // Transaction 3: Resend a request in MyRealm's protection space -- | |
4991 // succeed with preemptive authorization. | |
4992 { | |
4993 HttpRequestInfo request; | |
4994 request.method = "GET"; | |
4995 request.url = GURL("http://www.google.com/x/y/z2"); | |
4996 request.load_flags = 0; | |
4997 | |
4998 scoped_ptr<HttpTransaction> trans( | |
4999 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
5000 | |
5001 MockWrite data_writes1[] = { | |
5002 MockWrite("GET /x/y/z2 HTTP/1.1\r\n" | |
5003 "Host: www.google.com\r\n" | |
5004 "Connection: keep-alive\r\n" | |
5005 // The authorization for MyRealm1 gets sent preemptively | |
5006 // (since the url is in the same protection space) | |
5007 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
5008 }; | |
5009 | |
5010 // Sever accepts the preemptive authorization | |
5011 MockRead data_reads1[] = { | |
5012 MockRead("HTTP/1.0 200 OK\r\n"), | |
5013 MockRead("Content-Length: 100\r\n\r\n"), | |
5014 MockRead(SYNCHRONOUS, OK), | |
5015 }; | |
5016 | |
5017 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
5018 data_writes1, arraysize(data_writes1)); | |
5019 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
5020 | |
5021 TestCompletionCallback callback1; | |
5022 | |
5023 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
5024 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5025 | |
5026 rv = callback1.WaitForResult(); | |
5027 EXPECT_EQ(OK, rv); | |
5028 | |
5029 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5030 ASSERT_TRUE(response != NULL); | |
5031 | |
5032 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
5033 EXPECT_EQ(100, response->headers->GetContentLength()); | |
5034 } | |
5035 | |
5036 // ------------------------------------------------------------------------ | |
5037 | |
5038 // Transaction 4: request another URL in MyRealm (however the | |
5039 // url is not known to belong to the protection space, so no pre-auth). | |
5040 { | |
5041 HttpRequestInfo request; | |
5042 request.method = "GET"; | |
5043 request.url = GURL("http://www.google.com/x/1"); | |
5044 request.load_flags = 0; | |
5045 | |
5046 scoped_ptr<HttpTransaction> trans( | |
5047 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
5048 | |
5049 MockWrite data_writes1[] = { | |
5050 MockWrite("GET /x/1 HTTP/1.1\r\n" | |
5051 "Host: www.google.com\r\n" | |
5052 "Connection: keep-alive\r\n\r\n"), | |
5053 }; | |
5054 | |
5055 MockRead data_reads1[] = { | |
5056 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
5057 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
5058 MockRead("Content-Length: 10000\r\n\r\n"), | |
5059 MockRead(SYNCHRONOUS, ERR_FAILED), | |
5060 }; | |
5061 | |
5062 // Resend with authorization from MyRealm's cache. | |
5063 MockWrite data_writes2[] = { | |
5064 MockWrite("GET /x/1 HTTP/1.1\r\n" | |
5065 "Host: www.google.com\r\n" | |
5066 "Connection: keep-alive\r\n" | |
5067 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
5068 }; | |
5069 | |
5070 // Sever accepts the authorization. | |
5071 MockRead data_reads2[] = { | |
5072 MockRead("HTTP/1.0 200 OK\r\n"), | |
5073 MockRead("Content-Length: 100\r\n\r\n"), | |
5074 MockRead(SYNCHRONOUS, OK), | |
5075 }; | |
5076 | |
5077 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
5078 data_writes1, arraysize(data_writes1)); | |
5079 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
5080 data_writes2, arraysize(data_writes2)); | |
5081 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
5082 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
5083 | |
5084 TestCompletionCallback callback1; | |
5085 | |
5086 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
5087 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5088 | |
5089 rv = callback1.WaitForResult(); | |
5090 EXPECT_EQ(OK, rv); | |
5091 | |
5092 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); | |
5093 TestCompletionCallback callback2; | |
5094 rv = trans->RestartWithAuth(AuthCredentials(), callback2.callback()); | |
5095 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5096 rv = callback2.WaitForResult(); | |
5097 EXPECT_EQ(OK, rv); | |
5098 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); | |
5099 | |
5100 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5101 ASSERT_TRUE(response != NULL); | |
5102 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
5103 EXPECT_EQ(100, response->headers->GetContentLength()); | |
5104 } | |
5105 | |
5106 // ------------------------------------------------------------------------ | |
5107 | |
5108 // Transaction 5: request a URL in MyRealm, but the server rejects the | |
5109 // cached identity. Should invalidate and re-prompt. | |
5110 { | |
5111 HttpRequestInfo request; | |
5112 request.method = "GET"; | |
5113 request.url = GURL("http://www.google.com/p/q/t"); | |
5114 request.load_flags = 0; | |
5115 | |
5116 scoped_ptr<HttpTransaction> trans( | |
5117 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
5118 | |
5119 MockWrite data_writes1[] = { | |
5120 MockWrite("GET /p/q/t HTTP/1.1\r\n" | |
5121 "Host: www.google.com\r\n" | |
5122 "Connection: keep-alive\r\n\r\n"), | |
5123 }; | |
5124 | |
5125 MockRead data_reads1[] = { | |
5126 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
5127 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
5128 MockRead("Content-Length: 10000\r\n\r\n"), | |
5129 MockRead(SYNCHRONOUS, ERR_FAILED), | |
5130 }; | |
5131 | |
5132 // Resend with authorization from cache for MyRealm. | |
5133 MockWrite data_writes2[] = { | |
5134 MockWrite("GET /p/q/t HTTP/1.1\r\n" | |
5135 "Host: www.google.com\r\n" | |
5136 "Connection: keep-alive\r\n" | |
5137 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
5138 }; | |
5139 | |
5140 // Sever rejects the authorization. | |
5141 MockRead data_reads2[] = { | |
5142 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
5143 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
5144 MockRead("Content-Length: 10000\r\n\r\n"), | |
5145 MockRead(SYNCHRONOUS, ERR_FAILED), | |
5146 }; | |
5147 | |
5148 // At this point we should prompt for new credentials for MyRealm. | |
5149 // Restart with username=foo3, password=foo4. | |
5150 MockWrite data_writes3[] = { | |
5151 MockWrite("GET /p/q/t HTTP/1.1\r\n" | |
5152 "Host: www.google.com\r\n" | |
5153 "Connection: keep-alive\r\n" | |
5154 "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), | |
5155 }; | |
5156 | |
5157 // Sever accepts the authorization. | |
5158 MockRead data_reads3[] = { | |
5159 MockRead("HTTP/1.0 200 OK\r\n"), | |
5160 MockRead("Content-Length: 100\r\n\r\n"), | |
5161 MockRead(SYNCHRONOUS, OK), | |
5162 }; | |
5163 | |
5164 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
5165 data_writes1, arraysize(data_writes1)); | |
5166 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
5167 data_writes2, arraysize(data_writes2)); | |
5168 StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), | |
5169 data_writes3, arraysize(data_writes3)); | |
5170 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
5171 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
5172 session_deps_.socket_factory->AddSocketDataProvider(&data3); | |
5173 | |
5174 TestCompletionCallback callback1; | |
5175 | |
5176 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
5177 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5178 | |
5179 rv = callback1.WaitForResult(); | |
5180 EXPECT_EQ(OK, rv); | |
5181 | |
5182 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); | |
5183 TestCompletionCallback callback2; | |
5184 rv = trans->RestartWithAuth(AuthCredentials(), callback2.callback()); | |
5185 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5186 rv = callback2.WaitForResult(); | |
5187 EXPECT_EQ(OK, rv); | |
5188 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); | |
5189 | |
5190 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5191 ASSERT_TRUE(response != NULL); | |
5192 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
5193 | |
5194 TestCompletionCallback callback3; | |
5195 | |
5196 rv = trans->RestartWithAuth( | |
5197 AuthCredentials(kFoo3, kBar3), callback3.callback()); | |
5198 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5199 | |
5200 rv = callback3.WaitForResult(); | |
5201 EXPECT_EQ(OK, rv); | |
5202 | |
5203 response = trans->GetResponseInfo(); | |
5204 ASSERT_TRUE(response != NULL); | |
5205 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
5206 EXPECT_EQ(100, response->headers->GetContentLength()); | |
5207 } | |
5208 } | |
5209 | |
5210 // Tests that nonce count increments when multiple auth attempts | |
5211 // are started with the same nonce. | |
5212 TEST_F(HttpNetworkTransactionSpdy2Test, DigestPreAuthNonceCount) { | |
5213 HttpAuthHandlerDigest::Factory* digest_factory = | |
5214 new HttpAuthHandlerDigest::Factory(); | |
5215 HttpAuthHandlerDigest::FixedNonceGenerator* nonce_generator = | |
5216 new HttpAuthHandlerDigest::FixedNonceGenerator("0123456789abcdef"); | |
5217 digest_factory->set_nonce_generator(nonce_generator); | |
5218 session_deps_.http_auth_handler_factory.reset(digest_factory); | |
5219 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
5220 | |
5221 // Transaction 1: authenticate (foo, bar) on MyRealm1 | |
5222 { | |
5223 HttpRequestInfo request; | |
5224 request.method = "GET"; | |
5225 request.url = GURL("http://www.google.com/x/y/z"); | |
5226 request.load_flags = 0; | |
5227 | |
5228 scoped_ptr<HttpTransaction> trans( | |
5229 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
5230 | |
5231 MockWrite data_writes1[] = { | |
5232 MockWrite("GET /x/y/z HTTP/1.1\r\n" | |
5233 "Host: www.google.com\r\n" | |
5234 "Connection: keep-alive\r\n\r\n"), | |
5235 }; | |
5236 | |
5237 MockRead data_reads1[] = { | |
5238 MockRead("HTTP/1.0 401 Unauthorized\r\n"), | |
5239 MockRead("WWW-Authenticate: Digest realm=\"digestive\", nonce=\"OU812\", " | |
5240 "algorithm=MD5, qop=\"auth\"\r\n\r\n"), | |
5241 MockRead(SYNCHRONOUS, OK), | |
5242 }; | |
5243 | |
5244 // Resend with authorization (username=foo, password=bar) | |
5245 MockWrite data_writes2[] = { | |
5246 MockWrite("GET /x/y/z HTTP/1.1\r\n" | |
5247 "Host: www.google.com\r\n" | |
5248 "Connection: keep-alive\r\n" | |
5249 "Authorization: Digest username=\"foo\", realm=\"digestive\", " | |
5250 "nonce=\"OU812\", uri=\"/x/y/z\", algorithm=MD5, " | |
5251 "response=\"03ffbcd30add722589c1de345d7a927f\", qop=auth, " | |
5252 "nc=00000001, cnonce=\"0123456789abcdef\"\r\n\r\n"), | |
5253 }; | |
5254 | |
5255 // Sever accepts the authorization. | |
5256 MockRead data_reads2[] = { | |
5257 MockRead("HTTP/1.0 200 OK\r\n"), | |
5258 MockRead(SYNCHRONOUS, OK), | |
5259 }; | |
5260 | |
5261 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
5262 data_writes1, arraysize(data_writes1)); | |
5263 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
5264 data_writes2, arraysize(data_writes2)); | |
5265 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
5266 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
5267 | |
5268 TestCompletionCallback callback1; | |
5269 | |
5270 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
5271 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5272 | |
5273 rv = callback1.WaitForResult(); | |
5274 EXPECT_EQ(OK, rv); | |
5275 | |
5276 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5277 ASSERT_TRUE(response != NULL); | |
5278 EXPECT_TRUE(CheckDigestServerAuth(response->auth_challenge.get())); | |
5279 | |
5280 TestCompletionCallback callback2; | |
5281 | |
5282 rv = trans->RestartWithAuth( | |
5283 AuthCredentials(kFoo, kBar), callback2.callback()); | |
5284 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5285 | |
5286 rv = callback2.WaitForResult(); | |
5287 EXPECT_EQ(OK, rv); | |
5288 | |
5289 response = trans->GetResponseInfo(); | |
5290 ASSERT_TRUE(response != NULL); | |
5291 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
5292 } | |
5293 | |
5294 // ------------------------------------------------------------------------ | |
5295 | |
5296 // Transaction 2: Request another resource in digestive's protection space. | |
5297 // This will preemptively add an Authorization header which should have an | |
5298 // "nc" value of 2 (as compared to 1 in the first use. | |
5299 { | |
5300 HttpRequestInfo request; | |
5301 request.method = "GET"; | |
5302 // Note that Transaction 1 was at /x/y/z, so this is in the same | |
5303 // protection space as digest. | |
5304 request.url = GURL("http://www.google.com/x/y/a/b"); | |
5305 request.load_flags = 0; | |
5306 | |
5307 scoped_ptr<HttpTransaction> trans( | |
5308 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
5309 | |
5310 MockWrite data_writes1[] = { | |
5311 MockWrite("GET /x/y/a/b HTTP/1.1\r\n" | |
5312 "Host: www.google.com\r\n" | |
5313 "Connection: keep-alive\r\n" | |
5314 "Authorization: Digest username=\"foo\", realm=\"digestive\", " | |
5315 "nonce=\"OU812\", uri=\"/x/y/a/b\", algorithm=MD5, " | |
5316 "response=\"d6f9a2c07d1c5df7b89379dca1269b35\", qop=auth, " | |
5317 "nc=00000002, cnonce=\"0123456789abcdef\"\r\n\r\n"), | |
5318 }; | |
5319 | |
5320 // Sever accepts the authorization. | |
5321 MockRead data_reads1[] = { | |
5322 MockRead("HTTP/1.0 200 OK\r\n"), | |
5323 MockRead("Content-Length: 100\r\n\r\n"), | |
5324 MockRead(SYNCHRONOUS, OK), | |
5325 }; | |
5326 | |
5327 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
5328 data_writes1, arraysize(data_writes1)); | |
5329 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
5330 | |
5331 TestCompletionCallback callback1; | |
5332 | |
5333 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
5334 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5335 | |
5336 rv = callback1.WaitForResult(); | |
5337 EXPECT_EQ(OK, rv); | |
5338 | |
5339 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5340 ASSERT_TRUE(response != NULL); | |
5341 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
5342 } | |
5343 } | |
5344 | |
5345 // Test the ResetStateForRestart() private method. | |
5346 TEST_F(HttpNetworkTransactionSpdy2Test, ResetStateForRestart) { | |
5347 // Create a transaction (the dependencies aren't important). | |
5348 scoped_ptr<HttpNetworkTransaction> trans( | |
5349 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
5350 CreateSession(&session_deps_))); | |
5351 | |
5352 // Setup some state (which we expect ResetStateForRestart() will clear). | |
5353 trans->read_buf_ = new IOBuffer(15); | |
5354 trans->read_buf_len_ = 15; | |
5355 trans->request_headers_.SetHeader("Authorization", "NTLM"); | |
5356 | |
5357 // Setup state in response_ | |
5358 HttpResponseInfo* response = &trans->response_; | |
5359 response->auth_challenge = new AuthChallengeInfo(); | |
5360 response->ssl_info.cert_status = static_cast<CertStatus>(-1); // Nonsensical. | |
5361 response->response_time = base::Time::Now(); | |
5362 response->was_cached = true; // (Wouldn't ever actually be true...) | |
5363 | |
5364 { // Setup state for response_.vary_data | |
5365 HttpRequestInfo request; | |
5366 std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); | |
5367 std::replace(temp.begin(), temp.end(), '\n', '\0'); | |
5368 scoped_refptr<HttpResponseHeaders> headers(new HttpResponseHeaders(temp)); | |
5369 request.extra_headers.SetHeader("Foo", "1"); | |
5370 request.extra_headers.SetHeader("bar", "23"); | |
5371 EXPECT_TRUE(response->vary_data.Init(request, *headers.get())); | |
5372 } | |
5373 | |
5374 // Cause the above state to be reset. | |
5375 trans->ResetStateForRestart(); | |
5376 | |
5377 // Verify that the state that needed to be reset, has been reset. | |
5378 EXPECT_TRUE(trans->read_buf_.get() == NULL); | |
5379 EXPECT_EQ(0, trans->read_buf_len_); | |
5380 EXPECT_TRUE(trans->request_headers_.IsEmpty()); | |
5381 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
5382 EXPECT_TRUE(response->headers.get() == NULL); | |
5383 EXPECT_FALSE(response->was_cached); | |
5384 EXPECT_EQ(0U, response->ssl_info.cert_status); | |
5385 EXPECT_FALSE(response->vary_data.is_valid()); | |
5386 } | |
5387 | |
5388 // Test HTTPS connections to a site with a bad certificate | |
5389 TEST_F(HttpNetworkTransactionSpdy2Test, HTTPSBadCertificate) { | |
5390 HttpRequestInfo request; | |
5391 request.method = "GET"; | |
5392 request.url = GURL("https://www.google.com/"); | |
5393 request.load_flags = 0; | |
5394 | |
5395 scoped_ptr<HttpTransaction> trans( | |
5396 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
5397 CreateSession(&session_deps_))); | |
5398 | |
5399 MockWrite data_writes[] = { | |
5400 MockWrite("GET / HTTP/1.1\r\n" | |
5401 "Host: www.google.com\r\n" | |
5402 "Connection: keep-alive\r\n\r\n"), | |
5403 }; | |
5404 | |
5405 MockRead data_reads[] = { | |
5406 MockRead("HTTP/1.0 200 OK\r\n"), | |
5407 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
5408 MockRead("Content-Length: 100\r\n\r\n"), | |
5409 MockRead(SYNCHRONOUS, OK), | |
5410 }; | |
5411 | |
5412 StaticSocketDataProvider ssl_bad_certificate; | |
5413 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
5414 data_writes, arraysize(data_writes)); | |
5415 SSLSocketDataProvider ssl_bad(ASYNC, ERR_CERT_AUTHORITY_INVALID); | |
5416 SSLSocketDataProvider ssl(ASYNC, OK); | |
5417 | |
5418 session_deps_.socket_factory->AddSocketDataProvider(&ssl_bad_certificate); | |
5419 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
5420 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bad); | |
5421 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
5422 | |
5423 TestCompletionCallback callback; | |
5424 | |
5425 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
5426 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5427 | |
5428 rv = callback.WaitForResult(); | |
5429 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); | |
5430 | |
5431 rv = trans->RestartIgnoringLastError(callback.callback()); | |
5432 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5433 | |
5434 rv = callback.WaitForResult(); | |
5435 EXPECT_EQ(OK, rv); | |
5436 | |
5437 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5438 | |
5439 ASSERT_TRUE(response != NULL); | |
5440 EXPECT_EQ(100, response->headers->GetContentLength()); | |
5441 } | |
5442 | |
5443 // Test HTTPS connections to a site with a bad certificate, going through a | |
5444 // proxy | |
5445 TEST_F(HttpNetworkTransactionSpdy2Test, HTTPSBadCertificateViaProxy) { | |
5446 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
5447 | |
5448 HttpRequestInfo request; | |
5449 request.method = "GET"; | |
5450 request.url = GURL("https://www.google.com/"); | |
5451 request.load_flags = 0; | |
5452 | |
5453 MockWrite proxy_writes[] = { | |
5454 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
5455 "Host: www.google.com\r\n" | |
5456 "Proxy-Connection: keep-alive\r\n\r\n"), | |
5457 }; | |
5458 | |
5459 MockRead proxy_reads[] = { | |
5460 MockRead("HTTP/1.0 200 Connected\r\n\r\n"), | |
5461 MockRead(SYNCHRONOUS, OK) | |
5462 }; | |
5463 | |
5464 MockWrite data_writes[] = { | |
5465 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
5466 "Host: www.google.com\r\n" | |
5467 "Proxy-Connection: keep-alive\r\n\r\n"), | |
5468 MockWrite("GET / HTTP/1.1\r\n" | |
5469 "Host: www.google.com\r\n" | |
5470 "Connection: keep-alive\r\n\r\n"), | |
5471 }; | |
5472 | |
5473 MockRead data_reads[] = { | |
5474 MockRead("HTTP/1.0 200 Connected\r\n\r\n"), | |
5475 MockRead("HTTP/1.0 200 OK\r\n"), | |
5476 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
5477 MockRead("Content-Length: 100\r\n\r\n"), | |
5478 MockRead(SYNCHRONOUS, OK), | |
5479 }; | |
5480 | |
5481 StaticSocketDataProvider ssl_bad_certificate( | |
5482 proxy_reads, arraysize(proxy_reads), | |
5483 proxy_writes, arraysize(proxy_writes)); | |
5484 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
5485 data_writes, arraysize(data_writes)); | |
5486 SSLSocketDataProvider ssl_bad(ASYNC, ERR_CERT_AUTHORITY_INVALID); | |
5487 SSLSocketDataProvider ssl(ASYNC, OK); | |
5488 | |
5489 session_deps_.socket_factory->AddSocketDataProvider(&ssl_bad_certificate); | |
5490 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
5491 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bad); | |
5492 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
5493 | |
5494 TestCompletionCallback callback; | |
5495 | |
5496 for (int i = 0; i < 2; i++) { | |
5497 session_deps_.socket_factory->ResetNextMockIndexes(); | |
5498 | |
5499 scoped_ptr<HttpTransaction> trans( | |
5500 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
5501 CreateSession(&session_deps_))); | |
5502 | |
5503 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
5504 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5505 | |
5506 rv = callback.WaitForResult(); | |
5507 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); | |
5508 | |
5509 rv = trans->RestartIgnoringLastError(callback.callback()); | |
5510 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5511 | |
5512 rv = callback.WaitForResult(); | |
5513 EXPECT_EQ(OK, rv); | |
5514 | |
5515 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5516 | |
5517 ASSERT_TRUE(response != NULL); | |
5518 EXPECT_EQ(100, response->headers->GetContentLength()); | |
5519 } | |
5520 } | |
5521 | |
5522 | |
5523 // Test HTTPS connections to a site, going through an HTTPS proxy | |
5524 TEST_F(HttpNetworkTransactionSpdy2Test, HTTPSViaHttpsProxy) { | |
5525 session_deps_.proxy_service.reset( | |
5526 ProxyService::CreateFixedFromPacResult("HTTPS proxy:70")); | |
5527 CapturingNetLog net_log; | |
5528 session_deps_.net_log = &net_log; | |
5529 | |
5530 HttpRequestInfo request; | |
5531 request.method = "GET"; | |
5532 request.url = GURL("https://www.google.com/"); | |
5533 request.load_flags = 0; | |
5534 | |
5535 MockWrite data_writes[] = { | |
5536 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
5537 "Host: www.google.com\r\n" | |
5538 "Proxy-Connection: keep-alive\r\n\r\n"), | |
5539 MockWrite("GET / HTTP/1.1\r\n" | |
5540 "Host: www.google.com\r\n" | |
5541 "Connection: keep-alive\r\n\r\n"), | |
5542 }; | |
5543 | |
5544 MockRead data_reads[] = { | |
5545 MockRead("HTTP/1.0 200 Connected\r\n\r\n"), | |
5546 MockRead("HTTP/1.1 200 OK\r\n"), | |
5547 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
5548 MockRead("Content-Length: 100\r\n\r\n"), | |
5549 MockRead(SYNCHRONOUS, OK), | |
5550 }; | |
5551 | |
5552 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
5553 data_writes, arraysize(data_writes)); | |
5554 SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy | |
5555 SSLSocketDataProvider tunnel_ssl(ASYNC, OK); // SSL through the tunnel | |
5556 | |
5557 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
5558 session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); | |
5559 session_deps_.socket_factory->AddSSLSocketDataProvider(&tunnel_ssl); | |
5560 | |
5561 TestCompletionCallback callback; | |
5562 | |
5563 scoped_ptr<HttpTransaction> trans( | |
5564 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
5565 CreateSession(&session_deps_))); | |
5566 | |
5567 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
5568 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5569 | |
5570 rv = callback.WaitForResult(); | |
5571 EXPECT_EQ(OK, rv); | |
5572 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5573 | |
5574 ASSERT_TRUE(response != NULL); | |
5575 | |
5576 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
5577 EXPECT_EQ(200, response->headers->response_code()); | |
5578 EXPECT_EQ(100, response->headers->GetContentLength()); | |
5579 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
5580 | |
5581 LoadTimingInfo load_timing_info; | |
5582 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
5583 TestLoadTimingNotReusedWithPac(load_timing_info, | |
5584 CONNECT_TIMING_HAS_SSL_TIMES); | |
5585 } | |
5586 | |
5587 // Test an HTTPS Proxy's ability to redirect a CONNECT request | |
5588 TEST_F(HttpNetworkTransactionSpdy2Test, RedirectOfHttpsConnectViaHttpsProxy) { | |
5589 session_deps_.proxy_service.reset( | |
5590 ProxyService::CreateFixedFromPacResult("HTTPS proxy:70")); | |
5591 CapturingNetLog net_log; | |
5592 session_deps_.net_log = &net_log; | |
5593 | |
5594 HttpRequestInfo request; | |
5595 request.method = "GET"; | |
5596 request.url = GURL("https://www.google.com/"); | |
5597 request.load_flags = 0; | |
5598 | |
5599 MockWrite data_writes[] = { | |
5600 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
5601 "Host: www.google.com\r\n" | |
5602 "Proxy-Connection: keep-alive\r\n\r\n"), | |
5603 }; | |
5604 | |
5605 MockRead data_reads[] = { | |
5606 MockRead("HTTP/1.1 302 Redirect\r\n"), | |
5607 MockRead("Location: http://login.example.com/\r\n"), | |
5608 MockRead("Content-Length: 0\r\n\r\n"), | |
5609 MockRead(SYNCHRONOUS, OK), | |
5610 }; | |
5611 | |
5612 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
5613 data_writes, arraysize(data_writes)); | |
5614 SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy | |
5615 | |
5616 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
5617 session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); | |
5618 | |
5619 TestCompletionCallback callback; | |
5620 | |
5621 scoped_ptr<HttpTransaction> trans( | |
5622 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
5623 CreateSession(&session_deps_))); | |
5624 | |
5625 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
5626 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5627 | |
5628 rv = callback.WaitForResult(); | |
5629 EXPECT_EQ(OK, rv); | |
5630 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5631 | |
5632 ASSERT_TRUE(response != NULL); | |
5633 | |
5634 EXPECT_EQ(302, response->headers->response_code()); | |
5635 std::string url; | |
5636 EXPECT_TRUE(response->headers->IsRedirect(&url)); | |
5637 EXPECT_EQ("http://login.example.com/", url); | |
5638 | |
5639 // In the case of redirects from proxies, HttpNetworkTransaction returns | |
5640 // timing for the proxy connection instead of the connection to the host, | |
5641 // and no send / receive times. | |
5642 // See HttpNetworkTransaction::OnHttpsProxyTunnelResponse. | |
5643 LoadTimingInfo load_timing_info; | |
5644 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
5645 | |
5646 EXPECT_FALSE(load_timing_info.socket_reused); | |
5647 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | |
5648 | |
5649 EXPECT_FALSE(load_timing_info.proxy_resolve_start.is_null()); | |
5650 EXPECT_LE(load_timing_info.proxy_resolve_start, | |
5651 load_timing_info.proxy_resolve_end); | |
5652 EXPECT_LE(load_timing_info.proxy_resolve_end, | |
5653 load_timing_info.connect_timing.connect_start); | |
5654 ExpectConnectTimingHasTimes( | |
5655 load_timing_info.connect_timing, | |
5656 CONNECT_TIMING_HAS_DNS_TIMES | CONNECT_TIMING_HAS_SSL_TIMES); | |
5657 | |
5658 EXPECT_TRUE(load_timing_info.send_start.is_null()); | |
5659 EXPECT_TRUE(load_timing_info.send_end.is_null()); | |
5660 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | |
5661 } | |
5662 | |
5663 // Test an HTTPS (SPDY) Proxy's ability to redirect a CONNECT request | |
5664 TEST_F(HttpNetworkTransactionSpdy2Test, RedirectOfHttpsConnectViaSpdyProxy) { | |
5665 session_deps_.proxy_service.reset( | |
5666 ProxyService::CreateFixed("https://proxy:70")); | |
5667 | |
5668 HttpRequestInfo request; | |
5669 request.method = "GET"; | |
5670 request.url = GURL("https://www.google.com/"); | |
5671 request.load_flags = 0; | |
5672 | |
5673 scoped_ptr<SpdyFrame> conn(spdy_util_.ConstructSpdyConnect(NULL, 0, 1)); | |
5674 scoped_ptr<SpdyFrame> goaway( | |
5675 spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL)); | |
5676 MockWrite data_writes[] = { | |
5677 CreateMockWrite(*conn.get(), 0, SYNCHRONOUS), | |
5678 }; | |
5679 | |
5680 static const char* const kExtraHeaders[] = { | |
5681 "location", | |
5682 "http://login.example.com/", | |
5683 }; | |
5684 scoped_ptr<SpdyFrame> resp( | |
5685 ConstructSpdySynReplyError("302 Redirect", kExtraHeaders, | |
5686 arraysize(kExtraHeaders)/2, 1)); | |
5687 MockRead data_reads[] = { | |
5688 CreateMockRead(*resp.get(), 1, SYNCHRONOUS), | |
5689 MockRead(ASYNC, 0, 2), // EOF | |
5690 }; | |
5691 | |
5692 DelayedSocketData data( | |
5693 1, // wait for one write to finish before reading. | |
5694 data_reads, arraysize(data_reads), | |
5695 data_writes, arraysize(data_writes)); | |
5696 SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy | |
5697 proxy_ssl.SetNextProto(kProtoSPDY2); | |
5698 | |
5699 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
5700 session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); | |
5701 | |
5702 TestCompletionCallback callback; | |
5703 | |
5704 scoped_ptr<HttpTransaction> trans( | |
5705 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
5706 CreateSession(&session_deps_))); | |
5707 | |
5708 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
5709 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5710 | |
5711 rv = callback.WaitForResult(); | |
5712 EXPECT_EQ(OK, rv); | |
5713 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5714 | |
5715 ASSERT_TRUE(response != NULL); | |
5716 | |
5717 EXPECT_EQ(302, response->headers->response_code()); | |
5718 std::string url; | |
5719 EXPECT_TRUE(response->headers->IsRedirect(&url)); | |
5720 EXPECT_EQ("http://login.example.com/", url); | |
5721 } | |
5722 | |
5723 // Test that an HTTPS proxy's response to a CONNECT request is filtered. | |
5724 TEST_F(HttpNetworkTransactionSpdy2Test, | |
5725 ErrorResponseToHttpsConnectViaHttpsProxy) { | |
5726 session_deps_.proxy_service.reset( | |
5727 ProxyService::CreateFixed("https://proxy:70")); | |
5728 | |
5729 HttpRequestInfo request; | |
5730 request.method = "GET"; | |
5731 request.url = GURL("https://www.google.com/"); | |
5732 request.load_flags = 0; | |
5733 | |
5734 MockWrite data_writes[] = { | |
5735 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
5736 "Host: www.google.com\r\n" | |
5737 "Proxy-Connection: keep-alive\r\n\r\n"), | |
5738 }; | |
5739 | |
5740 MockRead data_reads[] = { | |
5741 MockRead("HTTP/1.1 404 Not Found\r\n"), | |
5742 MockRead("Content-Length: 23\r\n\r\n"), | |
5743 MockRead("The host does not exist"), | |
5744 MockRead(SYNCHRONOUS, OK), | |
5745 }; | |
5746 | |
5747 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
5748 data_writes, arraysize(data_writes)); | |
5749 SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy | |
5750 | |
5751 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
5752 session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); | |
5753 | |
5754 TestCompletionCallback callback; | |
5755 | |
5756 scoped_ptr<HttpTransaction> trans( | |
5757 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
5758 CreateSession(&session_deps_))); | |
5759 | |
5760 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
5761 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5762 | |
5763 rv = callback.WaitForResult(); | |
5764 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); | |
5765 | |
5766 // TODO(ttuttle): Anything else to check here? | |
5767 } | |
5768 | |
5769 // Test that a SPDY proxy's response to a CONNECT request is filtered. | |
5770 TEST_F(HttpNetworkTransactionSpdy2Test, | |
5771 ErrorResponseToHttpsConnectViaSpdyProxy) { | |
5772 session_deps_.proxy_service.reset( | |
5773 ProxyService::CreateFixed("https://proxy:70")); | |
5774 | |
5775 HttpRequestInfo request; | |
5776 request.method = "GET"; | |
5777 request.url = GURL("https://www.google.com/"); | |
5778 request.load_flags = 0; | |
5779 | |
5780 scoped_ptr<SpdyFrame> conn(spdy_util_.ConstructSpdyConnect(NULL, 0, 1)); | |
5781 scoped_ptr<SpdyFrame> rst( | |
5782 spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL)); | |
5783 MockWrite data_writes[] = { | |
5784 CreateMockWrite(*conn.get(), 0, SYNCHRONOUS), | |
5785 CreateMockWrite(*rst.get(), 3, SYNCHRONOUS), | |
5786 }; | |
5787 | |
5788 static const char* const kExtraHeaders[] = { | |
5789 "location", | |
5790 "http://login.example.com/", | |
5791 }; | |
5792 scoped_ptr<SpdyFrame> resp( | |
5793 ConstructSpdySynReplyError("404 Not Found", kExtraHeaders, | |
5794 arraysize(kExtraHeaders)/2, 1)); | |
5795 scoped_ptr<SpdyFrame> body( | |
5796 ConstructSpdyBodyFrame(1, "The host does not exist", 23, true)); | |
5797 MockRead data_reads[] = { | |
5798 CreateMockRead(*resp.get(), 1, SYNCHRONOUS), | |
5799 CreateMockRead(*body.get(), 2, SYNCHRONOUS), | |
5800 MockRead(ASYNC, 0, 4), // EOF | |
5801 }; | |
5802 | |
5803 DelayedSocketData data( | |
5804 1, // wait for one write to finish before reading. | |
5805 data_reads, arraysize(data_reads), | |
5806 data_writes, arraysize(data_writes)); | |
5807 SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy | |
5808 proxy_ssl.SetNextProto(kProtoSPDY2); | |
5809 | |
5810 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
5811 session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); | |
5812 | |
5813 TestCompletionCallback callback; | |
5814 | |
5815 scoped_ptr<HttpTransaction> trans( | |
5816 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
5817 CreateSession(&session_deps_))); | |
5818 | |
5819 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
5820 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5821 | |
5822 rv = callback.WaitForResult(); | |
5823 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); | |
5824 | |
5825 // TODO(ttuttle): Anything else to check here? | |
5826 } | |
5827 | |
5828 // Test the request-challenge-retry sequence for basic auth, through | |
5829 // a SPDY proxy over a single SPDY session. | |
5830 TEST_F(HttpNetworkTransactionSpdy2Test, BasicAuthSpdyProxy) { | |
5831 HttpRequestInfo request; | |
5832 request.method = "GET"; | |
5833 request.url = GURL("https://www.google.com/"); | |
5834 // when the no authentication data flag is set. | |
5835 request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; | |
5836 | |
5837 // Configure against https proxy server "myproxy:70". | |
5838 session_deps_.proxy_service.reset( | |
5839 ProxyService::CreateFixedFromPacResult("HTTPS myproxy:70")); | |
5840 CapturingBoundNetLog log; | |
5841 session_deps_.net_log = log.bound().net_log(); | |
5842 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
5843 | |
5844 // Since we have proxy, should try to establish tunnel. | |
5845 scoped_ptr<SpdyFrame> req(spdy_util_.ConstructSpdyConnect(NULL, 0, 1)); | |
5846 scoped_ptr<SpdyFrame> rst( | |
5847 spdy_util_.ConstructSpdyRstStream(1, RST_STREAM_CANCEL)); | |
5848 | |
5849 // After calling trans->RestartWithAuth(), this is the request we should | |
5850 // be issuing -- the final header line contains the credentials. | |
5851 const char* const kAuthCredentials[] = { | |
5852 "proxy-authorization", "Basic Zm9vOmJhcg==", | |
5853 }; | |
5854 scoped_ptr<SpdyFrame> connect2(spdy_util_.ConstructSpdyConnect( | |
5855 kAuthCredentials, arraysize(kAuthCredentials) / 2, 3)); | |
5856 // fetch https://www.google.com/ via HTTP | |
5857 const char get[] = "GET / HTTP/1.1\r\n" | |
5858 "Host: www.google.com\r\n" | |
5859 "Connection: keep-alive\r\n\r\n"; | |
5860 scoped_ptr<SpdyFrame> wrapped_get( | |
5861 ConstructSpdyBodyFrame(3, get, strlen(get), false)); | |
5862 | |
5863 MockWrite spdy_writes[] = { | |
5864 CreateMockWrite(*req, 1, ASYNC), | |
5865 CreateMockWrite(*rst, 4, ASYNC), | |
5866 CreateMockWrite(*connect2, 5), | |
5867 CreateMockWrite(*wrapped_get, 8), | |
5868 }; | |
5869 | |
5870 // The proxy responds to the connect with a 407, using a persistent | |
5871 // connection. | |
5872 const char* const kAuthChallenge[] = { | |
5873 "status", "407 Proxy Authentication Required", | |
5874 "version", "HTTP/1.1", | |
5875 "proxy-authenticate", "Basic realm=\"MyRealm1\"", | |
5876 }; | |
5877 | |
5878 scoped_ptr<SpdyFrame> conn_auth_resp( | |
5879 spdy_util_.ConstructSpdyControlFrame(NULL, | |
5880 0, | |
5881 false, | |
5882 1, | |
5883 LOWEST, | |
5884 SYN_REPLY, | |
5885 CONTROL_FLAG_NONE, | |
5886 kAuthChallenge, | |
5887 arraysize(kAuthChallenge), | |
5888 0)); | |
5889 | |
5890 scoped_ptr<SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
5891 const char resp[] = "HTTP/1.1 200 OK\r\n" | |
5892 "Content-Length: 5\r\n\r\n"; | |
5893 | |
5894 scoped_ptr<SpdyFrame> wrapped_get_resp( | |
5895 ConstructSpdyBodyFrame(3, resp, strlen(resp), false)); | |
5896 scoped_ptr<SpdyFrame> wrapped_body( | |
5897 ConstructSpdyBodyFrame(3, "hello", 5, false)); | |
5898 MockRead spdy_reads[] = { | |
5899 CreateMockRead(*conn_auth_resp, 2, ASYNC), | |
5900 CreateMockRead(*conn_resp, 6, ASYNC), | |
5901 CreateMockRead(*wrapped_get_resp, 9, ASYNC), | |
5902 CreateMockRead(*wrapped_body, 10, ASYNC), | |
5903 MockRead(ASYNC, OK, 11), // EOF. May or may not be read. | |
5904 }; | |
5905 | |
5906 OrderedSocketData spdy_data( | |
5907 spdy_reads, arraysize(spdy_reads), | |
5908 spdy_writes, arraysize(spdy_writes)); | |
5909 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
5910 // Negotiate SPDY to the proxy | |
5911 SSLSocketDataProvider proxy(ASYNC, OK); | |
5912 proxy.SetNextProto(kProtoSPDY2); | |
5913 session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); | |
5914 // Vanilla SSL to the server | |
5915 SSLSocketDataProvider server(ASYNC, OK); | |
5916 session_deps_.socket_factory->AddSSLSocketDataProvider(&server); | |
5917 | |
5918 TestCompletionCallback callback1; | |
5919 | |
5920 scoped_ptr<HttpTransaction> trans( | |
5921 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
5922 | |
5923 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
5924 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5925 | |
5926 rv = callback1.WaitForResult(); | |
5927 EXPECT_EQ(OK, rv); | |
5928 net::CapturingNetLog::CapturedEntryList entries; | |
5929 log.GetEntries(&entries); | |
5930 size_t pos = ExpectLogContainsSomewhere( | |
5931 entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, | |
5932 NetLog::PHASE_NONE); | |
5933 ExpectLogContainsSomewhere( | |
5934 entries, pos, | |
5935 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, | |
5936 NetLog::PHASE_NONE); | |
5937 | |
5938 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
5939 ASSERT_TRUE(response != NULL); | |
5940 ASSERT_FALSE(response->headers.get() == NULL); | |
5941 EXPECT_EQ(407, response->headers->response_code()); | |
5942 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
5943 EXPECT_TRUE(response->auth_challenge.get() != NULL); | |
5944 EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); | |
5945 | |
5946 TestCompletionCallback callback2; | |
5947 | |
5948 rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), | |
5949 callback2.callback()); | |
5950 EXPECT_EQ(ERR_IO_PENDING, rv); | |
5951 | |
5952 rv = callback2.WaitForResult(); | |
5953 EXPECT_EQ(OK, rv); | |
5954 | |
5955 response = trans->GetResponseInfo(); | |
5956 ASSERT_TRUE(response != NULL); | |
5957 | |
5958 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
5959 EXPECT_EQ(200, response->headers->response_code()); | |
5960 EXPECT_EQ(5, response->headers->GetContentLength()); | |
5961 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
5962 | |
5963 // The password prompt info should not be set. | |
5964 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
5965 | |
5966 LoadTimingInfo load_timing_info; | |
5967 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
5968 TestLoadTimingNotReusedWithPac(load_timing_info, | |
5969 CONNECT_TIMING_HAS_SSL_TIMES); | |
5970 | |
5971 trans.reset(); | |
5972 session->CloseAllConnections(); | |
5973 } | |
5974 | |
5975 // Test that an explicitly trusted SPDY proxy can push a resource from an | |
5976 // origin that is different from that of its associated resource. | |
5977 TEST_F(HttpNetworkTransactionSpdy2Test, CrossOriginProxyPush) { | |
5978 HttpRequestInfo request; | |
5979 HttpRequestInfo push_request; | |
5980 | |
5981 static const unsigned char kPushBodyFrame[] = { | |
5982 0x00, 0x00, 0x00, 0x02, // header, ID | |
5983 0x01, 0x00, 0x00, 0x06, // FIN, length | |
5984 'p', 'u', 's', 'h', 'e', 'd' // "pushed" | |
5985 }; | |
5986 | |
5987 request.method = "GET"; | |
5988 request.url = GURL("http://www.google.com/"); | |
5989 push_request.method = "GET"; | |
5990 push_request.url = GURL("http://www.another-origin.com/foo.dat"); | |
5991 | |
5992 // Configure against https proxy server "myproxy:70". | |
5993 session_deps_.proxy_service.reset( | |
5994 ProxyService::CreateFixedFromPacResult("HTTPS myproxy:70")); | |
5995 CapturingBoundNetLog log; | |
5996 session_deps_.net_log = log.bound().net_log(); | |
5997 | |
5998 // Enable cross-origin push. | |
5999 session_deps_.trusted_spdy_proxy = "myproxy:70"; | |
6000 | |
6001 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
6002 | |
6003 scoped_ptr<SpdyFrame> stream1_syn( | |
6004 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, false)); | |
6005 | |
6006 MockWrite spdy_writes[] = { | |
6007 CreateMockWrite(*stream1_syn, 1, ASYNC), | |
6008 }; | |
6009 | |
6010 scoped_ptr<SpdyFrame> | |
6011 stream1_reply(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
6012 | |
6013 scoped_ptr<SpdyFrame> | |
6014 stream1_body(ConstructSpdyBodyFrame(1, true)); | |
6015 | |
6016 scoped_ptr<SpdyFrame> | |
6017 stream2_syn(ConstructSpdyPush(NULL, | |
6018 0, | |
6019 2, | |
6020 1, | |
6021 "http://www.another-origin.com/foo.dat")); | |
6022 | |
6023 MockRead spdy_reads[] = { | |
6024 CreateMockRead(*stream1_reply, 2, ASYNC), | |
6025 CreateMockRead(*stream2_syn, 3, ASYNC), | |
6026 CreateMockRead(*stream1_body, 4, ASYNC), | |
6027 MockRead(ASYNC, reinterpret_cast<const char*>(kPushBodyFrame), | |
6028 arraysize(kPushBodyFrame), 5), | |
6029 MockRead(ASYNC, ERR_IO_PENDING, 6), // Force a pause | |
6030 }; | |
6031 | |
6032 OrderedSocketData spdy_data( | |
6033 spdy_reads, arraysize(spdy_reads), | |
6034 spdy_writes, arraysize(spdy_writes)); | |
6035 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
6036 // Negotiate SPDY to the proxy | |
6037 SSLSocketDataProvider proxy(ASYNC, OK); | |
6038 proxy.SetNextProto(kProtoSPDY2); | |
6039 session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); | |
6040 | |
6041 scoped_ptr<HttpTransaction> trans( | |
6042 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
6043 TestCompletionCallback callback; | |
6044 int rv = trans->Start(&request, callback.callback(), log.bound()); | |
6045 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6046 | |
6047 rv = callback.WaitForResult(); | |
6048 EXPECT_EQ(OK, rv); | |
6049 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
6050 | |
6051 scoped_ptr<HttpTransaction> push_trans( | |
6052 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
6053 rv = push_trans->Start(&push_request, callback.callback(), log.bound()); | |
6054 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6055 | |
6056 rv = callback.WaitForResult(); | |
6057 EXPECT_EQ(OK, rv); | |
6058 const HttpResponseInfo* push_response = push_trans->GetResponseInfo(); | |
6059 | |
6060 ASSERT_TRUE(response != NULL); | |
6061 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
6062 | |
6063 EXPECT_EQ(200, response->headers->response_code()); | |
6064 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
6065 | |
6066 std::string response_data; | |
6067 rv = ReadTransaction(trans.get(), &response_data); | |
6068 EXPECT_EQ(OK, rv); | |
6069 EXPECT_EQ("hello!", response_data); | |
6070 | |
6071 LoadTimingInfo load_timing_info; | |
6072 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
6073 TestLoadTimingNotReusedWithPac(load_timing_info, | |
6074 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
6075 | |
6076 // Verify the pushed stream. | |
6077 EXPECT_TRUE(push_response->headers.get() != NULL); | |
6078 EXPECT_EQ(200, push_response->headers->response_code()); | |
6079 | |
6080 rv = ReadTransaction(push_trans.get(), &response_data); | |
6081 EXPECT_EQ(OK, rv); | |
6082 EXPECT_EQ("pushed", response_data); | |
6083 | |
6084 LoadTimingInfo push_load_timing_info; | |
6085 EXPECT_TRUE(push_trans->GetLoadTimingInfo(&push_load_timing_info)); | |
6086 TestLoadTimingReusedWithPac(push_load_timing_info); | |
6087 // The transactions should share a socket ID, despite being for different | |
6088 // origins. | |
6089 EXPECT_EQ(load_timing_info.socket_log_id, | |
6090 push_load_timing_info.socket_log_id); | |
6091 | |
6092 trans.reset(); | |
6093 push_trans.reset(); | |
6094 session->CloseAllConnections(); | |
6095 } | |
6096 | |
6097 // Test that an explicitly trusted SPDY proxy cannot push HTTPS content. | |
6098 TEST_F(HttpNetworkTransactionSpdy2Test, CrossOriginProxyPushCorrectness) { | |
6099 HttpRequestInfo request; | |
6100 | |
6101 request.method = "GET"; | |
6102 request.url = GURL("http://www.google.com/"); | |
6103 | |
6104 // Configure against https proxy server "myproxy:70". | |
6105 session_deps_.proxy_service.reset( | |
6106 ProxyService::CreateFixed("https://myproxy:70")); | |
6107 CapturingBoundNetLog log; | |
6108 session_deps_.net_log = log.bound().net_log(); | |
6109 | |
6110 // Enable cross-origin push. | |
6111 session_deps_.trusted_spdy_proxy = "myproxy:70"; | |
6112 | |
6113 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
6114 | |
6115 scoped_ptr<SpdyFrame> stream1_syn( | |
6116 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, false)); | |
6117 | |
6118 scoped_ptr<SpdyFrame> push_rst( | |
6119 spdy_util_.ConstructSpdyRstStream(2, RST_STREAM_REFUSED_STREAM)); | |
6120 | |
6121 MockWrite spdy_writes[] = { | |
6122 CreateMockWrite(*stream1_syn, 1, ASYNC), | |
6123 CreateMockWrite(*push_rst, 4), | |
6124 }; | |
6125 | |
6126 scoped_ptr<SpdyFrame> | |
6127 stream1_reply(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
6128 | |
6129 scoped_ptr<SpdyFrame> | |
6130 stream1_body(ConstructSpdyBodyFrame(1, true)); | |
6131 | |
6132 scoped_ptr<SpdyFrame> | |
6133 stream2_syn(ConstructSpdyPush(NULL, | |
6134 0, | |
6135 2, | |
6136 1, | |
6137 "https://www.another-origin.com/foo.dat")); | |
6138 | |
6139 MockRead spdy_reads[] = { | |
6140 CreateMockRead(*stream1_reply, 2, ASYNC), | |
6141 CreateMockRead(*stream2_syn, 3, ASYNC), | |
6142 CreateMockRead(*stream1_body, 5, ASYNC), | |
6143 MockRead(ASYNC, ERR_IO_PENDING, 6), // Force a pause | |
6144 }; | |
6145 | |
6146 OrderedSocketData spdy_data( | |
6147 spdy_reads, arraysize(spdy_reads), | |
6148 spdy_writes, arraysize(spdy_writes)); | |
6149 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
6150 // Negotiate SPDY to the proxy | |
6151 SSLSocketDataProvider proxy(ASYNC, OK); | |
6152 proxy.SetNextProto(kProtoSPDY2); | |
6153 session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); | |
6154 | |
6155 scoped_ptr<HttpTransaction> trans( | |
6156 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
6157 TestCompletionCallback callback; | |
6158 int rv = trans->Start(&request, callback.callback(), log.bound()); | |
6159 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6160 | |
6161 rv = callback.WaitForResult(); | |
6162 EXPECT_EQ(OK, rv); | |
6163 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
6164 | |
6165 ASSERT_TRUE(response != NULL); | |
6166 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
6167 | |
6168 EXPECT_EQ(200, response->headers->response_code()); | |
6169 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
6170 | |
6171 std::string response_data; | |
6172 rv = ReadTransaction(trans.get(), &response_data); | |
6173 EXPECT_EQ(OK, rv); | |
6174 EXPECT_EQ("hello!", response_data); | |
6175 | |
6176 trans.reset(); | |
6177 session->CloseAllConnections(); | |
6178 } | |
6179 | |
6180 // Test HTTPS connections to a site with a bad certificate, going through an | |
6181 // HTTPS proxy | |
6182 TEST_F(HttpNetworkTransactionSpdy2Test, HTTPSBadCertificateViaHttpsProxy) { | |
6183 session_deps_.proxy_service.reset(ProxyService::CreateFixed( | |
6184 "https://proxy:70")); | |
6185 | |
6186 HttpRequestInfo request; | |
6187 request.method = "GET"; | |
6188 request.url = GURL("https://www.google.com/"); | |
6189 request.load_flags = 0; | |
6190 | |
6191 // Attempt to fetch the URL from a server with a bad cert | |
6192 MockWrite bad_cert_writes[] = { | |
6193 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
6194 "Host: www.google.com\r\n" | |
6195 "Proxy-Connection: keep-alive\r\n\r\n"), | |
6196 }; | |
6197 | |
6198 MockRead bad_cert_reads[] = { | |
6199 MockRead("HTTP/1.0 200 Connected\r\n\r\n"), | |
6200 MockRead(SYNCHRONOUS, OK) | |
6201 }; | |
6202 | |
6203 // Attempt to fetch the URL with a good cert | |
6204 MockWrite good_data_writes[] = { | |
6205 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
6206 "Host: www.google.com\r\n" | |
6207 "Proxy-Connection: keep-alive\r\n\r\n"), | |
6208 MockWrite("GET / HTTP/1.1\r\n" | |
6209 "Host: www.google.com\r\n" | |
6210 "Connection: keep-alive\r\n\r\n"), | |
6211 }; | |
6212 | |
6213 MockRead good_cert_reads[] = { | |
6214 MockRead("HTTP/1.0 200 Connected\r\n\r\n"), | |
6215 MockRead("HTTP/1.0 200 OK\r\n"), | |
6216 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6217 MockRead("Content-Length: 100\r\n\r\n"), | |
6218 MockRead(SYNCHRONOUS, OK), | |
6219 }; | |
6220 | |
6221 StaticSocketDataProvider ssl_bad_certificate( | |
6222 bad_cert_reads, arraysize(bad_cert_reads), | |
6223 bad_cert_writes, arraysize(bad_cert_writes)); | |
6224 StaticSocketDataProvider data(good_cert_reads, arraysize(good_cert_reads), | |
6225 good_data_writes, arraysize(good_data_writes)); | |
6226 SSLSocketDataProvider ssl_bad(ASYNC, ERR_CERT_AUTHORITY_INVALID); | |
6227 SSLSocketDataProvider ssl(ASYNC, OK); | |
6228 | |
6229 // SSL to the proxy, then CONNECT request, then SSL with bad certificate | |
6230 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
6231 session_deps_.socket_factory->AddSocketDataProvider(&ssl_bad_certificate); | |
6232 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bad); | |
6233 | |
6234 // SSL to the proxy, then CONNECT request, then valid SSL certificate | |
6235 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
6236 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6237 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
6238 | |
6239 TestCompletionCallback callback; | |
6240 | |
6241 scoped_ptr<HttpTransaction> trans( | |
6242 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6243 CreateSession(&session_deps_))); | |
6244 | |
6245 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6246 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6247 | |
6248 rv = callback.WaitForResult(); | |
6249 EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); | |
6250 | |
6251 rv = trans->RestartIgnoringLastError(callback.callback()); | |
6252 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6253 | |
6254 rv = callback.WaitForResult(); | |
6255 EXPECT_EQ(OK, rv); | |
6256 | |
6257 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
6258 | |
6259 ASSERT_TRUE(response != NULL); | |
6260 EXPECT_EQ(100, response->headers->GetContentLength()); | |
6261 } | |
6262 | |
6263 TEST_F(HttpNetworkTransactionSpdy2Test, BuildRequest_UserAgent) { | |
6264 HttpRequestInfo request; | |
6265 request.method = "GET"; | |
6266 request.url = GURL("http://www.google.com/"); | |
6267 request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, | |
6268 "Chromium Ultra Awesome X Edition"); | |
6269 | |
6270 scoped_ptr<HttpTransaction> trans( | |
6271 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6272 CreateSession(&session_deps_))); | |
6273 | |
6274 MockWrite data_writes[] = { | |
6275 MockWrite("GET / HTTP/1.1\r\n" | |
6276 "Host: www.google.com\r\n" | |
6277 "Connection: keep-alive\r\n" | |
6278 "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), | |
6279 }; | |
6280 | |
6281 // Lastly, the server responds with the actual content. | |
6282 MockRead data_reads[] = { | |
6283 MockRead("HTTP/1.0 200 OK\r\n"), | |
6284 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6285 MockRead("Content-Length: 100\r\n\r\n"), | |
6286 MockRead(SYNCHRONOUS, OK), | |
6287 }; | |
6288 | |
6289 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6290 data_writes, arraysize(data_writes)); | |
6291 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6292 | |
6293 TestCompletionCallback callback; | |
6294 | |
6295 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6296 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6297 | |
6298 rv = callback.WaitForResult(); | |
6299 EXPECT_EQ(OK, rv); | |
6300 } | |
6301 | |
6302 TEST_F(HttpNetworkTransactionSpdy2Test, BuildRequest_UserAgentOverTunnel) { | |
6303 HttpRequestInfo request; | |
6304 request.method = "GET"; | |
6305 request.url = GURL("https://www.google.com/"); | |
6306 request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, | |
6307 "Chromium Ultra Awesome X Edition"); | |
6308 | |
6309 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
6310 scoped_ptr<HttpTransaction> trans( | |
6311 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6312 CreateSession(&session_deps_))); | |
6313 | |
6314 MockWrite data_writes[] = { | |
6315 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
6316 "Host: www.google.com\r\n" | |
6317 "Proxy-Connection: keep-alive\r\n" | |
6318 "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), | |
6319 }; | |
6320 MockRead data_reads[] = { | |
6321 // Return an error, so the transaction stops here (this test isn't | |
6322 // interested in the rest). | |
6323 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), | |
6324 MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
6325 MockRead("Proxy-Connection: close\r\n\r\n"), | |
6326 }; | |
6327 | |
6328 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6329 data_writes, arraysize(data_writes)); | |
6330 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6331 | |
6332 TestCompletionCallback callback; | |
6333 | |
6334 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6335 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6336 | |
6337 rv = callback.WaitForResult(); | |
6338 EXPECT_EQ(OK, rv); | |
6339 } | |
6340 | |
6341 TEST_F(HttpNetworkTransactionSpdy2Test, BuildRequest_Referer) { | |
6342 HttpRequestInfo request; | |
6343 request.method = "GET"; | |
6344 request.url = GURL("http://www.google.com/"); | |
6345 request.load_flags = 0; | |
6346 request.extra_headers.SetHeader(HttpRequestHeaders::kReferer, | |
6347 "http://the.previous.site.com/"); | |
6348 | |
6349 scoped_ptr<HttpTransaction> trans( | |
6350 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6351 CreateSession(&session_deps_))); | |
6352 | |
6353 MockWrite data_writes[] = { | |
6354 MockWrite("GET / HTTP/1.1\r\n" | |
6355 "Host: www.google.com\r\n" | |
6356 "Connection: keep-alive\r\n" | |
6357 "Referer: http://the.previous.site.com/\r\n\r\n"), | |
6358 }; | |
6359 | |
6360 // Lastly, the server responds with the actual content. | |
6361 MockRead data_reads[] = { | |
6362 MockRead("HTTP/1.0 200 OK\r\n"), | |
6363 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6364 MockRead("Content-Length: 100\r\n\r\n"), | |
6365 MockRead(SYNCHRONOUS, OK), | |
6366 }; | |
6367 | |
6368 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6369 data_writes, arraysize(data_writes)); | |
6370 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6371 | |
6372 TestCompletionCallback callback; | |
6373 | |
6374 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6375 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6376 | |
6377 rv = callback.WaitForResult(); | |
6378 EXPECT_EQ(OK, rv); | |
6379 } | |
6380 | |
6381 TEST_F(HttpNetworkTransactionSpdy2Test, BuildRequest_PostContentLengthZero) { | |
6382 HttpRequestInfo request; | |
6383 request.method = "POST"; | |
6384 request.url = GURL("http://www.google.com/"); | |
6385 | |
6386 scoped_ptr<HttpTransaction> trans( | |
6387 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6388 CreateSession(&session_deps_))); | |
6389 | |
6390 MockWrite data_writes[] = { | |
6391 MockWrite("POST / HTTP/1.1\r\n" | |
6392 "Host: www.google.com\r\n" | |
6393 "Connection: keep-alive\r\n" | |
6394 "Content-Length: 0\r\n\r\n"), | |
6395 }; | |
6396 | |
6397 // Lastly, the server responds with the actual content. | |
6398 MockRead data_reads[] = { | |
6399 MockRead("HTTP/1.0 200 OK\r\n"), | |
6400 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6401 MockRead("Content-Length: 100\r\n\r\n"), | |
6402 MockRead(SYNCHRONOUS, OK), | |
6403 }; | |
6404 | |
6405 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6406 data_writes, arraysize(data_writes)); | |
6407 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6408 | |
6409 TestCompletionCallback callback; | |
6410 | |
6411 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6412 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6413 | |
6414 rv = callback.WaitForResult(); | |
6415 EXPECT_EQ(OK, rv); | |
6416 } | |
6417 | |
6418 TEST_F(HttpNetworkTransactionSpdy2Test, BuildRequest_PutContentLengthZero) { | |
6419 HttpRequestInfo request; | |
6420 request.method = "PUT"; | |
6421 request.url = GURL("http://www.google.com/"); | |
6422 | |
6423 scoped_ptr<HttpTransaction> trans( | |
6424 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6425 CreateSession(&session_deps_))); | |
6426 | |
6427 MockWrite data_writes[] = { | |
6428 MockWrite("PUT / HTTP/1.1\r\n" | |
6429 "Host: www.google.com\r\n" | |
6430 "Connection: keep-alive\r\n" | |
6431 "Content-Length: 0\r\n\r\n"), | |
6432 }; | |
6433 | |
6434 // Lastly, the server responds with the actual content. | |
6435 MockRead data_reads[] = { | |
6436 MockRead("HTTP/1.0 200 OK\r\n"), | |
6437 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6438 MockRead("Content-Length: 100\r\n\r\n"), | |
6439 MockRead(SYNCHRONOUS, OK), | |
6440 }; | |
6441 | |
6442 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6443 data_writes, arraysize(data_writes)); | |
6444 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6445 | |
6446 TestCompletionCallback callback; | |
6447 | |
6448 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6449 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6450 | |
6451 rv = callback.WaitForResult(); | |
6452 EXPECT_EQ(OK, rv); | |
6453 } | |
6454 | |
6455 TEST_F(HttpNetworkTransactionSpdy2Test, BuildRequest_HeadContentLengthZero) { | |
6456 HttpRequestInfo request; | |
6457 request.method = "HEAD"; | |
6458 request.url = GURL("http://www.google.com/"); | |
6459 | |
6460 scoped_ptr<HttpTransaction> trans( | |
6461 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6462 CreateSession(&session_deps_))); | |
6463 | |
6464 MockWrite data_writes[] = { | |
6465 MockWrite("HEAD / HTTP/1.1\r\n" | |
6466 "Host: www.google.com\r\n" | |
6467 "Connection: keep-alive\r\n" | |
6468 "Content-Length: 0\r\n\r\n"), | |
6469 }; | |
6470 | |
6471 // Lastly, the server responds with the actual content. | |
6472 MockRead data_reads[] = { | |
6473 MockRead("HTTP/1.0 200 OK\r\n"), | |
6474 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6475 MockRead("Content-Length: 100\r\n\r\n"), | |
6476 MockRead(SYNCHRONOUS, OK), | |
6477 }; | |
6478 | |
6479 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6480 data_writes, arraysize(data_writes)); | |
6481 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6482 | |
6483 TestCompletionCallback callback; | |
6484 | |
6485 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6486 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6487 | |
6488 rv = callback.WaitForResult(); | |
6489 EXPECT_EQ(OK, rv); | |
6490 } | |
6491 | |
6492 TEST_F(HttpNetworkTransactionSpdy2Test, BuildRequest_CacheControlNoCache) { | |
6493 HttpRequestInfo request; | |
6494 request.method = "GET"; | |
6495 request.url = GURL("http://www.google.com/"); | |
6496 request.load_flags = LOAD_BYPASS_CACHE; | |
6497 | |
6498 scoped_ptr<HttpTransaction> trans( | |
6499 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6500 CreateSession(&session_deps_))); | |
6501 | |
6502 MockWrite data_writes[] = { | |
6503 MockWrite("GET / HTTP/1.1\r\n" | |
6504 "Host: www.google.com\r\n" | |
6505 "Connection: keep-alive\r\n" | |
6506 "Pragma: no-cache\r\n" | |
6507 "Cache-Control: no-cache\r\n\r\n"), | |
6508 }; | |
6509 | |
6510 // Lastly, the server responds with the actual content. | |
6511 MockRead data_reads[] = { | |
6512 MockRead("HTTP/1.0 200 OK\r\n"), | |
6513 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6514 MockRead("Content-Length: 100\r\n\r\n"), | |
6515 MockRead(SYNCHRONOUS, OK), | |
6516 }; | |
6517 | |
6518 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6519 data_writes, arraysize(data_writes)); | |
6520 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6521 | |
6522 TestCompletionCallback callback; | |
6523 | |
6524 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6525 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6526 | |
6527 rv = callback.WaitForResult(); | |
6528 EXPECT_EQ(OK, rv); | |
6529 } | |
6530 | |
6531 TEST_F(HttpNetworkTransactionSpdy2Test, | |
6532 BuildRequest_CacheControlValidateCache) { | |
6533 HttpRequestInfo request; | |
6534 request.method = "GET"; | |
6535 request.url = GURL("http://www.google.com/"); | |
6536 request.load_flags = LOAD_VALIDATE_CACHE; | |
6537 | |
6538 scoped_ptr<HttpTransaction> trans( | |
6539 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6540 CreateSession(&session_deps_))); | |
6541 | |
6542 MockWrite data_writes[] = { | |
6543 MockWrite("GET / HTTP/1.1\r\n" | |
6544 "Host: www.google.com\r\n" | |
6545 "Connection: keep-alive\r\n" | |
6546 "Cache-Control: max-age=0\r\n\r\n"), | |
6547 }; | |
6548 | |
6549 // Lastly, the server responds with the actual content. | |
6550 MockRead data_reads[] = { | |
6551 MockRead("HTTP/1.0 200 OK\r\n"), | |
6552 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6553 MockRead("Content-Length: 100\r\n\r\n"), | |
6554 MockRead(SYNCHRONOUS, OK), | |
6555 }; | |
6556 | |
6557 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6558 data_writes, arraysize(data_writes)); | |
6559 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6560 | |
6561 TestCompletionCallback callback; | |
6562 | |
6563 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6564 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6565 | |
6566 rv = callback.WaitForResult(); | |
6567 EXPECT_EQ(OK, rv); | |
6568 } | |
6569 | |
6570 TEST_F(HttpNetworkTransactionSpdy2Test, BuildRequest_ExtraHeaders) { | |
6571 HttpRequestInfo request; | |
6572 request.method = "GET"; | |
6573 request.url = GURL("http://www.google.com/"); | |
6574 request.extra_headers.SetHeader("FooHeader", "Bar"); | |
6575 | |
6576 scoped_ptr<HttpTransaction> trans( | |
6577 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6578 CreateSession(&session_deps_))); | |
6579 | |
6580 MockWrite data_writes[] = { | |
6581 MockWrite("GET / HTTP/1.1\r\n" | |
6582 "Host: www.google.com\r\n" | |
6583 "Connection: keep-alive\r\n" | |
6584 "FooHeader: Bar\r\n\r\n"), | |
6585 }; | |
6586 | |
6587 // Lastly, the server responds with the actual content. | |
6588 MockRead data_reads[] = { | |
6589 MockRead("HTTP/1.0 200 OK\r\n"), | |
6590 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6591 MockRead("Content-Length: 100\r\n\r\n"), | |
6592 MockRead(SYNCHRONOUS, OK), | |
6593 }; | |
6594 | |
6595 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6596 data_writes, arraysize(data_writes)); | |
6597 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6598 | |
6599 TestCompletionCallback callback; | |
6600 | |
6601 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6602 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6603 | |
6604 rv = callback.WaitForResult(); | |
6605 EXPECT_EQ(OK, rv); | |
6606 } | |
6607 | |
6608 TEST_F(HttpNetworkTransactionSpdy2Test, BuildRequest_ExtraHeadersStripped) { | |
6609 HttpRequestInfo request; | |
6610 request.method = "GET"; | |
6611 request.url = GURL("http://www.google.com/"); | |
6612 request.extra_headers.SetHeader("referer", "www.foo.com"); | |
6613 request.extra_headers.SetHeader("hEllo", "Kitty"); | |
6614 request.extra_headers.SetHeader("FoO", "bar"); | |
6615 | |
6616 scoped_ptr<HttpTransaction> trans( | |
6617 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6618 CreateSession(&session_deps_))); | |
6619 | |
6620 MockWrite data_writes[] = { | |
6621 MockWrite("GET / HTTP/1.1\r\n" | |
6622 "Host: www.google.com\r\n" | |
6623 "Connection: keep-alive\r\n" | |
6624 "referer: www.foo.com\r\n" | |
6625 "hEllo: Kitty\r\n" | |
6626 "FoO: bar\r\n\r\n"), | |
6627 }; | |
6628 | |
6629 // Lastly, the server responds with the actual content. | |
6630 MockRead data_reads[] = { | |
6631 MockRead("HTTP/1.0 200 OK\r\n"), | |
6632 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
6633 MockRead("Content-Length: 100\r\n\r\n"), | |
6634 MockRead(SYNCHRONOUS, OK), | |
6635 }; | |
6636 | |
6637 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6638 data_writes, arraysize(data_writes)); | |
6639 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6640 | |
6641 TestCompletionCallback callback; | |
6642 | |
6643 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6644 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6645 | |
6646 rv = callback.WaitForResult(); | |
6647 EXPECT_EQ(OK, rv); | |
6648 } | |
6649 | |
6650 TEST_F(HttpNetworkTransactionSpdy2Test, SOCKS4_HTTP_GET) { | |
6651 HttpRequestInfo request; | |
6652 request.method = "GET"; | |
6653 request.url = GURL("http://www.google.com/"); | |
6654 request.load_flags = 0; | |
6655 | |
6656 session_deps_.proxy_service.reset( | |
6657 ProxyService::CreateFixedFromPacResult("SOCKS myproxy:1080")); | |
6658 CapturingNetLog net_log; | |
6659 session_deps_.net_log = &net_log; | |
6660 | |
6661 scoped_ptr<HttpTransaction> trans( | |
6662 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6663 CreateSession(&session_deps_))); | |
6664 | |
6665 char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; | |
6666 char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; | |
6667 | |
6668 MockWrite data_writes[] = { | |
6669 MockWrite(ASYNC, write_buffer, arraysize(write_buffer)), | |
6670 MockWrite("GET / HTTP/1.1\r\n" | |
6671 "Host: www.google.com\r\n" | |
6672 "Connection: keep-alive\r\n\r\n") | |
6673 }; | |
6674 | |
6675 MockRead data_reads[] = { | |
6676 MockRead(ASYNC, read_buffer, arraysize(read_buffer)), | |
6677 MockRead("HTTP/1.0 200 OK\r\n"), | |
6678 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), | |
6679 MockRead("Payload"), | |
6680 MockRead(SYNCHRONOUS, OK) | |
6681 }; | |
6682 | |
6683 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6684 data_writes, arraysize(data_writes)); | |
6685 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6686 | |
6687 TestCompletionCallback callback; | |
6688 | |
6689 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6690 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6691 | |
6692 rv = callback.WaitForResult(); | |
6693 EXPECT_EQ(OK, rv); | |
6694 | |
6695 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
6696 ASSERT_TRUE(response != NULL); | |
6697 | |
6698 LoadTimingInfo load_timing_info; | |
6699 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
6700 TestLoadTimingNotReusedWithPac(load_timing_info, | |
6701 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
6702 | |
6703 std::string response_text; | |
6704 rv = ReadTransaction(trans.get(), &response_text); | |
6705 EXPECT_EQ(OK, rv); | |
6706 EXPECT_EQ("Payload", response_text); | |
6707 } | |
6708 | |
6709 TEST_F(HttpNetworkTransactionSpdy2Test, SOCKS4_SSL_GET) { | |
6710 HttpRequestInfo request; | |
6711 request.method = "GET"; | |
6712 request.url = GURL("https://www.google.com/"); | |
6713 request.load_flags = 0; | |
6714 | |
6715 session_deps_.proxy_service.reset( | |
6716 ProxyService::CreateFixedFromPacResult("SOCKS myproxy:1080")); | |
6717 CapturingNetLog net_log; | |
6718 session_deps_.net_log = &net_log; | |
6719 | |
6720 scoped_ptr<HttpTransaction> trans( | |
6721 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6722 CreateSession(&session_deps_))); | |
6723 | |
6724 unsigned char write_buffer[] = { 0x04, 0x01, 0x01, 0xBB, 127, 0, 0, 1, 0 }; | |
6725 unsigned char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; | |
6726 | |
6727 MockWrite data_writes[] = { | |
6728 MockWrite(ASYNC, reinterpret_cast<char*>(write_buffer), | |
6729 arraysize(write_buffer)), | |
6730 MockWrite("GET / HTTP/1.1\r\n" | |
6731 "Host: www.google.com\r\n" | |
6732 "Connection: keep-alive\r\n\r\n") | |
6733 }; | |
6734 | |
6735 MockRead data_reads[] = { | |
6736 MockRead(ASYNC, reinterpret_cast<char*>(read_buffer), | |
6737 arraysize(read_buffer)), | |
6738 MockRead("HTTP/1.0 200 OK\r\n"), | |
6739 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), | |
6740 MockRead("Payload"), | |
6741 MockRead(SYNCHRONOUS, OK) | |
6742 }; | |
6743 | |
6744 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6745 data_writes, arraysize(data_writes)); | |
6746 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6747 | |
6748 SSLSocketDataProvider ssl(ASYNC, OK); | |
6749 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
6750 | |
6751 TestCompletionCallback callback; | |
6752 | |
6753 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6754 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6755 | |
6756 rv = callback.WaitForResult(); | |
6757 EXPECT_EQ(OK, rv); | |
6758 | |
6759 LoadTimingInfo load_timing_info; | |
6760 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
6761 TestLoadTimingNotReusedWithPac(load_timing_info, | |
6762 CONNECT_TIMING_HAS_SSL_TIMES); | |
6763 | |
6764 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
6765 ASSERT_TRUE(response != NULL); | |
6766 | |
6767 std::string response_text; | |
6768 rv = ReadTransaction(trans.get(), &response_text); | |
6769 EXPECT_EQ(OK, rv); | |
6770 EXPECT_EQ("Payload", response_text); | |
6771 } | |
6772 | |
6773 TEST_F(HttpNetworkTransactionSpdy2Test, SOCKS4_HTTP_GET_no_PAC) { | |
6774 HttpRequestInfo request; | |
6775 request.method = "GET"; | |
6776 request.url = GURL("http://www.google.com/"); | |
6777 request.load_flags = 0; | |
6778 | |
6779 session_deps_.proxy_service.reset( | |
6780 ProxyService::CreateFixed("socks4://myproxy:1080")); | |
6781 CapturingNetLog net_log; | |
6782 session_deps_.net_log = &net_log; | |
6783 | |
6784 scoped_ptr<HttpTransaction> trans( | |
6785 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6786 CreateSession(&session_deps_))); | |
6787 | |
6788 char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; | |
6789 char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; | |
6790 | |
6791 MockWrite data_writes[] = { | |
6792 MockWrite(ASYNC, write_buffer, arraysize(write_buffer)), | |
6793 MockWrite("GET / HTTP/1.1\r\n" | |
6794 "Host: www.google.com\r\n" | |
6795 "Connection: keep-alive\r\n\r\n") | |
6796 }; | |
6797 | |
6798 MockRead data_reads[] = { | |
6799 MockRead(ASYNC, read_buffer, arraysize(read_buffer)), | |
6800 MockRead("HTTP/1.0 200 OK\r\n"), | |
6801 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), | |
6802 MockRead("Payload"), | |
6803 MockRead(SYNCHRONOUS, OK) | |
6804 }; | |
6805 | |
6806 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6807 data_writes, arraysize(data_writes)); | |
6808 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6809 | |
6810 TestCompletionCallback callback; | |
6811 | |
6812 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6813 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6814 | |
6815 rv = callback.WaitForResult(); | |
6816 EXPECT_EQ(OK, rv); | |
6817 | |
6818 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
6819 ASSERT_TRUE(response != NULL); | |
6820 | |
6821 LoadTimingInfo load_timing_info; | |
6822 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
6823 TestLoadTimingNotReused(load_timing_info, | |
6824 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
6825 | |
6826 std::string response_text; | |
6827 rv = ReadTransaction(trans.get(), &response_text); | |
6828 EXPECT_EQ(OK, rv); | |
6829 EXPECT_EQ("Payload", response_text); | |
6830 } | |
6831 | |
6832 TEST_F(HttpNetworkTransactionSpdy2Test, SOCKS5_HTTP_GET) { | |
6833 HttpRequestInfo request; | |
6834 request.method = "GET"; | |
6835 request.url = GURL("http://www.google.com/"); | |
6836 request.load_flags = 0; | |
6837 | |
6838 session_deps_.proxy_service.reset( | |
6839 ProxyService::CreateFixedFromPacResult("SOCKS5 myproxy:1080")); | |
6840 CapturingNetLog net_log; | |
6841 session_deps_.net_log = &net_log; | |
6842 | |
6843 scoped_ptr<HttpTransaction> trans( | |
6844 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6845 CreateSession(&session_deps_))); | |
6846 | |
6847 const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; | |
6848 const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; | |
6849 const char kSOCKS5OkRequest[] = { | |
6850 0x05, // Version | |
6851 0x01, // Command (CONNECT) | |
6852 0x00, // Reserved. | |
6853 0x03, // Address type (DOMAINNAME). | |
6854 0x0E, // Length of domain (14) | |
6855 // Domain string: | |
6856 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', | |
6857 0x00, 0x50, // 16-bit port (80) | |
6858 }; | |
6859 const char kSOCKS5OkResponse[] = | |
6860 { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; | |
6861 | |
6862 MockWrite data_writes[] = { | |
6863 MockWrite(ASYNC, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), | |
6864 MockWrite(ASYNC, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)), | |
6865 MockWrite("GET / HTTP/1.1\r\n" | |
6866 "Host: www.google.com\r\n" | |
6867 "Connection: keep-alive\r\n\r\n") | |
6868 }; | |
6869 | |
6870 MockRead data_reads[] = { | |
6871 MockRead(ASYNC, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), | |
6872 MockRead(ASYNC, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), | |
6873 MockRead("HTTP/1.0 200 OK\r\n"), | |
6874 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), | |
6875 MockRead("Payload"), | |
6876 MockRead(SYNCHRONOUS, OK) | |
6877 }; | |
6878 | |
6879 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6880 data_writes, arraysize(data_writes)); | |
6881 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6882 | |
6883 TestCompletionCallback callback; | |
6884 | |
6885 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6886 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6887 | |
6888 rv = callback.WaitForResult(); | |
6889 EXPECT_EQ(OK, rv); | |
6890 | |
6891 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
6892 ASSERT_TRUE(response != NULL); | |
6893 | |
6894 LoadTimingInfo load_timing_info; | |
6895 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
6896 TestLoadTimingNotReusedWithPac(load_timing_info, | |
6897 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
6898 | |
6899 std::string response_text; | |
6900 rv = ReadTransaction(trans.get(), &response_text); | |
6901 EXPECT_EQ(OK, rv); | |
6902 EXPECT_EQ("Payload", response_text); | |
6903 } | |
6904 | |
6905 TEST_F(HttpNetworkTransactionSpdy2Test, SOCKS5_SSL_GET) { | |
6906 HttpRequestInfo request; | |
6907 request.method = "GET"; | |
6908 request.url = GURL("https://www.google.com/"); | |
6909 request.load_flags = 0; | |
6910 | |
6911 session_deps_.proxy_service.reset( | |
6912 ProxyService::CreateFixedFromPacResult("SOCKS5 myproxy:1080")); | |
6913 CapturingNetLog net_log; | |
6914 session_deps_.net_log = &net_log; | |
6915 | |
6916 scoped_ptr<HttpTransaction> trans( | |
6917 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
6918 CreateSession(&session_deps_))); | |
6919 | |
6920 const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; | |
6921 const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; | |
6922 const unsigned char kSOCKS5OkRequest[] = { | |
6923 0x05, // Version | |
6924 0x01, // Command (CONNECT) | |
6925 0x00, // Reserved. | |
6926 0x03, // Address type (DOMAINNAME). | |
6927 0x0E, // Length of domain (14) | |
6928 // Domain string: | |
6929 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', | |
6930 0x01, 0xBB, // 16-bit port (443) | |
6931 }; | |
6932 | |
6933 const char kSOCKS5OkResponse[] = | |
6934 { 0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0x00, 0x00 }; | |
6935 | |
6936 MockWrite data_writes[] = { | |
6937 MockWrite(ASYNC, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), | |
6938 MockWrite(ASYNC, reinterpret_cast<const char*>(kSOCKS5OkRequest), | |
6939 arraysize(kSOCKS5OkRequest)), | |
6940 MockWrite("GET / HTTP/1.1\r\n" | |
6941 "Host: www.google.com\r\n" | |
6942 "Connection: keep-alive\r\n\r\n") | |
6943 }; | |
6944 | |
6945 MockRead data_reads[] = { | |
6946 MockRead(ASYNC, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), | |
6947 MockRead(ASYNC, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), | |
6948 MockRead("HTTP/1.0 200 OK\r\n"), | |
6949 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), | |
6950 MockRead("Payload"), | |
6951 MockRead(SYNCHRONOUS, OK) | |
6952 }; | |
6953 | |
6954 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
6955 data_writes, arraysize(data_writes)); | |
6956 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
6957 | |
6958 SSLSocketDataProvider ssl(ASYNC, OK); | |
6959 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
6960 | |
6961 TestCompletionCallback callback; | |
6962 | |
6963 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6964 EXPECT_EQ(ERR_IO_PENDING, rv); | |
6965 | |
6966 rv = callback.WaitForResult(); | |
6967 EXPECT_EQ(OK, rv); | |
6968 | |
6969 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
6970 ASSERT_TRUE(response != NULL); | |
6971 | |
6972 LoadTimingInfo load_timing_info; | |
6973 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
6974 TestLoadTimingNotReusedWithPac(load_timing_info, | |
6975 CONNECT_TIMING_HAS_SSL_TIMES); | |
6976 | |
6977 std::string response_text; | |
6978 rv = ReadTransaction(trans.get(), &response_text); | |
6979 EXPECT_EQ(OK, rv); | |
6980 EXPECT_EQ("Payload", response_text); | |
6981 } | |
6982 | |
6983 namespace { | |
6984 | |
6985 // Tests that for connection endpoints the group names are correctly set. | |
6986 | |
6987 struct GroupNameTest { | |
6988 std::string proxy_server; | |
6989 std::string url; | |
6990 std::string expected_group_name; | |
6991 bool ssl; | |
6992 }; | |
6993 | |
6994 scoped_refptr<HttpNetworkSession> SetupSessionForGroupNameTests( | |
6995 SpdySessionDependencies* session_deps_) { | |
6996 scoped_refptr<HttpNetworkSession> session(CreateSession(session_deps_)); | |
6997 | |
6998 HttpServerProperties* http_server_properties = | |
6999 session->http_server_properties(); | |
7000 http_server_properties->SetAlternateProtocol( | |
7001 HostPortPair("host.with.alternate", 80), 443, | |
7002 NPN_SPDY_2); | |
7003 | |
7004 return session; | |
7005 } | |
7006 | |
7007 int GroupNameTransactionHelper( | |
7008 const std::string& url, | |
7009 const scoped_refptr<HttpNetworkSession>& session) { | |
7010 HttpRequestInfo request; | |
7011 request.method = "GET"; | |
7012 request.url = GURL(url); | |
7013 request.load_flags = 0; | |
7014 | |
7015 scoped_ptr<HttpTransaction> trans( | |
7016 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
7017 | |
7018 TestCompletionCallback callback; | |
7019 | |
7020 // We do not complete this request, the dtor will clean the transaction up. | |
7021 return trans->Start(&request, callback.callback(), BoundNetLog()); | |
7022 } | |
7023 | |
7024 } // namespace | |
7025 | |
7026 TEST_F(HttpNetworkTransactionSpdy2Test, GroupNameForDirectConnections) { | |
7027 const GroupNameTest tests[] = { | |
7028 { | |
7029 "", // unused | |
7030 "http://www.google.com/direct", | |
7031 "www.google.com:80", | |
7032 false, | |
7033 }, | |
7034 { | |
7035 "", // unused | |
7036 "http://[2001:1418:13:1::25]/direct", | |
7037 "[2001:1418:13:1::25]:80", | |
7038 false, | |
7039 }, | |
7040 | |
7041 // SSL Tests | |
7042 { | |
7043 "", // unused | |
7044 "https://www.google.com/direct_ssl", | |
7045 "ssl/www.google.com:443", | |
7046 true, | |
7047 }, | |
7048 { | |
7049 "", // unused | |
7050 "https://[2001:1418:13:1::25]/direct", | |
7051 "ssl/[2001:1418:13:1::25]:443", | |
7052 true, | |
7053 }, | |
7054 { | |
7055 "", // unused | |
7056 "http://host.with.alternate/direct", | |
7057 "ssl/host.with.alternate:443", | |
7058 true, | |
7059 }, | |
7060 }; | |
7061 | |
7062 HttpStreamFactory::set_use_alternate_protocols(true); | |
7063 | |
7064 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | |
7065 session_deps_.proxy_service.reset( | |
7066 ProxyService::CreateFixed(tests[i].proxy_server)); | |
7067 scoped_refptr<HttpNetworkSession> session( | |
7068 SetupSessionForGroupNameTests(&session_deps_)); | |
7069 | |
7070 HttpNetworkSessionPeer peer(session); | |
7071 CaptureGroupNameTransportSocketPool* transport_conn_pool = | |
7072 new CaptureGroupNameTransportSocketPool(NULL, NULL); | |
7073 CaptureGroupNameSSLSocketPool* ssl_conn_pool = | |
7074 new CaptureGroupNameSSLSocketPool(NULL, NULL); | |
7075 MockClientSocketPoolManager* mock_pool_manager = | |
7076 new MockClientSocketPoolManager; | |
7077 mock_pool_manager->SetTransportSocketPool(transport_conn_pool); | |
7078 mock_pool_manager->SetSSLSocketPool(ssl_conn_pool); | |
7079 peer.SetClientSocketPoolManager(mock_pool_manager); | |
7080 | |
7081 EXPECT_EQ(ERR_IO_PENDING, | |
7082 GroupNameTransactionHelper(tests[i].url, session)); | |
7083 if (tests[i].ssl) | |
7084 EXPECT_EQ(tests[i].expected_group_name, | |
7085 ssl_conn_pool->last_group_name_received()); | |
7086 else | |
7087 EXPECT_EQ(tests[i].expected_group_name, | |
7088 transport_conn_pool->last_group_name_received()); | |
7089 } | |
7090 | |
7091 } | |
7092 | |
7093 TEST_F(HttpNetworkTransactionSpdy2Test, GroupNameForHTTPProxyConnections) { | |
7094 const GroupNameTest tests[] = { | |
7095 { | |
7096 "http_proxy", | |
7097 "http://www.google.com/http_proxy_normal", | |
7098 "www.google.com:80", | |
7099 false, | |
7100 }, | |
7101 | |
7102 // SSL Tests | |
7103 { | |
7104 "http_proxy", | |
7105 "https://www.google.com/http_connect_ssl", | |
7106 "ssl/www.google.com:443", | |
7107 true, | |
7108 }, | |
7109 | |
7110 { | |
7111 "http_proxy", | |
7112 "http://host.with.alternate/direct", | |
7113 "ssl/host.with.alternate:443", | |
7114 true, | |
7115 }, | |
7116 | |
7117 { | |
7118 "http_proxy", | |
7119 "ftp://ftp.google.com/http_proxy_normal", | |
7120 "ftp/ftp.google.com:21", | |
7121 false, | |
7122 }, | |
7123 }; | |
7124 | |
7125 HttpStreamFactory::set_use_alternate_protocols(true); | |
7126 | |
7127 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | |
7128 session_deps_.proxy_service.reset( | |
7129 ProxyService::CreateFixed(tests[i].proxy_server)); | |
7130 scoped_refptr<HttpNetworkSession> session( | |
7131 SetupSessionForGroupNameTests(&session_deps_)); | |
7132 | |
7133 HttpNetworkSessionPeer peer(session); | |
7134 | |
7135 HostPortPair proxy_host("http_proxy", 80); | |
7136 CaptureGroupNameHttpProxySocketPool* http_proxy_pool = | |
7137 new CaptureGroupNameHttpProxySocketPool(NULL, NULL); | |
7138 CaptureGroupNameSSLSocketPool* ssl_conn_pool = | |
7139 new CaptureGroupNameSSLSocketPool(NULL, NULL); | |
7140 | |
7141 MockClientSocketPoolManager* mock_pool_manager = | |
7142 new MockClientSocketPoolManager; | |
7143 mock_pool_manager->SetSocketPoolForHTTPProxy(proxy_host, http_proxy_pool); | |
7144 mock_pool_manager->SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); | |
7145 peer.SetClientSocketPoolManager(mock_pool_manager); | |
7146 | |
7147 EXPECT_EQ(ERR_IO_PENDING, | |
7148 GroupNameTransactionHelper(tests[i].url, session)); | |
7149 if (tests[i].ssl) | |
7150 EXPECT_EQ(tests[i].expected_group_name, | |
7151 ssl_conn_pool->last_group_name_received()); | |
7152 else | |
7153 EXPECT_EQ(tests[i].expected_group_name, | |
7154 http_proxy_pool->last_group_name_received()); | |
7155 } | |
7156 } | |
7157 | |
7158 TEST_F(HttpNetworkTransactionSpdy2Test, GroupNameForSOCKSConnections) { | |
7159 const GroupNameTest tests[] = { | |
7160 { | |
7161 "socks4://socks_proxy:1080", | |
7162 "http://www.google.com/socks4_direct", | |
7163 "socks4/www.google.com:80", | |
7164 false, | |
7165 }, | |
7166 { | |
7167 "socks5://socks_proxy:1080", | |
7168 "http://www.google.com/socks5_direct", | |
7169 "socks5/www.google.com:80", | |
7170 false, | |
7171 }, | |
7172 | |
7173 // SSL Tests | |
7174 { | |
7175 "socks4://socks_proxy:1080", | |
7176 "https://www.google.com/socks4_ssl", | |
7177 "socks4/ssl/www.google.com:443", | |
7178 true, | |
7179 }, | |
7180 { | |
7181 "socks5://socks_proxy:1080", | |
7182 "https://www.google.com/socks5_ssl", | |
7183 "socks5/ssl/www.google.com:443", | |
7184 true, | |
7185 }, | |
7186 | |
7187 { | |
7188 "socks4://socks_proxy:1080", | |
7189 "http://host.with.alternate/direct", | |
7190 "socks4/ssl/host.with.alternate:443", | |
7191 true, | |
7192 }, | |
7193 }; | |
7194 | |
7195 HttpStreamFactory::set_use_alternate_protocols(true); | |
7196 | |
7197 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | |
7198 session_deps_.proxy_service.reset( | |
7199 ProxyService::CreateFixed(tests[i].proxy_server)); | |
7200 scoped_refptr<HttpNetworkSession> session( | |
7201 SetupSessionForGroupNameTests(&session_deps_)); | |
7202 | |
7203 HttpNetworkSessionPeer peer(session); | |
7204 | |
7205 HostPortPair proxy_host("socks_proxy", 1080); | |
7206 CaptureGroupNameSOCKSSocketPool* socks_conn_pool = | |
7207 new CaptureGroupNameSOCKSSocketPool(NULL, NULL); | |
7208 CaptureGroupNameSSLSocketPool* ssl_conn_pool = | |
7209 new CaptureGroupNameSSLSocketPool(NULL, NULL); | |
7210 | |
7211 MockClientSocketPoolManager* mock_pool_manager = | |
7212 new MockClientSocketPoolManager; | |
7213 mock_pool_manager->SetSocketPoolForSOCKSProxy(proxy_host, socks_conn_pool); | |
7214 mock_pool_manager->SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); | |
7215 peer.SetClientSocketPoolManager(mock_pool_manager); | |
7216 | |
7217 scoped_ptr<HttpTransaction> trans( | |
7218 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
7219 | |
7220 EXPECT_EQ(ERR_IO_PENDING, | |
7221 GroupNameTransactionHelper(tests[i].url, session)); | |
7222 if (tests[i].ssl) | |
7223 EXPECT_EQ(tests[i].expected_group_name, | |
7224 ssl_conn_pool->last_group_name_received()); | |
7225 else | |
7226 EXPECT_EQ(tests[i].expected_group_name, | |
7227 socks_conn_pool->last_group_name_received()); | |
7228 } | |
7229 } | |
7230 | |
7231 TEST_F(HttpNetworkTransactionSpdy2Test, ReconsiderProxyAfterFailedConnection) { | |
7232 HttpRequestInfo request; | |
7233 request.method = "GET"; | |
7234 request.url = GURL("http://www.google.com/"); | |
7235 | |
7236 session_deps_.proxy_service.reset( | |
7237 ProxyService::CreateFixed("myproxy:70;foobar:80")); | |
7238 | |
7239 // This simulates failure resolving all hostnames; that means we will fail | |
7240 // connecting to both proxies (myproxy:70 and foobar:80). | |
7241 session_deps_.host_resolver->rules()->AddSimulatedFailure("*"); | |
7242 | |
7243 scoped_ptr<HttpTransaction> trans( | |
7244 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7245 CreateSession(&session_deps_))); | |
7246 | |
7247 TestCompletionCallback callback; | |
7248 | |
7249 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
7250 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7251 | |
7252 rv = callback.WaitForResult(); | |
7253 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, rv); | |
7254 } | |
7255 | |
7256 // Base test to make sure that when the load flags for a request specify to | |
7257 // bypass the cache, the DNS cache is not used. | |
7258 void HttpNetworkTransactionSpdy2Test::BypassHostCacheOnRefreshHelper( | |
7259 int load_flags) { | |
7260 // Issue a request, asking to bypass the cache(s). | |
7261 HttpRequestInfo request; | |
7262 request.method = "GET"; | |
7263 request.load_flags = load_flags; | |
7264 request.url = GURL("http://www.google.com/"); | |
7265 | |
7266 // Select a host resolver that does caching. | |
7267 session_deps_.host_resolver.reset(new MockCachingHostResolver); | |
7268 | |
7269 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7270 CreateSession(&session_deps_))); | |
7271 | |
7272 // Warm up the host cache so it has an entry for "www.google.com". | |
7273 AddressList addrlist; | |
7274 TestCompletionCallback callback; | |
7275 int rv = session_deps_.host_resolver->Resolve( | |
7276 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, | |
7277 callback.callback(), NULL, BoundNetLog()); | |
7278 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7279 rv = callback.WaitForResult(); | |
7280 EXPECT_EQ(OK, rv); | |
7281 | |
7282 // Verify that it was added to host cache, by doing a subsequent async lookup | |
7283 // and confirming it completes synchronously. | |
7284 rv = session_deps_.host_resolver->Resolve( | |
7285 HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, | |
7286 callback.callback(), NULL, BoundNetLog()); | |
7287 ASSERT_EQ(OK, rv); | |
7288 | |
7289 // Inject a failure the next time that "www.google.com" is resolved. This way | |
7290 // we can tell if the next lookup hit the cache, or the "network". | |
7291 // (cache --> success, "network" --> failure). | |
7292 session_deps_.host_resolver->rules()->AddSimulatedFailure("www.google.com"); | |
7293 | |
7294 // Connect up a mock socket which will fail with ERR_UNEXPECTED during the | |
7295 // first read -- this won't be reached as the host resolution will fail first. | |
7296 MockRead data_reads[] = { MockRead(SYNCHRONOUS, ERR_UNEXPECTED) }; | |
7297 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
7298 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
7299 | |
7300 // Run the request. | |
7301 rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
7302 ASSERT_EQ(ERR_IO_PENDING, rv); | |
7303 rv = callback.WaitForResult(); | |
7304 | |
7305 // If we bypassed the cache, we would have gotten a failure while resolving | |
7306 // "www.google.com". | |
7307 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | |
7308 } | |
7309 | |
7310 // There are multiple load flags that should trigger the host cache bypass. | |
7311 // Test each in isolation: | |
7312 TEST_F(HttpNetworkTransactionSpdy2Test, BypassHostCacheOnRefresh1) { | |
7313 BypassHostCacheOnRefreshHelper(LOAD_BYPASS_CACHE); | |
7314 } | |
7315 | |
7316 TEST_F(HttpNetworkTransactionSpdy2Test, BypassHostCacheOnRefresh2) { | |
7317 BypassHostCacheOnRefreshHelper(LOAD_VALIDATE_CACHE); | |
7318 } | |
7319 | |
7320 TEST_F(HttpNetworkTransactionSpdy2Test, BypassHostCacheOnRefresh3) { | |
7321 BypassHostCacheOnRefreshHelper(LOAD_DISABLE_CACHE); | |
7322 } | |
7323 | |
7324 // Make sure we can handle an error when writing the request. | |
7325 TEST_F(HttpNetworkTransactionSpdy2Test, RequestWriteError) { | |
7326 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
7327 | |
7328 HttpRequestInfo request; | |
7329 request.method = "GET"; | |
7330 request.url = GURL("http://www.foo.com/"); | |
7331 request.load_flags = 0; | |
7332 | |
7333 MockWrite write_failure[] = { | |
7334 MockWrite(ASYNC, ERR_CONNECTION_RESET), | |
7335 }; | |
7336 StaticSocketDataProvider data(NULL, 0, | |
7337 write_failure, arraysize(write_failure)); | |
7338 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
7339 | |
7340 TestCompletionCallback callback; | |
7341 | |
7342 scoped_ptr<HttpTransaction> trans( | |
7343 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7344 CreateSession(&session_deps_))); | |
7345 | |
7346 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
7347 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7348 | |
7349 rv = callback.WaitForResult(); | |
7350 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | |
7351 } | |
7352 | |
7353 // Check that a connection closed after the start of the headers finishes ok. | |
7354 TEST_F(HttpNetworkTransactionSpdy2Test, ConnectionClosedAfterStartOfHeaders) { | |
7355 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
7356 | |
7357 HttpRequestInfo request; | |
7358 request.method = "GET"; | |
7359 request.url = GURL("http://www.foo.com/"); | |
7360 request.load_flags = 0; | |
7361 | |
7362 MockRead data_reads[] = { | |
7363 MockRead("HTTP/1."), | |
7364 MockRead(SYNCHRONOUS, OK), | |
7365 }; | |
7366 | |
7367 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
7368 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
7369 | |
7370 TestCompletionCallback callback; | |
7371 | |
7372 scoped_ptr<HttpTransaction> trans( | |
7373 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7374 CreateSession(&session_deps_))); | |
7375 | |
7376 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
7377 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7378 | |
7379 rv = callback.WaitForResult(); | |
7380 EXPECT_EQ(OK, rv); | |
7381 | |
7382 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
7383 ASSERT_TRUE(response != NULL); | |
7384 | |
7385 EXPECT_TRUE(response->headers.get() != NULL); | |
7386 EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); | |
7387 | |
7388 std::string response_data; | |
7389 rv = ReadTransaction(trans.get(), &response_data); | |
7390 EXPECT_EQ(OK, rv); | |
7391 EXPECT_EQ("", response_data); | |
7392 } | |
7393 | |
7394 // Make sure that a dropped connection while draining the body for auth | |
7395 // restart does the right thing. | |
7396 TEST_F(HttpNetworkTransactionSpdy2Test, DrainResetOK) { | |
7397 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
7398 | |
7399 HttpRequestInfo request; | |
7400 request.method = "GET"; | |
7401 request.url = GURL("http://www.google.com/"); | |
7402 request.load_flags = 0; | |
7403 | |
7404 MockWrite data_writes1[] = { | |
7405 MockWrite("GET / HTTP/1.1\r\n" | |
7406 "Host: www.google.com\r\n" | |
7407 "Connection: keep-alive\r\n\r\n"), | |
7408 }; | |
7409 | |
7410 MockRead data_reads1[] = { | |
7411 MockRead("HTTP/1.1 401 Unauthorized\r\n"), | |
7412 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
7413 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
7414 MockRead("Content-Length: 14\r\n\r\n"), | |
7415 MockRead("Unauth"), | |
7416 MockRead(ASYNC, ERR_CONNECTION_RESET), | |
7417 }; | |
7418 | |
7419 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
7420 data_writes1, arraysize(data_writes1)); | |
7421 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
7422 | |
7423 // After calling trans->RestartWithAuth(), this is the request we should | |
7424 // be issuing -- the final header line contains the credentials. | |
7425 MockWrite data_writes2[] = { | |
7426 MockWrite("GET / HTTP/1.1\r\n" | |
7427 "Host: www.google.com\r\n" | |
7428 "Connection: keep-alive\r\n" | |
7429 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
7430 }; | |
7431 | |
7432 // Lastly, the server responds with the actual content. | |
7433 MockRead data_reads2[] = { | |
7434 MockRead("HTTP/1.1 200 OK\r\n"), | |
7435 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
7436 MockRead("Content-Length: 100\r\n\r\n"), | |
7437 MockRead(SYNCHRONOUS, OK), | |
7438 }; | |
7439 | |
7440 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
7441 data_writes2, arraysize(data_writes2)); | |
7442 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
7443 | |
7444 TestCompletionCallback callback1; | |
7445 | |
7446 scoped_ptr<HttpTransaction> trans( | |
7447 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
7448 | |
7449 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
7450 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7451 | |
7452 rv = callback1.WaitForResult(); | |
7453 EXPECT_EQ(OK, rv); | |
7454 | |
7455 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
7456 ASSERT_TRUE(response != NULL); | |
7457 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
7458 | |
7459 TestCompletionCallback callback2; | |
7460 | |
7461 rv = trans->RestartWithAuth( | |
7462 AuthCredentials(kFoo, kBar), callback2.callback()); | |
7463 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7464 | |
7465 rv = callback2.WaitForResult(); | |
7466 EXPECT_EQ(OK, rv); | |
7467 | |
7468 response = trans->GetResponseInfo(); | |
7469 ASSERT_TRUE(response != NULL); | |
7470 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
7471 EXPECT_EQ(100, response->headers->GetContentLength()); | |
7472 } | |
7473 | |
7474 // Test HTTPS connections going through a proxy that sends extra data. | |
7475 TEST_F(HttpNetworkTransactionSpdy2Test, HTTPSViaProxyWithExtraData) { | |
7476 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
7477 | |
7478 HttpRequestInfo request; | |
7479 request.method = "GET"; | |
7480 request.url = GURL("https://www.google.com/"); | |
7481 request.load_flags = 0; | |
7482 | |
7483 MockRead proxy_reads[] = { | |
7484 MockRead("HTTP/1.0 200 Connected\r\n\r\nExtra data"), | |
7485 MockRead(SYNCHRONOUS, OK) | |
7486 }; | |
7487 | |
7488 StaticSocketDataProvider data(proxy_reads, arraysize(proxy_reads), NULL, 0); | |
7489 SSLSocketDataProvider ssl(ASYNC, OK); | |
7490 | |
7491 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
7492 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
7493 | |
7494 TestCompletionCallback callback; | |
7495 | |
7496 session_deps_.socket_factory->ResetNextMockIndexes(); | |
7497 | |
7498 scoped_ptr<HttpTransaction> trans( | |
7499 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7500 CreateSession(&session_deps_))); | |
7501 | |
7502 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
7503 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7504 | |
7505 rv = callback.WaitForResult(); | |
7506 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); | |
7507 } | |
7508 | |
7509 TEST_F(HttpNetworkTransactionSpdy2Test, LargeContentLengthThenClose) { | |
7510 HttpRequestInfo request; | |
7511 request.method = "GET"; | |
7512 request.url = GURL("http://www.google.com/"); | |
7513 request.load_flags = 0; | |
7514 | |
7515 scoped_ptr<HttpTransaction> trans( | |
7516 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7517 CreateSession(&session_deps_))); | |
7518 | |
7519 MockRead data_reads[] = { | |
7520 MockRead("HTTP/1.0 200 OK\r\nContent-Length:6719476739\r\n\r\n"), | |
7521 MockRead(SYNCHRONOUS, OK), | |
7522 }; | |
7523 | |
7524 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
7525 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
7526 | |
7527 TestCompletionCallback callback; | |
7528 | |
7529 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
7530 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7531 | |
7532 EXPECT_EQ(OK, callback.WaitForResult()); | |
7533 | |
7534 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
7535 ASSERT_TRUE(response != NULL); | |
7536 | |
7537 EXPECT_TRUE(response->headers.get() != NULL); | |
7538 EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); | |
7539 | |
7540 std::string response_data; | |
7541 rv = ReadTransaction(trans.get(), &response_data); | |
7542 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, rv); | |
7543 } | |
7544 | |
7545 TEST_F(HttpNetworkTransactionSpdy2Test, UploadFileSmallerThanLength) { | |
7546 base::FilePath temp_file_path; | |
7547 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); | |
7548 const uint64 kFakeSize = 100000; // file is actually blank | |
7549 UploadFileElementReader::ScopedOverridingContentLengthForTests | |
7550 overriding_content_length(kFakeSize); | |
7551 | |
7552 ScopedVector<UploadElementReader> element_readers; | |
7553 element_readers.push_back( | |
7554 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | |
7555 temp_file_path, | |
7556 0, | |
7557 kuint64max, | |
7558 base::Time())); | |
7559 UploadDataStream upload_data_stream(&element_readers, 0); | |
7560 | |
7561 HttpRequestInfo request; | |
7562 request.method = "POST"; | |
7563 request.url = GURL("http://www.google.com/upload"); | |
7564 request.upload_data_stream = &upload_data_stream; | |
7565 request.load_flags = 0; | |
7566 | |
7567 scoped_ptr<HttpTransaction> trans( | |
7568 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7569 CreateSession(&session_deps_))); | |
7570 | |
7571 MockRead data_reads[] = { | |
7572 MockRead("HTTP/1.0 200 OK\r\n\r\n"), | |
7573 MockRead("hello world"), | |
7574 MockRead(SYNCHRONOUS, OK), | |
7575 }; | |
7576 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
7577 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
7578 | |
7579 TestCompletionCallback callback; | |
7580 | |
7581 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
7582 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7583 | |
7584 rv = callback.WaitForResult(); | |
7585 EXPECT_EQ(OK, rv); | |
7586 | |
7587 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
7588 ASSERT_TRUE(response != NULL); | |
7589 | |
7590 EXPECT_TRUE(response->headers.get() != NULL); | |
7591 EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); | |
7592 | |
7593 std::string response_data; | |
7594 rv = ReadTransaction(trans.get(), &response_data); | |
7595 EXPECT_EQ(OK, rv); | |
7596 EXPECT_EQ("hello world", response_data); | |
7597 | |
7598 file_util::Delete(temp_file_path, false); | |
7599 } | |
7600 | |
7601 TEST_F(HttpNetworkTransactionSpdy2Test, UploadUnreadableFile) { | |
7602 base::FilePath temp_file; | |
7603 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); | |
7604 std::string temp_file_content("Unreadable file."); | |
7605 ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_content.c_str(), | |
7606 temp_file_content.length())); | |
7607 ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); | |
7608 | |
7609 ScopedVector<UploadElementReader> element_readers; | |
7610 element_readers.push_back( | |
7611 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | |
7612 temp_file, | |
7613 0, | |
7614 kuint64max, | |
7615 base::Time())); | |
7616 UploadDataStream upload_data_stream(&element_readers, 0); | |
7617 | |
7618 HttpRequestInfo request; | |
7619 request.method = "POST"; | |
7620 request.url = GURL("http://www.google.com/upload"); | |
7621 request.upload_data_stream = &upload_data_stream; | |
7622 request.load_flags = 0; | |
7623 | |
7624 // If we try to upload an unreadable file, the network stack should report | |
7625 // the file size as zero and upload zero bytes for that file. | |
7626 scoped_ptr<HttpTransaction> trans( | |
7627 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7628 CreateSession(&session_deps_))); | |
7629 | |
7630 MockRead data_reads[] = { | |
7631 MockRead("HTTP/1.0 200 OK\r\n\r\n"), | |
7632 MockRead(SYNCHRONOUS, OK), | |
7633 }; | |
7634 MockWrite data_writes[] = { | |
7635 MockWrite("POST /upload HTTP/1.1\r\n" | |
7636 "Host: www.google.com\r\n" | |
7637 "Connection: keep-alive\r\n" | |
7638 "Content-Length: 0\r\n\r\n"), | |
7639 MockWrite(SYNCHRONOUS, OK), | |
7640 }; | |
7641 StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, | |
7642 arraysize(data_writes)); | |
7643 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
7644 | |
7645 TestCompletionCallback callback; | |
7646 | |
7647 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
7648 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7649 | |
7650 rv = callback.WaitForResult(); | |
7651 EXPECT_EQ(OK, rv); | |
7652 | |
7653 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
7654 ASSERT_TRUE(response != NULL); | |
7655 EXPECT_TRUE(response->headers.get() != NULL); | |
7656 EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); | |
7657 | |
7658 file_util::Delete(temp_file, false); | |
7659 } | |
7660 | |
7661 TEST_F(HttpNetworkTransactionSpdy2Test, UnreadableUploadFileAfterAuthRestart) { | |
7662 base::FilePath temp_file; | |
7663 ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); | |
7664 std::string temp_file_contents("Unreadable file."); | |
7665 std::string unreadable_contents(temp_file_contents.length(), '\0'); | |
7666 ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_contents.c_str(), | |
7667 temp_file_contents.length())); | |
7668 | |
7669 ScopedVector<UploadElementReader> element_readers; | |
7670 element_readers.push_back( | |
7671 new UploadFileElementReader(base::MessageLoopProxy::current().get(), | |
7672 temp_file, | |
7673 0, | |
7674 kuint64max, | |
7675 base::Time())); | |
7676 UploadDataStream upload_data_stream(&element_readers, 0); | |
7677 | |
7678 HttpRequestInfo request; | |
7679 request.method = "POST"; | |
7680 request.url = GURL("http://www.google.com/upload"); | |
7681 request.upload_data_stream = &upload_data_stream; | |
7682 request.load_flags = 0; | |
7683 | |
7684 scoped_ptr<HttpTransaction> trans( | |
7685 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7686 CreateSession(&session_deps_))); | |
7687 | |
7688 MockRead data_reads[] = { | |
7689 MockRead("HTTP/1.1 401 Unauthorized\r\n"), | |
7690 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), | |
7691 MockRead("Content-Length: 0\r\n\r\n"), // No response body. | |
7692 | |
7693 MockRead("HTTP/1.1 200 OK\r\n"), | |
7694 MockRead("Content-Length: 0\r\n\r\n"), | |
7695 MockRead(SYNCHRONOUS, OK), | |
7696 }; | |
7697 MockWrite data_writes[] = { | |
7698 MockWrite("POST /upload HTTP/1.1\r\n" | |
7699 "Host: www.google.com\r\n" | |
7700 "Connection: keep-alive\r\n" | |
7701 "Content-Length: 16\r\n\r\n"), | |
7702 MockWrite(SYNCHRONOUS, temp_file_contents.c_str()), | |
7703 | |
7704 MockWrite("POST /upload HTTP/1.1\r\n" | |
7705 "Host: www.google.com\r\n" | |
7706 "Connection: keep-alive\r\n" | |
7707 "Content-Length: 0\r\n" | |
7708 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
7709 MockWrite(SYNCHRONOUS, unreadable_contents.c_str(), | |
7710 temp_file_contents.length()), | |
7711 MockWrite(SYNCHRONOUS, OK), | |
7712 }; | |
7713 StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, | |
7714 arraysize(data_writes)); | |
7715 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
7716 | |
7717 TestCompletionCallback callback1; | |
7718 | |
7719 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
7720 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7721 | |
7722 rv = callback1.WaitForResult(); | |
7723 EXPECT_EQ(OK, rv); | |
7724 | |
7725 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
7726 ASSERT_TRUE(response != NULL); | |
7727 ASSERT_TRUE(response->headers.get() != NULL); | |
7728 EXPECT_EQ("HTTP/1.1 401 Unauthorized", response->headers->GetStatusLine()); | |
7729 EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); | |
7730 | |
7731 // Now make the file unreadable and try again. | |
7732 ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); | |
7733 | |
7734 TestCompletionCallback callback2; | |
7735 | |
7736 rv = trans->RestartWithAuth( | |
7737 AuthCredentials(kFoo, kBar), callback2.callback()); | |
7738 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7739 | |
7740 rv = callback2.WaitForResult(); | |
7741 EXPECT_EQ(OK, rv); | |
7742 | |
7743 response = trans->GetResponseInfo(); | |
7744 ASSERT_TRUE(response != NULL); | |
7745 EXPECT_TRUE(response->headers.get() != NULL); | |
7746 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
7747 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
7748 | |
7749 file_util::Delete(temp_file, false); | |
7750 } | |
7751 | |
7752 // Tests that changes to Auth realms are treated like auth rejections. | |
7753 TEST_F(HttpNetworkTransactionSpdy2Test, ChangeAuthRealms) { | |
7754 | |
7755 HttpRequestInfo request; | |
7756 request.method = "GET"; | |
7757 request.url = GURL("http://www.google.com/"); | |
7758 request.load_flags = 0; | |
7759 | |
7760 // First transaction will request a resource and receive a Basic challenge | |
7761 // with realm="first_realm". | |
7762 MockWrite data_writes1[] = { | |
7763 MockWrite("GET / HTTP/1.1\r\n" | |
7764 "Host: www.google.com\r\n" | |
7765 "Connection: keep-alive\r\n" | |
7766 "\r\n"), | |
7767 }; | |
7768 MockRead data_reads1[] = { | |
7769 MockRead("HTTP/1.1 401 Unauthorized\r\n" | |
7770 "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" | |
7771 "\r\n"), | |
7772 }; | |
7773 | |
7774 // After calling trans->RestartWithAuth(), provide an Authentication header | |
7775 // for first_realm. The server will reject and provide a challenge with | |
7776 // second_realm. | |
7777 MockWrite data_writes2[] = { | |
7778 MockWrite("GET / HTTP/1.1\r\n" | |
7779 "Host: www.google.com\r\n" | |
7780 "Connection: keep-alive\r\n" | |
7781 "Authorization: Basic Zmlyc3Q6YmF6\r\n" | |
7782 "\r\n"), | |
7783 }; | |
7784 MockRead data_reads2[] = { | |
7785 MockRead("HTTP/1.1 401 Unauthorized\r\n" | |
7786 "WWW-Authenticate: Basic realm=\"second_realm\"\r\n" | |
7787 "\r\n"), | |
7788 }; | |
7789 | |
7790 // This again fails, and goes back to first_realm. Make sure that the | |
7791 // entry is removed from cache. | |
7792 MockWrite data_writes3[] = { | |
7793 MockWrite("GET / HTTP/1.1\r\n" | |
7794 "Host: www.google.com\r\n" | |
7795 "Connection: keep-alive\r\n" | |
7796 "Authorization: Basic c2Vjb25kOmZvdQ==\r\n" | |
7797 "\r\n"), | |
7798 }; | |
7799 MockRead data_reads3[] = { | |
7800 MockRead("HTTP/1.1 401 Unauthorized\r\n" | |
7801 "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" | |
7802 "\r\n"), | |
7803 }; | |
7804 | |
7805 // Try one last time (with the correct password) and get the resource. | |
7806 MockWrite data_writes4[] = { | |
7807 MockWrite("GET / HTTP/1.1\r\n" | |
7808 "Host: www.google.com\r\n" | |
7809 "Connection: keep-alive\r\n" | |
7810 "Authorization: Basic Zmlyc3Q6YmFy\r\n" | |
7811 "\r\n"), | |
7812 }; | |
7813 MockRead data_reads4[] = { | |
7814 MockRead("HTTP/1.1 200 OK\r\n" | |
7815 "Content-Type: text/html; charset=iso-8859-1\r\n" | |
7816 "Content-Length: 5\r\n" | |
7817 "\r\n" | |
7818 "hello"), | |
7819 }; | |
7820 | |
7821 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
7822 data_writes1, arraysize(data_writes1)); | |
7823 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
7824 data_writes2, arraysize(data_writes2)); | |
7825 StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), | |
7826 data_writes3, arraysize(data_writes3)); | |
7827 StaticSocketDataProvider data4(data_reads4, arraysize(data_reads4), | |
7828 data_writes4, arraysize(data_writes4)); | |
7829 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
7830 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
7831 session_deps_.socket_factory->AddSocketDataProvider(&data3); | |
7832 session_deps_.socket_factory->AddSocketDataProvider(&data4); | |
7833 | |
7834 TestCompletionCallback callback1; | |
7835 | |
7836 scoped_ptr<HttpTransaction> trans( | |
7837 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
7838 CreateSession(&session_deps_))); | |
7839 | |
7840 // Issue the first request with Authorize headers. There should be a | |
7841 // password prompt for first_realm waiting to be filled in after the | |
7842 // transaction completes. | |
7843 int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); | |
7844 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7845 rv = callback1.WaitForResult(); | |
7846 EXPECT_EQ(OK, rv); | |
7847 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
7848 ASSERT_TRUE(response != NULL); | |
7849 const AuthChallengeInfo* challenge = response->auth_challenge.get(); | |
7850 ASSERT_FALSE(challenge == NULL); | |
7851 EXPECT_FALSE(challenge->is_proxy); | |
7852 EXPECT_EQ("www.google.com:80", challenge->challenger.ToString()); | |
7853 EXPECT_EQ("first_realm", challenge->realm); | |
7854 EXPECT_EQ("basic", challenge->scheme); | |
7855 | |
7856 // Issue the second request with an incorrect password. There should be a | |
7857 // password prompt for second_realm waiting to be filled in after the | |
7858 // transaction completes. | |
7859 TestCompletionCallback callback2; | |
7860 rv = trans->RestartWithAuth( | |
7861 AuthCredentials(kFirst, kBaz), callback2.callback()); | |
7862 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7863 rv = callback2.WaitForResult(); | |
7864 EXPECT_EQ(OK, rv); | |
7865 response = trans->GetResponseInfo(); | |
7866 ASSERT_TRUE(response != NULL); | |
7867 challenge = response->auth_challenge.get(); | |
7868 ASSERT_FALSE(challenge == NULL); | |
7869 EXPECT_FALSE(challenge->is_proxy); | |
7870 EXPECT_EQ("www.google.com:80", challenge->challenger.ToString()); | |
7871 EXPECT_EQ("second_realm", challenge->realm); | |
7872 EXPECT_EQ("basic", challenge->scheme); | |
7873 | |
7874 // Issue the third request with another incorrect password. There should be | |
7875 // a password prompt for first_realm waiting to be filled in. If the password | |
7876 // prompt is not present, it indicates that the HttpAuthCacheEntry for | |
7877 // first_realm was not correctly removed. | |
7878 TestCompletionCallback callback3; | |
7879 rv = trans->RestartWithAuth( | |
7880 AuthCredentials(kSecond, kFou), callback3.callback()); | |
7881 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7882 rv = callback3.WaitForResult(); | |
7883 EXPECT_EQ(OK, rv); | |
7884 response = trans->GetResponseInfo(); | |
7885 ASSERT_TRUE(response != NULL); | |
7886 challenge = response->auth_challenge.get(); | |
7887 ASSERT_FALSE(challenge == NULL); | |
7888 EXPECT_FALSE(challenge->is_proxy); | |
7889 EXPECT_EQ("www.google.com:80", challenge->challenger.ToString()); | |
7890 EXPECT_EQ("first_realm", challenge->realm); | |
7891 EXPECT_EQ("basic", challenge->scheme); | |
7892 | |
7893 // Issue the fourth request with the correct password and username. | |
7894 TestCompletionCallback callback4; | |
7895 rv = trans->RestartWithAuth( | |
7896 AuthCredentials(kFirst, kBar), callback4.callback()); | |
7897 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7898 rv = callback4.WaitForResult(); | |
7899 EXPECT_EQ(OK, rv); | |
7900 response = trans->GetResponseInfo(); | |
7901 ASSERT_TRUE(response != NULL); | |
7902 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
7903 } | |
7904 | |
7905 TEST_F(HttpNetworkTransactionSpdy2Test, HonorAlternateProtocolHeader) { | |
7906 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
7907 HttpStreamFactory::set_use_alternate_protocols(true); | |
7908 | |
7909 MockRead data_reads[] = { | |
7910 MockRead("HTTP/1.1 200 OK\r\n"), | |
7911 MockRead(kAlternateProtocolHttpHeader), | |
7912 MockRead("hello world"), | |
7913 MockRead(SYNCHRONOUS, OK), | |
7914 }; | |
7915 | |
7916 HttpRequestInfo request; | |
7917 request.method = "GET"; | |
7918 request.url = GURL("http://www.google.com/"); | |
7919 request.load_flags = 0; | |
7920 | |
7921 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
7922 | |
7923 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
7924 | |
7925 TestCompletionCallback callback; | |
7926 | |
7927 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
7928 scoped_ptr<HttpTransaction> trans( | |
7929 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
7930 | |
7931 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
7932 EXPECT_EQ(ERR_IO_PENDING, rv); | |
7933 | |
7934 HostPortPair http_host_port_pair("www.google.com", 80); | |
7935 const HttpServerProperties& http_server_properties = | |
7936 *session->http_server_properties(); | |
7937 EXPECT_FALSE( | |
7938 http_server_properties.HasAlternateProtocol(http_host_port_pair)); | |
7939 | |
7940 EXPECT_EQ(OK, callback.WaitForResult()); | |
7941 | |
7942 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
7943 ASSERT_TRUE(response != NULL); | |
7944 ASSERT_TRUE(response->headers.get() != NULL); | |
7945 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
7946 EXPECT_FALSE(response->was_fetched_via_spdy); | |
7947 EXPECT_FALSE(response->was_npn_negotiated); | |
7948 | |
7949 std::string response_data; | |
7950 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
7951 EXPECT_EQ("hello world", response_data); | |
7952 | |
7953 ASSERT_TRUE(http_server_properties.HasAlternateProtocol(http_host_port_pair)); | |
7954 const PortAlternateProtocolPair alternate = | |
7955 http_server_properties.GetAlternateProtocol(http_host_port_pair); | |
7956 PortAlternateProtocolPair expected_alternate; | |
7957 expected_alternate.port = 443; | |
7958 expected_alternate.protocol = NPN_SPDY_2; | |
7959 EXPECT_TRUE(expected_alternate.Equals(alternate)); | |
7960 | |
7961 HttpStreamFactory::SetNextProtos(std::vector<std::string>()); | |
7962 } | |
7963 | |
7964 TEST_F(HttpNetworkTransactionSpdy2Test, | |
7965 MarkBrokenAlternateProtocolAndFallback) { | |
7966 HttpStreamFactory::set_use_alternate_protocols(true); | |
7967 | |
7968 HttpRequestInfo request; | |
7969 request.method = "GET"; | |
7970 request.url = GURL("http://www.google.com/"); | |
7971 request.load_flags = 0; | |
7972 | |
7973 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); | |
7974 StaticSocketDataProvider first_data; | |
7975 first_data.set_connect_data(mock_connect); | |
7976 session_deps_.socket_factory->AddSocketDataProvider(&first_data); | |
7977 | |
7978 MockRead data_reads[] = { | |
7979 MockRead("HTTP/1.1 200 OK\r\n\r\n"), | |
7980 MockRead("hello world"), | |
7981 MockRead(ASYNC, OK), | |
7982 }; | |
7983 StaticSocketDataProvider second_data( | |
7984 data_reads, arraysize(data_reads), NULL, 0); | |
7985 session_deps_.socket_factory->AddSocketDataProvider(&second_data); | |
7986 | |
7987 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
7988 | |
7989 HttpServerProperties* http_server_properties = | |
7990 session->http_server_properties(); | |
7991 // Port must be < 1024, or the header will be ignored (since initial port was | |
7992 // port 80 (another restricted port). | |
7993 http_server_properties->SetAlternateProtocol( | |
7994 HostPortPair::FromURL(request.url), | |
7995 666 /* port is ignored by MockConnect anyway */, | |
7996 NPN_SPDY_2); | |
7997 | |
7998 scoped_ptr<HttpTransaction> trans( | |
7999 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8000 TestCompletionCallback callback; | |
8001 | |
8002 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8003 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8004 EXPECT_EQ(OK, callback.WaitForResult()); | |
8005 | |
8006 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
8007 ASSERT_TRUE(response != NULL); | |
8008 ASSERT_TRUE(response->headers.get() != NULL); | |
8009 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8010 | |
8011 std::string response_data; | |
8012 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8013 EXPECT_EQ("hello world", response_data); | |
8014 | |
8015 ASSERT_TRUE(http_server_properties->HasAlternateProtocol( | |
8016 HostPortPair::FromURL(request.url))); | |
8017 const PortAlternateProtocolPair alternate = | |
8018 http_server_properties->GetAlternateProtocol( | |
8019 HostPortPair::FromURL(request.url)); | |
8020 EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol); | |
8021 } | |
8022 | |
8023 TEST_F(HttpNetworkTransactionSpdy2Test, | |
8024 AlternateProtocolPortRestrictedBlocked) { | |
8025 // Ensure that we're not allowed to redirect traffic via an alternate | |
8026 // protocol to an unrestricted (port >= 1024) when the original traffic was | |
8027 // on a restricted port (port < 1024). Ensure that we can redirect in all | |
8028 // other cases. | |
8029 HttpStreamFactory::set_use_alternate_protocols(true); | |
8030 | |
8031 HttpRequestInfo restricted_port_request; | |
8032 restricted_port_request.method = "GET"; | |
8033 restricted_port_request.url = GURL("http://www.google.com:1023/"); | |
8034 restricted_port_request.load_flags = 0; | |
8035 | |
8036 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); | |
8037 StaticSocketDataProvider first_data; | |
8038 first_data.set_connect_data(mock_connect); | |
8039 session_deps_.socket_factory->AddSocketDataProvider(&first_data); | |
8040 | |
8041 MockRead data_reads[] = { | |
8042 MockRead("HTTP/1.1 200 OK\r\n\r\n"), | |
8043 MockRead("hello world"), | |
8044 MockRead(ASYNC, OK), | |
8045 }; | |
8046 StaticSocketDataProvider second_data( | |
8047 data_reads, arraysize(data_reads), NULL, 0); | |
8048 session_deps_.socket_factory->AddSocketDataProvider(&second_data); | |
8049 | |
8050 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8051 | |
8052 HttpServerProperties* http_server_properties = | |
8053 session->http_server_properties(); | |
8054 const int kUnrestrictedAlternatePort = 1024; | |
8055 http_server_properties->SetAlternateProtocol( | |
8056 HostPortPair::FromURL(restricted_port_request.url), | |
8057 kUnrestrictedAlternatePort, | |
8058 NPN_SPDY_2); | |
8059 | |
8060 scoped_ptr<HttpTransaction> trans( | |
8061 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8062 TestCompletionCallback callback; | |
8063 | |
8064 int rv = trans->Start( | |
8065 &restricted_port_request, | |
8066 callback.callback(), BoundNetLog()); | |
8067 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8068 // Invalid change to unrestricted port should fail. | |
8069 EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult()); | |
8070 } | |
8071 | |
8072 TEST_F(HttpNetworkTransactionSpdy2Test, | |
8073 AlternateProtocolPortRestrictedPermitted) { | |
8074 // Ensure that we're allowed to redirect traffic via an alternate | |
8075 // protocol to an unrestricted (port >= 1024) when the original traffic was | |
8076 // on a restricted port (port < 1024) if we set | |
8077 // enable_user_alternate_protocol_ports. | |
8078 | |
8079 HttpStreamFactory::set_use_alternate_protocols(true); | |
8080 session_deps_.enable_user_alternate_protocol_ports = true; | |
8081 | |
8082 HttpRequestInfo restricted_port_request; | |
8083 restricted_port_request.method = "GET"; | |
8084 restricted_port_request.url = GURL("http://www.google.com:1023/"); | |
8085 restricted_port_request.load_flags = 0; | |
8086 | |
8087 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); | |
8088 StaticSocketDataProvider first_data; | |
8089 first_data.set_connect_data(mock_connect); | |
8090 session_deps_.socket_factory->AddSocketDataProvider(&first_data); | |
8091 | |
8092 MockRead data_reads[] = { | |
8093 MockRead("HTTP/1.1 200 OK\r\n\r\n"), | |
8094 MockRead("hello world"), | |
8095 MockRead(ASYNC, OK), | |
8096 }; | |
8097 StaticSocketDataProvider second_data( | |
8098 data_reads, arraysize(data_reads), NULL, 0); | |
8099 session_deps_.socket_factory->AddSocketDataProvider(&second_data); | |
8100 | |
8101 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8102 | |
8103 HttpServerProperties* http_server_properties = | |
8104 session->http_server_properties(); | |
8105 const int kUnrestrictedAlternatePort = 1024; | |
8106 http_server_properties->SetAlternateProtocol( | |
8107 HostPortPair::FromURL(restricted_port_request.url), | |
8108 kUnrestrictedAlternatePort, | |
8109 NPN_SPDY_3); | |
8110 | |
8111 scoped_ptr<HttpTransaction> trans( | |
8112 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8113 TestCompletionCallback callback; | |
8114 | |
8115 EXPECT_EQ(ERR_IO_PENDING, trans->Start( | |
8116 &restricted_port_request, | |
8117 callback.callback(), BoundNetLog())); | |
8118 // Change to unrestricted port should succeed. | |
8119 EXPECT_EQ(OK, callback.WaitForResult()); | |
8120 } | |
8121 | |
8122 TEST_F(HttpNetworkTransactionSpdy2Test, | |
8123 AlternateProtocolPortRestrictedAllowed) { | |
8124 // Ensure that we're not allowed to redirect traffic via an alternate | |
8125 // protocol to an unrestricted (port >= 1024) when the original traffic was | |
8126 // on a restricted port (port < 1024). Ensure that we can redirect in all | |
8127 // other cases. | |
8128 HttpStreamFactory::set_use_alternate_protocols(true); | |
8129 | |
8130 HttpRequestInfo restricted_port_request; | |
8131 restricted_port_request.method = "GET"; | |
8132 restricted_port_request.url = GURL("http://www.google.com:1023/"); | |
8133 restricted_port_request.load_flags = 0; | |
8134 | |
8135 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); | |
8136 StaticSocketDataProvider first_data; | |
8137 first_data.set_connect_data(mock_connect); | |
8138 session_deps_.socket_factory->AddSocketDataProvider(&first_data); | |
8139 | |
8140 MockRead data_reads[] = { | |
8141 MockRead("HTTP/1.1 200 OK\r\n\r\n"), | |
8142 MockRead("hello world"), | |
8143 MockRead(ASYNC, OK), | |
8144 }; | |
8145 StaticSocketDataProvider second_data( | |
8146 data_reads, arraysize(data_reads), NULL, 0); | |
8147 session_deps_.socket_factory->AddSocketDataProvider(&second_data); | |
8148 | |
8149 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8150 | |
8151 HttpServerProperties* http_server_properties = | |
8152 session->http_server_properties(); | |
8153 const int kRestrictedAlternatePort = 80; | |
8154 http_server_properties->SetAlternateProtocol( | |
8155 HostPortPair::FromURL(restricted_port_request.url), | |
8156 kRestrictedAlternatePort, | |
8157 NPN_SPDY_2); | |
8158 | |
8159 scoped_ptr<HttpTransaction> trans( | |
8160 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8161 TestCompletionCallback callback; | |
8162 | |
8163 int rv = trans->Start( | |
8164 &restricted_port_request, | |
8165 callback.callback(), BoundNetLog()); | |
8166 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8167 // Valid change to restricted port should pass. | |
8168 EXPECT_EQ(OK, callback.WaitForResult()); | |
8169 } | |
8170 | |
8171 TEST_F(HttpNetworkTransactionSpdy2Test, | |
8172 AlternateProtocolPortUnrestrictedAllowed1) { | |
8173 // Ensure that we're not allowed to redirect traffic via an alternate | |
8174 // protocol to an unrestricted (port >= 1024) when the original traffic was | |
8175 // on a restricted port (port < 1024). Ensure that we can redirect in all | |
8176 // other cases. | |
8177 HttpStreamFactory::set_use_alternate_protocols(true); | |
8178 | |
8179 HttpRequestInfo unrestricted_port_request; | |
8180 unrestricted_port_request.method = "GET"; | |
8181 unrestricted_port_request.url = GURL("http://www.google.com:1024/"); | |
8182 unrestricted_port_request.load_flags = 0; | |
8183 | |
8184 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); | |
8185 StaticSocketDataProvider first_data; | |
8186 first_data.set_connect_data(mock_connect); | |
8187 session_deps_.socket_factory->AddSocketDataProvider(&first_data); | |
8188 | |
8189 MockRead data_reads[] = { | |
8190 MockRead("HTTP/1.1 200 OK\r\n\r\n"), | |
8191 MockRead("hello world"), | |
8192 MockRead(ASYNC, OK), | |
8193 }; | |
8194 StaticSocketDataProvider second_data( | |
8195 data_reads, arraysize(data_reads), NULL, 0); | |
8196 session_deps_.socket_factory->AddSocketDataProvider(&second_data); | |
8197 | |
8198 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8199 | |
8200 HttpServerProperties* http_server_properties = | |
8201 session->http_server_properties(); | |
8202 const int kRestrictedAlternatePort = 80; | |
8203 http_server_properties->SetAlternateProtocol( | |
8204 HostPortPair::FromURL(unrestricted_port_request.url), | |
8205 kRestrictedAlternatePort, | |
8206 NPN_SPDY_2); | |
8207 | |
8208 scoped_ptr<HttpTransaction> trans( | |
8209 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8210 TestCompletionCallback callback; | |
8211 | |
8212 int rv = trans->Start( | |
8213 &unrestricted_port_request, callback.callback(), BoundNetLog()); | |
8214 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8215 // Valid change to restricted port should pass. | |
8216 EXPECT_EQ(OK, callback.WaitForResult()); | |
8217 } | |
8218 | |
8219 TEST_F(HttpNetworkTransactionSpdy2Test, | |
8220 AlternateProtocolPortUnrestrictedAllowed2) { | |
8221 // Ensure that we're not allowed to redirect traffic via an alternate | |
8222 // protocol to an unrestricted (port >= 1024) when the original traffic was | |
8223 // on a restricted port (port < 1024). Ensure that we can redirect in all | |
8224 // other cases. | |
8225 HttpStreamFactory::set_use_alternate_protocols(true); | |
8226 | |
8227 HttpRequestInfo unrestricted_port_request; | |
8228 unrestricted_port_request.method = "GET"; | |
8229 unrestricted_port_request.url = GURL("http://www.google.com:1024/"); | |
8230 unrestricted_port_request.load_flags = 0; | |
8231 | |
8232 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); | |
8233 StaticSocketDataProvider first_data; | |
8234 first_data.set_connect_data(mock_connect); | |
8235 session_deps_.socket_factory->AddSocketDataProvider(&first_data); | |
8236 | |
8237 MockRead data_reads[] = { | |
8238 MockRead("HTTP/1.1 200 OK\r\n\r\n"), | |
8239 MockRead("hello world"), | |
8240 MockRead(ASYNC, OK), | |
8241 }; | |
8242 StaticSocketDataProvider second_data( | |
8243 data_reads, arraysize(data_reads), NULL, 0); | |
8244 session_deps_.socket_factory->AddSocketDataProvider(&second_data); | |
8245 | |
8246 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8247 | |
8248 HttpServerProperties* http_server_properties = | |
8249 session->http_server_properties(); | |
8250 const int kUnrestrictedAlternatePort = 1024; | |
8251 http_server_properties->SetAlternateProtocol( | |
8252 HostPortPair::FromURL(unrestricted_port_request.url), | |
8253 kUnrestrictedAlternatePort, | |
8254 NPN_SPDY_2); | |
8255 | |
8256 scoped_ptr<HttpTransaction> trans( | |
8257 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8258 TestCompletionCallback callback; | |
8259 | |
8260 int rv = trans->Start( | |
8261 &unrestricted_port_request, callback.callback(), BoundNetLog()); | |
8262 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8263 // Valid change to an unrestricted port should pass. | |
8264 EXPECT_EQ(OK, callback.WaitForResult()); | |
8265 } | |
8266 | |
8267 TEST_F(HttpNetworkTransactionSpdy2Test, | |
8268 AlternateProtocolUnsafeBlocked) { | |
8269 // Ensure that we're not allowed to redirect traffic via an alternate | |
8270 // protocol to an unsafe port, and that we resume the second | |
8271 // HttpStreamFactoryImpl::Job once the alternate protocol request fails. | |
8272 HttpStreamFactory::set_use_alternate_protocols(true); | |
8273 | |
8274 HttpRequestInfo request; | |
8275 request.method = "GET"; | |
8276 request.url = GURL("http://www.google.com/"); | |
8277 request.load_flags = 0; | |
8278 | |
8279 // The alternate protocol request will error out before we attempt to connect, | |
8280 // so only the standard HTTP request will try to connect. | |
8281 MockRead data_reads[] = { | |
8282 MockRead("HTTP/1.1 200 OK\r\n\r\n"), | |
8283 MockRead("hello world"), | |
8284 MockRead(ASYNC, OK), | |
8285 }; | |
8286 StaticSocketDataProvider data( | |
8287 data_reads, arraysize(data_reads), NULL, 0); | |
8288 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
8289 | |
8290 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8291 | |
8292 HttpServerProperties* http_server_properties = | |
8293 session->http_server_properties(); | |
8294 const int kUnsafePort = 7; | |
8295 http_server_properties->SetAlternateProtocol( | |
8296 HostPortPair::FromURL(request.url), | |
8297 kUnsafePort, | |
8298 NPN_SPDY_2); | |
8299 | |
8300 scoped_ptr<HttpTransaction> trans( | |
8301 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8302 TestCompletionCallback callback; | |
8303 | |
8304 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8305 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8306 // The HTTP request should succeed. | |
8307 EXPECT_EQ(OK, callback.WaitForResult()); | |
8308 | |
8309 // Disable alternate protocol before the asserts. | |
8310 HttpStreamFactory::set_use_alternate_protocols(false); | |
8311 | |
8312 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
8313 ASSERT_TRUE(response != NULL); | |
8314 ASSERT_TRUE(response->headers.get() != NULL); | |
8315 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8316 | |
8317 std::string response_data; | |
8318 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8319 EXPECT_EQ("hello world", response_data); | |
8320 } | |
8321 | |
8322 TEST_F(HttpNetworkTransactionSpdy2Test, UseAlternateProtocolForNpnSpdy) { | |
8323 HttpStreamFactory::set_use_alternate_protocols(true); | |
8324 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
8325 | |
8326 HttpRequestInfo request; | |
8327 request.method = "GET"; | |
8328 request.url = GURL("http://www.google.com/"); | |
8329 request.load_flags = 0; | |
8330 | |
8331 MockRead data_reads[] = { | |
8332 MockRead("HTTP/1.1 200 OK\r\n"), | |
8333 MockRead(kAlternateProtocolHttpHeader), | |
8334 MockRead("hello world"), | |
8335 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), | |
8336 MockRead(ASYNC, OK) | |
8337 }; | |
8338 | |
8339 StaticSocketDataProvider first_transaction( | |
8340 data_reads, arraysize(data_reads), NULL, 0); | |
8341 session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); | |
8342 | |
8343 SSLSocketDataProvider ssl(ASYNC, OK); | |
8344 ssl.SetNextProto(kProtoSPDY2); | |
8345 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
8346 | |
8347 scoped_ptr<SpdyFrame> req( | |
8348 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true)); | |
8349 MockWrite spdy_writes[] = { CreateMockWrite(*req) }; | |
8350 | |
8351 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
8352 scoped_ptr<SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); | |
8353 MockRead spdy_reads[] = { | |
8354 CreateMockRead(*resp), | |
8355 CreateMockRead(*data), | |
8356 MockRead(ASYNC, 0, 0), | |
8357 }; | |
8358 | |
8359 DelayedSocketData spdy_data( | |
8360 1, // wait for one write to finish before reading. | |
8361 spdy_reads, arraysize(spdy_reads), | |
8362 spdy_writes, arraysize(spdy_writes)); | |
8363 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
8364 | |
8365 MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); | |
8366 StaticSocketDataProvider hanging_non_alternate_protocol_socket( | |
8367 NULL, 0, NULL, 0); | |
8368 hanging_non_alternate_protocol_socket.set_connect_data( | |
8369 never_finishing_connect); | |
8370 session_deps_.socket_factory->AddSocketDataProvider( | |
8371 &hanging_non_alternate_protocol_socket); | |
8372 | |
8373 TestCompletionCallback callback; | |
8374 | |
8375 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8376 scoped_ptr<HttpTransaction> trans( | |
8377 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8378 | |
8379 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8380 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8381 EXPECT_EQ(OK, callback.WaitForResult()); | |
8382 | |
8383 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
8384 ASSERT_TRUE(response != NULL); | |
8385 ASSERT_TRUE(response->headers.get() != NULL); | |
8386 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8387 | |
8388 std::string response_data; | |
8389 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8390 EXPECT_EQ("hello world", response_data); | |
8391 | |
8392 trans.reset(new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8393 | |
8394 rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8395 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8396 EXPECT_EQ(OK, callback.WaitForResult()); | |
8397 | |
8398 response = trans->GetResponseInfo(); | |
8399 ASSERT_TRUE(response != NULL); | |
8400 ASSERT_TRUE(response->headers.get() != NULL); | |
8401 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8402 EXPECT_TRUE(response->was_fetched_via_spdy); | |
8403 EXPECT_TRUE(response->was_npn_negotiated); | |
8404 | |
8405 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8406 EXPECT_EQ("hello!", response_data); | |
8407 } | |
8408 | |
8409 TEST_F(HttpNetworkTransactionSpdy2Test, AlternateProtocolWithSpdyLateBinding) { | |
8410 HttpStreamFactory::set_use_alternate_protocols(true); | |
8411 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
8412 | |
8413 HttpRequestInfo request; | |
8414 request.method = "GET"; | |
8415 request.url = GURL("http://www.google.com/"); | |
8416 request.load_flags = 0; | |
8417 | |
8418 MockRead data_reads[] = { | |
8419 MockRead("HTTP/1.1 200 OK\r\n"), | |
8420 MockRead(kAlternateProtocolHttpHeader), | |
8421 MockRead("hello world"), | |
8422 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), | |
8423 MockRead(ASYNC, OK), | |
8424 }; | |
8425 | |
8426 StaticSocketDataProvider first_transaction( | |
8427 data_reads, arraysize(data_reads), NULL, 0); | |
8428 // Socket 1 is the HTTP transaction with the Alternate-Protocol header. | |
8429 session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); | |
8430 | |
8431 MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); | |
8432 StaticSocketDataProvider hanging_socket( | |
8433 NULL, 0, NULL, 0); | |
8434 hanging_socket.set_connect_data(never_finishing_connect); | |
8435 // Socket 2 and 3 are the hanging Alternate-Protocol and | |
8436 // non-Alternate-Protocol jobs from the 2nd transaction. | |
8437 session_deps_.socket_factory->AddSocketDataProvider(&hanging_socket); | |
8438 session_deps_.socket_factory->AddSocketDataProvider(&hanging_socket); | |
8439 | |
8440 SSLSocketDataProvider ssl(ASYNC, OK); | |
8441 ssl.SetNextProto(kProtoSPDY2); | |
8442 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
8443 | |
8444 scoped_ptr<SpdyFrame> req1( | |
8445 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true)); | |
8446 scoped_ptr<SpdyFrame> req2( | |
8447 spdy_util_.ConstructSpdyGet(NULL, 0, false, 3, LOWEST, true)); | |
8448 MockWrite spdy_writes[] = { | |
8449 CreateMockWrite(*req1), | |
8450 CreateMockWrite(*req2), | |
8451 }; | |
8452 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
8453 scoped_ptr<SpdyFrame> data1(ConstructSpdyBodyFrame(1, true)); | |
8454 scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
8455 scoped_ptr<SpdyFrame> data2(ConstructSpdyBodyFrame(3, true)); | |
8456 MockRead spdy_reads[] = { | |
8457 CreateMockRead(*resp1), | |
8458 CreateMockRead(*data1), | |
8459 CreateMockRead(*resp2), | |
8460 CreateMockRead(*data2), | |
8461 MockRead(ASYNC, 0, 0), | |
8462 }; | |
8463 | |
8464 DelayedSocketData spdy_data( | |
8465 2, // wait for writes to finish before reading. | |
8466 spdy_reads, arraysize(spdy_reads), | |
8467 spdy_writes, arraysize(spdy_writes)); | |
8468 // Socket 4 is the successful Alternate-Protocol for transaction 3. | |
8469 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
8470 | |
8471 // Socket 5 is the unsuccessful non-Alternate-Protocol for transaction 3. | |
8472 session_deps_.socket_factory->AddSocketDataProvider(&hanging_socket); | |
8473 | |
8474 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8475 TestCompletionCallback callback1; | |
8476 HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); | |
8477 | |
8478 int rv = trans1.Start(&request, callback1.callback(), BoundNetLog()); | |
8479 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8480 EXPECT_EQ(OK, callback1.WaitForResult()); | |
8481 | |
8482 const HttpResponseInfo* response = trans1.GetResponseInfo(); | |
8483 ASSERT_TRUE(response != NULL); | |
8484 ASSERT_TRUE(response->headers.get() != NULL); | |
8485 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8486 | |
8487 std::string response_data; | |
8488 ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); | |
8489 EXPECT_EQ("hello world", response_data); | |
8490 | |
8491 TestCompletionCallback callback2; | |
8492 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); | |
8493 rv = trans2.Start(&request, callback2.callback(), BoundNetLog()); | |
8494 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8495 | |
8496 TestCompletionCallback callback3; | |
8497 HttpNetworkTransaction trans3(DEFAULT_PRIORITY, session.get()); | |
8498 rv = trans3.Start(&request, callback3.callback(), BoundNetLog()); | |
8499 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8500 | |
8501 EXPECT_EQ(OK, callback2.WaitForResult()); | |
8502 EXPECT_EQ(OK, callback3.WaitForResult()); | |
8503 | |
8504 response = trans2.GetResponseInfo(); | |
8505 ASSERT_TRUE(response != NULL); | |
8506 ASSERT_TRUE(response->headers.get() != NULL); | |
8507 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8508 EXPECT_TRUE(response->was_fetched_via_spdy); | |
8509 EXPECT_TRUE(response->was_npn_negotiated); | |
8510 ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); | |
8511 EXPECT_EQ("hello!", response_data); | |
8512 | |
8513 response = trans3.GetResponseInfo(); | |
8514 ASSERT_TRUE(response != NULL); | |
8515 ASSERT_TRUE(response->headers.get() != NULL); | |
8516 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8517 EXPECT_TRUE(response->was_fetched_via_spdy); | |
8518 EXPECT_TRUE(response->was_npn_negotiated); | |
8519 ASSERT_EQ(OK, ReadTransaction(&trans3, &response_data)); | |
8520 EXPECT_EQ("hello!", response_data); | |
8521 } | |
8522 | |
8523 TEST_F(HttpNetworkTransactionSpdy2Test, StallAlternateProtocolForNpnSpdy) { | |
8524 HttpStreamFactory::set_use_alternate_protocols(true); | |
8525 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
8526 | |
8527 HttpRequestInfo request; | |
8528 request.method = "GET"; | |
8529 request.url = GURL("http://www.google.com/"); | |
8530 request.load_flags = 0; | |
8531 | |
8532 MockRead data_reads[] = { | |
8533 MockRead("HTTP/1.1 200 OK\r\n"), | |
8534 MockRead(kAlternateProtocolHttpHeader), | |
8535 MockRead("hello world"), | |
8536 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), | |
8537 MockRead(ASYNC, OK), | |
8538 }; | |
8539 | |
8540 StaticSocketDataProvider first_transaction( | |
8541 data_reads, arraysize(data_reads), NULL, 0); | |
8542 session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); | |
8543 | |
8544 SSLSocketDataProvider ssl(ASYNC, OK); | |
8545 ssl.SetNextProto(kProtoSPDY2); | |
8546 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
8547 | |
8548 MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); | |
8549 StaticSocketDataProvider hanging_alternate_protocol_socket( | |
8550 NULL, 0, NULL, 0); | |
8551 hanging_alternate_protocol_socket.set_connect_data( | |
8552 never_finishing_connect); | |
8553 session_deps_.socket_factory->AddSocketDataProvider( | |
8554 &hanging_alternate_protocol_socket); | |
8555 | |
8556 // 2nd request is just a copy of the first one, over HTTP again. | |
8557 session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); | |
8558 | |
8559 TestCompletionCallback callback; | |
8560 | |
8561 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8562 scoped_ptr<HttpTransaction> trans( | |
8563 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8564 | |
8565 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8566 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8567 EXPECT_EQ(OK, callback.WaitForResult()); | |
8568 | |
8569 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
8570 ASSERT_TRUE(response != NULL); | |
8571 ASSERT_TRUE(response->headers.get() != NULL); | |
8572 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8573 | |
8574 std::string response_data; | |
8575 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8576 EXPECT_EQ("hello world", response_data); | |
8577 | |
8578 trans.reset(new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8579 | |
8580 rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8581 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8582 EXPECT_EQ(OK, callback.WaitForResult()); | |
8583 | |
8584 response = trans->GetResponseInfo(); | |
8585 ASSERT_TRUE(response != NULL); | |
8586 ASSERT_TRUE(response->headers.get() != NULL); | |
8587 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8588 EXPECT_FALSE(response->was_fetched_via_spdy); | |
8589 EXPECT_FALSE(response->was_npn_negotiated); | |
8590 | |
8591 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8592 EXPECT_EQ("hello world", response_data); | |
8593 } | |
8594 | |
8595 class CapturingProxyResolver : public ProxyResolver { | |
8596 public: | |
8597 CapturingProxyResolver() : ProxyResolver(false /* expects_pac_bytes */) {} | |
8598 virtual ~CapturingProxyResolver() {} | |
8599 | |
8600 virtual int GetProxyForURL(const GURL& url, | |
8601 ProxyInfo* results, | |
8602 const CompletionCallback& callback, | |
8603 RequestHandle* request, | |
8604 const BoundNetLog& net_log) OVERRIDE { | |
8605 ProxyServer proxy_server(ProxyServer::SCHEME_HTTP, | |
8606 HostPortPair("myproxy", 80)); | |
8607 results->UseProxyServer(proxy_server); | |
8608 resolved_.push_back(url); | |
8609 return OK; | |
8610 } | |
8611 | |
8612 virtual void CancelRequest(RequestHandle request) OVERRIDE { | |
8613 NOTREACHED(); | |
8614 } | |
8615 | |
8616 virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE { | |
8617 NOTREACHED(); | |
8618 return LOAD_STATE_IDLE; | |
8619 } | |
8620 | |
8621 virtual void CancelSetPacScript() OVERRIDE { | |
8622 NOTREACHED(); | |
8623 } | |
8624 | |
8625 virtual int SetPacScript(const scoped_refptr<ProxyResolverScriptData>&, | |
8626 const CompletionCallback& /*callback*/) OVERRIDE { | |
8627 return OK; | |
8628 } | |
8629 | |
8630 const std::vector<GURL>& resolved() const { return resolved_; } | |
8631 | |
8632 private: | |
8633 std::vector<GURL> resolved_; | |
8634 | |
8635 DISALLOW_COPY_AND_ASSIGN(CapturingProxyResolver); | |
8636 }; | |
8637 | |
8638 TEST_F(HttpNetworkTransactionSpdy2Test, | |
8639 UseAlternateProtocolForTunneledNpnSpdy) { | |
8640 HttpStreamFactory::set_use_alternate_protocols(true); | |
8641 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
8642 | |
8643 ProxyConfig proxy_config; | |
8644 proxy_config.set_auto_detect(true); | |
8645 proxy_config.set_pac_url(GURL("http://fooproxyurl")); | |
8646 | |
8647 CapturingProxyResolver* capturing_proxy_resolver = | |
8648 new CapturingProxyResolver(); | |
8649 session_deps_.proxy_service.reset(new ProxyService( | |
8650 new ProxyConfigServiceFixed(proxy_config), capturing_proxy_resolver, | |
8651 NULL)); | |
8652 CapturingNetLog net_log; | |
8653 session_deps_.net_log = &net_log; | |
8654 | |
8655 HttpRequestInfo request; | |
8656 request.method = "GET"; | |
8657 request.url = GURL("http://www.google.com/"); | |
8658 request.load_flags = 0; | |
8659 | |
8660 MockRead data_reads[] = { | |
8661 MockRead("HTTP/1.1 200 OK\r\n"), | |
8662 MockRead(kAlternateProtocolHttpHeader), | |
8663 MockRead("hello world"), | |
8664 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), | |
8665 MockRead(ASYNC, OK), | |
8666 }; | |
8667 | |
8668 StaticSocketDataProvider first_transaction( | |
8669 data_reads, arraysize(data_reads), NULL, 0); | |
8670 session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); | |
8671 | |
8672 SSLSocketDataProvider ssl(ASYNC, OK); | |
8673 ssl.SetNextProto(kProtoSPDY2); | |
8674 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
8675 | |
8676 scoped_ptr<SpdyFrame> req( | |
8677 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true)); | |
8678 MockWrite spdy_writes[] = { | |
8679 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
8680 "Host: www.google.com\r\n" | |
8681 "Proxy-Connection: keep-alive\r\n\r\n"), // 0 | |
8682 CreateMockWrite(*req), // 3 | |
8683 }; | |
8684 | |
8685 const char kCONNECTResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; | |
8686 | |
8687 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
8688 scoped_ptr<SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); | |
8689 MockRead spdy_reads[] = { | |
8690 MockRead(ASYNC, kCONNECTResponse, arraysize(kCONNECTResponse) - 1, 1), // 1 | |
8691 CreateMockRead(*resp.get(), 4), // 2, 4 | |
8692 CreateMockRead(*data.get(), 4), // 5 | |
8693 MockRead(ASYNC, 0, 0, 4), // 6 | |
8694 }; | |
8695 | |
8696 OrderedSocketData spdy_data( | |
8697 spdy_reads, arraysize(spdy_reads), | |
8698 spdy_writes, arraysize(spdy_writes)); | |
8699 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
8700 | |
8701 MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); | |
8702 StaticSocketDataProvider hanging_non_alternate_protocol_socket( | |
8703 NULL, 0, NULL, 0); | |
8704 hanging_non_alternate_protocol_socket.set_connect_data( | |
8705 never_finishing_connect); | |
8706 session_deps_.socket_factory->AddSocketDataProvider( | |
8707 &hanging_non_alternate_protocol_socket); | |
8708 | |
8709 TestCompletionCallback callback; | |
8710 | |
8711 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8712 scoped_ptr<HttpTransaction> trans( | |
8713 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8714 | |
8715 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8716 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8717 EXPECT_EQ(OK, callback.WaitForResult()); | |
8718 | |
8719 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
8720 ASSERT_TRUE(response != NULL); | |
8721 ASSERT_TRUE(response->headers.get() != NULL); | |
8722 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8723 EXPECT_FALSE(response->was_fetched_via_spdy); | |
8724 EXPECT_FALSE(response->was_npn_negotiated); | |
8725 | |
8726 std::string response_data; | |
8727 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8728 EXPECT_EQ("hello world", response_data); | |
8729 | |
8730 trans.reset(new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8731 | |
8732 rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8733 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8734 EXPECT_EQ(OK, callback.WaitForResult()); | |
8735 | |
8736 response = trans->GetResponseInfo(); | |
8737 ASSERT_TRUE(response != NULL); | |
8738 ASSERT_TRUE(response->headers.get() != NULL); | |
8739 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8740 EXPECT_TRUE(response->was_fetched_via_spdy); | |
8741 EXPECT_TRUE(response->was_npn_negotiated); | |
8742 | |
8743 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8744 EXPECT_EQ("hello!", response_data); | |
8745 ASSERT_EQ(3u, capturing_proxy_resolver->resolved().size()); | |
8746 EXPECT_EQ("http://www.google.com/", | |
8747 capturing_proxy_resolver->resolved()[0].spec()); | |
8748 EXPECT_EQ("https://www.google.com/", | |
8749 capturing_proxy_resolver->resolved()[1].spec()); | |
8750 | |
8751 LoadTimingInfo load_timing_info; | |
8752 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
8753 TestLoadTimingNotReusedWithPac(load_timing_info, | |
8754 CONNECT_TIMING_HAS_SSL_TIMES); | |
8755 } | |
8756 | |
8757 TEST_F(HttpNetworkTransactionSpdy2Test, | |
8758 UseAlternateProtocolForNpnSpdyWithExistingSpdySession) { | |
8759 HttpStreamFactory::set_use_alternate_protocols(true); | |
8760 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
8761 | |
8762 HttpRequestInfo request; | |
8763 request.method = "GET"; | |
8764 request.url = GURL("http://www.google.com/"); | |
8765 request.load_flags = 0; | |
8766 | |
8767 MockRead data_reads[] = { | |
8768 MockRead("HTTP/1.1 200 OK\r\n"), | |
8769 MockRead(kAlternateProtocolHttpHeader), | |
8770 MockRead("hello world"), | |
8771 MockRead(ASYNC, OK), | |
8772 }; | |
8773 | |
8774 StaticSocketDataProvider first_transaction( | |
8775 data_reads, arraysize(data_reads), NULL, 0); | |
8776 session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); | |
8777 | |
8778 SSLSocketDataProvider ssl(ASYNC, OK); | |
8779 ssl.SetNextProto(kProtoSPDY2); | |
8780 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
8781 | |
8782 scoped_ptr<SpdyFrame> req( | |
8783 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true)); | |
8784 MockWrite spdy_writes[] = { CreateMockWrite(*req) }; | |
8785 | |
8786 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
8787 scoped_ptr<SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); | |
8788 MockRead spdy_reads[] = { | |
8789 CreateMockRead(*resp), | |
8790 CreateMockRead(*data), | |
8791 MockRead(ASYNC, 0, 0), | |
8792 }; | |
8793 | |
8794 DelayedSocketData spdy_data( | |
8795 1, // wait for one write to finish before reading. | |
8796 spdy_reads, arraysize(spdy_reads), | |
8797 spdy_writes, arraysize(spdy_writes)); | |
8798 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
8799 | |
8800 TestCompletionCallback callback; | |
8801 | |
8802 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
8803 | |
8804 scoped_ptr<HttpTransaction> trans( | |
8805 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8806 | |
8807 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8808 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8809 EXPECT_EQ(OK, callback.WaitForResult()); | |
8810 | |
8811 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
8812 ASSERT_TRUE(response != NULL); | |
8813 ASSERT_TRUE(response->headers.get() != NULL); | |
8814 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8815 | |
8816 std::string response_data; | |
8817 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8818 EXPECT_EQ("hello world", response_data); | |
8819 | |
8820 // Set up an initial SpdySession in the pool to reuse. | |
8821 HostPortPair host_port_pair("www.google.com", 443); | |
8822 SpdySessionKey key(host_port_pair, ProxyServer::Direct(), | |
8823 kPrivacyModeDisabled); | |
8824 scoped_refptr<SpdySession> spdy_session = | |
8825 session->spdy_session_pool()->Get(key, BoundNetLog()); | |
8826 scoped_refptr<TransportSocketParams> transport_params( | |
8827 new TransportSocketParams(host_port_pair, MEDIUM, false, false, | |
8828 OnHostResolutionCallback())); | |
8829 | |
8830 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | |
8831 EXPECT_EQ(ERR_IO_PENDING, | |
8832 connection->Init(host_port_pair.ToString(), | |
8833 transport_params, | |
8834 LOWEST, | |
8835 callback.callback(), | |
8836 session->GetTransportSocketPool( | |
8837 HttpNetworkSession::NORMAL_SOCKET_POOL), | |
8838 BoundNetLog())); | |
8839 EXPECT_EQ(OK, callback.WaitForResult()); | |
8840 | |
8841 SSLConfig ssl_config; | |
8842 session->ssl_config_service()->GetSSLConfig(&ssl_config); | |
8843 scoped_ptr<ClientSocketHandle> ssl_connection(new ClientSocketHandle); | |
8844 SSLClientSocketContext context; | |
8845 context.cert_verifier = session_deps_.cert_verifier.get(); | |
8846 context.transport_security_state = | |
8847 session_deps_.transport_security_state.get(); | |
8848 ssl_connection->set_socket( | |
8849 session_deps_.socket_factory->CreateSSLClientSocket( | |
8850 connection.release(), | |
8851 HostPortPair(std::string(), 443), | |
8852 ssl_config, | |
8853 context)); | |
8854 EXPECT_EQ(ERR_IO_PENDING, | |
8855 ssl_connection->socket()->Connect(callback.callback())); | |
8856 EXPECT_EQ(OK, callback.WaitForResult()); | |
8857 | |
8858 EXPECT_EQ(OK, spdy_session->InitializeWithSocket(ssl_connection.release(), | |
8859 true, OK)); | |
8860 | |
8861 trans.reset(new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
8862 | |
8863 rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
8864 EXPECT_EQ(ERR_IO_PENDING, rv); | |
8865 EXPECT_EQ(OK, callback.WaitForResult()); | |
8866 | |
8867 response = trans->GetResponseInfo(); | |
8868 ASSERT_TRUE(response != NULL); | |
8869 ASSERT_TRUE(response->headers.get() != NULL); | |
8870 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
8871 EXPECT_TRUE(response->was_fetched_via_spdy); | |
8872 EXPECT_TRUE(response->was_npn_negotiated); | |
8873 | |
8874 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
8875 EXPECT_EQ("hello!", response_data); | |
8876 } | |
8877 | |
8878 // GenerateAuthToken is a mighty big test. | |
8879 // It tests all permutation of GenerateAuthToken behavior: | |
8880 // - Synchronous and Asynchronous completion. | |
8881 // - OK or error on completion. | |
8882 // - Direct connection, non-authenticating proxy, and authenticating proxy. | |
8883 // - HTTP or HTTPS backend (to include proxy tunneling). | |
8884 // - Non-authenticating and authenticating backend. | |
8885 // | |
8886 // In all, there are 44 reasonable permuations (for example, if there are | |
8887 // problems generating an auth token for an authenticating proxy, we don't | |
8888 // need to test all permutations of the backend server). | |
8889 // | |
8890 // The test proceeds by going over each of the configuration cases, and | |
8891 // potentially running up to three rounds in each of the tests. The TestConfig | |
8892 // specifies both the configuration for the test as well as the expectations | |
8893 // for the results. | |
8894 TEST_F(HttpNetworkTransactionSpdy2Test, GenerateAuthToken) { | |
8895 static const char kServer[] = "http://www.example.com"; | |
8896 static const char kSecureServer[] = "https://www.example.com"; | |
8897 static const char kProxy[] = "myproxy:70"; | |
8898 const int kAuthErr = ERR_INVALID_AUTH_CREDENTIALS; | |
8899 | |
8900 enum AuthTiming { | |
8901 AUTH_NONE, | |
8902 AUTH_SYNC, | |
8903 AUTH_ASYNC, | |
8904 }; | |
8905 | |
8906 const MockWrite kGet( | |
8907 "GET / HTTP/1.1\r\n" | |
8908 "Host: www.example.com\r\n" | |
8909 "Connection: keep-alive\r\n\r\n"); | |
8910 const MockWrite kGetProxy( | |
8911 "GET http://www.example.com/ HTTP/1.1\r\n" | |
8912 "Host: www.example.com\r\n" | |
8913 "Proxy-Connection: keep-alive\r\n\r\n"); | |
8914 const MockWrite kGetAuth( | |
8915 "GET / HTTP/1.1\r\n" | |
8916 "Host: www.example.com\r\n" | |
8917 "Connection: keep-alive\r\n" | |
8918 "Authorization: auth_token\r\n\r\n"); | |
8919 const MockWrite kGetProxyAuth( | |
8920 "GET http://www.example.com/ HTTP/1.1\r\n" | |
8921 "Host: www.example.com\r\n" | |
8922 "Proxy-Connection: keep-alive\r\n" | |
8923 "Proxy-Authorization: auth_token\r\n\r\n"); | |
8924 const MockWrite kGetAuthThroughProxy( | |
8925 "GET http://www.example.com/ HTTP/1.1\r\n" | |
8926 "Host: www.example.com\r\n" | |
8927 "Proxy-Connection: keep-alive\r\n" | |
8928 "Authorization: auth_token\r\n\r\n"); | |
8929 const MockWrite kGetAuthWithProxyAuth( | |
8930 "GET http://www.example.com/ HTTP/1.1\r\n" | |
8931 "Host: www.example.com\r\n" | |
8932 "Proxy-Connection: keep-alive\r\n" | |
8933 "Proxy-Authorization: auth_token\r\n" | |
8934 "Authorization: auth_token\r\n\r\n"); | |
8935 const MockWrite kConnect( | |
8936 "CONNECT www.example.com:443 HTTP/1.1\r\n" | |
8937 "Host: www.example.com\r\n" | |
8938 "Proxy-Connection: keep-alive\r\n\r\n"); | |
8939 const MockWrite kConnectProxyAuth( | |
8940 "CONNECT www.example.com:443 HTTP/1.1\r\n" | |
8941 "Host: www.example.com\r\n" | |
8942 "Proxy-Connection: keep-alive\r\n" | |
8943 "Proxy-Authorization: auth_token\r\n\r\n"); | |
8944 | |
8945 const MockRead kSuccess( | |
8946 "HTTP/1.1 200 OK\r\n" | |
8947 "Content-Type: text/html; charset=iso-8859-1\r\n" | |
8948 "Content-Length: 3\r\n\r\n" | |
8949 "Yes"); | |
8950 const MockRead kFailure( | |
8951 "Should not be called."); | |
8952 const MockRead kServerChallenge( | |
8953 "HTTP/1.1 401 Unauthorized\r\n" | |
8954 "WWW-Authenticate: Mock realm=server\r\n" | |
8955 "Content-Type: text/html; charset=iso-8859-1\r\n" | |
8956 "Content-Length: 14\r\n\r\n" | |
8957 "Unauthorized\r\n"); | |
8958 const MockRead kProxyChallenge( | |
8959 "HTTP/1.1 407 Unauthorized\r\n" | |
8960 "Proxy-Authenticate: Mock realm=proxy\r\n" | |
8961 "Proxy-Connection: close\r\n" | |
8962 "Content-Type: text/html; charset=iso-8859-1\r\n" | |
8963 "Content-Length: 14\r\n\r\n" | |
8964 "Unauthorized\r\n"); | |
8965 const MockRead kProxyConnected( | |
8966 "HTTP/1.1 200 Connection Established\r\n\r\n"); | |
8967 | |
8968 // NOTE(cbentzel): I wanted TestReadWriteRound to be a simple struct with | |
8969 // no constructors, but the C++ compiler on Windows warns about | |
8970 // unspecified data in compound literals. So, moved to using constructors, | |
8971 // and TestRound's created with the default constructor should not be used. | |
8972 struct TestRound { | |
8973 TestRound() | |
8974 : expected_rv(ERR_UNEXPECTED), | |
8975 extra_write(NULL), | |
8976 extra_read(NULL) { | |
8977 } | |
8978 TestRound(const MockWrite& write_arg, const MockRead& read_arg, | |
8979 int expected_rv_arg) | |
8980 : write(write_arg), | |
8981 read(read_arg), | |
8982 expected_rv(expected_rv_arg), | |
8983 extra_write(NULL), | |
8984 extra_read(NULL) { | |
8985 } | |
8986 TestRound(const MockWrite& write_arg, const MockRead& read_arg, | |
8987 int expected_rv_arg, const MockWrite* extra_write_arg, | |
8988 const MockRead* extra_read_arg) | |
8989 : write(write_arg), | |
8990 read(read_arg), | |
8991 expected_rv(expected_rv_arg), | |
8992 extra_write(extra_write_arg), | |
8993 extra_read(extra_read_arg) { | |
8994 } | |
8995 MockWrite write; | |
8996 MockRead read; | |
8997 int expected_rv; | |
8998 const MockWrite* extra_write; | |
8999 const MockRead* extra_read; | |
9000 }; | |
9001 | |
9002 static const int kNoSSL = 500; | |
9003 | |
9004 struct TestConfig { | |
9005 const char* proxy_url; | |
9006 AuthTiming proxy_auth_timing; | |
9007 int proxy_auth_rv; | |
9008 const char* server_url; | |
9009 AuthTiming server_auth_timing; | |
9010 int server_auth_rv; | |
9011 int num_auth_rounds; | |
9012 int first_ssl_round; | |
9013 TestRound rounds[3]; | |
9014 } test_configs[] = { | |
9015 // Non-authenticating HTTP server with a direct connection. | |
9016 { NULL, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, | |
9017 { TestRound(kGet, kSuccess, OK)}}, | |
9018 // Authenticating HTTP server with a direct connection. | |
9019 { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, | |
9020 { TestRound(kGet, kServerChallenge, OK), | |
9021 TestRound(kGetAuth, kSuccess, OK)}}, | |
9022 { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, | |
9023 { TestRound(kGet, kServerChallenge, OK), | |
9024 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9025 { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, | |
9026 { TestRound(kGet, kServerChallenge, OK), | |
9027 TestRound(kGetAuth, kSuccess, OK)}}, | |
9028 { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, | |
9029 { TestRound(kGet, kServerChallenge, OK), | |
9030 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9031 // Non-authenticating HTTP server through a non-authenticating proxy. | |
9032 { kProxy, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, | |
9033 { TestRound(kGetProxy, kSuccess, OK)}}, | |
9034 // Authenticating HTTP server through a non-authenticating proxy. | |
9035 { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, | |
9036 { TestRound(kGetProxy, kServerChallenge, OK), | |
9037 TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, | |
9038 { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, | |
9039 { TestRound(kGetProxy, kServerChallenge, OK), | |
9040 TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, | |
9041 { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, | |
9042 { TestRound(kGetProxy, kServerChallenge, OK), | |
9043 TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, | |
9044 { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, | |
9045 { TestRound(kGetProxy, kServerChallenge, OK), | |
9046 TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, | |
9047 // Non-authenticating HTTP server through an authenticating proxy. | |
9048 { kProxy, AUTH_SYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, | |
9049 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9050 TestRound(kGetProxyAuth, kSuccess, OK)}}, | |
9051 { kProxy, AUTH_SYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, | |
9052 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9053 TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, | |
9054 { kProxy, AUTH_ASYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, | |
9055 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9056 TestRound(kGetProxyAuth, kSuccess, OK)}}, | |
9057 { kProxy, AUTH_ASYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, | |
9058 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9059 TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, | |
9060 // Authenticating HTTP server through an authenticating proxy. | |
9061 { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, | |
9062 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9063 TestRound(kGetProxyAuth, kServerChallenge, OK), | |
9064 TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, | |
9065 { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, | |
9066 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9067 TestRound(kGetProxyAuth, kServerChallenge, OK), | |
9068 TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, | |
9069 { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, | |
9070 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9071 TestRound(kGetProxyAuth, kServerChallenge, OK), | |
9072 TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, | |
9073 { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, | |
9074 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9075 TestRound(kGetProxyAuth, kServerChallenge, OK), | |
9076 TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, | |
9077 { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, | |
9078 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9079 TestRound(kGetProxyAuth, kServerChallenge, OK), | |
9080 TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, | |
9081 { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, | |
9082 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9083 TestRound(kGetProxyAuth, kServerChallenge, OK), | |
9084 TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, | |
9085 { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, | |
9086 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9087 TestRound(kGetProxyAuth, kServerChallenge, OK), | |
9088 TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, | |
9089 { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, | |
9090 { TestRound(kGetProxy, kProxyChallenge, OK), | |
9091 TestRound(kGetProxyAuth, kServerChallenge, OK), | |
9092 TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, | |
9093 // Non-authenticating HTTPS server with a direct connection. | |
9094 { NULL, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, | |
9095 { TestRound(kGet, kSuccess, OK)}}, | |
9096 // Authenticating HTTPS server with a direct connection. | |
9097 { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, | |
9098 { TestRound(kGet, kServerChallenge, OK), | |
9099 TestRound(kGetAuth, kSuccess, OK)}}, | |
9100 { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, | |
9101 { TestRound(kGet, kServerChallenge, OK), | |
9102 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9103 { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, | |
9104 { TestRound(kGet, kServerChallenge, OK), | |
9105 TestRound(kGetAuth, kSuccess, OK)}}, | |
9106 { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, | |
9107 { TestRound(kGet, kServerChallenge, OK), | |
9108 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9109 // Non-authenticating HTTPS server with a non-authenticating proxy. | |
9110 { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, | |
9111 { TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, | |
9112 // Authenticating HTTPS server through a non-authenticating proxy. | |
9113 { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, | |
9114 { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), | |
9115 TestRound(kGetAuth, kSuccess, OK)}}, | |
9116 { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, | |
9117 { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), | |
9118 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9119 { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, | |
9120 { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), | |
9121 TestRound(kGetAuth, kSuccess, OK)}}, | |
9122 { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, | |
9123 { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), | |
9124 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9125 // Non-Authenticating HTTPS server through an authenticating proxy. | |
9126 { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, | |
9127 { TestRound(kConnect, kProxyChallenge, OK), | |
9128 TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, | |
9129 { kProxy, AUTH_SYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, | |
9130 { TestRound(kConnect, kProxyChallenge, OK), | |
9131 TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, | |
9132 { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, | |
9133 { TestRound(kConnect, kProxyChallenge, OK), | |
9134 TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, | |
9135 { kProxy, AUTH_ASYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, | |
9136 { TestRound(kConnect, kProxyChallenge, OK), | |
9137 TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, | |
9138 // Authenticating HTTPS server through an authenticating proxy. | |
9139 { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, | |
9140 { TestRound(kConnect, kProxyChallenge, OK), | |
9141 TestRound(kConnectProxyAuth, kProxyConnected, OK, | |
9142 &kGet, &kServerChallenge), | |
9143 TestRound(kGetAuth, kSuccess, OK)}}, | |
9144 { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, | |
9145 { TestRound(kConnect, kProxyChallenge, OK), | |
9146 TestRound(kConnectProxyAuth, kProxyConnected, OK, | |
9147 &kGet, &kServerChallenge), | |
9148 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9149 { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, | |
9150 { TestRound(kConnect, kProxyChallenge, OK), | |
9151 TestRound(kConnectProxyAuth, kProxyConnected, OK, | |
9152 &kGet, &kServerChallenge), | |
9153 TestRound(kGetAuth, kSuccess, OK)}}, | |
9154 { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, | |
9155 { TestRound(kConnect, kProxyChallenge, OK), | |
9156 TestRound(kConnectProxyAuth, kProxyConnected, OK, | |
9157 &kGet, &kServerChallenge), | |
9158 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9159 { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, | |
9160 { TestRound(kConnect, kProxyChallenge, OK), | |
9161 TestRound(kConnectProxyAuth, kProxyConnected, OK, | |
9162 &kGet, &kServerChallenge), | |
9163 TestRound(kGetAuth, kSuccess, OK)}}, | |
9164 { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, | |
9165 { TestRound(kConnect, kProxyChallenge, OK), | |
9166 TestRound(kConnectProxyAuth, kProxyConnected, OK, | |
9167 &kGet, &kServerChallenge), | |
9168 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9169 { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, | |
9170 { TestRound(kConnect, kProxyChallenge, OK), | |
9171 TestRound(kConnectProxyAuth, kProxyConnected, OK, | |
9172 &kGet, &kServerChallenge), | |
9173 TestRound(kGetAuth, kSuccess, OK)}}, | |
9174 { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, | |
9175 { TestRound(kConnect, kProxyChallenge, OK), | |
9176 TestRound(kConnectProxyAuth, kProxyConnected, OK, | |
9177 &kGet, &kServerChallenge), | |
9178 TestRound(kGetAuth, kFailure, kAuthErr)}}, | |
9179 }; | |
9180 | |
9181 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_configs); ++i) { | |
9182 HttpAuthHandlerMock::Factory* auth_factory( | |
9183 new HttpAuthHandlerMock::Factory()); | |
9184 session_deps_.http_auth_handler_factory.reset(auth_factory); | |
9185 const TestConfig& test_config = test_configs[i]; | |
9186 | |
9187 // Set up authentication handlers as necessary. | |
9188 if (test_config.proxy_auth_timing != AUTH_NONE) { | |
9189 for (int n = 0; n < 2; n++) { | |
9190 HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); | |
9191 std::string auth_challenge = "Mock realm=proxy"; | |
9192 GURL origin(test_config.proxy_url); | |
9193 HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), | |
9194 auth_challenge.end()); | |
9195 auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_PROXY, | |
9196 origin, BoundNetLog()); | |
9197 auth_handler->SetGenerateExpectation( | |
9198 test_config.proxy_auth_timing == AUTH_ASYNC, | |
9199 test_config.proxy_auth_rv); | |
9200 auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); | |
9201 } | |
9202 } | |
9203 if (test_config.server_auth_timing != AUTH_NONE) { | |
9204 HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); | |
9205 std::string auth_challenge = "Mock realm=server"; | |
9206 GURL origin(test_config.server_url); | |
9207 HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), | |
9208 auth_challenge.end()); | |
9209 auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, | |
9210 origin, BoundNetLog()); | |
9211 auth_handler->SetGenerateExpectation( | |
9212 test_config.server_auth_timing == AUTH_ASYNC, | |
9213 test_config.server_auth_rv); | |
9214 auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); | |
9215 } | |
9216 if (test_config.proxy_url) { | |
9217 session_deps_.proxy_service.reset( | |
9218 ProxyService::CreateFixed(test_config.proxy_url)); | |
9219 } else { | |
9220 session_deps_.proxy_service.reset(ProxyService::CreateDirect()); | |
9221 } | |
9222 | |
9223 HttpRequestInfo request; | |
9224 request.method = "GET"; | |
9225 request.url = GURL(test_config.server_url); | |
9226 request.load_flags = 0; | |
9227 | |
9228 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
9229 HttpNetworkTransaction trans( | |
9230 DEFAULT_PRIORITY, CreateSession(&session_deps_)); | |
9231 | |
9232 for (int round = 0; round < test_config.num_auth_rounds; ++round) { | |
9233 const TestRound& read_write_round = test_config.rounds[round]; | |
9234 | |
9235 // Set up expected reads and writes. | |
9236 MockRead reads[2]; | |
9237 reads[0] = read_write_round.read; | |
9238 size_t length_reads = 1; | |
9239 if (read_write_round.extra_read) { | |
9240 reads[1] = *read_write_round.extra_read; | |
9241 length_reads = 2; | |
9242 } | |
9243 | |
9244 MockWrite writes[2]; | |
9245 writes[0] = read_write_round.write; | |
9246 size_t length_writes = 1; | |
9247 if (read_write_round.extra_write) { | |
9248 writes[1] = *read_write_round.extra_write; | |
9249 length_writes = 2; | |
9250 } | |
9251 StaticSocketDataProvider data_provider( | |
9252 reads, length_reads, writes, length_writes); | |
9253 session_deps_.socket_factory->AddSocketDataProvider(&data_provider); | |
9254 | |
9255 // Add an SSL sequence if necessary. | |
9256 SSLSocketDataProvider ssl_socket_data_provider(SYNCHRONOUS, OK); | |
9257 if (round >= test_config.first_ssl_round) | |
9258 session_deps_.socket_factory->AddSSLSocketDataProvider( | |
9259 &ssl_socket_data_provider); | |
9260 | |
9261 // Start or restart the transaction. | |
9262 TestCompletionCallback callback; | |
9263 int rv; | |
9264 if (round == 0) { | |
9265 rv = trans.Start(&request, callback.callback(), BoundNetLog()); | |
9266 } else { | |
9267 rv = trans.RestartWithAuth( | |
9268 AuthCredentials(kFoo, kBar), callback.callback()); | |
9269 } | |
9270 if (rv == ERR_IO_PENDING) | |
9271 rv = callback.WaitForResult(); | |
9272 | |
9273 // Compare results with expected data. | |
9274 EXPECT_EQ(read_write_round.expected_rv, rv); | |
9275 const HttpResponseInfo* response = trans.GetResponseInfo(); | |
9276 if (read_write_round.expected_rv == OK) { | |
9277 ASSERT_TRUE(response != NULL); | |
9278 } else { | |
9279 EXPECT_TRUE(response == NULL); | |
9280 EXPECT_EQ(round + 1, test_config.num_auth_rounds); | |
9281 continue; | |
9282 } | |
9283 if (round + 1 < test_config.num_auth_rounds) { | |
9284 EXPECT_FALSE(response->auth_challenge.get() == NULL); | |
9285 } else { | |
9286 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
9287 } | |
9288 } | |
9289 } | |
9290 } | |
9291 | |
9292 TEST_F(HttpNetworkTransactionSpdy2Test, MultiRoundAuth) { | |
9293 // Do multi-round authentication and make sure it works correctly. | |
9294 HttpAuthHandlerMock::Factory* auth_factory( | |
9295 new HttpAuthHandlerMock::Factory()); | |
9296 session_deps_.http_auth_handler_factory.reset(auth_factory); | |
9297 session_deps_.proxy_service.reset(ProxyService::CreateDirect()); | |
9298 session_deps_.host_resolver->rules()->AddRule("www.example.com", "10.0.0.1"); | |
9299 session_deps_.host_resolver->set_synchronous_mode(true); | |
9300 | |
9301 HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); | |
9302 auth_handler->set_connection_based(true); | |
9303 std::string auth_challenge = "Mock realm=server"; | |
9304 GURL origin("http://www.example.com"); | |
9305 HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), | |
9306 auth_challenge.end()); | |
9307 auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, | |
9308 origin, BoundNetLog()); | |
9309 auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); | |
9310 | |
9311 int rv = OK; | |
9312 const HttpResponseInfo* response = NULL; | |
9313 HttpRequestInfo request; | |
9314 request.method = "GET"; | |
9315 request.url = origin; | |
9316 request.load_flags = 0; | |
9317 | |
9318 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
9319 | |
9320 // Use a TCP Socket Pool with only one connection per group. This is used | |
9321 // to validate that the TCP socket is not released to the pool between | |
9322 // each round of multi-round authentication. | |
9323 HttpNetworkSessionPeer session_peer(session); | |
9324 ClientSocketPoolHistograms transport_pool_histograms("SmallTCP"); | |
9325 TransportClientSocketPool* transport_pool = new TransportClientSocketPool( | |
9326 50, // Max sockets for pool | |
9327 1, // Max sockets per group | |
9328 &transport_pool_histograms, | |
9329 session_deps_.host_resolver.get(), | |
9330 session_deps_.socket_factory.get(), | |
9331 session_deps_.net_log); | |
9332 MockClientSocketPoolManager* mock_pool_manager = | |
9333 new MockClientSocketPoolManager; | |
9334 mock_pool_manager->SetTransportSocketPool(transport_pool); | |
9335 session_peer.SetClientSocketPoolManager(mock_pool_manager); | |
9336 | |
9337 scoped_ptr<HttpTransaction> trans( | |
9338 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
9339 TestCompletionCallback callback; | |
9340 | |
9341 const MockWrite kGet( | |
9342 "GET / HTTP/1.1\r\n" | |
9343 "Host: www.example.com\r\n" | |
9344 "Connection: keep-alive\r\n\r\n"); | |
9345 const MockWrite kGetAuth( | |
9346 "GET / HTTP/1.1\r\n" | |
9347 "Host: www.example.com\r\n" | |
9348 "Connection: keep-alive\r\n" | |
9349 "Authorization: auth_token\r\n\r\n"); | |
9350 | |
9351 const MockRead kServerChallenge( | |
9352 "HTTP/1.1 401 Unauthorized\r\n" | |
9353 "WWW-Authenticate: Mock realm=server\r\n" | |
9354 "Content-Type: text/html; charset=iso-8859-1\r\n" | |
9355 "Content-Length: 14\r\n\r\n" | |
9356 "Unauthorized\r\n"); | |
9357 const MockRead kSuccess( | |
9358 "HTTP/1.1 200 OK\r\n" | |
9359 "Content-Type: text/html; charset=iso-8859-1\r\n" | |
9360 "Content-Length: 3\r\n\r\n" | |
9361 "Yes"); | |
9362 | |
9363 MockWrite writes[] = { | |
9364 // First round | |
9365 kGet, | |
9366 // Second round | |
9367 kGetAuth, | |
9368 // Third round | |
9369 kGetAuth, | |
9370 // Fourth round | |
9371 kGetAuth, | |
9372 // Competing request | |
9373 kGet, | |
9374 }; | |
9375 MockRead reads[] = { | |
9376 // First round | |
9377 kServerChallenge, | |
9378 // Second round | |
9379 kServerChallenge, | |
9380 // Third round | |
9381 kServerChallenge, | |
9382 // Fourth round | |
9383 kSuccess, | |
9384 // Competing response | |
9385 kSuccess, | |
9386 }; | |
9387 StaticSocketDataProvider data_provider(reads, arraysize(reads), | |
9388 writes, arraysize(writes)); | |
9389 session_deps_.socket_factory->AddSocketDataProvider(&data_provider); | |
9390 | |
9391 const char* const kSocketGroup = "www.example.com:80"; | |
9392 | |
9393 // First round of authentication. | |
9394 auth_handler->SetGenerateExpectation(false, OK); | |
9395 rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
9396 if (rv == ERR_IO_PENDING) | |
9397 rv = callback.WaitForResult(); | |
9398 EXPECT_EQ(OK, rv); | |
9399 response = trans->GetResponseInfo(); | |
9400 ASSERT_TRUE(response != NULL); | |
9401 EXPECT_FALSE(response->auth_challenge.get() == NULL); | |
9402 EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); | |
9403 | |
9404 // In between rounds, another request comes in for the same domain. | |
9405 // It should not be able to grab the TCP socket that trans has already | |
9406 // claimed. | |
9407 scoped_ptr<HttpTransaction> trans_compete( | |
9408 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
9409 TestCompletionCallback callback_compete; | |
9410 rv = trans_compete->Start( | |
9411 &request, callback_compete.callback(), BoundNetLog()); | |
9412 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9413 // callback_compete.WaitForResult at this point would stall forever, | |
9414 // since the HttpNetworkTransaction does not release the request back to | |
9415 // the pool until after authentication completes. | |
9416 | |
9417 // Second round of authentication. | |
9418 auth_handler->SetGenerateExpectation(false, OK); | |
9419 rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); | |
9420 if (rv == ERR_IO_PENDING) | |
9421 rv = callback.WaitForResult(); | |
9422 EXPECT_EQ(OK, rv); | |
9423 response = trans->GetResponseInfo(); | |
9424 ASSERT_TRUE(response != NULL); | |
9425 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
9426 EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); | |
9427 | |
9428 // Third round of authentication. | |
9429 auth_handler->SetGenerateExpectation(false, OK); | |
9430 rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); | |
9431 if (rv == ERR_IO_PENDING) | |
9432 rv = callback.WaitForResult(); | |
9433 EXPECT_EQ(OK, rv); | |
9434 response = trans->GetResponseInfo(); | |
9435 ASSERT_TRUE(response != NULL); | |
9436 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
9437 EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); | |
9438 | |
9439 // Fourth round of authentication, which completes successfully. | |
9440 auth_handler->SetGenerateExpectation(false, OK); | |
9441 rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); | |
9442 if (rv == ERR_IO_PENDING) | |
9443 rv = callback.WaitForResult(); | |
9444 EXPECT_EQ(OK, rv); | |
9445 response = trans->GetResponseInfo(); | |
9446 ASSERT_TRUE(response != NULL); | |
9447 EXPECT_TRUE(response->auth_challenge.get() == NULL); | |
9448 EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); | |
9449 | |
9450 // Read the body since the fourth round was successful. This will also | |
9451 // release the socket back to the pool. | |
9452 scoped_refptr<IOBufferWithSize> io_buf(new IOBufferWithSize(50)); | |
9453 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); | |
9454 if (rv == ERR_IO_PENDING) | |
9455 rv = callback.WaitForResult(); | |
9456 EXPECT_EQ(3, rv); | |
9457 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); | |
9458 EXPECT_EQ(0, rv); | |
9459 // There are still 0 idle sockets, since the trans_compete transaction | |
9460 // will be handed it immediately after trans releases it to the group. | |
9461 EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); | |
9462 | |
9463 // The competing request can now finish. Wait for the headers and then | |
9464 // read the body. | |
9465 rv = callback_compete.WaitForResult(); | |
9466 EXPECT_EQ(OK, rv); | |
9467 rv = trans_compete->Read(io_buf.get(), io_buf->size(), callback.callback()); | |
9468 if (rv == ERR_IO_PENDING) | |
9469 rv = callback.WaitForResult(); | |
9470 EXPECT_EQ(3, rv); | |
9471 rv = trans_compete->Read(io_buf.get(), io_buf->size(), callback.callback()); | |
9472 EXPECT_EQ(0, rv); | |
9473 | |
9474 // Finally, the socket is released to the group. | |
9475 EXPECT_EQ(1, transport_pool->IdleSocketCountInGroup(kSocketGroup)); | |
9476 } | |
9477 | |
9478 // This tests the case that a request is issued via http instead of spdy after | |
9479 // npn is negotiated. | |
9480 TEST_F(HttpNetworkTransactionSpdy2Test, NpnWithHttpOverSSL) { | |
9481 HttpStreamFactory::set_use_alternate_protocols(true); | |
9482 HttpStreamFactory::SetNextProtos( | |
9483 MakeNextProtos("http/1.1", "http1.1", NULL)); | |
9484 HttpRequestInfo request; | |
9485 request.method = "GET"; | |
9486 request.url = GURL("https://www.google.com/"); | |
9487 request.load_flags = 0; | |
9488 | |
9489 MockWrite data_writes[] = { | |
9490 MockWrite("GET / HTTP/1.1\r\n" | |
9491 "Host: www.google.com\r\n" | |
9492 "Connection: keep-alive\r\n\r\n"), | |
9493 }; | |
9494 | |
9495 MockRead data_reads[] = { | |
9496 MockRead("HTTP/1.1 200 OK\r\n"), | |
9497 MockRead(kAlternateProtocolHttpHeader), | |
9498 MockRead("hello world"), | |
9499 MockRead(SYNCHRONOUS, OK), | |
9500 }; | |
9501 | |
9502 SSLSocketDataProvider ssl(ASYNC, OK); | |
9503 ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; | |
9504 ssl.next_proto = "http/1.1"; | |
9505 ssl.protocol_negotiated = kProtoHTTP11; | |
9506 | |
9507 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
9508 | |
9509 StaticSocketDataProvider data(data_reads, arraysize(data_reads), | |
9510 data_writes, arraysize(data_writes)); | |
9511 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
9512 | |
9513 TestCompletionCallback callback; | |
9514 | |
9515 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
9516 scoped_ptr<HttpTransaction> trans( | |
9517 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
9518 | |
9519 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
9520 | |
9521 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9522 EXPECT_EQ(OK, callback.WaitForResult()); | |
9523 | |
9524 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
9525 ASSERT_TRUE(response != NULL); | |
9526 ASSERT_TRUE(response->headers.get() != NULL); | |
9527 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
9528 | |
9529 std::string response_data; | |
9530 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
9531 EXPECT_EQ("hello world", response_data); | |
9532 | |
9533 EXPECT_FALSE(response->was_fetched_via_spdy); | |
9534 EXPECT_TRUE(response->was_npn_negotiated); | |
9535 } | |
9536 | |
9537 TEST_F(HttpNetworkTransactionSpdy2Test, SpdyPostNPNServerHangup) { | |
9538 // Simulate the SSL handshake completing with an NPN negotiation | |
9539 // followed by an immediate server closing of the socket. | |
9540 // Fix crash: http://crbug.com/46369 | |
9541 HttpStreamFactory::set_use_alternate_protocols(true); | |
9542 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
9543 | |
9544 HttpRequestInfo request; | |
9545 request.method = "GET"; | |
9546 request.url = GURL("https://www.google.com/"); | |
9547 request.load_flags = 0; | |
9548 | |
9549 SSLSocketDataProvider ssl(ASYNC, OK); | |
9550 ssl.SetNextProto(kProtoSPDY2); | |
9551 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
9552 | |
9553 scoped_ptr<SpdyFrame> req( | |
9554 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true)); | |
9555 MockWrite spdy_writes[] = { CreateMockWrite(*req) }; | |
9556 | |
9557 MockRead spdy_reads[] = { | |
9558 MockRead(SYNCHRONOUS, 0, 0) // Not async - return 0 immediately. | |
9559 }; | |
9560 | |
9561 DelayedSocketData spdy_data( | |
9562 0, // don't wait in this case, immediate hangup. | |
9563 spdy_reads, arraysize(spdy_reads), | |
9564 spdy_writes, arraysize(spdy_writes)); | |
9565 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
9566 | |
9567 TestCompletionCallback callback; | |
9568 | |
9569 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
9570 scoped_ptr<HttpTransaction> trans( | |
9571 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
9572 | |
9573 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
9574 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9575 EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult()); | |
9576 } | |
9577 | |
9578 TEST_F(HttpNetworkTransactionSpdy2Test, SpdyAlternateProtocolThroughProxy) { | |
9579 // This test ensures that the URL passed into the proxy is upgraded | |
9580 // to https when doing an Alternate Protocol upgrade. | |
9581 HttpStreamFactory::set_use_alternate_protocols(true); | |
9582 HttpStreamFactory::SetNextProtos( | |
9583 MakeNextProtos( | |
9584 "http/1.1", "http1.1", "spdy/2", "spdy", NULL)); | |
9585 | |
9586 session_deps_.proxy_service.reset( | |
9587 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70")); | |
9588 CapturingNetLog net_log; | |
9589 session_deps_.net_log = &net_log; | |
9590 HttpAuthHandlerMock::Factory* auth_factory = | |
9591 new HttpAuthHandlerMock::Factory(); | |
9592 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); | |
9593 auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); | |
9594 auth_factory->set_do_init_from_challenge(true); | |
9595 session_deps_.http_auth_handler_factory.reset(auth_factory); | |
9596 | |
9597 HttpRequestInfo request; | |
9598 request.method = "GET"; | |
9599 request.url = GURL("http://www.google.com"); | |
9600 request.load_flags = 0; | |
9601 | |
9602 // First round goes unauthenticated through the proxy. | |
9603 MockWrite data_writes_1[] = { | |
9604 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | |
9605 "Host: www.google.com\r\n" | |
9606 "Proxy-Connection: keep-alive\r\n" | |
9607 "\r\n"), | |
9608 }; | |
9609 MockRead data_reads_1[] = { | |
9610 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), | |
9611 MockRead("HTTP/1.1 200 OK\r\n" | |
9612 "Alternate-Protocol: 443:npn-spdy/2\r\n" | |
9613 "Proxy-Connection: close\r\n" | |
9614 "\r\n"), | |
9615 }; | |
9616 StaticSocketDataProvider data_1(data_reads_1, arraysize(data_reads_1), | |
9617 data_writes_1, arraysize(data_writes_1)); | |
9618 | |
9619 // Second round tries to tunnel to www.google.com due to the | |
9620 // Alternate-Protocol announcement in the first round. It fails due | |
9621 // to a proxy authentication challenge. | |
9622 // After the failure, a tunnel is established to www.google.com using | |
9623 // Proxy-Authorization headers. There is then a SPDY request round. | |
9624 // | |
9625 // NOTE: Despite the "Proxy-Connection: Close", these are done on the | |
9626 // same MockTCPClientSocket since the underlying HttpNetworkClientSocket | |
9627 // does a Disconnect and Connect on the same socket, rather than trying | |
9628 // to obtain a new one. | |
9629 // | |
9630 // NOTE: Originally, the proxy response to the second CONNECT request | |
9631 // simply returned another 407 so the unit test could skip the SSL connection | |
9632 // establishment and SPDY framing issues. Alas, the | |
9633 // retry-http-when-alternate-protocol fails logic kicks in, which was more | |
9634 // complicated to set up expectations for than the SPDY session. | |
9635 | |
9636 scoped_ptr<SpdyFrame> req( | |
9637 spdy_util_.ConstructSpdyGet(NULL, 0, false, 1, LOWEST, true)); | |
9638 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
9639 scoped_ptr<SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); | |
9640 | |
9641 MockWrite data_writes_2[] = { | |
9642 // First connection attempt without Proxy-Authorization. | |
9643 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
9644 "Host: www.google.com\r\n" | |
9645 "Proxy-Connection: keep-alive\r\n" | |
9646 "\r\n"), | |
9647 | |
9648 // Second connection attempt with Proxy-Authorization. | |
9649 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
9650 "Host: www.google.com\r\n" | |
9651 "Proxy-Connection: keep-alive\r\n" | |
9652 "Proxy-Authorization: auth_token\r\n" | |
9653 "\r\n"), | |
9654 | |
9655 // SPDY request | |
9656 CreateMockWrite(*req), | |
9657 }; | |
9658 const char kRejectConnectResponse[] = ("HTTP/1.1 407 Unauthorized\r\n" | |
9659 "Proxy-Authenticate: Mock\r\n" | |
9660 "Proxy-Connection: close\r\n" | |
9661 "\r\n"); | |
9662 const char kAcceptConnectResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; | |
9663 MockRead data_reads_2[] = { | |
9664 // First connection attempt fails | |
9665 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, 1), | |
9666 MockRead(ASYNC, kRejectConnectResponse, | |
9667 arraysize(kRejectConnectResponse) - 1, 1), | |
9668 | |
9669 // Second connection attempt passes | |
9670 MockRead(ASYNC, kAcceptConnectResponse, | |
9671 arraysize(kAcceptConnectResponse) -1, 4), | |
9672 | |
9673 // SPDY response | |
9674 CreateMockRead(*resp.get(), 6), | |
9675 CreateMockRead(*data.get(), 6), | |
9676 MockRead(ASYNC, 0, 0, 6), | |
9677 }; | |
9678 OrderedSocketData data_2( | |
9679 data_reads_2, arraysize(data_reads_2), | |
9680 data_writes_2, arraysize(data_writes_2)); | |
9681 | |
9682 SSLSocketDataProvider ssl(ASYNC, OK); | |
9683 ssl.SetNextProto(kProtoSPDY2); | |
9684 | |
9685 MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); | |
9686 StaticSocketDataProvider hanging_non_alternate_protocol_socket( | |
9687 NULL, 0, NULL, 0); | |
9688 hanging_non_alternate_protocol_socket.set_connect_data( | |
9689 never_finishing_connect); | |
9690 | |
9691 session_deps_.socket_factory->AddSocketDataProvider(&data_1); | |
9692 session_deps_.socket_factory->AddSocketDataProvider(&data_2); | |
9693 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
9694 session_deps_.socket_factory->AddSocketDataProvider( | |
9695 &hanging_non_alternate_protocol_socket); | |
9696 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
9697 | |
9698 // First round should work and provide the Alternate-Protocol state. | |
9699 TestCompletionCallback callback_1; | |
9700 scoped_ptr<HttpTransaction> trans_1( | |
9701 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
9702 int rv = trans_1->Start(&request, callback_1.callback(), BoundNetLog()); | |
9703 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9704 EXPECT_EQ(OK, callback_1.WaitForResult()); | |
9705 | |
9706 // Second round should attempt a tunnel connect and get an auth challenge. | |
9707 TestCompletionCallback callback_2; | |
9708 scoped_ptr<HttpTransaction> trans_2( | |
9709 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
9710 rv = trans_2->Start(&request, callback_2.callback(), BoundNetLog()); | |
9711 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9712 EXPECT_EQ(OK, callback_2.WaitForResult()); | |
9713 const HttpResponseInfo* response = trans_2->GetResponseInfo(); | |
9714 ASSERT_TRUE(response != NULL); | |
9715 ASSERT_FALSE(response->auth_challenge.get() == NULL); | |
9716 | |
9717 // Restart with auth. Tunnel should work and response received. | |
9718 TestCompletionCallback callback_3; | |
9719 rv = trans_2->RestartWithAuth( | |
9720 AuthCredentials(kFoo, kBar), callback_3.callback()); | |
9721 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9722 EXPECT_EQ(OK, callback_3.WaitForResult()); | |
9723 | |
9724 // After all that work, these two lines (or actually, just the scheme) are | |
9725 // what this test is all about. Make sure it happens correctly. | |
9726 const GURL& request_url = auth_handler->request_url(); | |
9727 EXPECT_EQ("https", request_url.scheme()); | |
9728 EXPECT_EQ("www.google.com", request_url.host()); | |
9729 | |
9730 LoadTimingInfo load_timing_info; | |
9731 EXPECT_TRUE(trans_2->GetLoadTimingInfo(&load_timing_info)); | |
9732 TestLoadTimingNotReusedWithPac(load_timing_info, | |
9733 CONNECT_TIMING_HAS_SSL_TIMES); | |
9734 } | |
9735 | |
9736 // Test that if we cancel the transaction as the connection is completing, that | |
9737 // everything tears down correctly. | |
9738 TEST_F(HttpNetworkTransactionSpdy2Test, SimpleCancel) { | |
9739 // Setup everything about the connection to complete synchronously, so that | |
9740 // after calling HttpNetworkTransaction::Start, the only thing we're waiting | |
9741 // for is the callback from the HttpStreamRequest. | |
9742 // Then cancel the transaction. | |
9743 // Verify that we don't crash. | |
9744 MockConnect mock_connect(SYNCHRONOUS, OK); | |
9745 MockRead data_reads[] = { | |
9746 MockRead(SYNCHRONOUS, "HTTP/1.0 200 OK\r\n\r\n"), | |
9747 MockRead(SYNCHRONOUS, "hello world"), | |
9748 MockRead(SYNCHRONOUS, OK), | |
9749 }; | |
9750 | |
9751 HttpRequestInfo request; | |
9752 request.method = "GET"; | |
9753 request.url = GURL("http://www.google.com/"); | |
9754 request.load_flags = 0; | |
9755 | |
9756 session_deps_.host_resolver->set_synchronous_mode(true); | |
9757 scoped_ptr<HttpTransaction> trans( | |
9758 new HttpNetworkTransaction(DEFAULT_PRIORITY, | |
9759 CreateSession(&session_deps_))); | |
9760 | |
9761 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); | |
9762 data.set_connect_data(mock_connect); | |
9763 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
9764 | |
9765 TestCompletionCallback callback; | |
9766 | |
9767 CapturingBoundNetLog log; | |
9768 int rv = trans->Start(&request, callback.callback(), log.bound()); | |
9769 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9770 trans.reset(); // Cancel the transaction here. | |
9771 | |
9772 base::MessageLoop::current()->RunUntilIdle(); | |
9773 } | |
9774 | |
9775 // Test a basic GET request through a proxy. | |
9776 TEST_F(HttpNetworkTransactionSpdy2Test, ProxyGet) { | |
9777 session_deps_.proxy_service.reset( | |
9778 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70")); | |
9779 CapturingBoundNetLog log; | |
9780 session_deps_.net_log = log.bound().net_log(); | |
9781 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
9782 | |
9783 HttpRequestInfo request; | |
9784 request.method = "GET"; | |
9785 request.url = GURL("http://www.google.com/"); | |
9786 | |
9787 MockWrite data_writes1[] = { | |
9788 MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" | |
9789 "Host: www.google.com\r\n" | |
9790 "Proxy-Connection: keep-alive\r\n\r\n"), | |
9791 }; | |
9792 | |
9793 MockRead data_reads1[] = { | |
9794 MockRead("HTTP/1.1 200 OK\r\n"), | |
9795 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
9796 MockRead("Content-Length: 100\r\n\r\n"), | |
9797 MockRead(SYNCHRONOUS, OK), | |
9798 }; | |
9799 | |
9800 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
9801 data_writes1, arraysize(data_writes1)); | |
9802 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
9803 | |
9804 TestCompletionCallback callback1; | |
9805 | |
9806 scoped_ptr<HttpTransaction> trans( | |
9807 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
9808 | |
9809 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
9810 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9811 | |
9812 rv = callback1.WaitForResult(); | |
9813 EXPECT_EQ(OK, rv); | |
9814 | |
9815 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
9816 ASSERT_TRUE(response != NULL); | |
9817 | |
9818 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
9819 EXPECT_EQ(200, response->headers->response_code()); | |
9820 EXPECT_EQ(100, response->headers->GetContentLength()); | |
9821 EXPECT_TRUE(response->was_fetched_via_proxy); | |
9822 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
9823 | |
9824 LoadTimingInfo load_timing_info; | |
9825 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
9826 TestLoadTimingNotReusedWithPac(load_timing_info, | |
9827 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
9828 } | |
9829 | |
9830 // Test a basic HTTPS GET request through a proxy. | |
9831 TEST_F(HttpNetworkTransactionSpdy2Test, ProxyTunnelGet) { | |
9832 session_deps_.proxy_service.reset( | |
9833 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70")); | |
9834 CapturingBoundNetLog log; | |
9835 session_deps_.net_log = log.bound().net_log(); | |
9836 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
9837 | |
9838 HttpRequestInfo request; | |
9839 request.method = "GET"; | |
9840 request.url = GURL("https://www.google.com/"); | |
9841 | |
9842 // Since we have proxy, should try to establish tunnel. | |
9843 MockWrite data_writes1[] = { | |
9844 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
9845 "Host: www.google.com\r\n" | |
9846 "Proxy-Connection: keep-alive\r\n\r\n"), | |
9847 | |
9848 MockWrite("GET / HTTP/1.1\r\n" | |
9849 "Host: www.google.com\r\n" | |
9850 "Connection: keep-alive\r\n\r\n"), | |
9851 }; | |
9852 | |
9853 MockRead data_reads1[] = { | |
9854 MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), | |
9855 | |
9856 MockRead("HTTP/1.1 200 OK\r\n"), | |
9857 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
9858 MockRead("Content-Length: 100\r\n\r\n"), | |
9859 MockRead(SYNCHRONOUS, OK), | |
9860 }; | |
9861 | |
9862 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
9863 data_writes1, arraysize(data_writes1)); | |
9864 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
9865 SSLSocketDataProvider ssl(ASYNC, OK); | |
9866 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
9867 | |
9868 TestCompletionCallback callback1; | |
9869 | |
9870 scoped_ptr<HttpTransaction> trans( | |
9871 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
9872 | |
9873 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
9874 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9875 | |
9876 rv = callback1.WaitForResult(); | |
9877 EXPECT_EQ(OK, rv); | |
9878 net::CapturingNetLog::CapturedEntryList entries; | |
9879 log.GetEntries(&entries); | |
9880 size_t pos = ExpectLogContainsSomewhere( | |
9881 entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, | |
9882 NetLog::PHASE_NONE); | |
9883 ExpectLogContainsSomewhere( | |
9884 entries, pos, | |
9885 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, | |
9886 NetLog::PHASE_NONE); | |
9887 | |
9888 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
9889 ASSERT_TRUE(response != NULL); | |
9890 | |
9891 EXPECT_TRUE(response->headers->IsKeepAlive()); | |
9892 EXPECT_EQ(200, response->headers->response_code()); | |
9893 EXPECT_EQ(100, response->headers->GetContentLength()); | |
9894 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); | |
9895 EXPECT_TRUE(response->was_fetched_via_proxy); | |
9896 | |
9897 LoadTimingInfo load_timing_info; | |
9898 EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); | |
9899 TestLoadTimingNotReusedWithPac(load_timing_info, | |
9900 CONNECT_TIMING_HAS_SSL_TIMES); | |
9901 } | |
9902 | |
9903 // Test a basic HTTPS GET request through a proxy, but the server hangs up | |
9904 // while establishing the tunnel. | |
9905 TEST_F(HttpNetworkTransactionSpdy2Test, ProxyTunnelGetHangup) { | |
9906 session_deps_.proxy_service.reset(ProxyService::CreateFixed("myproxy:70")); | |
9907 CapturingBoundNetLog log; | |
9908 session_deps_.net_log = log.bound().net_log(); | |
9909 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
9910 | |
9911 HttpRequestInfo request; | |
9912 request.method = "GET"; | |
9913 request.url = GURL("https://www.google.com/"); | |
9914 | |
9915 // Since we have proxy, should try to establish tunnel. | |
9916 MockWrite data_writes1[] = { | |
9917 MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" | |
9918 "Host: www.google.com\r\n" | |
9919 "Proxy-Connection: keep-alive\r\n\r\n"), | |
9920 | |
9921 MockWrite("GET / HTTP/1.1\r\n" | |
9922 "Host: www.google.com\r\n" | |
9923 "Connection: keep-alive\r\n\r\n"), | |
9924 }; | |
9925 | |
9926 MockRead data_reads1[] = { | |
9927 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), | |
9928 MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), | |
9929 MockRead(ASYNC, 0, 0), // EOF | |
9930 }; | |
9931 | |
9932 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), | |
9933 data_writes1, arraysize(data_writes1)); | |
9934 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
9935 SSLSocketDataProvider ssl(ASYNC, OK); | |
9936 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
9937 | |
9938 TestCompletionCallback callback1; | |
9939 | |
9940 scoped_ptr<HttpTransaction> trans( | |
9941 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
9942 | |
9943 int rv = trans->Start(&request, callback1.callback(), log.bound()); | |
9944 EXPECT_EQ(ERR_IO_PENDING, rv); | |
9945 | |
9946 rv = callback1.WaitForResult(); | |
9947 EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); | |
9948 net::CapturingNetLog::CapturedEntryList entries; | |
9949 log.GetEntries(&entries); | |
9950 size_t pos = ExpectLogContainsSomewhere( | |
9951 entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, | |
9952 NetLog::PHASE_NONE); | |
9953 ExpectLogContainsSomewhere( | |
9954 entries, pos, | |
9955 NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, | |
9956 NetLog::PHASE_NONE); | |
9957 } | |
9958 | |
9959 // Test for crbug.com/55424. | |
9960 TEST_F(HttpNetworkTransactionSpdy2Test, PreconnectWithExistingSpdySession) { | |
9961 | |
9962 scoped_ptr<SpdyFrame> req( | |
9963 spdy_util_.ConstructSpdyGet("https://www.google.com", false, 1, LOWEST)); | |
9964 MockWrite spdy_writes[] = { CreateMockWrite(*req) }; | |
9965 | |
9966 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
9967 scoped_ptr<SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); | |
9968 MockRead spdy_reads[] = { | |
9969 CreateMockRead(*resp), | |
9970 CreateMockRead(*data), | |
9971 MockRead(ASYNC, 0, 0), | |
9972 }; | |
9973 | |
9974 DelayedSocketData spdy_data( | |
9975 1, // wait for one write to finish before reading. | |
9976 spdy_reads, arraysize(spdy_reads), | |
9977 spdy_writes, arraysize(spdy_writes)); | |
9978 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
9979 | |
9980 SSLSocketDataProvider ssl(ASYNC, OK); | |
9981 ssl.SetNextProto(kProtoSPDY2); | |
9982 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
9983 | |
9984 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
9985 | |
9986 // Set up an initial SpdySession in the pool to reuse. | |
9987 HostPortPair host_port_pair("www.google.com", 443); | |
9988 SpdySessionKey key(host_port_pair, ProxyServer::Direct(), | |
9989 kPrivacyModeDisabled); | |
9990 scoped_refptr<SpdySession> spdy_session = | |
9991 session->spdy_session_pool()->Get(key, BoundNetLog()); | |
9992 scoped_refptr<TransportSocketParams> transport_params( | |
9993 new TransportSocketParams(host_port_pair, MEDIUM, false, false, | |
9994 OnHostResolutionCallback())); | |
9995 TestCompletionCallback callback; | |
9996 | |
9997 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | |
9998 EXPECT_EQ(ERR_IO_PENDING, | |
9999 connection->Init(host_port_pair.ToString(), | |
10000 transport_params, | |
10001 LOWEST, | |
10002 callback.callback(), | |
10003 session->GetTransportSocketPool( | |
10004 HttpNetworkSession::NORMAL_SOCKET_POOL), | |
10005 BoundNetLog())); | |
10006 EXPECT_EQ(OK, callback.WaitForResult()); | |
10007 spdy_session->InitializeWithSocket(connection.release(), false, OK); | |
10008 | |
10009 HttpRequestInfo request; | |
10010 request.method = "GET"; | |
10011 request.url = GURL("https://www.google.com/"); | |
10012 request.load_flags = 0; | |
10013 | |
10014 // This is the important line that marks this as a preconnect. | |
10015 request.motivation = HttpRequestInfo::PRECONNECT_MOTIVATED; | |
10016 | |
10017 scoped_ptr<HttpTransaction> trans( | |
10018 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
10019 | |
10020 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
10021 EXPECT_EQ(ERR_IO_PENDING, rv); | |
10022 EXPECT_EQ(OK, callback.WaitForResult()); | |
10023 } | |
10024 | |
10025 // Given a net error, cause that error to be returned from the first Write() | |
10026 // call and verify that the HttpTransaction fails with that error. | |
10027 void HttpNetworkTransactionSpdy2Test::CheckErrorIsPassedBack( | |
10028 int error, IoMode mode) { | |
10029 net::HttpRequestInfo request_info; | |
10030 request_info.url = GURL("https://www.example.com/"); | |
10031 request_info.method = "GET"; | |
10032 request_info.load_flags = net::LOAD_NORMAL; | |
10033 | |
10034 SSLSocketDataProvider ssl_data(mode, OK); | |
10035 net::MockWrite data_writes[] = { | |
10036 net::MockWrite(mode, error), | |
10037 }; | |
10038 net::StaticSocketDataProvider data(NULL, 0, | |
10039 data_writes, arraysize(data_writes)); | |
10040 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
10041 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data); | |
10042 | |
10043 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
10044 scoped_ptr<HttpTransaction> trans( | |
10045 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
10046 | |
10047 TestCompletionCallback callback; | |
10048 int rv = trans->Start(&request_info, callback.callback(), net::BoundNetLog()); | |
10049 if (rv == net::ERR_IO_PENDING) | |
10050 rv = callback.WaitForResult(); | |
10051 ASSERT_EQ(error, rv); | |
10052 } | |
10053 | |
10054 TEST_F(HttpNetworkTransactionSpdy2Test, SSLWriteCertError) { | |
10055 // Just check a grab bag of cert errors. | |
10056 static const int kErrors[] = { | |
10057 ERR_CERT_COMMON_NAME_INVALID, | |
10058 ERR_CERT_AUTHORITY_INVALID, | |
10059 ERR_CERT_DATE_INVALID, | |
10060 }; | |
10061 for (size_t i = 0; i < arraysize(kErrors); i++) { | |
10062 CheckErrorIsPassedBack(kErrors[i], ASYNC); | |
10063 CheckErrorIsPassedBack(kErrors[i], SYNCHRONOUS); | |
10064 } | |
10065 } | |
10066 | |
10067 // Ensure that a client certificate is removed from the SSL client auth | |
10068 // cache when: | |
10069 // 1) No proxy is involved. | |
10070 // 2) TLS False Start is disabled. | |
10071 // 3) The initial TLS handshake requests a client certificate. | |
10072 // 4) The client supplies an invalid/unacceptable certificate. | |
10073 TEST_F(HttpNetworkTransactionSpdy2Test, | |
10074 ClientAuthCertCache_Direct_NoFalseStart) { | |
10075 net::HttpRequestInfo request_info; | |
10076 request_info.url = GURL("https://www.example.com/"); | |
10077 request_info.method = "GET"; | |
10078 request_info.load_flags = net::LOAD_NORMAL; | |
10079 | |
10080 scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); | |
10081 cert_request->host_and_port = "www.example.com:443"; | |
10082 | |
10083 // [ssl_]data1 contains the data for the first SSL handshake. When a | |
10084 // CertificateRequest is received for the first time, the handshake will | |
10085 // be aborted to allow the caller to provide a certificate. | |
10086 SSLSocketDataProvider ssl_data1(ASYNC, net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); | |
10087 ssl_data1.cert_request_info = cert_request.get(); | |
10088 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); | |
10089 net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); | |
10090 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
10091 | |
10092 // [ssl_]data2 contains the data for the second SSL handshake. When TLS | |
10093 // False Start is not being used, the result of the SSL handshake will be | |
10094 // returned as part of the SSLClientSocket::Connect() call. This test | |
10095 // matches the result of a server sending a handshake_failure alert, | |
10096 // rather than a Finished message, because it requires a client | |
10097 // certificate and none was supplied. | |
10098 SSLSocketDataProvider ssl_data2(ASYNC, net::ERR_SSL_PROTOCOL_ERROR); | |
10099 ssl_data2.cert_request_info = cert_request.get(); | |
10100 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); | |
10101 net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); | |
10102 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
10103 | |
10104 // [ssl_]data3 contains the data for the third SSL handshake. When a | |
10105 // connection to a server fails during an SSL handshake, | |
10106 // HttpNetworkTransaction will attempt to fallback to TLSv1 if the previous | |
10107 // connection was attempted with TLSv1.1. This is transparent to the caller | |
10108 // of the HttpNetworkTransaction. Because this test failure is due to | |
10109 // requiring a client certificate, this fallback handshake should also | |
10110 // fail. | |
10111 SSLSocketDataProvider ssl_data3(ASYNC, net::ERR_SSL_PROTOCOL_ERROR); | |
10112 ssl_data3.cert_request_info = cert_request.get(); | |
10113 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data3); | |
10114 net::StaticSocketDataProvider data3(NULL, 0, NULL, 0); | |
10115 session_deps_.socket_factory->AddSocketDataProvider(&data3); | |
10116 | |
10117 // [ssl_]data4 contains the data for the fourth SSL handshake. When a | |
10118 // connection to a server fails during an SSL handshake, | |
10119 // HttpNetworkTransaction will attempt to fallback to SSLv3 if the previous | |
10120 // connection was attempted with TLSv1. This is transparent to the caller | |
10121 // of the HttpNetworkTransaction. Because this test failure is due to | |
10122 // requiring a client certificate, this fallback handshake should also | |
10123 // fail. | |
10124 SSLSocketDataProvider ssl_data4(ASYNC, net::ERR_SSL_PROTOCOL_ERROR); | |
10125 ssl_data4.cert_request_info = cert_request.get(); | |
10126 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data4); | |
10127 net::StaticSocketDataProvider data4(NULL, 0, NULL, 0); | |
10128 session_deps_.socket_factory->AddSocketDataProvider(&data4); | |
10129 | |
10130 // Need one more if TLSv1.2 is enabled. | |
10131 SSLSocketDataProvider ssl_data5(ASYNC, net::ERR_SSL_PROTOCOL_ERROR); | |
10132 ssl_data5.cert_request_info = cert_request.get(); | |
10133 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data5); | |
10134 net::StaticSocketDataProvider data5(NULL, 0, NULL, 0); | |
10135 session_deps_.socket_factory->AddSocketDataProvider(&data5); | |
10136 | |
10137 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
10138 scoped_ptr<HttpTransaction> trans( | |
10139 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
10140 | |
10141 // Begin the SSL handshake with the peer. This consumes ssl_data1. | |
10142 TestCompletionCallback callback; | |
10143 int rv = trans->Start(&request_info, callback.callback(), net::BoundNetLog()); | |
10144 ASSERT_EQ(net::ERR_IO_PENDING, rv); | |
10145 | |
10146 // Complete the SSL handshake, which should abort due to requiring a | |
10147 // client certificate. | |
10148 rv = callback.WaitForResult(); | |
10149 ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); | |
10150 | |
10151 // Indicate that no certificate should be supplied. From the perspective | |
10152 // of SSLClientCertCache, NULL is just as meaningful as a real | |
10153 // certificate, so this is the same as supply a | |
10154 // legitimate-but-unacceptable certificate. | |
10155 rv = trans->RestartWithCertificate(NULL, callback.callback()); | |
10156 ASSERT_EQ(net::ERR_IO_PENDING, rv); | |
10157 | |
10158 // Ensure the certificate was added to the client auth cache before | |
10159 // allowing the connection to continue restarting. | |
10160 scoped_refptr<X509Certificate> client_cert; | |
10161 ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", | |
10162 &client_cert)); | |
10163 ASSERT_EQ(NULL, client_cert.get()); | |
10164 | |
10165 // Restart the handshake. This will consume ssl_data2, which fails, and | |
10166 // then consume ssl_data3 and ssl_data4, both of which should also fail. | |
10167 // The result code is checked against what ssl_data4 should return. | |
10168 rv = callback.WaitForResult(); | |
10169 ASSERT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); | |
10170 | |
10171 // Ensure that the client certificate is removed from the cache on a | |
10172 // handshake failure. | |
10173 ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", | |
10174 &client_cert)); | |
10175 } | |
10176 | |
10177 // Ensure that a client certificate is removed from the SSL client auth | |
10178 // cache when: | |
10179 // 1) No proxy is involved. | |
10180 // 2) TLS False Start is enabled. | |
10181 // 3) The initial TLS handshake requests a client certificate. | |
10182 // 4) The client supplies an invalid/unacceptable certificate. | |
10183 TEST_F(HttpNetworkTransactionSpdy2Test, | |
10184 ClientAuthCertCache_Direct_FalseStart) { | |
10185 net::HttpRequestInfo request_info; | |
10186 request_info.url = GURL("https://www.example.com/"); | |
10187 request_info.method = "GET"; | |
10188 request_info.load_flags = net::LOAD_NORMAL; | |
10189 | |
10190 scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); | |
10191 cert_request->host_and_port = "www.example.com:443"; | |
10192 | |
10193 // When TLS False Start is used, SSLClientSocket::Connect() calls will | |
10194 // return successfully after reading up to the peer's Certificate message. | |
10195 // This is to allow the caller to call SSLClientSocket::Write(), which can | |
10196 // enqueue application data to be sent in the same packet as the | |
10197 // ChangeCipherSpec and Finished messages. | |
10198 // The actual handshake will be finished when SSLClientSocket::Read() is | |
10199 // called, which expects to process the peer's ChangeCipherSpec and | |
10200 // Finished messages. If there was an error negotiating with the peer, | |
10201 // such as due to the peer requiring a client certificate when none was | |
10202 // supplied, the alert sent by the peer won't be processed until Read() is | |
10203 // called. | |
10204 | |
10205 // Like the non-False Start case, when a client certificate is requested by | |
10206 // the peer, the handshake is aborted during the Connect() call. | |
10207 // [ssl_]data1 represents the initial SSL handshake with the peer. | |
10208 SSLSocketDataProvider ssl_data1(ASYNC, net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); | |
10209 ssl_data1.cert_request_info = cert_request.get(); | |
10210 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); | |
10211 net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); | |
10212 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
10213 | |
10214 // When a client certificate is supplied, Connect() will not be aborted | |
10215 // when the peer requests the certificate. Instead, the handshake will | |
10216 // artificially succeed, allowing the caller to write the HTTP request to | |
10217 // the socket. The handshake messages are not processed until Read() is | |
10218 // called, which then detects that the handshake was aborted, due to the | |
10219 // peer sending a handshake_failure because it requires a client | |
10220 // certificate. | |
10221 SSLSocketDataProvider ssl_data2(ASYNC, net::OK); | |
10222 ssl_data2.cert_request_info = cert_request.get(); | |
10223 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); | |
10224 net::MockRead data2_reads[] = { | |
10225 net::MockRead(ASYNC /* async */, net::ERR_SSL_PROTOCOL_ERROR), | |
10226 }; | |
10227 net::StaticSocketDataProvider data2( | |
10228 data2_reads, arraysize(data2_reads), NULL, 0); | |
10229 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
10230 | |
10231 // As described in ClientAuthCertCache_Direct_NoFalseStart, [ssl_]data3 is | |
10232 // the data for the SSL handshake once the TLSv1.1 connection falls back to | |
10233 // TLSv1. It has the same behaviour as [ssl_]data2. | |
10234 SSLSocketDataProvider ssl_data3(ASYNC, net::OK); | |
10235 ssl_data3.cert_request_info = cert_request.get(); | |
10236 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data3); | |
10237 net::StaticSocketDataProvider data3( | |
10238 data2_reads, arraysize(data2_reads), NULL, 0); | |
10239 session_deps_.socket_factory->AddSocketDataProvider(&data3); | |
10240 | |
10241 // [ssl_]data4 is the data for the SSL handshake once the TLSv1 connection | |
10242 // falls back to SSLv3. It has the same behaviour as [ssl_]data2. | |
10243 SSLSocketDataProvider ssl_data4(ASYNC, net::OK); | |
10244 ssl_data4.cert_request_info = cert_request.get(); | |
10245 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data4); | |
10246 net::StaticSocketDataProvider data4( | |
10247 data2_reads, arraysize(data2_reads), NULL, 0); | |
10248 session_deps_.socket_factory->AddSocketDataProvider(&data4); | |
10249 | |
10250 // Need one more if TLSv1.2 is enabled. | |
10251 SSLSocketDataProvider ssl_data5(ASYNC, net::OK); | |
10252 ssl_data5.cert_request_info = cert_request.get(); | |
10253 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data5); | |
10254 net::StaticSocketDataProvider data5( | |
10255 data2_reads, arraysize(data2_reads), NULL, 0); | |
10256 session_deps_.socket_factory->AddSocketDataProvider(&data5); | |
10257 | |
10258 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
10259 scoped_ptr<HttpTransaction> trans( | |
10260 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
10261 | |
10262 // Begin the initial SSL handshake. | |
10263 TestCompletionCallback callback; | |
10264 int rv = trans->Start(&request_info, callback.callback(), net::BoundNetLog()); | |
10265 ASSERT_EQ(net::ERR_IO_PENDING, rv); | |
10266 | |
10267 // Complete the SSL handshake, which should abort due to requiring a | |
10268 // client certificate. | |
10269 rv = callback.WaitForResult(); | |
10270 ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); | |
10271 | |
10272 // Indicate that no certificate should be supplied. From the perspective | |
10273 // of SSLClientCertCache, NULL is just as meaningful as a real | |
10274 // certificate, so this is the same as supply a | |
10275 // legitimate-but-unacceptable certificate. | |
10276 rv = trans->RestartWithCertificate(NULL, callback.callback()); | |
10277 ASSERT_EQ(net::ERR_IO_PENDING, rv); | |
10278 | |
10279 // Ensure the certificate was added to the client auth cache before | |
10280 // allowing the connection to continue restarting. | |
10281 scoped_refptr<X509Certificate> client_cert; | |
10282 ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", | |
10283 &client_cert)); | |
10284 ASSERT_EQ(NULL, client_cert.get()); | |
10285 | |
10286 // Restart the handshake. This will consume ssl_data2, which fails, and | |
10287 // then consume ssl_data3 and ssl_data4, both of which should also fail. | |
10288 // The result code is checked against what ssl_data4 should return. | |
10289 rv = callback.WaitForResult(); | |
10290 ASSERT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); | |
10291 | |
10292 // Ensure that the client certificate is removed from the cache on a | |
10293 // handshake failure. | |
10294 ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", | |
10295 &client_cert)); | |
10296 } | |
10297 | |
10298 // Ensure that a client certificate is removed from the SSL client auth | |
10299 // cache when: | |
10300 // 1) An HTTPS proxy is involved. | |
10301 // 3) The HTTPS proxy requests a client certificate. | |
10302 // 4) The client supplies an invalid/unacceptable certificate for the | |
10303 // proxy. | |
10304 // The test is repeated twice, first for connecting to an HTTPS endpoint, | |
10305 // then for connecting to an HTTP endpoint. | |
10306 TEST_F(HttpNetworkTransactionSpdy2Test, ClientAuthCertCache_Proxy_Fail) { | |
10307 session_deps_.proxy_service.reset( | |
10308 ProxyService::CreateFixed("https://proxy:70")); | |
10309 CapturingBoundNetLog log; | |
10310 session_deps_.net_log = log.bound().net_log(); | |
10311 | |
10312 scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); | |
10313 cert_request->host_and_port = "proxy:70"; | |
10314 | |
10315 // See ClientAuthCertCache_Direct_NoFalseStart for the explanation of | |
10316 // [ssl_]data[1-3]. Rather than represending the endpoint | |
10317 // (www.example.com:443), they represent failures with the HTTPS proxy | |
10318 // (proxy:70). | |
10319 SSLSocketDataProvider ssl_data1(ASYNC, net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); | |
10320 ssl_data1.cert_request_info = cert_request.get(); | |
10321 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); | |
10322 net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); | |
10323 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
10324 | |
10325 SSLSocketDataProvider ssl_data2(ASYNC, net::ERR_SSL_PROTOCOL_ERROR); | |
10326 ssl_data2.cert_request_info = cert_request.get(); | |
10327 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); | |
10328 net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); | |
10329 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
10330 | |
10331 // TODO(wtc): find out why this unit test doesn't need [ssl_]data3. | |
10332 #if 0 | |
10333 SSLSocketDataProvider ssl_data3(ASYNC, net::ERR_SSL_PROTOCOL_ERROR); | |
10334 ssl_data3.cert_request_info = cert_request.get(); | |
10335 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data3); | |
10336 net::StaticSocketDataProvider data3(NULL, 0, NULL, 0); | |
10337 session_deps_.socket_factory->AddSocketDataProvider(&data3); | |
10338 #endif | |
10339 | |
10340 net::HttpRequestInfo requests[2]; | |
10341 requests[0].url = GURL("https://www.example.com/"); | |
10342 requests[0].method = "GET"; | |
10343 requests[0].load_flags = net::LOAD_NORMAL; | |
10344 | |
10345 requests[1].url = GURL("http://www.example.com/"); | |
10346 requests[1].method = "GET"; | |
10347 requests[1].load_flags = net::LOAD_NORMAL; | |
10348 | |
10349 for (size_t i = 0; i < arraysize(requests); ++i) { | |
10350 session_deps_.socket_factory->ResetNextMockIndexes(); | |
10351 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
10352 scoped_ptr<HttpNetworkTransaction> trans( | |
10353 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
10354 | |
10355 // Begin the SSL handshake with the proxy. | |
10356 TestCompletionCallback callback; | |
10357 int rv = trans->Start( | |
10358 &requests[i], callback.callback(), net::BoundNetLog()); | |
10359 ASSERT_EQ(net::ERR_IO_PENDING, rv); | |
10360 | |
10361 // Complete the SSL handshake, which should abort due to requiring a | |
10362 // client certificate. | |
10363 rv = callback.WaitForResult(); | |
10364 ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); | |
10365 | |
10366 // Indicate that no certificate should be supplied. From the perspective | |
10367 // of SSLClientCertCache, NULL is just as meaningful as a real | |
10368 // certificate, so this is the same as supply a | |
10369 // legitimate-but-unacceptable certificate. | |
10370 rv = trans->RestartWithCertificate(NULL, callback.callback()); | |
10371 ASSERT_EQ(net::ERR_IO_PENDING, rv); | |
10372 | |
10373 // Ensure the certificate was added to the client auth cache before | |
10374 // allowing the connection to continue restarting. | |
10375 scoped_refptr<X509Certificate> client_cert; | |
10376 ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("proxy:70", | |
10377 &client_cert)); | |
10378 ASSERT_EQ(NULL, client_cert.get()); | |
10379 // Ensure the certificate was NOT cached for the endpoint. This only | |
10380 // applies to HTTPS requests, but is fine to check for HTTP requests. | |
10381 ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", | |
10382 &client_cert)); | |
10383 | |
10384 // Restart the handshake. This will consume ssl_data2, which fails, and | |
10385 // then consume ssl_data3, which should also fail. The result code is | |
10386 // checked against what ssl_data3 should return. | |
10387 rv = callback.WaitForResult(); | |
10388 ASSERT_EQ(net::ERR_PROXY_CONNECTION_FAILED, rv); | |
10389 | |
10390 // Now that the new handshake has failed, ensure that the client | |
10391 // certificate was removed from the client auth cache. | |
10392 ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("proxy:70", | |
10393 &client_cert)); | |
10394 ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", | |
10395 &client_cert)); | |
10396 } | |
10397 } | |
10398 | |
10399 // Times out on Win7 dbg(2) bot. http://crbug.com/124776 | |
10400 #if defined(OS_WIN) | |
10401 #define MAYBE_UseIPConnectionPooling DISABLED_UseIPConnectionPooling | |
10402 #else | |
10403 #define MAYBE_UseIPConnectionPooling UseIPConnectionPooling | |
10404 #endif | |
10405 TEST_F(HttpNetworkTransactionSpdy2Test, MAYBE_UseIPConnectionPooling) { | |
10406 HttpStreamFactory::set_use_alternate_protocols(true); | |
10407 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
10408 | |
10409 // Set up a special HttpNetworkSession with a MockCachingHostResolver. | |
10410 session_deps_.host_resolver.reset(new MockCachingHostResolver()); | |
10411 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
10412 SpdySessionPoolPeer pool_peer(session->spdy_session_pool()); | |
10413 pool_peer.DisableDomainAuthenticationVerification(); | |
10414 | |
10415 SSLSocketDataProvider ssl(ASYNC, OK); | |
10416 ssl.SetNextProto(kProtoSPDY2); | |
10417 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
10418 | |
10419 scoped_ptr<SpdyFrame> host1_req( | |
10420 spdy_util_.ConstructSpdyGet("https://www.google.com", false, 1, LOWEST)); | |
10421 scoped_ptr<SpdyFrame> host2_req( | |
10422 spdy_util_.ConstructSpdyGet("https://www.gmail.com", false, 3, LOWEST)); | |
10423 MockWrite spdy_writes[] = { | |
10424 CreateMockWrite(*host1_req, 1), | |
10425 CreateMockWrite(*host2_req, 4), | |
10426 }; | |
10427 scoped_ptr<SpdyFrame> host1_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
10428 scoped_ptr<SpdyFrame> host1_resp_body(ConstructSpdyBodyFrame(1, true)); | |
10429 scoped_ptr<SpdyFrame> host2_resp(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
10430 scoped_ptr<SpdyFrame> host2_resp_body(ConstructSpdyBodyFrame(3, true)); | |
10431 MockRead spdy_reads[] = { | |
10432 CreateMockRead(*host1_resp, 2), | |
10433 CreateMockRead(*host1_resp_body, 3), | |
10434 CreateMockRead(*host2_resp, 5), | |
10435 CreateMockRead(*host2_resp_body, 6), | |
10436 MockRead(ASYNC, 0, 7), | |
10437 }; | |
10438 | |
10439 IPAddressNumber ip; | |
10440 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &ip)); | |
10441 IPEndPoint peer_addr = IPEndPoint(ip, 443); | |
10442 MockConnect connect(ASYNC, OK, peer_addr); | |
10443 OrderedSocketData spdy_data( | |
10444 connect, | |
10445 spdy_reads, arraysize(spdy_reads), | |
10446 spdy_writes, arraysize(spdy_writes)); | |
10447 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
10448 | |
10449 TestCompletionCallback callback; | |
10450 HttpRequestInfo request1; | |
10451 request1.method = "GET"; | |
10452 request1.url = GURL("https://www.google.com/"); | |
10453 request1.load_flags = 0; | |
10454 HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); | |
10455 | |
10456 int rv = trans1.Start(&request1, callback.callback(), BoundNetLog()); | |
10457 EXPECT_EQ(ERR_IO_PENDING, rv); | |
10458 EXPECT_EQ(OK, callback.WaitForResult()); | |
10459 | |
10460 const HttpResponseInfo* response = trans1.GetResponseInfo(); | |
10461 ASSERT_TRUE(response != NULL); | |
10462 ASSERT_TRUE(response->headers.get() != NULL); | |
10463 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
10464 | |
10465 std::string response_data; | |
10466 ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); | |
10467 EXPECT_EQ("hello!", response_data); | |
10468 | |
10469 // Preload www.gmail.com into HostCache. | |
10470 HostPortPair host_port("www.gmail.com", 443); | |
10471 HostResolver::RequestInfo resolve_info(host_port); | |
10472 AddressList ignored; | |
10473 rv = session_deps_.host_resolver->Resolve(resolve_info, &ignored, | |
10474 callback.callback(), NULL, | |
10475 BoundNetLog()); | |
10476 EXPECT_EQ(ERR_IO_PENDING, rv); | |
10477 rv = callback.WaitForResult(); | |
10478 EXPECT_EQ(OK, rv); | |
10479 | |
10480 HttpRequestInfo request2; | |
10481 request2.method = "GET"; | |
10482 request2.url = GURL("https://www.gmail.com/"); | |
10483 request2.load_flags = 0; | |
10484 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); | |
10485 | |
10486 rv = trans2.Start(&request2, callback.callback(), BoundNetLog()); | |
10487 EXPECT_EQ(ERR_IO_PENDING, rv); | |
10488 EXPECT_EQ(OK, callback.WaitForResult()); | |
10489 | |
10490 response = trans2.GetResponseInfo(); | |
10491 ASSERT_TRUE(response != NULL); | |
10492 ASSERT_TRUE(response->headers.get() != NULL); | |
10493 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
10494 EXPECT_TRUE(response->was_fetched_via_spdy); | |
10495 EXPECT_TRUE(response->was_npn_negotiated); | |
10496 ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); | |
10497 EXPECT_EQ("hello!", response_data); | |
10498 } | |
10499 #undef MAYBE_UseIPConnectionPooling | |
10500 | |
10501 TEST_F(HttpNetworkTransactionSpdy2Test, UseIPConnectionPoolingAfterResolution) { | |
10502 HttpStreamFactory::set_use_alternate_protocols(true); | |
10503 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
10504 | |
10505 // Set up a special HttpNetworkSession with a MockCachingHostResolver. | |
10506 session_deps_.host_resolver.reset(new MockCachingHostResolver()); | |
10507 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
10508 SpdySessionPoolPeer pool_peer(session->spdy_session_pool()); | |
10509 pool_peer.DisableDomainAuthenticationVerification(); | |
10510 | |
10511 SSLSocketDataProvider ssl(ASYNC, OK); | |
10512 ssl.SetNextProto(kProtoSPDY2); | |
10513 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
10514 | |
10515 scoped_ptr<SpdyFrame> host1_req( | |
10516 spdy_util_.ConstructSpdyGet("https://www.google.com", false, 1, LOWEST)); | |
10517 scoped_ptr<SpdyFrame> host2_req( | |
10518 spdy_util_.ConstructSpdyGet("https://www.gmail.com", false, 3, LOWEST)); | |
10519 MockWrite spdy_writes[] = { | |
10520 CreateMockWrite(*host1_req, 1), | |
10521 CreateMockWrite(*host2_req, 4), | |
10522 }; | |
10523 scoped_ptr<SpdyFrame> host1_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
10524 scoped_ptr<SpdyFrame> host1_resp_body(ConstructSpdyBodyFrame(1, true)); | |
10525 scoped_ptr<SpdyFrame> host2_resp(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
10526 scoped_ptr<SpdyFrame> host2_resp_body(ConstructSpdyBodyFrame(3, true)); | |
10527 MockRead spdy_reads[] = { | |
10528 CreateMockRead(*host1_resp, 2), | |
10529 CreateMockRead(*host1_resp_body, 3), | |
10530 CreateMockRead(*host2_resp, 5), | |
10531 CreateMockRead(*host2_resp_body, 6), | |
10532 MockRead(ASYNC, 0, 7), | |
10533 }; | |
10534 | |
10535 IPAddressNumber ip; | |
10536 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &ip)); | |
10537 IPEndPoint peer_addr = IPEndPoint(ip, 443); | |
10538 MockConnect connect(ASYNC, OK, peer_addr); | |
10539 OrderedSocketData spdy_data( | |
10540 connect, | |
10541 spdy_reads, arraysize(spdy_reads), | |
10542 spdy_writes, arraysize(spdy_writes)); | |
10543 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
10544 | |
10545 TestCompletionCallback callback; | |
10546 HttpRequestInfo request1; | |
10547 request1.method = "GET"; | |
10548 request1.url = GURL("https://www.google.com/"); | |
10549 request1.load_flags = 0; | |
10550 HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); | |
10551 | |
10552 int rv = trans1.Start(&request1, callback.callback(), BoundNetLog()); | |
10553 EXPECT_EQ(ERR_IO_PENDING, rv); | |
10554 EXPECT_EQ(OK, callback.WaitForResult()); | |
10555 | |
10556 const HttpResponseInfo* response = trans1.GetResponseInfo(); | |
10557 ASSERT_TRUE(response != NULL); | |
10558 ASSERT_TRUE(response->headers.get() != NULL); | |
10559 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
10560 | |
10561 std::string response_data; | |
10562 ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); | |
10563 EXPECT_EQ("hello!", response_data); | |
10564 | |
10565 HttpRequestInfo request2; | |
10566 request2.method = "GET"; | |
10567 request2.url = GURL("https://www.gmail.com/"); | |
10568 request2.load_flags = 0; | |
10569 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); | |
10570 | |
10571 rv = trans2.Start(&request2, callback.callback(), BoundNetLog()); | |
10572 EXPECT_EQ(ERR_IO_PENDING, rv); | |
10573 EXPECT_EQ(OK, callback.WaitForResult()); | |
10574 | |
10575 response = trans2.GetResponseInfo(); | |
10576 ASSERT_TRUE(response != NULL); | |
10577 ASSERT_TRUE(response->headers.get() != NULL); | |
10578 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
10579 EXPECT_TRUE(response->was_fetched_via_spdy); | |
10580 EXPECT_TRUE(response->was_npn_negotiated); | |
10581 ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); | |
10582 EXPECT_EQ("hello!", response_data); | |
10583 } | |
10584 | |
10585 class OneTimeCachingHostResolver : public net::HostResolver { | |
10586 public: | |
10587 explicit OneTimeCachingHostResolver(const HostPortPair& host_port) | |
10588 : host_port_(host_port) {} | |
10589 virtual ~OneTimeCachingHostResolver() {} | |
10590 | |
10591 RuleBasedHostResolverProc* rules() { return host_resolver_.rules(); } | |
10592 | |
10593 // HostResolver methods: | |
10594 virtual int Resolve(const RequestInfo& info, | |
10595 AddressList* addresses, | |
10596 const CompletionCallback& callback, | |
10597 RequestHandle* out_req, | |
10598 const BoundNetLog& net_log) OVERRIDE { | |
10599 return host_resolver_.Resolve( | |
10600 info, addresses, callback, out_req, net_log); | |
10601 } | |
10602 | |
10603 virtual int ResolveFromCache(const RequestInfo& info, | |
10604 AddressList* addresses, | |
10605 const BoundNetLog& net_log) OVERRIDE { | |
10606 int rv = host_resolver_.ResolveFromCache(info, addresses, net_log); | |
10607 if (rv == OK && info.host_port_pair().Equals(host_port_)) | |
10608 host_resolver_.GetHostCache()->clear(); | |
10609 return rv; | |
10610 } | |
10611 | |
10612 virtual void CancelRequest(RequestHandle req) OVERRIDE { | |
10613 host_resolver_.CancelRequest(req); | |
10614 } | |
10615 | |
10616 MockCachingHostResolver* GetMockHostResolver() { | |
10617 return &host_resolver_; | |
10618 } | |
10619 | |
10620 private: | |
10621 MockCachingHostResolver host_resolver_; | |
10622 const HostPortPair host_port_; | |
10623 }; | |
10624 | |
10625 // Times out on Win7 dbg(2) bot. http://crbug.com/124776 | |
10626 #if defined(OS_WIN) | |
10627 #define MAYBE_UseIPConnectionPoolingWithHostCacheExpiration \ | |
10628 DISABLED_UseIPConnectionPoolingWithHostCacheExpiration | |
10629 #else | |
10630 #define MAYBE_UseIPConnectionPoolingWithHostCacheExpiration \ | |
10631 UseIPConnectionPoolingWithHostCacheExpiration | |
10632 #endif | |
10633 TEST_F(HttpNetworkTransactionSpdy2Test, | |
10634 MAYBE_UseIPConnectionPoolingWithHostCacheExpiration) { | |
10635 HttpStreamFactory::set_use_alternate_protocols(true); | |
10636 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
10637 | |
10638 // Set up a special HttpNetworkSession with a OneTimeCachingHostResolver. | |
10639 OneTimeCachingHostResolver host_resolver(HostPortPair("www.gmail.com", 443)); | |
10640 HttpNetworkSession::Params params = | |
10641 SpdySessionDependencies::CreateSessionParams(&session_deps_); | |
10642 params.host_resolver = &host_resolver; | |
10643 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
10644 SpdySessionPoolPeer pool_peer(session->spdy_session_pool()); | |
10645 pool_peer.DisableDomainAuthenticationVerification(); | |
10646 | |
10647 SSLSocketDataProvider ssl(ASYNC, OK); | |
10648 ssl.SetNextProto(kProtoSPDY2); | |
10649 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
10650 | |
10651 scoped_ptr<SpdyFrame> host1_req( | |
10652 spdy_util_.ConstructSpdyGet("https://www.google.com", false, 1, LOWEST)); | |
10653 scoped_ptr<SpdyFrame> host2_req( | |
10654 spdy_util_.ConstructSpdyGet("https://www.gmail.com", false, 3, LOWEST)); | |
10655 MockWrite spdy_writes[] = { | |
10656 CreateMockWrite(*host1_req, 1), | |
10657 CreateMockWrite(*host2_req, 4), | |
10658 }; | |
10659 scoped_ptr<SpdyFrame> host1_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
10660 scoped_ptr<SpdyFrame> host1_resp_body(ConstructSpdyBodyFrame(1, true)); | |
10661 scoped_ptr<SpdyFrame> host2_resp(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
10662 scoped_ptr<SpdyFrame> host2_resp_body(ConstructSpdyBodyFrame(3, true)); | |
10663 MockRead spdy_reads[] = { | |
10664 CreateMockRead(*host1_resp, 2), | |
10665 CreateMockRead(*host1_resp_body, 3), | |
10666 CreateMockRead(*host2_resp, 5), | |
10667 CreateMockRead(*host2_resp_body, 6), | |
10668 MockRead(ASYNC, 0, 7), | |
10669 }; | |
10670 | |
10671 IPAddressNumber ip; | |
10672 ASSERT_TRUE(ParseIPLiteralToNumber("127.0.0.1", &ip)); | |
10673 IPEndPoint peer_addr = IPEndPoint(ip, 443); | |
10674 MockConnect connect(ASYNC, OK, peer_addr); | |
10675 OrderedSocketData spdy_data( | |
10676 connect, | |
10677 spdy_reads, arraysize(spdy_reads), | |
10678 spdy_writes, arraysize(spdy_writes)); | |
10679 session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); | |
10680 | |
10681 TestCompletionCallback callback; | |
10682 HttpRequestInfo request1; | |
10683 request1.method = "GET"; | |
10684 request1.url = GURL("https://www.google.com/"); | |
10685 request1.load_flags = 0; | |
10686 HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); | |
10687 | |
10688 int rv = trans1.Start(&request1, callback.callback(), BoundNetLog()); | |
10689 EXPECT_EQ(ERR_IO_PENDING, rv); | |
10690 EXPECT_EQ(OK, callback.WaitForResult()); | |
10691 | |
10692 const HttpResponseInfo* response = trans1.GetResponseInfo(); | |
10693 ASSERT_TRUE(response != NULL); | |
10694 ASSERT_TRUE(response->headers.get() != NULL); | |
10695 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
10696 | |
10697 std::string response_data; | |
10698 ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); | |
10699 EXPECT_EQ("hello!", response_data); | |
10700 | |
10701 // Preload cache entries into HostCache. | |
10702 HostResolver::RequestInfo resolve_info(HostPortPair("www.gmail.com", 443)); | |
10703 AddressList ignored; | |
10704 rv = host_resolver.Resolve(resolve_info, &ignored, callback.callback(), | |
10705 NULL, BoundNetLog()); | |
10706 EXPECT_EQ(ERR_IO_PENDING, rv); | |
10707 rv = callback.WaitForResult(); | |
10708 EXPECT_EQ(OK, rv); | |
10709 | |
10710 HttpRequestInfo request2; | |
10711 request2.method = "GET"; | |
10712 request2.url = GURL("https://www.gmail.com/"); | |
10713 request2.load_flags = 0; | |
10714 HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); | |
10715 | |
10716 rv = trans2.Start(&request2, callback.callback(), BoundNetLog()); | |
10717 EXPECT_EQ(ERR_IO_PENDING, rv); | |
10718 EXPECT_EQ(OK, callback.WaitForResult()); | |
10719 | |
10720 response = trans2.GetResponseInfo(); | |
10721 ASSERT_TRUE(response != NULL); | |
10722 ASSERT_TRUE(response->headers.get() != NULL); | |
10723 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
10724 EXPECT_TRUE(response->was_fetched_via_spdy); | |
10725 EXPECT_TRUE(response->was_npn_negotiated); | |
10726 ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); | |
10727 EXPECT_EQ("hello!", response_data); | |
10728 } | |
10729 #undef MAYBE_UseIPConnectionPoolingWithHostCacheExpiration | |
10730 | |
10731 TEST_F(HttpNetworkTransactionSpdy2Test, ReadPipelineEvictionFallback) { | |
10732 MockRead data_reads1[] = { | |
10733 MockRead(SYNCHRONOUS, ERR_PIPELINE_EVICTION), | |
10734 }; | |
10735 MockRead data_reads2[] = { | |
10736 MockRead("HTTP/1.0 200 OK\r\n\r\n"), | |
10737 MockRead("hello world"), | |
10738 MockRead(SYNCHRONOUS, OK), | |
10739 }; | |
10740 StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), NULL, 0); | |
10741 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), NULL, 0); | |
10742 StaticSocketDataProvider* data[] = { &data1, &data2 }; | |
10743 | |
10744 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); | |
10745 | |
10746 EXPECT_EQ(OK, out.rv); | |
10747 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); | |
10748 EXPECT_EQ("hello world", out.response_data); | |
10749 } | |
10750 | |
10751 TEST_F(HttpNetworkTransactionSpdy2Test, SendPipelineEvictionFallback) { | |
10752 MockWrite data_writes1[] = { | |
10753 MockWrite(SYNCHRONOUS, ERR_PIPELINE_EVICTION), | |
10754 }; | |
10755 MockWrite data_writes2[] = { | |
10756 MockWrite("GET / HTTP/1.1\r\n" | |
10757 "Host: www.google.com\r\n" | |
10758 "Connection: keep-alive\r\n\r\n"), | |
10759 }; | |
10760 MockRead data_reads2[] = { | |
10761 MockRead("HTTP/1.0 200 OK\r\n\r\n"), | |
10762 MockRead("hello world"), | |
10763 MockRead(SYNCHRONOUS, OK), | |
10764 }; | |
10765 StaticSocketDataProvider data1(NULL, 0, | |
10766 data_writes1, arraysize(data_writes1)); | |
10767 StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), | |
10768 data_writes2, arraysize(data_writes2)); | |
10769 StaticSocketDataProvider* data[] = { &data1, &data2 }; | |
10770 | |
10771 SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); | |
10772 | |
10773 EXPECT_EQ(OK, out.rv); | |
10774 EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); | |
10775 EXPECT_EQ("hello world", out.response_data); | |
10776 } | |
10777 | |
10778 TEST_F(HttpNetworkTransactionSpdy2Test, DoNotUseSpdySessionForHttp) { | |
10779 const std::string https_url = "https://www.google.com/"; | |
10780 const std::string http_url = "http://www.google.com:443/"; | |
10781 | |
10782 // SPDY GET for HTTPS URL | |
10783 scoped_ptr<SpdyFrame> req1( | |
10784 spdy_util_.ConstructSpdyGet(https_url.c_str(), false, 1, LOWEST)); | |
10785 | |
10786 MockWrite writes1[] = { | |
10787 CreateMockWrite(*req1, 0), | |
10788 }; | |
10789 | |
10790 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
10791 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); | |
10792 MockRead reads1[] = { | |
10793 CreateMockRead(*resp1, 1), | |
10794 CreateMockRead(*body1, 2), | |
10795 MockRead(ASYNC, ERR_IO_PENDING, 3) | |
10796 }; | |
10797 | |
10798 DelayedSocketData data1( | |
10799 1, reads1, arraysize(reads1), | |
10800 writes1, arraysize(writes1)); | |
10801 MockConnect connect_data1(ASYNC, OK); | |
10802 data1.set_connect_data(connect_data1); | |
10803 | |
10804 // HTTP GET for the HTTP URL | |
10805 MockWrite writes2[] = { | |
10806 MockWrite(ASYNC, 4, | |
10807 "GET / HTTP/1.1\r\n" | |
10808 "Host: www.google.com:443\r\n" | |
10809 "Connection: keep-alive\r\n\r\n"), | |
10810 }; | |
10811 | |
10812 MockRead reads2[] = { | |
10813 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), | |
10814 MockRead(ASYNC, 6, "hello"), | |
10815 MockRead(ASYNC, 7, OK), | |
10816 }; | |
10817 | |
10818 DelayedSocketData data2( | |
10819 1, reads2, arraysize(reads2), | |
10820 writes2, arraysize(writes2)); | |
10821 | |
10822 SSLSocketDataProvider ssl(ASYNC, OK); | |
10823 ssl.SetNextProto(kProtoSPDY2); | |
10824 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
10825 session_deps_.socket_factory->AddSocketDataProvider(&data1); | |
10826 session_deps_.socket_factory->AddSocketDataProvider(&data2); | |
10827 | |
10828 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
10829 | |
10830 // Start the first transaction to set up the SpdySession | |
10831 HttpRequestInfo request1; | |
10832 request1.method = "GET"; | |
10833 request1.url = GURL(https_url); | |
10834 request1.load_flags = 0; | |
10835 HttpNetworkTransaction trans1(LOWEST, session.get()); | |
10836 TestCompletionCallback callback1; | |
10837 EXPECT_EQ(ERR_IO_PENDING, | |
10838 trans1.Start(&request1, callback1.callback(), BoundNetLog())); | |
10839 base::MessageLoop::current()->RunUntilIdle(); | |
10840 | |
10841 EXPECT_EQ(OK, callback1.WaitForResult()); | |
10842 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); | |
10843 | |
10844 // Now, start the HTTP request | |
10845 HttpRequestInfo request2; | |
10846 request2.method = "GET"; | |
10847 request2.url = GURL(http_url); | |
10848 request2.load_flags = 0; | |
10849 HttpNetworkTransaction trans2(MEDIUM, session.get()); | |
10850 TestCompletionCallback callback2; | |
10851 EXPECT_EQ(ERR_IO_PENDING, | |
10852 trans2.Start(&request2, callback2.callback(), BoundNetLog())); | |
10853 base::MessageLoop::current()->RunUntilIdle(); | |
10854 | |
10855 EXPECT_EQ(OK, callback2.WaitForResult()); | |
10856 EXPECT_FALSE(trans2.GetResponseInfo()->was_fetched_via_spdy); | |
10857 } | |
10858 | |
10859 TEST_F(HttpNetworkTransactionSpdy2Test, DoNotUseSpdySessionForHttpOverTunnel) { | |
10860 const std::string https_url = "https://www.google.com/"; | |
10861 const std::string http_url = "http://www.google.com:443/"; | |
10862 | |
10863 // SPDY GET for HTTPS URL (through CONNECT tunnel) | |
10864 scoped_ptr<SpdyFrame> connect(spdy_util_.ConstructSpdyConnect(NULL, 0, 1)); | |
10865 scoped_ptr<SpdyFrame> req1( | |
10866 spdy_util_.ConstructSpdyGet(https_url.c_str(), false, 1, LOWEST)); | |
10867 | |
10868 // SPDY GET for HTTP URL (through the proxy, but not the tunnel) | |
10869 scoped_ptr<SpdyFrame> wrapped_req1(ConstructWrappedSpdyFrame(req1, 1)); | |
10870 const char* const headers[] = { | |
10871 "method", "GET", | |
10872 "url", http_url.c_str(), | |
10873 "host", "www.google.com:443", | |
10874 "scheme", "http", | |
10875 "version", "HTTP/1.1" | |
10876 }; | |
10877 scoped_ptr<SpdyFrame> req2(spdy_util_.ConstructSpdyControlFrame( | |
10878 NULL, 0, false, 3, MEDIUM, SYN_STREAM, CONTROL_FLAG_FIN, | |
10879 headers, arraysize(headers), 0)); | |
10880 | |
10881 MockWrite writes1[] = { | |
10882 CreateMockWrite(*connect, 0), | |
10883 CreateMockWrite(*wrapped_req1, 2), | |
10884 CreateMockWrite(*req2, 5), | |
10885 }; | |
10886 | |
10887 scoped_ptr<SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
10888 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
10889 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); | |
10890 scoped_ptr<SpdyFrame> wrapped_resp1(ConstructWrappedSpdyFrame(resp1, 1)); | |
10891 scoped_ptr<SpdyFrame> wrapped_body1(ConstructWrappedSpdyFrame(body1, 1)); | |
10892 scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
10893 scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, true)); | |
10894 MockRead reads1[] = { | |
10895 CreateMockRead(*conn_resp, 1), | |
10896 CreateMockRead(*wrapped_resp1, 3), | |
10897 CreateMockRead(*wrapped_body1, 4), | |
10898 CreateMockRead(*resp2, 6), | |
10899 CreateMockRead(*body2, 7), | |
10900 MockRead(ASYNC, ERR_IO_PENDING, 8) | |
10901 }; | |
10902 | |
10903 DeterministicSocketData data1(reads1, arraysize(reads1), | |
10904 writes1, arraysize(writes1)); | |
10905 MockConnect connect_data1(ASYNC, OK); | |
10906 data1.set_connect_data(connect_data1); | |
10907 | |
10908 session_deps_.proxy_service.reset( | |
10909 ProxyService::CreateFixedFromPacResult("HTTPS proxy:70")); | |
10910 CapturingNetLog log; | |
10911 session_deps_.net_log = &log; | |
10912 SSLSocketDataProvider ssl1(ASYNC, OK); // to the proxy | |
10913 ssl1.SetNextProto(kProtoSPDY2); | |
10914 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl1); | |
10915 SSLSocketDataProvider ssl2(ASYNC, OK); // to the server | |
10916 ssl2.SetNextProto(kProtoSPDY2); | |
10917 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl2); | |
10918 session_deps_.deterministic_socket_factory->AddSocketDataProvider(&data1); | |
10919 | |
10920 scoped_refptr<HttpNetworkSession> session( | |
10921 SpdySessionDependencies::SpdyCreateSessionDeterministic(&session_deps_)); | |
10922 | |
10923 // Start the first transaction to set up the SpdySession | |
10924 HttpRequestInfo request1; | |
10925 request1.method = "GET"; | |
10926 request1.url = GURL(https_url); | |
10927 request1.load_flags = 0; | |
10928 HttpNetworkTransaction trans1(LOWEST, session.get()); | |
10929 TestCompletionCallback callback1; | |
10930 EXPECT_EQ(ERR_IO_PENDING, | |
10931 trans1.Start(&request1, callback1.callback(), BoundNetLog())); | |
10932 base::MessageLoop::current()->RunUntilIdle(); | |
10933 data1.RunFor(4); | |
10934 | |
10935 EXPECT_EQ(OK, callback1.WaitForResult()); | |
10936 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); | |
10937 | |
10938 LoadTimingInfo load_timing_info1; | |
10939 EXPECT_TRUE(trans1.GetLoadTimingInfo(&load_timing_info1)); | |
10940 TestLoadTimingNotReusedWithPac(load_timing_info1, | |
10941 CONNECT_TIMING_HAS_SSL_TIMES); | |
10942 | |
10943 // Now, start the HTTP request | |
10944 HttpRequestInfo request2; | |
10945 request2.method = "GET"; | |
10946 request2.url = GURL(http_url); | |
10947 request2.load_flags = 0; | |
10948 HttpNetworkTransaction trans2(MEDIUM, session.get()); | |
10949 TestCompletionCallback callback2; | |
10950 EXPECT_EQ(ERR_IO_PENDING, | |
10951 trans2.Start(&request2, callback2.callback(), BoundNetLog())); | |
10952 base::MessageLoop::current()->RunUntilIdle(); | |
10953 data1.RunFor(3); | |
10954 | |
10955 EXPECT_EQ(OK, callback2.WaitForResult()); | |
10956 EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); | |
10957 | |
10958 LoadTimingInfo load_timing_info2; | |
10959 EXPECT_TRUE(trans2.GetLoadTimingInfo(&load_timing_info2)); | |
10960 // The established SPDY sessions is considered reused by the HTTP request. | |
10961 TestLoadTimingReusedWithPac(load_timing_info2); | |
10962 // HTTP requests over a SPDY session should have a different connection | |
10963 // socket_log_id than requests over a tunnel. | |
10964 EXPECT_NE(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); | |
10965 } | |
10966 | |
10967 TEST_F(HttpNetworkTransactionSpdy2Test, UseSpdySessionForHttpWhenForced) { | |
10968 HttpStreamFactory::set_force_spdy_always(true); | |
10969 const std::string https_url = "https://www.google.com/"; | |
10970 const std::string http_url = "http://www.google.com:443/"; | |
10971 | |
10972 // SPDY GET for HTTPS URL | |
10973 scoped_ptr<SpdyFrame> req1( | |
10974 spdy_util_.ConstructSpdyGet(https_url.c_str(), false, 1, LOWEST)); | |
10975 // SPDY GET for the HTTP URL | |
10976 scoped_ptr<SpdyFrame> req2( | |
10977 spdy_util_.ConstructSpdyGet(http_url.c_str(), false, 3, MEDIUM)); | |
10978 | |
10979 MockWrite writes[] = { | |
10980 CreateMockWrite(*req1, 1), | |
10981 CreateMockWrite(*req2, 4), | |
10982 }; | |
10983 | |
10984 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
10985 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); | |
10986 scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); | |
10987 scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(3, true)); | |
10988 MockRead reads[] = { | |
10989 CreateMockRead(*resp1, 2), | |
10990 CreateMockRead(*body1, 3), | |
10991 CreateMockRead(*resp2, 5), | |
10992 CreateMockRead(*body2, 6), | |
10993 MockRead(ASYNC, ERR_IO_PENDING, 7) | |
10994 }; | |
10995 | |
10996 OrderedSocketData data(reads, arraysize(reads), | |
10997 writes, arraysize(writes)); | |
10998 | |
10999 SSLSocketDataProvider ssl(ASYNC, OK); | |
11000 ssl.SetNextProto(kProtoSPDY2); | |
11001 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); | |
11002 session_deps_.socket_factory->AddSocketDataProvider(&data); | |
11003 | |
11004 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
11005 | |
11006 // Start the first transaction to set up the SpdySession | |
11007 HttpRequestInfo request1; | |
11008 request1.method = "GET"; | |
11009 request1.url = GURL(https_url); | |
11010 request1.load_flags = 0; | |
11011 HttpNetworkTransaction trans1(LOWEST, session.get()); | |
11012 TestCompletionCallback callback1; | |
11013 EXPECT_EQ(ERR_IO_PENDING, | |
11014 trans1.Start(&request1, callback1.callback(), BoundNetLog())); | |
11015 base::MessageLoop::current()->RunUntilIdle(); | |
11016 | |
11017 EXPECT_EQ(OK, callback1.WaitForResult()); | |
11018 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); | |
11019 | |
11020 // Now, start the HTTP request | |
11021 HttpRequestInfo request2; | |
11022 request2.method = "GET"; | |
11023 request2.url = GURL(http_url); | |
11024 request2.load_flags = 0; | |
11025 HttpNetworkTransaction trans2(MEDIUM, session.get()); | |
11026 TestCompletionCallback callback2; | |
11027 EXPECT_EQ(ERR_IO_PENDING, | |
11028 trans2.Start(&request2, callback2.callback(), BoundNetLog())); | |
11029 base::MessageLoop::current()->RunUntilIdle(); | |
11030 | |
11031 EXPECT_EQ(OK, callback2.WaitForResult()); | |
11032 EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); | |
11033 } | |
11034 | |
11035 // Test that in the case where we have a SPDY session to a SPDY proxy | |
11036 // that we do not pool other origins that resolve to the same IP when | |
11037 // the certificate does not match the new origin. | |
11038 // http://crbug.com/134690 | |
11039 TEST_F(HttpNetworkTransactionSpdy2Test, DoNotUseSpdySessionIfCertDoesNotMatch) { | |
11040 const std::string url1 = "http://www.google.com/"; | |
11041 const std::string url2 = "https://mail.google.com/"; | |
11042 const std::string ip_addr = "1.2.3.4"; | |
11043 | |
11044 // SPDY GET for HTTP URL (through SPDY proxy) | |
11045 const char* const headers[] = { | |
11046 "method", "GET", | |
11047 "url", url1.c_str(), | |
11048 "host", "www.google.com", | |
11049 "scheme", "http", | |
11050 "version", "HTTP/1.1" | |
11051 }; | |
11052 scoped_ptr<SpdyFrame> req1(spdy_util_.ConstructSpdyControlFrame( | |
11053 NULL, 0, false, 1, LOWEST, SYN_STREAM, CONTROL_FLAG_FIN, | |
11054 headers, arraysize(headers), 0)); | |
11055 | |
11056 MockWrite writes1[] = { | |
11057 CreateMockWrite(*req1, 0), | |
11058 }; | |
11059 | |
11060 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
11061 scoped_ptr<SpdyFrame> body1(ConstructSpdyBodyFrame(1, true)); | |
11062 MockRead reads1[] = { | |
11063 CreateMockRead(*resp1, 1), | |
11064 CreateMockRead(*body1, 2), | |
11065 MockRead(ASYNC, OK, 3) // EOF | |
11066 }; | |
11067 | |
11068 scoped_ptr<DeterministicSocketData> data1( | |
11069 new DeterministicSocketData(reads1, arraysize(reads1), | |
11070 writes1, arraysize(writes1))); | |
11071 IPAddressNumber ip; | |
11072 ASSERT_TRUE(ParseIPLiteralToNumber(ip_addr, &ip)); | |
11073 IPEndPoint peer_addr = IPEndPoint(ip, 443); | |
11074 MockConnect connect_data1(ASYNC, OK, peer_addr); | |
11075 data1->set_connect_data(connect_data1); | |
11076 | |
11077 // SPDY GET for HTTPS URL (direct) | |
11078 scoped_ptr<SpdyFrame> req2( | |
11079 spdy_util_.ConstructSpdyGet(url2.c_str(), false, 1, MEDIUM)); | |
11080 | |
11081 MockWrite writes2[] = { | |
11082 CreateMockWrite(*req2, 0), | |
11083 }; | |
11084 | |
11085 scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
11086 scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(1, true)); | |
11087 MockRead reads2[] = { | |
11088 CreateMockRead(*resp2, 1), | |
11089 CreateMockRead(*body2, 2), | |
11090 MockRead(ASYNC, OK, 3) // EOF | |
11091 }; | |
11092 | |
11093 scoped_ptr<DeterministicSocketData> data2( | |
11094 new DeterministicSocketData(reads2, arraysize(reads2), | |
11095 writes2, arraysize(writes2))); | |
11096 MockConnect connect_data2(ASYNC, OK); | |
11097 data2->set_connect_data(connect_data2); | |
11098 | |
11099 // Set up a proxy config that sends HTTP requests to a proxy, and | |
11100 // all others direct. | |
11101 ProxyConfig proxy_config; | |
11102 proxy_config.proxy_rules().ParseFromString("http=https://proxy:443"); | |
11103 CapturingProxyResolver* capturing_proxy_resolver = | |
11104 new CapturingProxyResolver(); | |
11105 session_deps_.proxy_service.reset(new ProxyService( | |
11106 new ProxyConfigServiceFixed(proxy_config), capturing_proxy_resolver, | |
11107 NULL)); | |
11108 | |
11109 // Load a valid cert. Note, that this does not need to | |
11110 // be valid for proxy because the MockSSLClientSocket does | |
11111 // not actually verify it. But SpdySession will use this | |
11112 // to see if it is valid for the new origin | |
11113 base::FilePath certs_dir = GetTestCertsDirectory(); | |
11114 scoped_refptr<X509Certificate> server_cert( | |
11115 ImportCertFromFile(certs_dir, "ok_cert.pem")); | |
11116 ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); | |
11117 | |
11118 SSLSocketDataProvider ssl1(ASYNC, OK); // to the proxy | |
11119 ssl1.SetNextProto(kProtoSPDY2); | |
11120 ssl1.cert = server_cert; | |
11121 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl1); | |
11122 session_deps_.deterministic_socket_factory->AddSocketDataProvider( | |
11123 data1.get()); | |
11124 | |
11125 SSLSocketDataProvider ssl2(ASYNC, OK); // to the server | |
11126 ssl2.SetNextProto(kProtoSPDY2); | |
11127 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl2); | |
11128 session_deps_.deterministic_socket_factory->AddSocketDataProvider( | |
11129 data2.get()); | |
11130 | |
11131 session_deps_.host_resolver.reset(new MockCachingHostResolver()); | |
11132 session_deps_.host_resolver->rules()->AddRule("mail.google.com", ip_addr); | |
11133 session_deps_.host_resolver->rules()->AddRule("proxy", ip_addr); | |
11134 | |
11135 scoped_refptr<HttpNetworkSession> session( | |
11136 SpdySessionDependencies::SpdyCreateSessionDeterministic(&session_deps_)); | |
11137 | |
11138 // Start the first transaction to set up the SpdySession | |
11139 HttpRequestInfo request1; | |
11140 request1.method = "GET"; | |
11141 request1.url = GURL(url1); | |
11142 request1.load_flags = 0; | |
11143 HttpNetworkTransaction trans1(LOWEST, session.get()); | |
11144 TestCompletionCallback callback1; | |
11145 ASSERT_EQ(ERR_IO_PENDING, | |
11146 trans1.Start(&request1, callback1.callback(), BoundNetLog())); | |
11147 data1->RunFor(3); | |
11148 | |
11149 ASSERT_TRUE(callback1.have_result()); | |
11150 EXPECT_EQ(OK, callback1.WaitForResult()); | |
11151 EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); | |
11152 | |
11153 // Now, start the HTTP request | |
11154 HttpRequestInfo request2; | |
11155 request2.method = "GET"; | |
11156 request2.url = GURL(url2); | |
11157 request2.load_flags = 0; | |
11158 HttpNetworkTransaction trans2(MEDIUM, session.get()); | |
11159 TestCompletionCallback callback2; | |
11160 EXPECT_EQ(ERR_IO_PENDING, | |
11161 trans2.Start(&request2, callback2.callback(), BoundNetLog())); | |
11162 base::MessageLoop::current()->RunUntilIdle(); | |
11163 data2->RunFor(3); | |
11164 | |
11165 ASSERT_TRUE(callback2.have_result()); | |
11166 EXPECT_EQ(OK, callback2.WaitForResult()); | |
11167 EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); | |
11168 } | |
11169 | |
11170 // Test to verify that a failed socket read (due to an ERR_CONNECTION_CLOSED | |
11171 // error) in SPDY session, removes the socket from pool and closes the SPDY | |
11172 // session. Verify that new url's from the same HttpNetworkSession (and a new | |
11173 // SpdySession) do work. http://crbug.com/224701 | |
11174 TEST_F(HttpNetworkTransactionSpdy2Test, ErrorSocketNotConnected) { | |
11175 const std::string https_url = "https://www.google.com/"; | |
11176 | |
11177 MockRead reads1[] = { | |
11178 MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED, 0) | |
11179 }; | |
11180 | |
11181 scoped_ptr<DeterministicSocketData> data1( | |
11182 new DeterministicSocketData(reads1, arraysize(reads1), NULL, 0)); | |
11183 data1->SetStop(1); | |
11184 | |
11185 scoped_ptr<SpdyFrame> req2( | |
11186 spdy_util_.ConstructSpdyGet(https_url.c_str(), false, 1, MEDIUM)); | |
11187 MockWrite writes2[] = { | |
11188 CreateMockWrite(*req2, 0), | |
11189 }; | |
11190 | |
11191 scoped_ptr<SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
11192 scoped_ptr<SpdyFrame> body2(ConstructSpdyBodyFrame(1, true)); | |
11193 MockRead reads2[] = { | |
11194 CreateMockRead(*resp2, 1), | |
11195 CreateMockRead(*body2, 2), | |
11196 MockRead(ASYNC, OK, 3) // EOF | |
11197 }; | |
11198 | |
11199 scoped_ptr<DeterministicSocketData> data2( | |
11200 new DeterministicSocketData(reads2, arraysize(reads2), | |
11201 writes2, arraysize(writes2))); | |
11202 | |
11203 SSLSocketDataProvider ssl1(ASYNC, OK); | |
11204 ssl1.SetNextProto(kProtoSPDY2); | |
11205 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl1); | |
11206 session_deps_.deterministic_socket_factory->AddSocketDataProvider( | |
11207 data1.get()); | |
11208 | |
11209 SSLSocketDataProvider ssl2(ASYNC, OK); | |
11210 ssl2.SetNextProto(kProtoSPDY2); | |
11211 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl2); | |
11212 session_deps_.deterministic_socket_factory->AddSocketDataProvider( | |
11213 data2.get()); | |
11214 | |
11215 scoped_refptr<HttpNetworkSession> session( | |
11216 SpdySessionDependencies::SpdyCreateSessionDeterministic(&session_deps_)); | |
11217 | |
11218 // Start the first transaction to set up the SpdySession and verify that | |
11219 // connection was closed. | |
11220 HttpRequestInfo request1; | |
11221 request1.method = "GET"; | |
11222 request1.url = GURL(https_url); | |
11223 request1.load_flags = 0; | |
11224 HttpNetworkTransaction trans1(MEDIUM, session.get()); | |
11225 TestCompletionCallback callback1; | |
11226 EXPECT_EQ(ERR_IO_PENDING, | |
11227 trans1.Start(&request1, callback1.callback(), BoundNetLog())); | |
11228 base::MessageLoop::current()->RunUntilIdle(); | |
11229 EXPECT_EQ(ERR_CONNECTION_CLOSED, callback1.WaitForResult()); | |
11230 | |
11231 // Now, start the second request and make sure it succeeds. | |
11232 HttpRequestInfo request2; | |
11233 request2.method = "GET"; | |
11234 request2.url = GURL(https_url); | |
11235 request2.load_flags = 0; | |
11236 HttpNetworkTransaction trans2(MEDIUM, session.get()); | |
11237 TestCompletionCallback callback2; | |
11238 EXPECT_EQ(ERR_IO_PENDING, | |
11239 trans2.Start(&request2, callback2.callback(), BoundNetLog())); | |
11240 base::MessageLoop::current()->RunUntilIdle(); | |
11241 data2->RunFor(3); | |
11242 | |
11243 ASSERT_TRUE(callback2.have_result()); | |
11244 EXPECT_EQ(OK, callback2.WaitForResult()); | |
11245 EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); | |
11246 } | |
11247 | |
11248 TEST_F(HttpNetworkTransactionSpdy2Test, CloseIdleSpdySessionToOpenNewOne) { | |
11249 HttpStreamFactory::SetNextProtos(SpdyNextProtos()); | |
11250 ClientSocketPoolManager::set_max_sockets_per_group( | |
11251 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); | |
11252 ClientSocketPoolManager::set_max_sockets_per_pool( | |
11253 HttpNetworkSession::NORMAL_SOCKET_POOL, 1); | |
11254 | |
11255 // Use two different hosts with different IPs so they don't get pooled. | |
11256 session_deps_.host_resolver->rules()->AddRule("www.a.com", "10.0.0.1"); | |
11257 session_deps_.host_resolver->rules()->AddRule("www.b.com", "10.0.0.2"); | |
11258 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); | |
11259 | |
11260 SSLSocketDataProvider ssl1(ASYNC, OK); | |
11261 ssl1.SetNextProto(kProtoSPDY2); | |
11262 SSLSocketDataProvider ssl2(ASYNC, OK); | |
11263 ssl2.SetNextProto(kProtoSPDY2); | |
11264 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); | |
11265 session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); | |
11266 | |
11267 scoped_ptr<SpdyFrame> host1_req(spdy_util_.ConstructSpdyGet( | |
11268 "https://www.a.com", false, 1, DEFAULT_PRIORITY)); | |
11269 MockWrite spdy1_writes[] = { | |
11270 CreateMockWrite(*host1_req, 1), | |
11271 }; | |
11272 scoped_ptr<SpdyFrame> host1_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
11273 scoped_ptr<SpdyFrame> host1_resp_body(ConstructSpdyBodyFrame(1, true)); | |
11274 MockRead spdy1_reads[] = { | |
11275 CreateMockRead(*host1_resp, 2), | |
11276 CreateMockRead(*host1_resp_body, 3), | |
11277 MockRead(ASYNC, ERR_IO_PENDING, 4), | |
11278 }; | |
11279 | |
11280 scoped_ptr<OrderedSocketData> spdy1_data( | |
11281 new OrderedSocketData( | |
11282 spdy1_reads, arraysize(spdy1_reads), | |
11283 spdy1_writes, arraysize(spdy1_writes))); | |
11284 session_deps_.socket_factory->AddSocketDataProvider(spdy1_data.get()); | |
11285 | |
11286 scoped_ptr<SpdyFrame> host2_req(spdy_util_.ConstructSpdyGet( | |
11287 "https://www.b.com", false, 1, DEFAULT_PRIORITY)); | |
11288 MockWrite spdy2_writes[] = { | |
11289 CreateMockWrite(*host2_req, 1), | |
11290 }; | |
11291 scoped_ptr<SpdyFrame> host2_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
11292 scoped_ptr<SpdyFrame> host2_resp_body(ConstructSpdyBodyFrame(1, true)); | |
11293 MockRead spdy2_reads[] = { | |
11294 CreateMockRead(*host2_resp, 2), | |
11295 CreateMockRead(*host2_resp_body, 3), | |
11296 MockRead(ASYNC, ERR_IO_PENDING, 4), | |
11297 }; | |
11298 | |
11299 scoped_ptr<OrderedSocketData> spdy2_data( | |
11300 new OrderedSocketData( | |
11301 spdy2_reads, arraysize(spdy2_reads), | |
11302 spdy2_writes, arraysize(spdy2_writes))); | |
11303 session_deps_.socket_factory->AddSocketDataProvider(spdy2_data.get()); | |
11304 | |
11305 MockWrite http_write[] = { | |
11306 MockWrite("GET / HTTP/1.1\r\n" | |
11307 "Host: www.a.com\r\n" | |
11308 "Connection: keep-alive\r\n\r\n"), | |
11309 }; | |
11310 | |
11311 MockRead http_read[] = { | |
11312 MockRead("HTTP/1.1 200 OK\r\n"), | |
11313 MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), | |
11314 MockRead("Content-Length: 6\r\n\r\n"), | |
11315 MockRead("hello!"), | |
11316 }; | |
11317 StaticSocketDataProvider http_data(http_read, arraysize(http_read), | |
11318 http_write, arraysize(http_write)); | |
11319 session_deps_.socket_factory->AddSocketDataProvider(&http_data); | |
11320 | |
11321 HostPortPair host_port_pair_a("www.a.com", 443); | |
11322 SpdySessionKey spdy_session_key_a( | |
11323 host_port_pair_a, ProxyServer::Direct(), kPrivacyModeDisabled); | |
11324 EXPECT_FALSE( | |
11325 session->spdy_session_pool()->HasSession(spdy_session_key_a)); | |
11326 | |
11327 TestCompletionCallback callback; | |
11328 HttpRequestInfo request1; | |
11329 request1.method = "GET"; | |
11330 request1.url = GURL("https://www.a.com/"); | |
11331 request1.load_flags = 0; | |
11332 scoped_ptr<HttpNetworkTransaction> trans( | |
11333 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
11334 | |
11335 int rv = trans->Start(&request1, callback.callback(), BoundNetLog()); | |
11336 EXPECT_EQ(ERR_IO_PENDING, rv); | |
11337 EXPECT_EQ(OK, callback.WaitForResult()); | |
11338 | |
11339 const HttpResponseInfo* response = trans->GetResponseInfo(); | |
11340 ASSERT_TRUE(response != NULL); | |
11341 ASSERT_TRUE(response->headers.get() != NULL); | |
11342 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
11343 EXPECT_TRUE(response->was_fetched_via_spdy); | |
11344 EXPECT_TRUE(response->was_npn_negotiated); | |
11345 | |
11346 std::string response_data; | |
11347 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
11348 EXPECT_EQ("hello!", response_data); | |
11349 trans.reset(); | |
11350 EXPECT_TRUE( | |
11351 session->spdy_session_pool()->HasSession(spdy_session_key_a)); | |
11352 | |
11353 HostPortPair host_port_pair_b("www.b.com", 443); | |
11354 SpdySessionKey spdy_session_key_b( | |
11355 host_port_pair_b, ProxyServer::Direct(), kPrivacyModeDisabled); | |
11356 EXPECT_FALSE( | |
11357 session->spdy_session_pool()->HasSession(spdy_session_key_b)); | |
11358 HttpRequestInfo request2; | |
11359 request2.method = "GET"; | |
11360 request2.url = GURL("https://www.b.com/"); | |
11361 request2.load_flags = 0; | |
11362 trans.reset(new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
11363 | |
11364 rv = trans->Start(&request2, callback.callback(), BoundNetLog()); | |
11365 EXPECT_EQ(ERR_IO_PENDING, rv); | |
11366 EXPECT_EQ(OK, callback.WaitForResult()); | |
11367 | |
11368 response = trans->GetResponseInfo(); | |
11369 ASSERT_TRUE(response != NULL); | |
11370 ASSERT_TRUE(response->headers.get() != NULL); | |
11371 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
11372 EXPECT_TRUE(response->was_fetched_via_spdy); | |
11373 EXPECT_TRUE(response->was_npn_negotiated); | |
11374 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
11375 EXPECT_EQ("hello!", response_data); | |
11376 EXPECT_FALSE( | |
11377 session->spdy_session_pool()->HasSession(spdy_session_key_a)); | |
11378 EXPECT_TRUE( | |
11379 session->spdy_session_pool()->HasSession(spdy_session_key_b)); | |
11380 | |
11381 HostPortPair host_port_pair_a1("www.a.com", 80); | |
11382 SpdySessionKey spdy_session_key_a1( | |
11383 host_port_pair_a1, ProxyServer::Direct(), kPrivacyModeDisabled); | |
11384 EXPECT_FALSE( | |
11385 session->spdy_session_pool()->HasSession(spdy_session_key_a1)); | |
11386 HttpRequestInfo request3; | |
11387 request3.method = "GET"; | |
11388 request3.url = GURL("http://www.a.com/"); | |
11389 request3.load_flags = 0; | |
11390 trans.reset(new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); | |
11391 | |
11392 rv = trans->Start(&request3, callback.callback(), BoundNetLog()); | |
11393 EXPECT_EQ(ERR_IO_PENDING, rv); | |
11394 EXPECT_EQ(OK, callback.WaitForResult()); | |
11395 | |
11396 response = trans->GetResponseInfo(); | |
11397 ASSERT_TRUE(response != NULL); | |
11398 ASSERT_TRUE(response->headers.get() != NULL); | |
11399 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); | |
11400 EXPECT_FALSE(response->was_fetched_via_spdy); | |
11401 EXPECT_FALSE(response->was_npn_negotiated); | |
11402 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); | |
11403 EXPECT_EQ("hello!", response_data); | |
11404 EXPECT_FALSE( | |
11405 session->spdy_session_pool()->HasSession(spdy_session_key_a)); | |
11406 EXPECT_FALSE( | |
11407 session->spdy_session_pool()->HasSession(spdy_session_key_b)); | |
11408 | |
11409 HttpStreamFactory::SetNextProtos(std::vector<std::string>()); | |
11410 } | |
11411 | |
11412 } // namespace net | |
OLD | NEW |