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