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