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