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(ReliableQuicStream* stream) { | |
pthatcher2
2016/10/24 17:42:49
Should ActivateReliableStream also take a std::uni
zhihuang1
2016/10/24 19:08:10
Done.
| |
75 ActivateStream(std::unique_ptr<ReliableQuicStream>(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 QuicConnection* connection = 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, 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(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 QuartcStream* stream_ = nullptr; | |
183 std::unique_ptr<MockQuartcStreamDelegate> mock_stream_delegate_; | |
184 std::unique_ptr<MockQuicSession> session_; | |
185 // Data written by the ReliableQuicStreamAdapterTest. | |
186 std::string write_buffer_; | |
187 // Data read by the ReliableQuicStreamAdapterTest. | |
188 std::string read_buffer_; | |
189 std::unique_ptr<QuartcAlarmFactory> alarm_factory_; | |
190 // Used to implement the QuicConnectionHelperInterface. | |
191 SimpleBufferAllocator buffer_allocator_; | |
192 QuicClock clock_; | |
193 }; | |
194 | |
195 // Write an entire string. | |
196 TEST_F(QuartcStreamTest, WriteDataWhole) { | |
197 CreateReliableQuicStream(); | |
198 stream_->Write("Foo bar", 7, kDefaultParam); | |
199 EXPECT_EQ("Foo bar", write_buffer_); | |
200 } | |
201 | |
202 // Write part of a string. | |
203 TEST_F(QuartcStreamTest, WriteDataPartial) { | |
204 CreateReliableQuicStream(); | |
205 stream_->Write("Foo bar", 5, kDefaultParam); | |
206 EXPECT_EQ("Foo b", write_buffer_); | |
207 } | |
208 | |
209 // Test that strings are buffered correctly. | |
210 TEST_F(QuartcStreamTest, BufferData) { | |
211 CreateReliableQuicStream(); | |
212 | |
213 session_->set_writable(false); | |
214 stream_->Write("Foo bar", 7, kDefaultParam); | |
215 // The data will be buffered. | |
216 EXPECT_EQ(0ul, write_buffer_.size()); | |
217 EXPECT_TRUE(stream_->HasBufferedData()); | |
218 EXPECT_EQ(-1, mock_stream_delegate_->queued_bytes_amount()); | |
219 // The session is writable and the buffered data amount will change. | |
220 session_->set_writable(true); | |
221 stream_->OnCanWrite(); | |
222 EXPECT_EQ(0, mock_stream_delegate_->queued_bytes_amount()); | |
223 EXPECT_FALSE(stream_->HasBufferedData()); | |
224 EXPECT_EQ("Foo bar", write_buffer_); | |
225 | |
226 stream_->Write("xyzzy", 5, kDefaultParam); | |
227 EXPECT_EQ("Foo barxyzzy", write_buffer_); | |
228 } | |
229 | |
230 // Read an entire string. | |
231 TEST_F(QuartcStreamTest, ReadDataWhole) { | |
232 CreateReliableQuicStream(); | |
233 QuicStreamFrame frame(kStreamId, false, 0, "Hello, World!"); | |
234 stream_->OnStreamFrame(frame); | |
235 | |
236 EXPECT_EQ("Hello, World!", read_buffer_); | |
237 } | |
238 | |
239 // Read part of a string. | |
240 TEST_F(QuartcStreamTest, ReadDataPartial) { | |
241 CreateReliableQuicStream(); | |
242 QuicStreamFrame frame(kStreamId, false, 0, "Hello, World!"); | |
243 frame.data_length = 5; | |
244 stream_->OnStreamFrame(frame); | |
245 | |
246 EXPECT_EQ("Hello", read_buffer_); | |
247 } | |
248 | |
249 // Test that closing the stream results in a callback. | |
250 TEST_F(QuartcStreamTest, CloseStream) { | |
251 CreateReliableQuicStream(); | |
252 EXPECT_FALSE(mock_stream_delegate_->closed()); | |
253 stream_->OnClose(); | |
254 EXPECT_TRUE(mock_stream_delegate_->closed()); | |
255 } | |
256 | |
257 } // namespace | |
258 } // namespace test | |
259 } // namespace net | |
OLD | NEW |