OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/p2p/quic/reliablequicstream.h" | 11 #include "webrtc/p2p/quic/reliablequicstream.h" |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "net/base/ip_address_number.h" | 15 #include "net/base/ip_address_number.h" |
16 #include "net/quic/quic_connection.h" | 16 #include "net/quic/quic_connection.h" |
17 #include "net/quic/quic_protocol.h" | 17 #include "net/quic/quic_protocol.h" |
18 #include "net/quic/quic_session.h" | 18 #include "net/quic/quic_session.h" |
19 #include "webrtc/base/buffer.h" | 19 #include "webrtc/base/buffer.h" |
20 #include "webrtc/base/gunit.h" | 20 #include "webrtc/base/gunit.h" |
21 #include "webrtc/base/sigslot.h" | 21 #include "webrtc/base/sigslot.h" |
22 #include "webrtc/base/stream.h" | 22 #include "webrtc/base/stream.h" |
23 #include "webrtc/p2p/quic/quicconnectionhelper.h" | 23 #include "webrtc/p2p/quic/quicconnectionhelper.h" |
24 | 24 |
25 using cricket::QuicConnectionHelper; | 25 using cricket::QuicConnectionHelper; |
26 using cricket::ReliableQuicStream; | 26 using cricket::ReliableQuicStream; |
27 | 27 |
28 using net::FecProtection; | 28 using net::FecProtection; |
29 using net::IPAddressNumber; | 29 using net::IPAddress; |
30 using net::IPEndPoint; | 30 using net::IPEndPoint; |
| 31 using net::PerPacketOptions; |
31 using net::Perspective; | 32 using net::Perspective; |
32 using net::QuicAckListenerInterface; | 33 using net::QuicAckListenerInterface; |
33 using net::QuicConfig; | 34 using net::QuicConfig; |
34 using net::QuicConnection; | 35 using net::QuicConnection; |
35 using net::QuicConsumedData; | 36 using net::QuicConsumedData; |
36 using net::QuicCryptoStream; | 37 using net::QuicCryptoStream; |
37 using net::QuicErrorCode; | 38 using net::QuicErrorCode; |
38 using net::QuicIOVector; | 39 using net::QuicIOVector; |
39 using net::QuicPacketWriter; | 40 using net::QuicPacketWriter; |
40 using net::QuicRstStreamErrorCode; | 41 using net::QuicRstStreamErrorCode; |
41 using net::QuicSession; | 42 using net::QuicSession; |
42 using net::QuicStreamId; | 43 using net::QuicStreamId; |
43 using net::QuicStreamOffset; | 44 using net::QuicStreamOffset; |
44 using net::SpdyPriority; | 45 using net::SpdyPriority; |
45 | 46 |
46 using rtc::SR_SUCCESS; | 47 using rtc::SR_SUCCESS; |
47 using rtc::SR_BLOCK; | 48 using rtc::SR_BLOCK; |
48 | 49 |
| 50 // Arbitrary number for a stream's write blocked priority. |
| 51 static const SpdyPriority kDefaultPriority = 3; |
| 52 |
49 // QuicSession that does not create streams and writes data from | 53 // QuicSession that does not create streams and writes data from |
50 // ReliableQuicStream to a string. | 54 // ReliableQuicStream to a string. |
51 class MockQuicSession : public QuicSession { | 55 class MockQuicSession : public QuicSession { |
52 public: | 56 public: |
53 MockQuicSession(QuicConnection* connection, | 57 MockQuicSession(QuicConnection* connection, |
54 const QuicConfig& config, | 58 const QuicConfig& config, |
55 std::string* write_buffer) | 59 std::string* write_buffer) |
56 : QuicSession(connection, config), write_buffer_(write_buffer) {} | 60 : QuicSession(connection, config), write_buffer_(write_buffer) {} |
57 | 61 |
58 // Writes outgoing data from ReliableQuicStream to a string. | 62 // Writes outgoing data from ReliableQuicStream to a string. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 112 |
109 // Packet writer that does nothing. This is required for QuicConnection but | 113 // Packet writer that does nothing. This is required for QuicConnection but |
110 // isn't used for writing data. | 114 // isn't used for writing data. |
111 class DummyPacketWriter : public QuicPacketWriter { | 115 class DummyPacketWriter : public QuicPacketWriter { |
112 public: | 116 public: |
113 DummyPacketWriter() {} | 117 DummyPacketWriter() {} |
114 | 118 |
115 // QuicPacketWriter overrides. | 119 // QuicPacketWriter overrides. |
116 virtual net::WriteResult WritePacket(const char* buffer, | 120 virtual net::WriteResult WritePacket(const char* buffer, |
117 size_t buf_len, | 121 size_t buf_len, |
118 const IPAddressNumber& self_address, | 122 const IPAddress& self_address, |
119 const IPEndPoint& peer_address) { | 123 const IPEndPoint& peer_address, |
| 124 PerPacketOptions* options) { |
120 return net::WriteResult(net::WRITE_STATUS_ERROR, 0); | 125 return net::WriteResult(net::WRITE_STATUS_ERROR, 0); |
121 } | 126 } |
122 | 127 |
123 bool IsWriteBlockedDataBuffered() const override { return false; } | 128 bool IsWriteBlockedDataBuffered() const override { return false; } |
124 | 129 |
125 bool IsWriteBlocked() const override { return false; }; | 130 bool IsWriteBlocked() const override { return false; }; |
126 | 131 |
127 void SetWritable() override {} | 132 void SetWritable() override {} |
128 | 133 |
129 net::QuicByteCount GetMaxPacketSize( | 134 net::QuicByteCount GetMaxPacketSize( |
130 const net::IPEndPoint& peer_address) const override { | 135 const net::IPEndPoint& peer_address) const override { |
131 return 0; | 136 return 0; |
132 } | 137 } |
133 }; | 138 }; |
134 | 139 |
135 // QuicPacketWriter is not necessary, so this creates a packet writer that | |
136 // doesn't do anything. | |
137 class DummyPacketWriterFactory : public QuicConnection::PacketWriterFactory { | |
138 public: | |
139 DummyPacketWriterFactory() {} | |
140 | |
141 QuicPacketWriter* Create(QuicConnection* connection) const override { | |
142 return new DummyPacketWriter(); | |
143 } | |
144 }; | |
145 | |
146 class ReliableQuicStreamTest : public ::testing::Test, | 140 class ReliableQuicStreamTest : public ::testing::Test, |
147 public sigslot::has_slots<> { | 141 public sigslot::has_slots<> { |
148 public: | 142 public: |
149 ReliableQuicStreamTest() {} | 143 ReliableQuicStreamTest() {} |
150 | 144 |
151 void CreateReliableQuicStream() { | 145 void CreateReliableQuicStream() { |
152 const net::QuicStreamId kStreamId = 5; | 146 const net::QuicStreamId kStreamId = 5; |
153 | 147 |
154 // Arbitrary values for QuicConnection. | 148 // Arbitrary values for QuicConnection. |
155 QuicConnectionHelper* quic_helper = | 149 QuicConnectionHelper* quic_helper = |
156 new QuicConnectionHelper(rtc::Thread::Current()); | 150 new QuicConnectionHelper(rtc::Thread::Current()); |
157 Perspective perspective = Perspective::IS_SERVER; | 151 Perspective perspective = Perspective::IS_SERVER; |
158 net::IPAddressNumber ip(net::kIPv4AddressSize, 0); | 152 net::IPAddress ip(0, 0, 0, 0); |
159 | 153 |
160 bool owns_writer = false; | 154 bool owns_writer = true; |
161 | 155 |
162 QuicConnection* connection = new QuicConnection( | 156 QuicConnection* connection = new QuicConnection( |
163 0, IPEndPoint(ip, 0), quic_helper, DummyPacketWriterFactory(), | 157 0, IPEndPoint(ip, 0), quic_helper, new DummyPacketWriter(), owns_writer, |
164 owns_writer, perspective, net::QuicSupportedVersions()); | 158 perspective, net::QuicSupportedVersions()); |
165 | 159 |
166 session_.reset( | 160 session_.reset( |
167 new MockQuicSession(connection, QuicConfig(), &write_buffer_)); | 161 new MockQuicSession(connection, QuicConfig(), &write_buffer_)); |
168 stream_.reset(new ReliableQuicStream(kStreamId, session_.get())); | 162 stream_.reset(new ReliableQuicStream(kStreamId, session_.get())); |
169 stream_->SignalDataReceived.connect( | 163 stream_->SignalDataReceived.connect( |
170 this, &ReliableQuicStreamTest::OnDataReceived); | 164 this, &ReliableQuicStreamTest::OnDataReceived); |
171 stream_->SignalClosed.connect(this, &ReliableQuicStreamTest::OnClosed); | 165 stream_->SignalClosed.connect(this, &ReliableQuicStreamTest::OnClosed); |
172 | 166 |
173 session_->register_write_blocked_stream(stream_->id(), stream_->Priority()); | 167 session_->register_write_blocked_stream(stream_->id(), kDefaultPriority); |
174 } | 168 } |
175 | 169 |
176 void OnDataReceived(QuicStreamId id, const char* data, size_t length) { | 170 void OnDataReceived(QuicStreamId id, const char* data, size_t length) { |
177 ASSERT_EQ(id, stream_->id()); | 171 ASSERT_EQ(id, stream_->id()); |
178 read_buffer_.append(data, length); | 172 read_buffer_.append(data, length); |
179 } | 173 } |
180 | 174 |
181 void OnClosed(QuicStreamId id, QuicErrorCode err) { closed_ = true; } | 175 void OnClosed(QuicStreamId id, QuicErrorCode err) { closed_ = true; } |
182 | 176 |
183 protected: | 177 protected: |
184 scoped_ptr<ReliableQuicStream> stream_; | 178 rtc::scoped_ptr<ReliableQuicStream> stream_; |
185 scoped_ptr<MockQuicSession> session_; | 179 rtc::scoped_ptr<MockQuicSession> session_; |
186 | 180 |
187 // Data written by the ReliableQuicStream. | 181 // Data written by the ReliableQuicStream. |
188 std::string write_buffer_; | 182 std::string write_buffer_; |
189 // Data read by the ReliableQuicStream. | 183 // Data read by the ReliableQuicStream. |
190 std::string read_buffer_; | 184 std::string read_buffer_; |
191 // Whether the ReliableQuicStream is closed. | 185 // Whether the ReliableQuicStream is closed. |
192 bool closed_ = false; | 186 bool closed_ = false; |
193 }; | 187 }; |
194 | 188 |
195 // Write an entire string. | 189 // Write an entire string. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 EXPECT_EQ("Hello", read_buffer_); | 240 EXPECT_EQ("Hello", read_buffer_); |
247 } | 241 } |
248 | 242 |
249 // Test that closing the stream results in a callback. | 243 // Test that closing the stream results in a callback. |
250 TEST_F(ReliableQuicStreamTest, CloseStream) { | 244 TEST_F(ReliableQuicStreamTest, CloseStream) { |
251 CreateReliableQuicStream(); | 245 CreateReliableQuicStream(); |
252 EXPECT_FALSE(closed_); | 246 EXPECT_FALSE(closed_); |
253 stream_->OnClose(); | 247 stream_->OnClose(); |
254 EXPECT_TRUE(closed_); | 248 EXPECT_TRUE(closed_); |
255 } | 249 } |
OLD | NEW |