OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/tools/quic/quic_spdy_server_stream.h" | 5 #include "net/tools/quic/quic_spdy_server_stream.h" |
6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" |
7 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
8 #include "net/quic/quic_connection.h" | 9 #include "net/quic/quic_connection.h" |
9 #include "net/quic/quic_protocol.h" | 10 #include "net/quic/quic_protocol.h" |
| 11 #include "net/quic/quic_spdy_compressor.h" |
| 12 #include "net/quic/quic_utils.h" |
10 #include "net/quic/test_tools/quic_test_utils.h" | 13 #include "net/quic/test_tools/quic_test_utils.h" |
| 14 #include "net/tools/epoll_server/epoll_server.h" |
| 15 #include "net/tools/quic/quic_in_memory_cache.h" |
| 16 #include "net/tools/quic/spdy_utils.h" |
| 17 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h" |
11 #include "net/tools/quic/test_tools/quic_test_utils.h" | 18 #include "net/tools/quic/test_tools/quic_test_utils.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
13 | 21 |
14 using base::StringPiece; | 22 using base::StringPiece; |
15 using net::tools::test::MockConnection; | 23 using net::tools::test::MockConnection; |
16 using net::test::MockSession; | 24 using net::test::MockSession; |
| 25 using std::string; |
| 26 using testing::_; |
| 27 using testing::AnyNumber; |
| 28 using testing::Invoke; |
| 29 using testing::InvokeArgument; |
| 30 using testing::InSequence; |
| 31 using testing::Return; |
| 32 using testing::StrEq; |
| 33 using testing::StrictMock; |
| 34 using testing::WithArgs; |
17 | 35 |
18 namespace net { | 36 namespace net { |
19 namespace tools { | 37 namespace tools { |
20 namespace test { | 38 namespace test { |
| 39 |
| 40 class QuicSpdyServerStreamPeer : public QuicSpdyServerStream { |
| 41 public: |
| 42 QuicSpdyServerStreamPeer(QuicStreamId stream_id, QuicSession* session) |
| 43 : QuicSpdyServerStream(stream_id, session) { |
| 44 } |
| 45 |
| 46 using QuicSpdyServerStream::SendResponse; |
| 47 using QuicSpdyServerStream::SendErrorResponse; |
| 48 |
| 49 const string& body() { |
| 50 return body_; |
| 51 } |
| 52 |
| 53 const BalsaHeaders& headers() { |
| 54 return headers_; |
| 55 } |
| 56 |
| 57 BalsaHeaders* mutable_headers() { |
| 58 return &headers_; |
| 59 } |
| 60 }; |
| 61 |
21 namespace { | 62 namespace { |
22 | 63 |
23 class QuicSpdyServerStreamTest : public ::testing::Test { | 64 class QuicSpdyServerStreamTest : public ::testing::Test { |
24 public: | 65 public: |
25 QuicSpdyServerStreamTest() | 66 QuicSpdyServerStreamTest() |
26 : connection_(new MockConnection(1, IPEndPoint(), false)), | 67 : session_(new MockConnection(1, IPEndPoint(), true), true), |
27 session_(connection_, true), | 68 body_("hello world") { |
28 stream_(1, &session_) { | 69 BalsaHeaders request_headers; |
29 } | 70 request_headers.SetRequestFirstlineFromStringPieces( |
30 | 71 "POST", "https://www.google.com/", "HTTP/1.1"); |
31 MockConnection* connection_; | 72 request_headers.ReplaceOrAppendHeader("content-length", "11"); |
32 MockSession session_; | 73 |
33 QuicSpdyServerStream stream_; | 74 headers_string_ = SpdyUtils::SerializeRequestHeaders(request_headers); |
| 75 stream_.reset(new QuicSpdyServerStreamPeer(3, &session_)); |
| 76 } |
| 77 |
| 78 QuicConsumedData ValidateHeaders(const struct iovec* iov) { |
| 79 StringPiece headers = |
| 80 StringPiece(static_cast<const char*>(iov[0].iov_base), iov[0].iov_len); |
| 81 headers_string_ = SpdyUtils::SerializeResponseHeaders( |
| 82 response_headers_); |
| 83 QuicSpdyDecompressor decompressor; |
| 84 TestDecompressorVisitor visitor; |
| 85 |
| 86 // First the header id, then the compressed data. |
| 87 EXPECT_EQ(1, headers[0]); |
| 88 EXPECT_EQ(0, headers[1]); |
| 89 EXPECT_EQ(0, headers[2]); |
| 90 EXPECT_EQ(0, headers[3]); |
| 91 EXPECT_EQ(static_cast<size_t>(headers.length() - 4), |
| 92 decompressor.DecompressData(headers.substr(4), &visitor)); |
| 93 |
| 94 EXPECT_EQ(headers_string_, visitor.data()); |
| 95 |
| 96 return QuicConsumedData(headers.size(), false); |
| 97 } |
| 98 |
| 99 static void SetUpTestCase() { |
| 100 QuicInMemoryCachePeer::ResetForTests(); |
| 101 } |
| 102 |
| 103 virtual void SetUp() { |
| 104 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
| 105 |
| 106 BalsaHeaders request_headers, response_headers; |
| 107 StringPiece body("Yum"); |
| 108 request_headers.SetRequestFirstlineFromStringPieces( |
| 109 "GET", |
| 110 "https://www.google.com/foo", |
| 111 "HTTP/1.1"); |
| 112 response_headers.SetRequestFirstlineFromStringPieces("HTTP/1.1", |
| 113 "200", |
| 114 "OK"); |
| 115 response_headers.AppendHeader("content-length", |
| 116 base::IntToString(body.length())); |
| 117 |
| 118 // Check if response already exists and matches. |
| 119 const QuicInMemoryCache::Response* cached_response = |
| 120 cache->GetResponse(request_headers); |
| 121 if (cached_response != NULL) { |
| 122 string cached_response_headers_str, response_headers_str; |
| 123 cached_response->headers().DumpToString(&cached_response_headers_str); |
| 124 response_headers.DumpToString(&response_headers_str); |
| 125 CHECK_EQ(cached_response_headers_str, response_headers_str); |
| 126 CHECK_EQ(cached_response->body(), body); |
| 127 return; |
| 128 } |
| 129 |
| 130 cache->AddResponse(request_headers, response_headers, body); |
| 131 } |
| 132 |
| 133 BalsaHeaders response_headers_; |
| 134 EpollServer eps_; |
| 135 StrictMock<MockSession> session_; |
| 136 scoped_ptr<QuicSpdyServerStreamPeer> stream_; |
| 137 string headers_string_; |
| 138 string body_; |
34 }; | 139 }; |
35 | 140 |
| 141 QuicConsumedData ConsumeAllData( |
| 142 QuicStreamId id, |
| 143 const struct iovec* iov, |
| 144 int iov_count, |
| 145 QuicStreamOffset offset, |
| 146 bool fin, |
| 147 QuicAckNotifier::DelegateInterface* /*ack_notifier_delegate*/) { |
| 148 ssize_t consumed_length = 0; |
| 149 for (int i = 0; i < iov_count; ++i) { |
| 150 consumed_length += iov[i].iov_len; |
| 151 } |
| 152 return QuicConsumedData(consumed_length, fin); |
| 153 } |
| 154 |
| 155 TEST_F(QuicSpdyServerStreamTest, TestFraming) { |
| 156 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(AnyNumber()). |
| 157 WillRepeatedly(Invoke(ConsumeAllData)); |
| 158 |
| 159 EXPECT_EQ(headers_string_.size(), stream_->ProcessData( |
| 160 headers_string_.c_str(), headers_string_.size())); |
| 161 EXPECT_EQ(body_.size(), stream_->ProcessData(body_.c_str(), body_.size())); |
| 162 EXPECT_EQ(11u, stream_->headers().content_length()); |
| 163 EXPECT_EQ("https://www.google.com/", stream_->headers().request_uri()); |
| 164 EXPECT_EQ("POST", stream_->headers().request_method()); |
| 165 EXPECT_EQ(body_, stream_->body()); |
| 166 } |
| 167 |
| 168 TEST_F(QuicSpdyServerStreamTest, TestFramingOnePacket) { |
| 169 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(AnyNumber()). |
| 170 WillRepeatedly(Invoke(ConsumeAllData)); |
| 171 |
| 172 string message = headers_string_ + body_; |
| 173 |
| 174 EXPECT_EQ(message.size(), stream_->ProcessData( |
| 175 message.c_str(), message.size())); |
| 176 EXPECT_EQ(11u, stream_->headers().content_length()); |
| 177 EXPECT_EQ("https://www.google.com/", |
| 178 stream_->headers().request_uri()); |
| 179 EXPECT_EQ("POST", stream_->headers().request_method()); |
| 180 EXPECT_EQ(body_, stream_->body()); |
| 181 } |
| 182 |
| 183 TEST_F(QuicSpdyServerStreamTest, TestFramingExtraData) { |
| 184 string large_body = "hello world!!!!!!"; |
| 185 |
| 186 // We'll automatically write out an error (headers + body) |
| 187 EXPECT_CALL(session_, WritevData(_, _, _, _, _, _)).Times(AnyNumber()). |
| 188 WillRepeatedly(Invoke(ConsumeAllData)); |
| 189 |
| 190 EXPECT_EQ(headers_string_.size(), stream_->ProcessData( |
| 191 headers_string_.c_str(), headers_string_.size())); |
| 192 // Content length is still 11. This will register as an error and we won't |
| 193 // accept the bytes. |
| 194 stream_->ProcessData(large_body.c_str(), large_body.size()); |
| 195 EXPECT_EQ(11u, stream_->headers().content_length()); |
| 196 EXPECT_EQ("https://www.google.com/", stream_->headers().request_uri()); |
| 197 EXPECT_EQ("POST", stream_->headers().request_method()); |
| 198 } |
| 199 |
| 200 TEST_F(QuicSpdyServerStreamTest, TestSendResponse) { |
| 201 BalsaHeaders* request_headers = stream_->mutable_headers(); |
| 202 request_headers->SetRequestFirstlineFromStringPieces( |
| 203 "GET", |
| 204 "https://www.google.com/foo", |
| 205 "HTTP/1.1"); |
| 206 |
| 207 response_headers_.SetResponseFirstlineFromStringPieces( |
| 208 "HTTP/1.1", "200", "OK"); |
| 209 response_headers_.ReplaceOrAppendHeader("content-length", "3"); |
| 210 |
| 211 InSequence s; |
| 212 EXPECT_CALL(session_, WritevData(_, _, 1, _, _, _)).Times(1) |
| 213 .WillOnce(WithArgs<1>(Invoke( |
| 214 this, &QuicSpdyServerStreamTest::ValidateHeaders))); |
| 215 |
| 216 EXPECT_CALL(session_, WritevData(_, _, 1, _, _, _)).Times(1). |
| 217 WillOnce(Return(QuicConsumedData(3, true))); |
| 218 |
| 219 stream_->SendResponse(); |
| 220 EXPECT_TRUE(stream_->read_side_closed()); |
| 221 EXPECT_TRUE(stream_->write_side_closed()); |
| 222 } |
| 223 |
| 224 TEST_F(QuicSpdyServerStreamTest, TestSendErrorResponse) { |
| 225 response_headers_.SetResponseFirstlineFromStringPieces( |
| 226 "HTTP/1.1", "500", "Server Error"); |
| 227 response_headers_.ReplaceOrAppendHeader("content-length", "3"); |
| 228 |
| 229 InSequence s; |
| 230 EXPECT_CALL(session_, WritevData(_, _, 1, _, _, _)).Times(1) |
| 231 .WillOnce(WithArgs<1>(Invoke( |
| 232 this, &QuicSpdyServerStreamTest::ValidateHeaders))); |
| 233 |
| 234 EXPECT_CALL(session_, WritevData(_, _, 1, _, _, _)).Times(1). |
| 235 WillOnce(Return(QuicConsumedData(3, true))); |
| 236 |
| 237 stream_->SendErrorResponse(); |
| 238 EXPECT_TRUE(stream_->read_side_closed()); |
| 239 EXPECT_TRUE(stream_->write_side_closed()); |
| 240 } |
| 241 |
36 TEST_F(QuicSpdyServerStreamTest, InvalidHeadersWithFin) { | 242 TEST_F(QuicSpdyServerStreamTest, InvalidHeadersWithFin) { |
37 char arr[] = { | 243 char arr[] = { |
38 0x00, 0x00, 0x00, 0x05, // .... | 244 0x00, 0x00, 0x00, 0x05, // .... |
39 0x00, 0x00, 0x00, 0x05, // .... | 245 0x00, 0x00, 0x00, 0x05, // .... |
40 0x3a, 0x68, 0x6f, 0x73, // :hos | 246 0x3a, 0x68, 0x6f, 0x73, // :hos |
41 0x74, 0x00, 0x00, 0x00, // t... | 247 0x74, 0x00, 0x00, 0x00, // t... |
42 0x00, 0x00, 0x00, 0x00, // .... | 248 0x00, 0x00, 0x00, 0x00, // .... |
43 0x07, 0x3a, 0x6d, 0x65, // .:me | 249 0x07, 0x3a, 0x6d, 0x65, // .:me |
44 0x74, 0x68, 0x6f, 0x64, // thod | 250 0x74, 0x68, 0x6f, 0x64, // thod |
45 0x00, 0x00, 0x00, 0x03, // .... | 251 0x00, 0x00, 0x00, 0x03, // .... |
46 0x47, 0x45, 0x54, 0x00, // GET. | 252 0x47, 0x45, 0x54, 0x00, // GET. |
47 0x00, 0x00, 0x05, 0x3a, // ...: | 253 0x00, 0x00, 0x05, 0x3a, // ...: |
48 0x70, 0x61, 0x74, 0x68, // path | 254 0x70, 0x61, 0x74, 0x68, // path |
49 0x00, 0x00, 0x00, 0x04, // .... | 255 0x00, 0x00, 0x00, 0x04, // .... |
50 0x2f, 0x66, 0x6f, 0x6f, // /foo | 256 0x2f, 0x66, 0x6f, 0x6f, // /foo |
51 0x00, 0x00, 0x00, 0x07, // .... | 257 0x00, 0x00, 0x00, 0x07, // .... |
52 0x3a, 0x73, 0x63, 0x68, // :sch | 258 0x3a, 0x73, 0x63, 0x68, // :sch |
53 0x65, 0x6d, 0x65, 0x00, // eme. | 259 0x65, 0x6d, 0x65, 0x00, // eme. |
54 0x00, 0x00, 0x00, 0x00, // .... | 260 0x00, 0x00, 0x00, 0x00, // .... |
55 0x00, 0x00, 0x08, 0x3a, // ...: | 261 0x00, 0x00, 0x08, 0x3a, // ...: |
56 0x76, 0x65, 0x72, 0x73, // vers | 262 0x76, 0x65, 0x72, 0x73, // vers |
57 '\x96', 0x6f, 0x6e, 0x00, // <i(69)>on. | 263 '\x96', 0x6f, 0x6e, 0x00, // <i(69)>on. |
58 0x00, 0x00, 0x08, 0x48, // ...H | 264 0x00, 0x00, 0x08, 0x48, // ...H |
59 0x54, 0x54, 0x50, 0x2f, // TTP/ | 265 0x54, 0x54, 0x50, 0x2f, // TTP/ |
60 0x31, 0x2e, 0x31, // 1.1 | 266 0x31, 0x2e, 0x31, // 1.1 |
61 }; | 267 }; |
62 QuicStreamFrame frame( | 268 QuicStreamFrame frame( |
63 1, true, 0, MakeIOVector(StringPiece(arr, arraysize(arr)))); | 269 stream_->id(), true, 0, MakeIOVector(StringPiece(arr, arraysize(arr)))); |
64 // Verify that we don't crash when we get a invalid headers in stream frame. | 270 // Verify that we don't crash when we get a invalid headers in stream frame. |
65 stream_.OnStreamFrame(frame); | 271 stream_->OnStreamFrame(frame); |
66 } | 272 } |
67 | 273 |
68 } // namespace | 274 } // namespace |
69 } // namespace test | 275 } // namespace test |
70 } // namespace tools | 276 } // namespace tools |
71 } // namespace net | 277 } // namespace net |
OLD | NEW |