[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 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/quic/quic_http_stream.h" |
| 6 | |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "net/base/net_errors.h" |
| 10 | #include "net/base/test_completion_callback.h" |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 11 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 12 | #include "net/base/upload_data_stream.h" |
| 13 | #include "net/http/http_response_headers.h" |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 14 | #include "net/quic/congestion_control/receive_algorithm_interface.h" |
| 15 | #include "net/quic/congestion_control/send_algorithm_interface.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 16 | #include "net/quic/crypto/crypto_protocol.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 17 | #include "net/quic/crypto/quic_decrypter.h" |
| 18 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 19 | #include "net/quic/quic_client_session.h" |
| 20 | #include "net/quic/quic_connection.h" |
| 21 | #include "net/quic/quic_connection_helper.h" |
| 22 | #include "net/quic/test_tools/mock_clock.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 23 | #include "net/quic/test_tools/mock_crypto_client_stream_factory.h" |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 24 | #include "net/quic/test_tools/mock_random.h" |
[email protected] | b1f287d | 2012-12-22 17:25:39 | [diff] [blame] | 25 | #include "net/quic/test_tools/quic_connection_peer.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 26 | #include "net/quic/test_tools/quic_test_utils.h" |
| 27 | #include "net/quic/test_tools/test_task_runner.h" |
| 28 | #include "net/socket/socket_test_util.h" |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 29 | #include "net/spdy/spdy_frame_builder.h" |
| 30 | #include "net/spdy/spdy_framer.h" |
| 31 | #include "net/spdy/spdy_http_utils.h" |
| 32 | #include "net/spdy/spdy_protocol.h" |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 33 | #include "testing/gmock/include/gmock/gmock.h" |
| 34 | #include "testing/gtest/include/gtest/gtest.h" |
| 35 | |
| 36 | using testing::_; |
| 37 | |
| 38 | namespace net { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 39 | namespace test { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | const char kUploadData[] = "hello world!"; |
| 43 | |
| 44 | class TestQuicConnection : public QuicConnection { |
| 45 | public: |
| 46 | TestQuicConnection(QuicGuid guid, |
| 47 | IPEndPoint address, |
| 48 | QuicConnectionHelper* helper) |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 49 | : QuicConnection(guid, address, helper, false) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 52 | void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm) { |
| 53 | QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 54 | } |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 55 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 56 | void SetReceiveAlgorithm(ReceiveAlgorithmInterface* receive_algorithm) { |
| 57 | QuicConnectionPeer::SetReceiveAlgorithm(this, receive_algorithm); |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 61 | class TestReceiveAlgorithm : public ReceiveAlgorithmInterface { |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 62 | public: |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 63 | explicit TestReceiveAlgorithm(QuicCongestionFeedbackFrame* feedback) |
| 64 | : feedback_(feedback) { |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool GenerateCongestionFeedback( |
| 68 | QuicCongestionFeedbackFrame* congestion_feedback) { |
| 69 | if (feedback_ == NULL) { |
| 70 | return false; |
| 71 | } |
| 72 | *congestion_feedback = *feedback_; |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | MOCK_METHOD4(RecordIncomingPacket, |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 77 | void(QuicByteCount, QuicPacketSequenceNumber, QuicTime, bool)); |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | MockClock clock_; |
| 81 | QuicCongestionFeedbackFrame* feedback_; |
| 82 | |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 83 | DISALLOW_COPY_AND_ASSIGN(TestReceiveAlgorithm); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 84 | }; |
| 85 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 86 | // Subclass of QuicHttpStream that closes itself when the first piece of data |
| 87 | // is received. |
| 88 | class AutoClosingStream : public QuicHttpStream { |
| 89 | public: |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 90 | explicit AutoClosingStream(QuicReliableClientStream* stream) |
| 91 | : QuicHttpStream(stream) { |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 94 | virtual int OnDataReceived(const char* data, int length) OVERRIDE { |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 95 | Close(false); |
| 96 | return OK; |
| 97 | } |
| 98 | }; |
| 99 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 100 | } // namespace |
| 101 | |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 102 | class QuicHttpStreamTest : public ::testing::TestWithParam<bool> { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 103 | protected: |
| 104 | const static bool kFin = true; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 105 | // Holds a packet to be written to the wire, and the IO mode that should |
| 106 | // be used by the mock socket when performing the write. |
| 107 | struct PacketToWrite { |
| 108 | PacketToWrite(IoMode mode, QuicEncryptedPacket* packet) |
| 109 | : mode(mode), |
| 110 | packet(packet) { |
| 111 | } |
| 112 | IoMode mode; |
| 113 | QuicEncryptedPacket* packet; |
| 114 | }; |
| 115 | |
| 116 | QuicHttpStreamTest() |
| 117 | : net_log_(BoundNetLog()), |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 118 | use_closing_stream_(false), |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 119 | read_buffer_(new IOBufferWithSize(4096)), |
| 120 | guid_(2), |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 121 | framer_(kQuicVersion1, QuicTime::Zero(), false), |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 122 | creator_(guid_, &framer_, &random_, false) { |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 123 | IPAddressNumber ip; |
| 124 | CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
| 125 | peer_addr_ = IPEndPoint(ip, 443); |
| 126 | self_addr_ = IPEndPoint(ip, 8435); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | ~QuicHttpStreamTest() { |
| 130 | for (size_t i = 0; i < writes_.size(); i++) { |
| 131 | delete writes_[i].packet; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Adds a packet to the list of expected writes. |
| 136 | void AddWrite(IoMode mode, QuicEncryptedPacket* packet) { |
| 137 | writes_.push_back(PacketToWrite(mode, packet)); |
| 138 | } |
| 139 | |
| 140 | // Returns the packet to be written at position |pos|. |
| 141 | QuicEncryptedPacket* GetWrite(size_t pos) { |
| 142 | return writes_[pos].packet; |
| 143 | } |
| 144 | |
| 145 | bool AtEof() { |
| 146 | return socket_data_->at_read_eof() && socket_data_->at_write_eof(); |
| 147 | } |
| 148 | |
| 149 | void ProcessPacket(const QuicEncryptedPacket& packet) { |
| 150 | connection_->ProcessUdpPacket(self_addr_, peer_addr_, packet); |
| 151 | } |
| 152 | |
| 153 | // Configures the test fixture to use the list of expected writes. |
| 154 | void Initialize() { |
| 155 | mock_writes_.reset(new MockWrite[writes_.size()]); |
| 156 | for (size_t i = 0; i < writes_.size(); i++) { |
| 157 | mock_writes_[i] = MockWrite(writes_[i].mode, |
| 158 | writes_[i].packet->data(), |
| 159 | writes_[i].packet->length()); |
| 160 | }; |
| 161 | |
| 162 | socket_data_.reset(new StaticSocketDataProvider(NULL, 0, mock_writes_.get(), |
| 163 | writes_.size())); |
| 164 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 165 | MockUDPClientSocket* socket = new MockUDPClientSocket(socket_data_.get(), |
| 166 | net_log_.net_log()); |
| 167 | socket->Connect(peer_addr_); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 168 | runner_ = new TestTaskRunner(&clock_); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 169 | send_algorithm_ = new MockSendAlgorithm(); |
| 170 | receive_algorithm_ = new TestReceiveAlgorithm(NULL); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 171 | EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _)). |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 172 | WillRepeatedly(testing::Return(QuicTime::Delta::Zero())); |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 173 | helper_ = new QuicConnectionHelper(runner_.get(), &clock_, |
| 174 | &random_generator_, socket); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 175 | connection_ = new TestQuicConnection(guid_, peer_addr_, helper_); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 176 | connection_->set_visitor(&visitor_); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 177 | connection_->SetSendAlgorithm(send_algorithm_); |
| 178 | connection_->SetReceiveAlgorithm(receive_algorithm_); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 179 | crypto_config_.SetDefaults(); |
[email protected] | 6dcd1d5c | 2013-03-15 07:28:37 | [diff] [blame] | 180 | session_.reset(new QuicClientSession(connection_, socket, NULL, |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 181 | &crypto_client_stream_factory_, |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 182 | "www.google.com", DefaultQuicConfig(), |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 183 | &crypto_config_, NULL)); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 184 | session_->GetCryptoStream()->CryptoConnect(); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 185 | EXPECT_TRUE(session_->IsCryptoHandshakeConfirmed()); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 186 | QuicReliableClientStream* stream = |
| 187 | session_->CreateOutgoingReliableStream(); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 188 | stream_.reset(use_closing_stream_ ? |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 189 | new AutoClosingStream(stream) : |
| 190 | new QuicHttpStream(stream)); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void SetRequestString(const std::string& method, const std::string& path) { |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 194 | SpdyHeaderBlock headers; |
| 195 | headers[":method"] = method; |
| 196 | headers[":host"] = "www.google.com"; |
| 197 | headers[":path"] = path; |
| 198 | headers[":scheme"] = "http"; |
| 199 | headers[":version"] = "HTTP/1.1"; |
| 200 | request_data_ = SerializeHeaderBlock(headers); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void SetResponseString(const std::string& status, const std::string& body) { |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 204 | SpdyHeaderBlock headers; |
| 205 | headers[":status"] = status; |
| 206 | headers[":version"] = "HTTP/1.1"; |
| 207 | headers["content-type"] = "text/plain"; |
[email protected] | 821555c | 2013-05-16 20:20:17 | [diff] [blame] | 208 | response_data_ = SerializeHeaderBlock(headers) + body; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | std::string SerializeHeaderBlock(const SpdyHeaderBlock& headers) { |
[email protected] | 821555c | 2013-05-16 20:20:17 | [diff] [blame] | 212 | QuicSpdyCompressor compressor; |
| 213 | return compressor.CompressHeaders(headers); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 214 | } |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 215 | |
| 216 | // Returns a newly created packet to send kData on stream 1. |
| 217 | QuicEncryptedPacket* ConstructDataPacket( |
| 218 | QuicPacketSequenceNumber sequence_number, |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 219 | bool should_include_version, |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 220 | bool fin, |
| 221 | QuicStreamOffset offset, |
| 222 | base::StringPiece data) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 223 | InitializeHeader(sequence_number, should_include_version); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 224 | QuicStreamFrame frame(3, fin, offset, data); |
| 225 | return ConstructPacket(header_, QuicFrame(&frame)); |
| 226 | } |
| 227 | |
| 228 | // Returns a newly created packet to send ack data. |
| 229 | QuicEncryptedPacket* ConstructAckPacket( |
| 230 | QuicPacketSequenceNumber sequence_number, |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 231 | QuicPacketSequenceNumber largest_received, |
| 232 | QuicPacketSequenceNumber least_unacked) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 233 | InitializeHeader(sequence_number, false); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 234 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 235 | QuicAckFrame ack(largest_received, QuicTime::Zero(), least_unacked); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 236 | ack.sent_info.entropy_hash = 0; |
| 237 | ack.received_info.entropy_hash = 0; |
| 238 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 239 | return ConstructPacket(header_, QuicFrame(&ack)); |
| 240 | } |
| 241 | |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 242 | // Returns a newly created packet to send ack data. |
| 243 | QuicEncryptedPacket* ConstructRstPacket( |
| 244 | QuicPacketSequenceNumber sequence_number, |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 245 | QuicStreamId stream_id) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 246 | InitializeHeader(sequence_number, false); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 247 | |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 248 | QuicRstStreamFrame rst(stream_id, QUIC_STREAM_NO_ERROR); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 249 | return ConstructPacket(header_, QuicFrame(&rst)); |
| 250 | } |
| 251 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 252 | BoundNetLog net_log_; |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 253 | bool use_closing_stream_; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 254 | MockSendAlgorithm* send_algorithm_; |
| 255 | TestReceiveAlgorithm* receive_algorithm_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 256 | scoped_refptr<TestTaskRunner> runner_; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 257 | scoped_ptr<MockWrite[]> mock_writes_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 258 | MockClock clock_; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 259 | MockRandom random_generator_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 260 | TestQuicConnection* connection_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 261 | QuicConnectionHelper* helper_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 262 | testing::StrictMock<MockConnectionVisitor> visitor_; |
| 263 | scoped_ptr<QuicHttpStream> stream_; |
| 264 | scoped_ptr<QuicClientSession> session_; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 265 | QuicCryptoClientConfig crypto_config_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 266 | TestCompletionCallback callback_; |
| 267 | HttpRequestInfo request_; |
| 268 | HttpRequestHeaders headers_; |
| 269 | HttpResponseInfo response_; |
| 270 | scoped_refptr<IOBufferWithSize> read_buffer_; |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 271 | std::string request_data_; |
| 272 | std::string response_data_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 273 | |
| 274 | private: |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 275 | void InitializeHeader(QuicPacketSequenceNumber sequence_number, |
| 276 | bool should_include_version) { |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 277 | header_.public_header.guid = guid_; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 278 | header_.public_header.reset_flag = false; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 279 | header_.public_header.version_flag = should_include_version; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 280 | header_.packet_sequence_number = sequence_number; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 281 | header_.fec_group = 0; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 282 | header_.entropy_flag = false; |
| 283 | header_.fec_flag = false; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | QuicEncryptedPacket* ConstructPacket(const QuicPacketHeader& header, |
| 287 | const QuicFrame& frame) { |
| 288 | QuicFrames frames; |
| 289 | frames.push_back(frame); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 290 | scoped_ptr<QuicPacket> packet( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 291 | framer_.ConstructFrameDataPacket(header_, frames).packet); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 292 | return framer_.EncryptPacket( |
| 293 | ENCRYPTION_NONE, header.packet_sequence_number, *packet); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | const QuicGuid guid_; |
| 297 | QuicFramer framer_; |
| 298 | IPEndPoint self_addr_; |
| 299 | IPEndPoint peer_addr_; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 300 | MockRandom random_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 301 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 302 | QuicPacketCreator creator_; |
| 303 | QuicPacketHeader header_; |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 304 | scoped_ptr<StaticSocketDataProvider> socket_data_; |
| 305 | std::vector<PacketToWrite> writes_; |
| 306 | }; |
| 307 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 308 | TEST_F(QuicHttpStreamTest, RenewStreamForAuth) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 309 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 310 | EXPECT_EQ(NULL, stream_->RenewStreamForAuth()); |
| 311 | } |
| 312 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 313 | TEST_F(QuicHttpStreamTest, CanFindEndOfResponse) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 314 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 315 | EXPECT_TRUE(stream_->CanFindEndOfResponse()); |
| 316 | } |
| 317 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 318 | TEST_F(QuicHttpStreamTest, IsConnectionReusable) { |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 319 | Initialize(); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 320 | EXPECT_FALSE(stream_->IsConnectionReusable()); |
| 321 | } |
| 322 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 323 | TEST_F(QuicHttpStreamTest, GetRequest) { |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 324 | SetRequestString("GET", "/"); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 325 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, kFin, 0, |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 326 | request_data_)); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 327 | AddWrite(SYNCHRONOUS, ConstructAckPacket(2, 2, 1)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 328 | Initialize(); |
| 329 | |
| 330 | request_.method = "GET"; |
| 331 | request_.url = GURL("http://www.google.com/"); |
| 332 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 333 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 334 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 335 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 336 | callback_.callback())); |
| 337 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 338 | |
| 339 | // Ack the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 340 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 341 | ProcessPacket(*ack); |
| 342 | |
| 343 | EXPECT_EQ(ERR_IO_PENDING, |
| 344 | stream_->ReadResponseHeaders(callback_.callback())); |
| 345 | |
| 346 | // Send the response without a body. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 347 | SetResponseString("404 Not Found", std::string()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 348 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 349 | ConstructDataPacket(2, false, kFin, 0, response_data_)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 350 | ProcessPacket(*resp); |
| 351 | |
| 352 | // Now that the headers have been processed, the callback will return. |
| 353 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 354 | ASSERT_TRUE(response_.headers); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 355 | EXPECT_EQ(404, response_.headers->response_code()); |
| 356 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 357 | |
| 358 | // There is no body, so this should return immediately. |
| 359 | EXPECT_EQ(0, stream_->ReadResponseBody(read_buffer_.get(), |
| 360 | read_buffer_->size(), |
| 361 | callback_.callback())); |
| 362 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 363 | EXPECT_TRUE(AtEof()); |
| 364 | } |
| 365 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 366 | TEST_F(QuicHttpStreamTest, GetRequestFullResponseInSinglePacket) { |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 367 | SetRequestString("GET", "/"); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 368 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, kFin, 0, request_data_)); |
| 369 | AddWrite(SYNCHRONOUS, ConstructAckPacket(2, 2, 1)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 370 | Initialize(); |
| 371 | |
| 372 | request_.method = "GET"; |
| 373 | request_.url = GURL("http://www.google.com/"); |
| 374 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 375 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 376 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 377 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 378 | callback_.callback())); |
| 379 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 380 | |
| 381 | // Ack the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 382 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 383 | ProcessPacket(*ack); |
| 384 | |
| 385 | EXPECT_EQ(ERR_IO_PENDING, |
| 386 | stream_->ReadResponseHeaders(callback_.callback())); |
| 387 | |
| 388 | // Send the response with a body. |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 389 | SetResponseString("200 OK", "hello world!"); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 390 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 391 | ConstructDataPacket(2, false, kFin, 0, response_data_)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 392 | ProcessPacket(*resp); |
| 393 | |
| 394 | // Now that the headers have been processed, the callback will return. |
| 395 | EXPECT_EQ(OK, callback_.WaitForResult()); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 396 | ASSERT_TRUE(response_.headers); |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 397 | EXPECT_EQ(200, response_.headers->response_code()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 398 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 399 | |
| 400 | // There is no body, so this should return immediately. |
| 401 | // Since the body has already arrived, this should return immediately. |
| 402 | EXPECT_EQ(12, stream_->ReadResponseBody(read_buffer_.get(), |
| 403 | read_buffer_->size(), |
| 404 | callback_.callback())); |
| 405 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 406 | EXPECT_TRUE(AtEof()); |
| 407 | } |
| 408 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 409 | TEST_F(QuicHttpStreamTest, SendPostRequest) { |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 410 | SetRequestString("POST", "/"); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 411 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, !kFin, 0, request_data_)); |
| 412 | AddWrite(SYNCHRONOUS, ConstructDataPacket(2, true, kFin, |
| 413 | request_data_.length(), |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 414 | kUploadData)); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 415 | AddWrite(SYNCHRONOUS, ConstructAckPacket(3, 2, 1)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 416 | |
| 417 | Initialize(); |
| 418 | |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 419 | ScopedVector<UploadElementReader> element_readers; |
| 420 | element_readers.push_back( |
| 421 | new UploadBytesElementReader(kUploadData, strlen(kUploadData))); |
| 422 | UploadDataStream upload_data_stream(&element_readers, 0); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 423 | request_.method = "POST"; |
| 424 | request_.url = GURL("http://www.google.com/"); |
| 425 | request_.upload_data_stream = &upload_data_stream; |
[email protected] | 4db27d8 | 2012-12-20 11:50:24 | [diff] [blame] | 426 | ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 427 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 428 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 429 | net_log_, callback_.callback())); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 430 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 431 | callback_.callback())); |
| 432 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 433 | |
| 434 | // Ack both packets in the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 435 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 436 | ProcessPacket(*ack); |
| 437 | |
| 438 | // Send the response headers (but not the body). |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 439 | SetResponseString("200 OK", std::string()); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 440 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 441 | ConstructDataPacket(2, false, !kFin, 0, response_data_)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 442 | ProcessPacket(*resp); |
| 443 | |
| 444 | // Since the headers have already arrived, this should return immediately. |
| 445 | EXPECT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 446 | ASSERT_TRUE(response_.headers); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 447 | EXPECT_EQ(200, response_.headers->response_code()); |
| 448 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 449 | |
| 450 | // Send the response body. |
| 451 | const char kResponseBody[] = "Hello world!"; |
| 452 | scoped_ptr<QuicEncryptedPacket> resp_body( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 453 | ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 454 | kResponseBody)); |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 455 | ProcessPacket(*resp_body); |
| 456 | |
| 457 | // Since the body has already arrived, this should return immediately. |
| 458 | EXPECT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 459 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 460 | callback_.callback())); |
| 461 | |
| 462 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 463 | EXPECT_TRUE(AtEof()); |
| 464 | } |
| 465 | |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 466 | TEST_F(QuicHttpStreamTest, SendChunkedPostRequest) { |
| 467 | SetRequestString("POST", "/"); |
| 468 | size_t chunk_size = strlen(kUploadData); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 469 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, !kFin, 0, request_data_)); |
| 470 | AddWrite(SYNCHRONOUS, ConstructDataPacket(2, true, !kFin, |
| 471 | request_data_.length(), |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 472 | kUploadData)); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 473 | AddWrite(SYNCHRONOUS, ConstructDataPacket(3, true, kFin, |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 474 | request_data_.length() + chunk_size, |
| 475 | kUploadData)); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 476 | AddWrite(SYNCHRONOUS, ConstructAckPacket(4, 2, 1)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 477 | |
| 478 | Initialize(); |
| 479 | |
| 480 | UploadDataStream upload_data_stream(UploadDataStream::CHUNKED, 0); |
| 481 | upload_data_stream.AppendChunk(kUploadData, chunk_size, false); |
| 482 | |
| 483 | request_.method = "POST"; |
| 484 | request_.url = GURL("http://www.google.com/"); |
| 485 | request_.upload_data_stream = &upload_data_stream; |
| 486 | ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback())); |
| 487 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 488 | ASSERT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 489 | net_log_, callback_.callback())); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 490 | ASSERT_EQ(ERR_IO_PENDING, stream_->SendRequest(headers_, &response_, |
| 491 | callback_.callback())); |
| 492 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 493 | |
| 494 | upload_data_stream.AppendChunk(kUploadData, chunk_size, true); |
| 495 | |
| 496 | // Ack both packets in the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 497 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 498 | ProcessPacket(*ack); |
| 499 | |
| 500 | // Send the response headers (but not the body). |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 501 | SetResponseString("200 OK", std::string()); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 502 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 503 | ConstructDataPacket(2, false, !kFin, 0, response_data_)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 504 | ProcessPacket(*resp); |
| 505 | |
| 506 | // Since the headers have already arrived, this should return immediately. |
| 507 | ASSERT_EQ(OK, stream_->ReadResponseHeaders(callback_.callback())); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 508 | ASSERT_TRUE(response_.headers); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 509 | EXPECT_EQ(200, response_.headers->response_code()); |
| 510 | EXPECT_TRUE(response_.headers->HasHeaderValue("Content-Type", "text/plain")); |
| 511 | |
| 512 | // Send the response body. |
| 513 | const char kResponseBody[] = "Hello world!"; |
| 514 | scoped_ptr<QuicEncryptedPacket> resp_body( |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 515 | ConstructDataPacket(3, false, kFin, response_data_.length(), |
| 516 | kResponseBody)); |
[email protected] | c9e49a0 | 2013-02-26 05:56:47 | [diff] [blame] | 517 | ProcessPacket(*resp_body); |
| 518 | |
| 519 | // Since the body has already arrived, this should return immediately. |
| 520 | ASSERT_EQ(static_cast<int>(strlen(kResponseBody)), |
| 521 | stream_->ReadResponseBody(read_buffer_.get(), read_buffer_->size(), |
| 522 | callback_.callback())); |
| 523 | |
| 524 | EXPECT_TRUE(stream_->IsResponseBodyComplete()); |
| 525 | EXPECT_TRUE(AtEof()); |
| 526 | } |
| 527 | |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 528 | TEST_F(QuicHttpStreamTest, DestroyedEarly) { |
[email protected] | 6cca996b | 2013-01-25 07:43:36 | [diff] [blame] | 529 | SetRequestString("GET", "/"); |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 530 | AddWrite(SYNCHRONOUS, ConstructDataPacket(1, true, kFin, 0, request_data_)); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 531 | AddWrite(SYNCHRONOUS, ConstructAckPacket(2, 2, 1)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 532 | use_closing_stream_ = true; |
| 533 | Initialize(); |
| 534 | |
| 535 | request_.method = "GET"; |
| 536 | request_.url = GURL("http://www.google.com/"); |
| 537 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 538 | EXPECT_EQ(OK, stream_->InitializeStream(&request_, DEFAULT_PRIORITY, |
| 539 | net_log_, callback_.callback())); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 540 | EXPECT_EQ(OK, stream_->SendRequest(headers_, &response_, |
| 541 | callback_.callback())); |
| 542 | EXPECT_EQ(&response_, stream_->GetResponseInfo()); |
| 543 | |
| 544 | // Ack the request. |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 545 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(1, 0, 0)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 546 | ProcessPacket(*ack); |
| 547 | EXPECT_EQ(ERR_IO_PENDING, |
| 548 | stream_->ReadResponseHeaders(callback_.callback())); |
| 549 | |
| 550 | // Send the response with a body. |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 551 | SetResponseString("404 OK", "hello world!"); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 552 | scoped_ptr<QuicEncryptedPacket> resp( |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 553 | ConstructDataPacket(2, false, kFin, 0, response_data_)); |
[email protected] | 6353451 | 2012-12-23 18:49:00 | [diff] [blame] | 554 | |
| 555 | // In the course of processing this packet, the QuicHttpStream close itself. |
| 556 | ProcessPacket(*resp); |
| 557 | |
| 558 | EXPECT_TRUE(AtEof()); |
| 559 | } |
| 560 | |
[email protected] | f702d57 | 2012-12-04 15:56:20 | [diff] [blame] | 561 | } // namespace test |
| 562 | |
| 563 | } // namespace net |