OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 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/quartc/quartc_stream.h" |
| 6 |
| 7 #include "base/threading/thread_task_runner_handle.h" |
| 8 #include "net/quic/core/crypto/quic_random.h" |
| 9 #include "net/quic/core/quic_session.h" |
| 10 #include "net/quic/core/quic_simple_buffer_allocator.h" |
| 11 #include "net/quic/quartc/quartc_alarm_factory.h" |
| 12 #include "net/quic/quartc/quartc_stream_interface.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace net { |
| 16 namespace test { |
| 17 namespace { |
| 18 |
| 19 static const SpdyPriority kDefaultPriority = 3; |
| 20 static const QuicStreamId kStreamId = 5; |
| 21 static const QuartcStreamInterface::WriteParameters kDefaultParam; |
| 22 |
| 23 // MockQuicSession that does not create streams and writes data from |
| 24 // ReliableQuicStream to a string. |
| 25 class MockQuicSession : public QuicSession { |
| 26 public: |
| 27 MockQuicSession(QuicConnection* connection, |
| 28 const QuicConfig& config, |
| 29 std::string* write_buffer) |
| 30 : QuicSession(connection, config), write_buffer_(write_buffer) {} |
| 31 |
| 32 // Writes outgoing data from ReliableQuicStream to a string. |
| 33 QuicConsumedData WritevData( |
| 34 ReliableQuicStream* stream, |
| 35 QuicStreamId id, |
| 36 QuicIOVector iovector, |
| 37 QuicStreamOffset offset, |
| 38 bool fin, |
| 39 QuicAckListenerInterface* ack_notifier_delegate) override { |
| 40 if (!writable_) { |
| 41 return QuicConsumedData(0, false); |
| 42 } |
| 43 |
| 44 const char* data = reinterpret_cast<const char*>(iovector.iov->iov_base); |
| 45 size_t len = iovector.total_length; |
| 46 write_buffer_->append(data, len); |
| 47 return QuicConsumedData(len, fin); |
| 48 } |
| 49 |
| 50 QuartcStream* CreateIncomingDynamicStream(QuicStreamId id) override { |
| 51 return nullptr; |
| 52 } |
| 53 |
| 54 QuartcStream* CreateOutgoingDynamicStream(SpdyPriority priority) override { |
| 55 return nullptr; |
| 56 } |
| 57 |
| 58 QuicCryptoStream* GetCryptoStream() override { return nullptr; } |
| 59 |
| 60 // Called by ReliableQuicStream when they want to close stream. |
| 61 void SendRstStream(QuicStreamId id, |
| 62 QuicRstStreamErrorCode error, |
| 63 QuicStreamOffset bytes_written) override {} |
| 64 |
| 65 // Sets whether data is written to buffer, or else if this is write blocked. |
| 66 void set_writable(bool writable) { writable_ = writable; } |
| 67 |
| 68 // Tracks whether the stream is write blocked and its priority. |
| 69 void RegisterReliableStream(QuicStreamId stream_id, SpdyPriority priority) { |
| 70 write_blocked_streams()->RegisterStream(stream_id, priority); |
| 71 } |
| 72 |
| 73 // The session take ownership of the stream. |
| 74 void ActivateReliableStream(std::unique_ptr<ReliableQuicStream> stream) { |
| 75 ActivateStream(std::move(stream)); |
| 76 } |
| 77 |
| 78 private: |
| 79 // Stores written data from ReliableQuicStreamAdapter. |
| 80 std::string* write_buffer_; |
| 81 // Whether data is written to write_buffer_. |
| 82 bool writable_ = true; |
| 83 }; |
| 84 |
| 85 // Packet writer that does nothing. This is required for QuicConnection but |
| 86 // isn't used for writing data. |
| 87 class DummyPacketWriter : public QuicPacketWriter { |
| 88 public: |
| 89 DummyPacketWriter() {} |
| 90 |
| 91 // QuicPacketWriter overrides. |
| 92 WriteResult WritePacket(const char* buffer, |
| 93 size_t buf_len, |
| 94 const IPAddress& self_address, |
| 95 const IPEndPoint& peer_address, |
| 96 PerPacketOptions* options) override { |
| 97 return WriteResult(WRITE_STATUS_ERROR, 0); |
| 98 } |
| 99 |
| 100 bool IsWriteBlockedDataBuffered() const override { return false; } |
| 101 |
| 102 bool IsWriteBlocked() const override { return false; }; |
| 103 |
| 104 void SetWritable() override {} |
| 105 |
| 106 QuicByteCount GetMaxPacketSize( |
| 107 const IPEndPoint& peer_address) const override { |
| 108 return 0; |
| 109 } |
| 110 }; |
| 111 |
| 112 class MockQuartcStreamDelegate : public QuartcStreamInterface::Delegate { |
| 113 public: |
| 114 MockQuartcStreamDelegate(int id, std::string& read_buffer) |
| 115 : id_(id), read_buffer_(read_buffer) {} |
| 116 |
| 117 void OnBufferedAmountDecrease(QuartcStreamInterface* stream) override { |
| 118 queued_bytes_amount_ = stream->buffered_amount(); |
| 119 } |
| 120 |
| 121 void OnReceived(QuartcStreamInterface* stream, |
| 122 const char* data, |
| 123 size_t size) override { |
| 124 EXPECT_EQ(id_, stream->stream_id()); |
| 125 read_buffer_.append(data, size); |
| 126 } |
| 127 |
| 128 void OnClose(QuartcStreamInterface* stream, int error_code) override { |
| 129 closed_ = true; |
| 130 } |
| 131 |
| 132 bool closed() { return closed_; } |
| 133 |
| 134 int queued_bytes_amount() { return queued_bytes_amount_; } |
| 135 |
| 136 protected: |
| 137 uint32_t id_; |
| 138 // Data read by the ReliableQuicStream. |
| 139 std::string& read_buffer_; |
| 140 // Whether the ReliableQuicStream is closed. |
| 141 bool closed_ = false; |
| 142 int queued_bytes_amount_ = -1; |
| 143 }; |
| 144 |
| 145 class QuartcStreamTest : public ::testing::Test, |
| 146 public QuicConnectionHelperInterface { |
| 147 public: |
| 148 void CreateReliableQuicStream() { |
| 149 // Arbitrary values for QuicConnection. |
| 150 Perspective perspective = Perspective::IS_SERVER; |
| 151 IPAddress ip(0, 0, 0, 0); |
| 152 bool owns_writer = true; |
| 153 alarm_factory_.reset(new QuartcAlarmFactory( |
| 154 base::ThreadTaskRunnerHandle::Get().get(), GetClock())); |
| 155 |
| 156 connection_.reset(new QuicConnection( |
| 157 0, IPEndPoint(ip, 0), this /*QuicConnectionHelperInterface*/, |
| 158 alarm_factory_.get(), new DummyPacketWriter(), owns_writer, perspective, |
| 159 AllSupportedVersions())); |
| 160 |
| 161 session_.reset( |
| 162 new MockQuicSession(connection_.get(), QuicConfig(), &write_buffer_)); |
| 163 mock_stream_delegate_.reset( |
| 164 new MockQuartcStreamDelegate(kStreamId, read_buffer_)); |
| 165 stream_ = new QuartcStream(kStreamId, session_.get()); |
| 166 stream_->SetDelegate(mock_stream_delegate_.get()); |
| 167 session_->RegisterReliableStream(stream_->stream_id(), kDefaultPriority); |
| 168 session_->ActivateReliableStream(std::unique_ptr<QuartcStream>(stream_)); |
| 169 } |
| 170 |
| 171 const QuicClock* GetClock() const override { return &clock_; } |
| 172 |
| 173 QuicRandom* GetRandomGenerator() override { |
| 174 return QuicRandom::GetInstance(); |
| 175 } |
| 176 |
| 177 QuicBufferAllocator* GetBufferAllocator() override { |
| 178 return &buffer_allocator_; |
| 179 } |
| 180 |
| 181 protected: |
| 182 // The QuicSession will take the ownership. |
| 183 QuartcStream* stream_; |
| 184 std::unique_ptr<MockQuartcStreamDelegate> mock_stream_delegate_; |
| 185 std::unique_ptr<MockQuicSession> session_; |
| 186 // Data written by the ReliableQuicStreamAdapterTest. |
| 187 std::string write_buffer_; |
| 188 // Data read by the ReliableQuicStreamAdapterTest. |
| 189 std::string read_buffer_; |
| 190 std::unique_ptr<QuartcAlarmFactory> alarm_factory_; |
| 191 std::unique_ptr<QuicConnection> connection_; |
| 192 // Used to implement the QuicConnectionHelperInterface. |
| 193 SimpleBufferAllocator buffer_allocator_; |
| 194 QuicClock clock_; |
| 195 }; |
| 196 |
| 197 // Write an entire string. |
| 198 TEST_F(QuartcStreamTest, WriteDataWhole) { |
| 199 CreateReliableQuicStream(); |
| 200 stream_->Write("Foo bar", 7, kDefaultParam); |
| 201 EXPECT_EQ("Foo bar", write_buffer_); |
| 202 } |
| 203 |
| 204 // Write part of a string. |
| 205 TEST_F(QuartcStreamTest, WriteDataPartial) { |
| 206 CreateReliableQuicStream(); |
| 207 stream_->Write("Foo bar", 5, kDefaultParam); |
| 208 EXPECT_EQ("Foo b", write_buffer_); |
| 209 } |
| 210 |
| 211 // Test that strings are buffered correctly. |
| 212 TEST_F(QuartcStreamTest, BufferData) { |
| 213 CreateReliableQuicStream(); |
| 214 |
| 215 session_->set_writable(false); |
| 216 stream_->Write("Foo bar", 7, kDefaultParam); |
| 217 // The data will be buffered. |
| 218 EXPECT_EQ(0ul, write_buffer_.size()); |
| 219 EXPECT_TRUE(stream_->HasBufferedData()); |
| 220 EXPECT_EQ(-1, mock_stream_delegate_->queued_bytes_amount()); |
| 221 // The session is writable and the buffered data amount will change. |
| 222 session_->set_writable(true); |
| 223 stream_->OnCanWrite(); |
| 224 EXPECT_EQ(0, mock_stream_delegate_->queued_bytes_amount()); |
| 225 EXPECT_FALSE(stream_->HasBufferedData()); |
| 226 EXPECT_EQ("Foo bar", write_buffer_); |
| 227 |
| 228 stream_->Write("xyzzy", 5, kDefaultParam); |
| 229 EXPECT_EQ("Foo barxyzzy", write_buffer_); |
| 230 } |
| 231 |
| 232 // Read an entire string. |
| 233 TEST_F(QuartcStreamTest, ReadDataWhole) { |
| 234 CreateReliableQuicStream(); |
| 235 QuicStreamFrame frame(kStreamId, false, 0, "Hello, World!"); |
| 236 stream_->OnStreamFrame(frame); |
| 237 |
| 238 EXPECT_EQ("Hello, World!", read_buffer_); |
| 239 } |
| 240 |
| 241 // Read part of a string. |
| 242 TEST_F(QuartcStreamTest, ReadDataPartial) { |
| 243 CreateReliableQuicStream(); |
| 244 QuicStreamFrame frame(kStreamId, false, 0, "Hello, World!"); |
| 245 frame.data_length = 5; |
| 246 stream_->OnStreamFrame(frame); |
| 247 |
| 248 EXPECT_EQ("Hello", read_buffer_); |
| 249 } |
| 250 |
| 251 // Test that closing the stream results in a callback. |
| 252 TEST_F(QuartcStreamTest, CloseStream) { |
| 253 CreateReliableQuicStream(); |
| 254 EXPECT_FALSE(mock_stream_delegate_->closed()); |
| 255 stream_->OnClose(); |
| 256 EXPECT_TRUE(mock_stream_delegate_->closed()); |
| 257 } |
| 258 |
| 259 } // namespace |
| 260 } // namespace test |
| 261 } // namespace net |
OLD | NEW |