[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 5 | #include "base/memory/ref_counted.h" |
[email protected] | 5a76b81 | 2011-12-21 20:52:00 | [diff] [blame] | 6 | #include "net/base/completion_callback.h" |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 7 | #include "net/base/net_log_unittest.h" |
[email protected] | f54c8579 | 2012-03-08 19:06:41 | [diff] [blame] | 8 | #include "net/spdy/buffered_spdy_framer.h" |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 9 | #include "net/spdy/spdy_stream.h" |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 10 | #include "net/spdy/spdy_http_utils.h" |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 11 | #include "net/spdy/spdy_session.h" |
[email protected] | 39d13d94 | 2012-07-19 16:48:20 | [diff] [blame] | 12 | #include "net/spdy/spdy_stream_test_util.h" |
[email protected] | e3861ca | 2013-03-02 01:00:45 | [diff] [blame^] | 13 | #include "net/spdy/spdy_test_util_common.h" |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 14 | #include "net/spdy/spdy_test_util_spdy3.h" |
[email protected] | 5e248dc | 2012-08-21 02:27:32 | [diff] [blame] | 15 | #include "net/spdy/spdy_websocket_test_util_spdy3.h" |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 18 | using namespace net::test_spdy3; |
| 19 | |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 20 | // TODO(ukai): factor out common part with spdy_http_stream_unittest.cc |
[email protected] | b9ec688 | 2011-07-01 07:40:26 | [diff] [blame] | 21 | // |
| 22 | namespace net { |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 26 | SpdyFrame* ConstructSpdyBodyFrame(const char* data, int length) { |
[email protected] | f9cf557 | 2012-12-04 15:52:09 | [diff] [blame] | 27 | BufferedSpdyFramer framer(3, false); |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 28 | return framer.CreateDataFrame(1, data, length, DATA_FLAG_NONE); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | } // anonymous namespace |
| 32 | |
[email protected] | 39d13d94 | 2012-07-19 16:48:20 | [diff] [blame] | 33 | namespace test { |
| 34 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 35 | class SpdyStreamSpdy3Test : public testing::Test { |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 36 | protected: |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 37 | SpdyStreamSpdy3Test() { |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | scoped_refptr<SpdySession> CreateSpdySession() { |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 41 | HostPortPair host_port_pair("www.google.com", 80); |
[email protected] | 31e68d7 | 2010-08-25 06:36:58 | [diff] [blame] | 42 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 43 | scoped_refptr<SpdySession> session( |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 44 | session_->spdy_session_pool()->Get(pair, BoundNetLog())); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 45 | return session; |
| 46 | } |
| 47 | |
| 48 | virtual void TearDown() { |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 49 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | scoped_refptr<HttpNetworkSession> session_; |
| 53 | }; |
| 54 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 55 | TEST_F(SpdyStreamSpdy3Test, SendDataAfterOpen) { |
[email protected] | 30c942b | 2010-07-21 16:59:59 | [diff] [blame] | 56 | SpdySessionDependencies session_deps; |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 57 | |
[email protected] | 30c942b | 2010-07-21 16:59:59 | [diff] [blame] | 58 | session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 59 | SpdySessionPoolPeer pool_peer_(session_->spdy_session_pool()); |
| 60 | |
| 61 | const SpdyHeaderInfo kSynStartHeader = { |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 62 | SYN_STREAM, |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 63 | 1, |
| 64 | 0, |
[email protected] | 7207444 | 2012-04-17 20:51:57 | [diff] [blame] | 65 | ConvertRequestPriorityToSpdyPriority(LOWEST, 3), |
[email protected] | 1eb8143 | 2012-03-24 06:48:00 | [diff] [blame] | 66 | 0, |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 67 | CONTROL_FLAG_NONE, |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 68 | false, |
[email protected] | 5a7bb25 | 2013-02-09 00:21:05 | [diff] [blame] | 69 | RST_STREAM_INVALID, |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 70 | NULL, |
| 71 | 0, |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 72 | DATA_FLAG_NONE |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 73 | }; |
| 74 | static const char* const kGetHeaders[] = { |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 75 | ":method", |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 76 | "GET", |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 77 | ":scheme", |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 78 | "http", |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 79 | ":host", |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 80 | "www.google.com", |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 81 | ":path", |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 82 | "/", |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 83 | ":version", |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 84 | "HTTP/1.1", |
| 85 | }; |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 86 | scoped_ptr<SpdyFrame> req( |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 87 | ConstructSpdyPacket( |
| 88 | kSynStartHeader, NULL, 0, kGetHeaders, arraysize(kGetHeaders) / 2)); |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 89 | scoped_ptr<SpdyFrame> msg( |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 90 | ConstructSpdyBodyFrame("\0hello!\xff", 8)); |
| 91 | MockWrite writes[] = { |
| 92 | CreateMockWrite(*req), |
| 93 | CreateMockWrite(*msg), |
| 94 | }; |
| 95 | writes[0].sequence_number = 0; |
| 96 | writes[1].sequence_number = 2; |
| 97 | |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 98 | scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 99 | scoped_ptr<SpdyFrame> echo( |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 100 | ConstructSpdyBodyFrame("\0hello!\xff", 8)); |
| 101 | MockRead reads[] = { |
| 102 | CreateMockRead(*resp), |
| 103 | CreateMockRead(*echo), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 104 | MockRead(ASYNC, 0, 0), // EOF |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 105 | }; |
| 106 | reads[0].sequence_number = 1; |
| 107 | reads[1].sequence_number = 3; |
| 108 | reads[2].sequence_number = 4; |
| 109 | |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 110 | OrderedSocketData data(reads, arraysize(reads), |
| 111 | writes, arraysize(writes)); |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 112 | MockConnect connect_data(SYNCHRONOUS, OK); |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 113 | data.set_connect_data(connect_data); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 114 | |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 115 | session_deps.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 116 | |
| 117 | scoped_refptr<SpdySession> session(CreateSpdySession()); |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 118 | const char* kStreamUrl = "http://www.google.com/"; |
| 119 | GURL url(kStreamUrl); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 120 | |
| 121 | HostPortPair host_port_pair("www.google.com", 80); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 122 | scoped_refptr<TransportSocketParams> transport_params( |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 123 | new TransportSocketParams(host_port_pair, LOWEST, false, false, |
| 124 | OnHostResolutionCallback())); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 125 | |
| 126 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 127 | EXPECT_EQ(OK, connection->Init(host_port_pair.ToString(), transport_params, |
| 128 | LOWEST, CompletionCallback(), |
[email protected] | e5c02664 | 2012-03-17 00:14:02 | [diff] [blame] | 129 | session_->GetTransportSocketPool( |
| 130 | HttpNetworkSession::NORMAL_SOCKET_POOL), |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 131 | BoundNetLog())); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 132 | session->InitializeWithSocket(connection.release(), false, OK); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 133 | |
[email protected] | e3861ca | 2013-03-02 01:00:45 | [diff] [blame^] | 134 | |
| 135 | scoped_refptr<SpdyStream> stream = |
| 136 | CreateStreamSynchronously(session, url, LOWEST, BoundNetLog()); |
| 137 | ASSERT_TRUE(stream.get() != NULL); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 138 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(8)); |
| 139 | memcpy(buf->data(), "\0hello!\xff", 8); |
[email protected] | 5a76b81 | 2011-12-21 20:52:00 | [diff] [blame] | 140 | TestCompletionCallback callback; |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 141 | |
| 142 | scoped_ptr<TestSpdyStreamDelegate> delegate( |
[email protected] | 0837bc6 | 2012-08-03 07:31:26 | [diff] [blame] | 143 | new TestSpdyStreamDelegate( |
| 144 | stream.get(), NULL, buf.get(), callback.callback())); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 145 | stream->SetDelegate(delegate.get()); |
| 146 | |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 147 | EXPECT_FALSE(stream->HasUrl()); |
| 148 | |
[email protected] | 0a01d890fc | 2012-07-18 17:24:05 | [diff] [blame] | 149 | scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock); |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 150 | (*headers)[":method"] = "GET"; |
| 151 | (*headers)[":scheme"] = url.scheme(); |
| 152 | (*headers)[":host"] = url.host(); |
| 153 | (*headers)[":path"] = url.path(); |
| 154 | (*headers)[":version"] = "HTTP/1.1"; |
[email protected] | 0a01d890fc | 2012-07-18 17:24:05 | [diff] [blame] | 155 | stream->set_spdy_headers(headers.Pass()); |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 156 | EXPECT_TRUE(stream->HasUrl()); |
| 157 | EXPECT_EQ(kStreamUrl, stream->GetUrl().spec()); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 158 | |
[email protected] | a5c493b9 | 2010-08-06 23:04:29 | [diff] [blame] | 159 | EXPECT_EQ(ERR_IO_PENDING, stream->SendRequest(true)); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 160 | |
| 161 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 162 | |
| 163 | EXPECT_TRUE(delegate->send_headers_completed()); |
[email protected] | d42dedd0 | 2012-04-03 19:42:06 | [diff] [blame] | 164 | EXPECT_EQ("200", (*delegate->response())[":status"]); |
| 165 | EXPECT_EQ("HTTP/1.1", (*delegate->response())[":version"]); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 166 | EXPECT_EQ(std::string("\0hello!\xff", 8), delegate->received_data()); |
| 167 | EXPECT_EQ(8, delegate->data_sent()); |
| 168 | EXPECT_TRUE(delegate->closed()); |
| 169 | } |
| 170 | |
[email protected] | 0837bc6 | 2012-08-03 07:31:26 | [diff] [blame] | 171 | TEST_F(SpdyStreamSpdy3Test, SendHeaderAndDataAfterOpen) { |
| 172 | SpdySessionDependencies session_deps; |
| 173 | |
| 174 | session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps); |
| 175 | SpdySessionPoolPeer pool_peer_(session_->spdy_session_pool()); |
| 176 | |
[email protected] | 5e248dc | 2012-08-21 02:27:32 | [diff] [blame] | 177 | scoped_ptr<SpdyFrame> expected_request(ConstructSpdyWebSocketSynStream( |
[email protected] | 0837bc6 | 2012-08-03 07:31:26 | [diff] [blame] | 178 | 1, |
| 179 | "/chat", |
| 180 | "server.example.com", |
| 181 | "http://example.com")); |
| 182 | scoped_ptr<SpdyFrame> expected_headers(ConstructSpdyWebSocketHeadersFrame( |
| 183 | 1, "6", true)); |
| 184 | scoped_ptr<SpdyFrame> expected_message(ConstructSpdyBodyFrame("hello!", 6)); |
| 185 | MockWrite writes[] = { |
| 186 | CreateMockWrite(*expected_request), |
| 187 | CreateMockWrite(*expected_headers), |
| 188 | CreateMockWrite(*expected_message) |
| 189 | }; |
| 190 | writes[0].sequence_number = 0; |
| 191 | writes[1].sequence_number = 2; |
| 192 | writes[1].sequence_number = 3; |
| 193 | |
| 194 | scoped_ptr<SpdyFrame> response( |
| 195 | ConstructSpdyWebSocketSynReply(1)); |
| 196 | MockRead reads[] = { |
| 197 | CreateMockRead(*response), |
| 198 | MockRead(ASYNC, 0, 0), // EOF |
| 199 | }; |
| 200 | reads[0].sequence_number = 1; |
| 201 | reads[1].sequence_number = 4; |
| 202 | |
| 203 | OrderedSocketData data(reads, arraysize(reads), |
| 204 | writes, arraysize(writes)); |
| 205 | MockConnect connect_data(SYNCHRONOUS, OK); |
| 206 | data.set_connect_data(connect_data); |
| 207 | |
| 208 | session_deps.socket_factory->AddSocketDataProvider(&data); |
| 209 | |
| 210 | scoped_refptr<SpdySession> session(CreateSpdySession()); |
| 211 | const char* kStreamUrl = "ws://server.example.com/chat"; |
| 212 | GURL url(kStreamUrl); |
| 213 | |
| 214 | HostPortPair host_port_pair("server.example.com", 80); |
| 215 | scoped_refptr<TransportSocketParams> transport_params( |
| 216 | new TransportSocketParams(host_port_pair, LOWEST, false, false, |
| 217 | OnHostResolutionCallback())); |
| 218 | |
| 219 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 220 | EXPECT_EQ(OK, connection->Init(host_port_pair.ToString(), transport_params, |
| 221 | LOWEST, CompletionCallback(), |
| 222 | session_->GetTransportSocketPool( |
| 223 | HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 224 | BoundNetLog())); |
| 225 | session->InitializeWithSocket(connection.release(), false, OK); |
| 226 | |
[email protected] | e3861ca | 2013-03-02 01:00:45 | [diff] [blame^] | 227 | scoped_refptr<SpdyStream> stream = |
| 228 | CreateStreamSynchronously(session, url, HIGHEST, BoundNetLog()); |
| 229 | ASSERT_TRUE(stream.get() != NULL); |
[email protected] | 0837bc6 | 2012-08-03 07:31:26 | [diff] [blame] | 230 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(6)); |
| 231 | memcpy(buf->data(), "hello!", 6); |
| 232 | TestCompletionCallback callback; |
| 233 | scoped_ptr<SpdyHeaderBlock> message_headers(new SpdyHeaderBlock); |
| 234 | (*message_headers)[":opcode"] = "1"; |
| 235 | (*message_headers)[":length"] = "6"; |
| 236 | (*message_headers)[":fin"] = "1"; |
| 237 | |
| 238 | scoped_ptr<TestSpdyStreamDelegate> delegate( |
| 239 | new TestSpdyStreamDelegate(stream.get(), |
| 240 | message_headers.release(), |
| 241 | buf.get(), |
| 242 | callback.callback())); |
| 243 | stream->SetDelegate(delegate.get()); |
| 244 | |
| 245 | EXPECT_FALSE(stream->HasUrl()); |
| 246 | |
| 247 | scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock); |
| 248 | (*headers)[":path"] = url.path(); |
| 249 | (*headers)[":host"] = url.host(); |
| 250 | (*headers)[":version"] = "WebSocket/13"; |
| 251 | (*headers)[":scheme"] = url.scheme(); |
| 252 | (*headers)[":origin"] = "http://example.com"; |
| 253 | stream->set_spdy_headers(headers.Pass()); |
| 254 | EXPECT_TRUE(stream->HasUrl()); |
| 255 | |
| 256 | EXPECT_EQ(ERR_IO_PENDING, stream->SendRequest(true)); |
| 257 | |
| 258 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 259 | |
| 260 | EXPECT_TRUE(delegate->send_headers_completed()); |
| 261 | EXPECT_EQ("101", (*delegate->response())[":status"]); |
[email protected] | 050b360 | 2012-08-15 04:10:49 | [diff] [blame] | 262 | EXPECT_EQ(1, delegate->headers_sent()); |
[email protected] | 0837bc6 | 2012-08-03 07:31:26 | [diff] [blame] | 263 | EXPECT_EQ(std::string(), delegate->received_data()); |
[email protected] | 050b360 | 2012-08-15 04:10:49 | [diff] [blame] | 264 | EXPECT_EQ(6, delegate->data_sent()); |
[email protected] | 0837bc6 | 2012-08-03 07:31:26 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 267 | TEST_F(SpdyStreamSpdy3Test, PushedStream) { |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 268 | const char kStreamUrl[] = "http://www.google.com/"; |
| 269 | |
| 270 | SpdySessionDependencies session_deps; |
| 271 | session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps); |
| 272 | SpdySessionPoolPeer pool_peer_(session_->spdy_session_pool()); |
| 273 | scoped_refptr<SpdySession> spdy_session(CreateSpdySession()); |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 274 | |
| 275 | MockRead reads[] = { |
| 276 | MockRead(ASYNC, 0, 0), // EOF |
| 277 | }; |
| 278 | |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 279 | OrderedSocketData data(reads, arraysize(reads), NULL, 0); |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 280 | MockConnect connect_data(SYNCHRONOUS, OK); |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 281 | data.set_connect_data(connect_data); |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 282 | |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 283 | session_deps.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 284 | |
| 285 | HostPortPair host_port_pair("www.google.com", 80); |
| 286 | scoped_refptr<TransportSocketParams> transport_params( |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 287 | new TransportSocketParams(host_port_pair, LOWEST, false, false, |
| 288 | OnHostResolutionCallback())); |
| 289 | |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 290 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 291 | EXPECT_EQ(OK, connection->Init(host_port_pair.ToString(), transport_params, |
| 292 | LOWEST, CompletionCallback(), |
[email protected] | e5c02664 | 2012-03-17 00:14:02 | [diff] [blame] | 293 | session_->GetTransportSocketPool( |
| 294 | HttpNetworkSession::NORMAL_SOCKET_POOL), |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 295 | BoundNetLog())); |
| 296 | spdy_session->InitializeWithSocket(connection.release(), false, OK); |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 297 | BoundNetLog net_log; |
| 298 | |
| 299 | // Conjure up a stream. |
| 300 | scoped_refptr<SpdyStream> stream = new SpdyStream(spdy_session, |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 301 | true, |
| 302 | net_log); |
[email protected] | c92f4b454 | 2012-07-26 23:53:21 | [diff] [blame] | 303 | stream->set_stream_id(2); |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 304 | EXPECT_FALSE(stream->response_received()); |
| 305 | EXPECT_FALSE(stream->HasUrl()); |
| 306 | |
| 307 | // Set a couple of headers. |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 308 | SpdyHeaderBlock response; |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 309 | GURL url(kStreamUrl); |
| 310 | response[":host"] = url.host(); |
| 311 | response[":scheme"] = url.scheme(); |
| 312 | response[":path"] = url.path(); |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 313 | stream->OnResponseReceived(response); |
| 314 | |
| 315 | // Send some basic headers. |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 316 | SpdyHeaderBlock headers; |
[email protected] | d42dedd0 | 2012-04-03 19:42:06 | [diff] [blame] | 317 | response[":status"] = "200"; |
| 318 | response[":version"] = "OK"; |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 319 | stream->OnHeaders(headers); |
| 320 | |
| 321 | stream->set_response_received(); |
| 322 | EXPECT_TRUE(stream->response_received()); |
| 323 | EXPECT_TRUE(stream->HasUrl()); |
| 324 | EXPECT_EQ(kStreamUrl, stream->GetUrl().spec()); |
| 325 | } |
| 326 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 327 | TEST_F(SpdyStreamSpdy3Test, StreamError) { |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 328 | SpdySessionDependencies session_deps; |
| 329 | |
| 330 | session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps); |
| 331 | SpdySessionPoolPeer pool_peer_(session_->spdy_session_pool()); |
| 332 | |
| 333 | const SpdyHeaderInfo kSynStartHeader = { |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 334 | SYN_STREAM, |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 335 | 1, |
| 336 | 0, |
[email protected] | 7207444 | 2012-04-17 20:51:57 | [diff] [blame] | 337 | ConvertRequestPriorityToSpdyPriority(LOWEST, 3), |
[email protected] | 1eb8143 | 2012-03-24 06:48:00 | [diff] [blame] | 338 | 0, |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 339 | CONTROL_FLAG_NONE, |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 340 | false, |
[email protected] | 5a7bb25 | 2013-02-09 00:21:05 | [diff] [blame] | 341 | RST_STREAM_INVALID, |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 342 | NULL, |
| 343 | 0, |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 344 | DATA_FLAG_NONE |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 345 | }; |
| 346 | static const char* const kGetHeaders[] = { |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 347 | ":method", |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 348 | "GET", |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 349 | ":scheme", |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 350 | "http", |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 351 | ":host", |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 352 | "www.google.com", |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 353 | ":path", |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 354 | "/", |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 355 | ":version", |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 356 | "HTTP/1.1", |
| 357 | }; |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 358 | scoped_ptr<SpdyFrame> req( |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 359 | ConstructSpdyPacket( |
| 360 | kSynStartHeader, NULL, 0, kGetHeaders, arraysize(kGetHeaders) / 2)); |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 361 | scoped_ptr<SpdyFrame> msg( |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 362 | ConstructSpdyBodyFrame("\0hello!\xff", 8)); |
| 363 | MockWrite writes[] = { |
| 364 | CreateMockWrite(*req), |
| 365 | CreateMockWrite(*msg), |
| 366 | }; |
| 367 | writes[0].sequence_number = 0; |
| 368 | writes[1].sequence_number = 2; |
| 369 | |
[email protected] | ff98d7f0 | 2012-03-22 21:44:19 | [diff] [blame] | 370 | scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 371 | scoped_ptr<SpdyFrame> echo( |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 372 | ConstructSpdyBodyFrame("\0hello!\xff", 8)); |
| 373 | MockRead reads[] = { |
| 374 | CreateMockRead(*resp), |
| 375 | CreateMockRead(*echo), |
| 376 | MockRead(ASYNC, 0, 0), // EOF |
| 377 | }; |
| 378 | reads[0].sequence_number = 1; |
| 379 | reads[1].sequence_number = 3; |
| 380 | reads[2].sequence_number = 4; |
| 381 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 382 | CapturingBoundNetLog log; |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 383 | |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 384 | OrderedSocketData data(reads, arraysize(reads), |
| 385 | writes, arraysize(writes)); |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 386 | MockConnect connect_data(SYNCHRONOUS, OK); |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 387 | data.set_connect_data(connect_data); |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 388 | |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 389 | session_deps.socket_factory->AddSocketDataProvider(&data); |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 390 | |
| 391 | scoped_refptr<SpdySession> session(CreateSpdySession()); |
| 392 | const char* kStreamUrl = "http://www.google.com/"; |
| 393 | GURL url(kStreamUrl); |
| 394 | |
| 395 | HostPortPair host_port_pair("www.google.com", 80); |
| 396 | scoped_refptr<TransportSocketParams> transport_params( |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 397 | new TransportSocketParams(host_port_pair, LOWEST, false, false, |
| 398 | OnHostResolutionCallback())); |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 399 | |
| 400 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 401 | EXPECT_EQ(OK, connection->Init(host_port_pair.ToString(), transport_params, |
| 402 | LOWEST, CompletionCallback(), |
[email protected] | e5c02664 | 2012-03-17 00:14:02 | [diff] [blame] | 403 | session_->GetTransportSocketPool( |
| 404 | HttpNetworkSession::NORMAL_SOCKET_POOL), |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 405 | log.bound())); |
| 406 | session->InitializeWithSocket(connection.release(), false, OK); |
| 407 | |
[email protected] | e3861ca | 2013-03-02 01:00:45 | [diff] [blame^] | 408 | scoped_refptr<SpdyStream> stream = |
| 409 | CreateStreamSynchronously(session, url, LOWEST, log.bound()); |
| 410 | ASSERT_TRUE(stream.get() != NULL); |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 411 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(8)); |
| 412 | memcpy(buf->data(), "\0hello!\xff", 8); |
| 413 | TestCompletionCallback callback; |
| 414 | |
| 415 | scoped_ptr<TestSpdyStreamDelegate> delegate( |
[email protected] | 0837bc6 | 2012-08-03 07:31:26 | [diff] [blame] | 416 | new TestSpdyStreamDelegate( |
| 417 | stream.get(), NULL, buf.get(), callback.callback())); |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 418 | stream->SetDelegate(delegate.get()); |
| 419 | |
| 420 | EXPECT_FALSE(stream->HasUrl()); |
| 421 | |
[email protected] | 0a01d890fc | 2012-07-18 17:24:05 | [diff] [blame] | 422 | scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock); |
[email protected] | 0e861e9 | 2012-03-15 18:42:19 | [diff] [blame] | 423 | (*headers)[":method"] = "GET"; |
| 424 | (*headers)[":scheme"] = url.scheme(); |
| 425 | (*headers)[":host"] = url.host(); |
| 426 | (*headers)[":path"] = url.path(); |
| 427 | (*headers)[":version"] = "HTTP/1.1"; |
[email protected] | 0a01d890fc | 2012-07-18 17:24:05 | [diff] [blame] | 428 | stream->set_spdy_headers(headers.Pass()); |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 429 | EXPECT_TRUE(stream->HasUrl()); |
| 430 | EXPECT_EQ(kStreamUrl, stream->GetUrl().spec()); |
| 431 | |
| 432 | EXPECT_EQ(ERR_IO_PENDING, stream->SendRequest(true)); |
| 433 | |
[email protected] | 218f4b6 | 2012-07-20 20:12:41 | [diff] [blame] | 434 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 435 | |
[email protected] | c92f4b454 | 2012-07-26 23:53:21 | [diff] [blame] | 436 | const SpdyStreamId stream_id = stream->stream_id(); |
| 437 | |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 438 | EXPECT_TRUE(delegate->send_headers_completed()); |
[email protected] | d42dedd0 | 2012-04-03 19:42:06 | [diff] [blame] | 439 | EXPECT_EQ("200", (*delegate->response())[":status"]); |
| 440 | EXPECT_EQ("HTTP/1.1", (*delegate->response())[":version"]); |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 441 | EXPECT_EQ(std::string("\0hello!\xff", 8), delegate->received_data()); |
| 442 | EXPECT_EQ(8, delegate->data_sent()); |
| 443 | EXPECT_TRUE(delegate->closed()); |
| 444 | |
| 445 | // Check that the NetLog was filled reasonably. |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 446 | net::CapturingNetLog::CapturedEntryList entries; |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 447 | log.GetEntries(&entries); |
| 448 | EXPECT_LT(0u, entries.size()); |
| 449 | |
| 450 | // Check that we logged SPDY_STREAM_ERROR correctly. |
| 451 | int pos = net::ExpectLogContainsSomewhere( |
| 452 | entries, 0, |
| 453 | net::NetLog::TYPE_SPDY_STREAM_ERROR, |
| 454 | net::NetLog::PHASE_NONE); |
| 455 | |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 456 | int stream_id2; |
| 457 | ASSERT_TRUE(entries[pos].GetIntegerValue("stream_id", &stream_id2)); |
| 458 | EXPECT_EQ(static_cast<int>(stream_id), stream_id2); |
[email protected] | d245c34 | 2012-02-23 20:49:15 | [diff] [blame] | 459 | } |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 460 | |
[email protected] | 1e5ebd8 | 2013-02-20 20:07:24 | [diff] [blame] | 461 | // Call IncreaseSendWindowSize on a stream with a large enough delta |
| 462 | // to overflow an int32. The SpdyStream should handle that case |
| 463 | // gracefully. |
| 464 | TEST_F(SpdyStreamSpdy3Test, IncreaseSendWindowSizeOverflow) { |
| 465 | SpdySessionDependencies session_deps; |
| 466 | |
| 467 | session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps); |
| 468 | SpdySessionPoolPeer pool_peer_(session_->spdy_session_pool()); |
| 469 | |
| 470 | MockRead reads[] = { |
| 471 | MockRead(ASYNC, 0, 1), // EOF |
| 472 | }; |
| 473 | |
| 474 | // Triggered by the overflowing call to IncreaseSendWindowSize |
| 475 | // below. |
| 476 | scoped_ptr<SpdyFrame> rst( |
| 477 | ConstructSpdyRstStream(0, RST_STREAM_FLOW_CONTROL_ERROR)); |
| 478 | MockWrite writes[] = { |
| 479 | CreateMockWrite(*rst), |
| 480 | }; |
| 481 | writes[0].sequence_number = 0; |
| 482 | |
| 483 | CapturingBoundNetLog log; |
| 484 | |
| 485 | OrderedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 486 | MockConnect connect_data(SYNCHRONOUS, OK); |
| 487 | data.set_connect_data(connect_data); |
| 488 | |
| 489 | session_deps.socket_factory->AddSocketDataProvider(&data); |
| 490 | |
| 491 | scoped_refptr<SpdySession> session(CreateSpdySession()); |
| 492 | const char kStreamUrl[] = "http://www.google.com/"; |
| 493 | GURL url(kStreamUrl); |
| 494 | |
| 495 | HostPortPair host_port_pair("www.google.com", 80); |
| 496 | scoped_refptr<TransportSocketParams> transport_params( |
| 497 | new TransportSocketParams(host_port_pair, LOWEST, false, false, |
| 498 | OnHostResolutionCallback())); |
| 499 | |
| 500 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 501 | EXPECT_EQ(OK, connection->Init(host_port_pair.ToString(), transport_params, |
| 502 | LOWEST, CompletionCallback(), |
| 503 | session_->GetTransportSocketPool( |
| 504 | HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 505 | log.bound())); |
| 506 | session->InitializeWithSocket(connection.release(), false, OK); |
| 507 | |
[email protected] | e3861ca | 2013-03-02 01:00:45 | [diff] [blame^] | 508 | scoped_refptr<SpdyStream> stream = |
| 509 | CreateStreamSynchronously(session, url, LOWEST, log.bound()); |
| 510 | ASSERT_TRUE(stream.get() != NULL); |
[email protected] | 1e5ebd8 | 2013-02-20 20:07:24 | [diff] [blame] | 511 | TestCompletionCallback callback; |
| 512 | |
| 513 | scoped_ptr<TestSpdyStreamDelegate> delegate( |
| 514 | new TestSpdyStreamDelegate( |
| 515 | stream.get(), NULL, new IOBufferWithSize(8), callback.callback())); |
| 516 | stream->SetDelegate(delegate.get()); |
| 517 | |
| 518 | EXPECT_FALSE(stream->HasUrl()); |
| 519 | EXPECT_EQ(0u, stream->stream_id()); |
| 520 | EXPECT_FALSE(stream->closed()); |
| 521 | |
| 522 | int32 old_send_window_size = stream->send_window_size(); |
| 523 | ASSERT_GT(old_send_window_size, 0); |
| 524 | int32 delta_window_size = kint32max - old_send_window_size + 1; |
| 525 | stream->IncreaseSendWindowSize(delta_window_size); |
| 526 | EXPECT_EQ(old_send_window_size, stream->send_window_size()); |
| 527 | |
| 528 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 529 | } |
| 530 | |
[email protected] | 39d13d94 | 2012-07-19 16:48:20 | [diff] [blame] | 531 | } // namespace test |
| 532 | |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 533 | } // namespace net |