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