[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] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 7 | #include "net/spdy/spdy_stream.h" |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 8 | #include "net/spdy/spdy_http_utils.h" |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 9 | #include "net/spdy/spdy_session.h" |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 10 | #include "net/spdy/spdy_test_util.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 13 | // TODO(ukai): factor out common part with spdy_http_stream_unittest.cc |
[email protected] | b9ec688 | 2011-07-01 07:40:26 | [diff] [blame] | 14 | // |
| 15 | namespace net { |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 16 | |
| 17 | namespace { |
| 18 | |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 19 | class TestSpdyStreamDelegate : public SpdyStream::Delegate { |
| 20 | public: |
| 21 | TestSpdyStreamDelegate(SpdyStream* stream, |
| 22 | IOBufferWithSize* buf, |
[email protected] | 5a76b81 | 2011-12-21 20:52:00 | [diff] [blame] | 23 | const CompletionCallback& callback) |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 24 | : stream_(stream), |
| 25 | buf_(buf), |
| 26 | callback_(callback), |
| 27 | send_headers_completed_(false), |
| 28 | response_(new spdy::SpdyHeaderBlock), |
| 29 | data_sent_(0), |
| 30 | closed_(false) {} |
| 31 | virtual ~TestSpdyStreamDelegate() {} |
| 32 | |
| 33 | virtual bool OnSendHeadersComplete(int status) { |
| 34 | send_headers_completed_ = true; |
| 35 | return true; |
| 36 | } |
| 37 | virtual int OnSendBody() { |
| 38 | ADD_FAILURE() << "OnSendBody should not be called"; |
| 39 | return ERR_UNEXPECTED; |
| 40 | } |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 41 | virtual int OnSendBodyComplete(int /*status*/, bool* /*eof*/) { |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 42 | ADD_FAILURE() << "OnSendBodyComplete should not be called"; |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 43 | return ERR_UNEXPECTED; |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 44 | } |
[email protected] | 31024059 | 2010-08-05 21:04:19 | [diff] [blame] | 45 | |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 46 | virtual int OnResponseReceived(const spdy::SpdyHeaderBlock& response, |
| 47 | base::Time response_time, |
| 48 | int status) { |
| 49 | EXPECT_TRUE(send_headers_completed_); |
| 50 | *response_ = response; |
| 51 | if (buf_) { |
| 52 | EXPECT_EQ(ERR_IO_PENDING, |
| 53 | stream_->WriteStreamData(buf_.get(), buf_->size(), |
| 54 | spdy::DATA_FLAG_NONE)); |
| 55 | } |
| 56 | return status; |
| 57 | } |
| 58 | virtual void OnDataReceived(const char* buffer, int bytes) { |
| 59 | received_data_ += std::string(buffer, bytes); |
| 60 | } |
| 61 | virtual void OnDataSent(int length) { |
| 62 | data_sent_ += length; |
| 63 | } |
| 64 | virtual void OnClose(int status) { |
| 65 | closed_ = true; |
[email protected] | 5a76b81 | 2011-12-21 20:52:00 | [diff] [blame] | 66 | CompletionCallback callback = callback_; |
| 67 | callback_.Reset(); |
| 68 | callback.Run(OK); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 69 | } |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 70 | virtual void set_chunk_callback(net::ChunkCallback *) {} |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 71 | |
| 72 | bool send_headers_completed() const { return send_headers_completed_; } |
| 73 | const linked_ptr<spdy::SpdyHeaderBlock>& response() const { |
| 74 | return response_; |
| 75 | } |
| 76 | const std::string& received_data() const { return received_data_; } |
| 77 | int data_sent() const { return data_sent_; } |
| 78 | bool closed() const { return closed_; } |
| 79 | |
| 80 | private: |
| 81 | SpdyStream* stream_; |
| 82 | scoped_refptr<IOBufferWithSize> buf_; |
[email protected] | 5a76b81 | 2011-12-21 20:52:00 | [diff] [blame] | 83 | CompletionCallback callback_; |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 84 | bool send_headers_completed_; |
| 85 | linked_ptr<spdy::SpdyHeaderBlock> response_; |
| 86 | std::string received_data_; |
| 87 | int data_sent_; |
| 88 | bool closed_; |
| 89 | }; |
| 90 | |
| 91 | spdy::SpdyFrame* ConstructSpdyBodyFrame(const char* data, int length) { |
| 92 | spdy::SpdyFramer framer; |
| 93 | return framer.CreateDataFrame(1, data, length, spdy::DATA_FLAG_NONE); |
| 94 | } |
| 95 | |
| 96 | } // anonymous namespace |
| 97 | |
| 98 | class SpdyStreamTest : public testing::Test { |
| 99 | protected: |
| 100 | SpdyStreamTest() { |
| 101 | } |
| 102 | |
| 103 | scoped_refptr<SpdySession> CreateSpdySession() { |
| 104 | spdy::SpdyFramer::set_enable_compression_default(false); |
| 105 | HostPortPair host_port_pair("www.google.com", 80); |
[email protected] | 31e68d7 | 2010-08-25 06:36:58 | [diff] [blame] | 106 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 107 | scoped_refptr<SpdySession> session( |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 108 | session_->spdy_session_pool()->Get(pair, BoundNetLog())); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 109 | return session; |
| 110 | } |
| 111 | |
| 112 | virtual void TearDown() { |
| 113 | MessageLoop::current()->RunAllPending(); |
| 114 | } |
| 115 | |
| 116 | scoped_refptr<HttpNetworkSession> session_; |
| 117 | }; |
| 118 | |
| 119 | TEST_F(SpdyStreamTest, SendDataAfterOpen) { |
[email protected] | 30c942b | 2010-07-21 16:59:59 | [diff] [blame] | 120 | SpdySessionDependencies session_deps; |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 121 | |
[email protected] | 30c942b | 2010-07-21 16:59:59 | [diff] [blame] | 122 | session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 123 | SpdySessionPoolPeer pool_peer_(session_->spdy_session_pool()); |
| 124 | |
| 125 | const SpdyHeaderInfo kSynStartHeader = { |
| 126 | spdy::SYN_STREAM, |
| 127 | 1, |
| 128 | 0, |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 129 | net::ConvertRequestPriorityToSpdyPriority(LOWEST), |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 130 | spdy::CONTROL_FLAG_NONE, |
| 131 | false, |
| 132 | spdy::INVALID, |
| 133 | NULL, |
| 134 | 0, |
| 135 | spdy::DATA_FLAG_NONE |
| 136 | }; |
| 137 | static const char* const kGetHeaders[] = { |
| 138 | "method", |
| 139 | "GET", |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 140 | "scheme", |
| 141 | "http", |
| 142 | "host", |
| 143 | "www.google.com", |
| 144 | "path", |
| 145 | "/", |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 146 | "version", |
| 147 | "HTTP/1.1", |
| 148 | }; |
| 149 | scoped_ptr<spdy::SpdyFrame> req( |
| 150 | ConstructSpdyPacket( |
| 151 | kSynStartHeader, NULL, 0, kGetHeaders, arraysize(kGetHeaders) / 2)); |
| 152 | scoped_ptr<spdy::SpdyFrame> msg( |
| 153 | ConstructSpdyBodyFrame("\0hello!\xff", 8)); |
| 154 | MockWrite writes[] = { |
| 155 | CreateMockWrite(*req), |
| 156 | CreateMockWrite(*msg), |
| 157 | }; |
| 158 | writes[0].sequence_number = 0; |
| 159 | writes[1].sequence_number = 2; |
| 160 | |
| 161 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 162 | scoped_ptr<spdy::SpdyFrame> echo( |
| 163 | ConstructSpdyBodyFrame("\0hello!\xff", 8)); |
| 164 | MockRead reads[] = { |
| 165 | CreateMockRead(*resp), |
| 166 | CreateMockRead(*echo), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame^] | 167 | MockRead(ASYNC, 0, 0), // EOF |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 168 | }; |
| 169 | reads[0].sequence_number = 1; |
| 170 | reads[1].sequence_number = 3; |
| 171 | reads[2].sequence_number = 4; |
| 172 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 173 | scoped_ptr<OrderedSocketData> data( |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 174 | new OrderedSocketData(reads, arraysize(reads), |
| 175 | writes, arraysize(writes))); |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 176 | MockConnect connect_data(SYNCHRONOUS, OK); |
[email protected] | 1442b29a | 2010-07-20 11:14:54 | [diff] [blame] | 177 | data->set_connect_data(connect_data); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 178 | |
[email protected] | 3b782843 | 2010-08-18 18:33:27 | [diff] [blame] | 179 | session_deps.socket_factory->AddSocketDataProvider(data.get()); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 180 | SpdySession::SetSSLMode(false); |
| 181 | |
| 182 | scoped_refptr<SpdySession> session(CreateSpdySession()); |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 183 | const char* kStreamUrl = "http://www.google.com/"; |
| 184 | GURL url(kStreamUrl); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 185 | |
| 186 | HostPortPair host_port_pair("www.google.com", 80); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 187 | scoped_refptr<TransportSocketParams> transport_params( |
[email protected] | acdda41 | 2011-11-15 21:21:29 | [diff] [blame] | 188 | new TransportSocketParams(host_port_pair, LOWEST, false, false)); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 189 | |
| 190 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 191 | EXPECT_EQ(OK, connection->Init(host_port_pair.ToString(), transport_params, |
| 192 | LOWEST, CompletionCallback(), |
| 193 | session_->GetTransportSocketPool(), |
| 194 | BoundNetLog())); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 195 | session->InitializeWithSocket(connection.release(), false, OK); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 196 | |
| 197 | scoped_refptr<SpdyStream> stream; |
| 198 | ASSERT_EQ( |
| 199 | OK, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 200 | session->CreateStream(url, LOWEST, &stream, BoundNetLog(), |
| 201 | CompletionCallback())); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 202 | scoped_refptr<IOBufferWithSize> buf(new IOBufferWithSize(8)); |
| 203 | memcpy(buf->data(), "\0hello!\xff", 8); |
[email protected] | 5a76b81 | 2011-12-21 20:52:00 | [diff] [blame] | 204 | TestCompletionCallback callback; |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 205 | |
| 206 | scoped_ptr<TestSpdyStreamDelegate> delegate( |
[email protected] | 5a76b81 | 2011-12-21 20:52:00 | [diff] [blame] | 207 | new TestSpdyStreamDelegate(stream.get(), buf.get(), callback.callback())); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 208 | stream->SetDelegate(delegate.get()); |
| 209 | |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 210 | EXPECT_FALSE(stream->HasUrl()); |
| 211 | |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 212 | linked_ptr<spdy::SpdyHeaderBlock> headers(new spdy::SpdyHeaderBlock); |
| 213 | (*headers)["method"] = "GET"; |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 214 | (*headers)["scheme"] = url.scheme(); |
| 215 | (*headers)["host"] = url.host(); |
| 216 | (*headers)["path"] = url.path(); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 217 | (*headers)["version"] = "HTTP/1.1"; |
| 218 | stream->set_spdy_headers(headers); |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 219 | EXPECT_TRUE(stream->HasUrl()); |
| 220 | EXPECT_EQ(kStreamUrl, stream->GetUrl().spec()); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 221 | |
[email protected] | a5c493b9 | 2010-08-06 23:04:29 | [diff] [blame] | 222 | EXPECT_EQ(ERR_IO_PENDING, stream->SendRequest(true)); |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 223 | |
| 224 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 225 | |
| 226 | EXPECT_TRUE(delegate->send_headers_completed()); |
| 227 | EXPECT_EQ("200", (*delegate->response())["status"]); |
| 228 | EXPECT_EQ("HTTP/1.1", (*delegate->response())["version"]); |
| 229 | EXPECT_EQ(std::string("\0hello!\xff", 8), delegate->received_data()); |
| 230 | EXPECT_EQ(8, delegate->data_sent()); |
| 231 | EXPECT_TRUE(delegate->closed()); |
| 232 | } |
| 233 | |
[email protected] | a7a265ef | 2010-12-08 18:05:57 | [diff] [blame] | 234 | TEST_F(SpdyStreamTest, PushedStream) { |
| 235 | const char kStreamUrl[] = "http://www.google.com/"; |
| 236 | |
| 237 | SpdySessionDependencies session_deps; |
| 238 | session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps); |
| 239 | SpdySessionPoolPeer pool_peer_(session_->spdy_session_pool()); |
| 240 | scoped_refptr<SpdySession> spdy_session(CreateSpdySession()); |
| 241 | BoundNetLog net_log; |
| 242 | |
| 243 | // Conjure up a stream. |
| 244 | scoped_refptr<SpdyStream> stream = new SpdyStream(spdy_session, |
| 245 | 2, |
| 246 | true, |
| 247 | net_log); |
| 248 | EXPECT_FALSE(stream->response_received()); |
| 249 | EXPECT_FALSE(stream->HasUrl()); |
| 250 | |
| 251 | // Set a couple of headers. |
| 252 | spdy::SpdyHeaderBlock response; |
| 253 | response["url"] = kStreamUrl; |
| 254 | stream->OnResponseReceived(response); |
| 255 | |
| 256 | // Send some basic headers. |
| 257 | spdy::SpdyHeaderBlock headers; |
| 258 | response["status"] = "200"; |
| 259 | response["version"] = "OK"; |
| 260 | stream->OnHeaders(headers); |
| 261 | |
| 262 | stream->set_response_received(); |
| 263 | EXPECT_TRUE(stream->response_received()); |
| 264 | EXPECT_TRUE(stream->HasUrl()); |
| 265 | EXPECT_EQ(kStreamUrl, stream->GetUrl().spec()); |
| 266 | } |
| 267 | |
| 268 | |
[email protected] | 4f38642 | 2010-07-20 04:19:49 | [diff] [blame] | 269 | } // namespace net |