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