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