OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/api/quicdatachannel.h" | |
12 | |
13 #include <map> | |
14 #include <memory> | |
15 #include <sstream> | |
16 #include <string> | |
17 #include <vector> | |
18 | |
19 #include "webrtc/base/bind.h" | |
20 #include "webrtc/base/gunit.h" | |
21 #include "webrtc/base/scoped_ref_ptr.h" | |
22 #include "webrtc/p2p/base/faketransportcontroller.h" | |
23 #include "webrtc/p2p/quic/quictransportchannel.h" | |
24 #include "webrtc/p2p/quic/reliablequicstream.h" | |
25 | |
26 using cricket::FakeTransportChannel; | |
27 using cricket::QuicTransportChannel; | |
28 using cricket::ReliableQuicStream; | |
29 | |
30 using webrtc::DataBuffer; | |
31 using webrtc::DataChannelObserver; | |
32 using webrtc::DataChannelInit; | |
33 using webrtc::QuicDataChannel; | |
34 | |
35 namespace { | |
36 | |
37 // Timeout for asynchronous operations. | |
38 static const int kTimeoutMs = 1000; // milliseconds | |
39 | |
40 // Small messages that can be sent within a single QUIC packet. | |
41 static const std::string kSmallMessage1 = "Hello, world!"; | |
42 static const std::string kSmallMessage2 = "WebRTC"; | |
43 static const std::string kSmallMessage3 = "1"; | |
44 static const std::string kSmallMessage4 = "abcdefghijklmnopqrstuvwxyz"; | |
45 static const DataBuffer kSmallBuffer1(kSmallMessage1); | |
46 static const DataBuffer kSmallBuffer2(kSmallMessage2); | |
47 static const DataBuffer kSmallBuffer3(kSmallMessage3); | |
48 static const DataBuffer kSmallBuffer4(kSmallMessage4); | |
49 | |
50 // Large messages (> 1350 bytes) that exceed the max size of a QUIC packet. | |
51 // These are < 16 KB so they don't exceed the QUIC stream flow control limit. | |
52 static const std::string kLargeMessage1 = std::string("a", 2000); | |
53 static const std::string kLargeMessage2 = std::string("a", 4000); | |
54 static const std::string kLargeMessage3 = std::string("a", 8000); | |
55 static const std::string kLargeMessage4 = std::string("a", 12000); | |
56 static const DataBuffer kLargeBuffer1(kLargeMessage1); | |
57 static const DataBuffer kLargeBuffer2(kLargeMessage2); | |
58 static const DataBuffer kLargeBuffer3(kLargeMessage3); | |
59 static const DataBuffer kLargeBuffer4(kLargeMessage4); | |
60 | |
61 // Oversized message (> 16 KB) that violates the QUIC stream flow control limit. | |
62 static const std::string kOversizedMessage = std::string("a", 20000); | |
63 static const DataBuffer kOversizedBuffer(kOversizedMessage); | |
64 | |
65 // Creates a fingerprint from a certificate. | |
66 static rtc::SSLFingerprint* CreateFingerprint(rtc::RTCCertificate* cert) { | |
67 std::string digest_algorithm; | |
68 cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_algorithm); | |
69 std::unique_ptr<rtc::SSLFingerprint> fingerprint( | |
70 rtc::SSLFingerprint::Create(digest_algorithm, cert->identity())); | |
71 return fingerprint.release(); | |
72 } | |
73 | |
74 // FakeObserver receives messages from the QuicDataChannel. | |
75 class FakeObserver : public DataChannelObserver { | |
76 public: | |
77 FakeObserver() | |
78 : on_state_change_count_(0), on_buffered_amount_change_count_(0) {} | |
79 | |
80 // DataChannelObserver overrides. | |
81 void OnStateChange() override { ++on_state_change_count_; } | |
82 void OnBufferedAmountChange(uint64_t previous_amount) override { | |
83 ++on_buffered_amount_change_count_; | |
84 } | |
85 void OnMessage(const webrtc::DataBuffer& buffer) override { | |
86 messages_.push_back(std::string(buffer.data.data<char>(), buffer.size())); | |
87 } | |
88 | |
89 const std::vector<std::string>& messages() const { return messages_; } | |
90 | |
91 size_t messages_received() const { return messages_.size(); } | |
92 | |
93 size_t on_state_change_count() const { return on_state_change_count_; } | |
94 | |
95 size_t on_buffered_amount_change_count() const { | |
96 return on_buffered_amount_change_count_; | |
97 } | |
98 | |
99 private: | |
100 std::vector<std::string> messages_; | |
101 size_t on_state_change_count_; | |
102 size_t on_buffered_amount_change_count_; | |
103 }; | |
104 | |
105 // FakeQuicDataTransport simulates QuicDataTransport by dispatching QUIC | |
106 // stream messages to data channels and encoding/decoding messages. | |
107 class FakeQuicDataTransport : public sigslot::has_slots<> { | |
108 public: | |
109 FakeQuicDataTransport() {} | |
110 | |
111 void ConnectToTransportChannel(QuicTransportChannel* quic_transport_channel) { | |
112 quic_transport_channel->SignalIncomingStream.connect( | |
113 this, &FakeQuicDataTransport::OnIncomingStream); | |
114 } | |
115 | |
116 rtc::scoped_refptr<QuicDataChannel> CreateDataChannel( | |
117 int id, | |
118 const std::string& label, | |
119 const std::string& protocol) { | |
120 DataChannelInit config; | |
121 config.id = id; | |
122 config.protocol = protocol; | |
123 rtc::scoped_refptr<QuicDataChannel> data_channel( | |
124 new QuicDataChannel(rtc::Thread::Current(), rtc::Thread::Current(), | |
125 rtc::Thread::Current(), label, config)); | |
126 data_channel_by_id_[id] = data_channel; | |
127 return data_channel; | |
128 } | |
129 | |
130 private: | |
131 void OnIncomingStream(cricket::ReliableQuicStream* stream) { | |
132 incoming_stream_ = stream; | |
133 incoming_stream_->SignalDataReceived.connect( | |
134 this, &FakeQuicDataTransport::OnDataReceived); | |
135 } | |
136 | |
137 void OnDataReceived(net::QuicStreamId id, const char* data, size_t len) { | |
138 ASSERT_EQ(incoming_stream_->id(), id); | |
139 incoming_stream_->SignalDataReceived.disconnect(this); | |
140 // Retrieve the data channel ID and message ID. | |
141 int data_channel_id; | |
142 uint64_t message_id; | |
143 size_t bytes_read; | |
144 ASSERT_TRUE(webrtc::ParseQuicDataMessageHeader(data, len, &data_channel_id, | |
145 &message_id, &bytes_read)); | |
146 data += bytes_read; | |
147 len -= bytes_read; | |
148 // Dispatch the message to the matching QuicDataChannel. | |
149 const auto& kv = data_channel_by_id_.find(data_channel_id); | |
150 ASSERT_NE(kv, data_channel_by_id_.end()); | |
151 QuicDataChannel* data_channel = kv->second; | |
152 QuicDataChannel::Message message; | |
153 message.id = message_id; | |
154 message.buffer = rtc::CopyOnWriteBuffer(data, len); | |
155 message.stream = incoming_stream_; | |
156 data_channel->OnIncomingMessage(std::move(message)); | |
157 incoming_stream_ = nullptr; | |
158 } | |
159 | |
160 // Map of data channel ID => QuicDataChannel. | |
161 std::map<int, rtc::scoped_refptr<QuicDataChannel>> data_channel_by_id_; | |
162 // Last incoming QUIC stream which has arrived. | |
163 cricket::ReliableQuicStream* incoming_stream_ = nullptr; | |
164 }; | |
165 | |
166 // A peer who creates a QuicDataChannel to transfer data, and simulates network | |
167 // connectivity with a fake ICE channel wrapped by the QUIC transport channel. | |
168 class QuicDataChannelPeer { | |
169 public: | |
170 QuicDataChannelPeer() | |
171 : ice_transport_channel_(new FakeTransportChannel("data", 0)), | |
172 quic_transport_channel_(ice_transport_channel_) { | |
173 ice_transport_channel_->SetAsync(true); | |
174 fake_quic_data_transport_.ConnectToTransportChannel( | |
175 &quic_transport_channel_); | |
176 } | |
177 | |
178 void GenerateCertificateAndFingerprint() { | |
179 rtc::scoped_refptr<rtc::RTCCertificate> local_cert = | |
180 rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( | |
181 rtc::SSLIdentity::Generate("cert_name", rtc::KT_DEFAULT))); | |
182 quic_transport_channel_.SetLocalCertificate(local_cert); | |
183 local_fingerprint_.reset(CreateFingerprint(local_cert.get())); | |
184 } | |
185 | |
186 rtc::scoped_refptr<QuicDataChannel> CreateDataChannelWithTransportChannel( | |
187 int id, | |
188 const std::string& label, | |
189 const std::string& protocol) { | |
190 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
191 fake_quic_data_transport_.CreateDataChannel(id, label, protocol); | |
192 data_channel->SetTransportChannel(&quic_transport_channel_); | |
193 return data_channel; | |
194 } | |
195 | |
196 rtc::scoped_refptr<QuicDataChannel> CreateDataChannelWithoutTransportChannel( | |
197 int id, | |
198 const std::string& label, | |
199 const std::string& protocol) { | |
200 return fake_quic_data_transport_.CreateDataChannel(id, label, protocol); | |
201 } | |
202 | |
203 // Connects |ice_transport_channel_| to that of the other peer. | |
204 void Connect(QuicDataChannelPeer* other_peer) { | |
205 ice_transport_channel_->SetDestination(other_peer->ice_transport_channel_); | |
206 } | |
207 | |
208 std::unique_ptr<rtc::SSLFingerprint>& local_fingerprint() { | |
209 return local_fingerprint_; | |
210 } | |
211 | |
212 QuicTransportChannel* quic_transport_channel() { | |
213 return &quic_transport_channel_; | |
214 } | |
215 | |
216 FakeTransportChannel* ice_transport_channel() { | |
217 return ice_transport_channel_; | |
218 } | |
219 | |
220 private: | |
221 FakeTransportChannel* ice_transport_channel_; | |
222 QuicTransportChannel quic_transport_channel_; | |
223 | |
224 std::unique_ptr<rtc::SSLFingerprint> local_fingerprint_; | |
225 | |
226 FakeQuicDataTransport fake_quic_data_transport_; | |
227 }; | |
228 | |
229 class QuicDataChannelTest : public testing::Test { | |
230 public: | |
231 QuicDataChannelTest() {} | |
232 | |
233 // Connect the QuicTransportChannels and complete the crypto handshake. | |
234 void ConnectTransportChannels() { | |
235 SetCryptoParameters(); | |
236 peer1_.Connect(&peer2_); | |
237 ASSERT_TRUE_WAIT(peer1_.quic_transport_channel()->writable() && | |
238 peer2_.quic_transport_channel()->writable(), | |
239 kTimeoutMs); | |
240 } | |
241 | |
242 // Sets crypto parameters required for the QUIC handshake. | |
243 void SetCryptoParameters() { | |
244 peer1_.GenerateCertificateAndFingerprint(); | |
245 peer2_.GenerateCertificateAndFingerprint(); | |
246 | |
247 peer1_.quic_transport_channel()->SetSslRole(rtc::SSL_CLIENT); | |
248 peer2_.quic_transport_channel()->SetSslRole(rtc::SSL_SERVER); | |
249 | |
250 std::unique_ptr<rtc::SSLFingerprint>& peer1_fingerprint = | |
251 peer1_.local_fingerprint(); | |
252 std::unique_ptr<rtc::SSLFingerprint>& peer2_fingerprint = | |
253 peer2_.local_fingerprint(); | |
254 | |
255 peer1_.quic_transport_channel()->SetRemoteFingerprint( | |
256 peer2_fingerprint->algorithm, | |
257 reinterpret_cast<const uint8_t*>(peer2_fingerprint->digest.data()), | |
258 peer2_fingerprint->digest.size()); | |
259 peer2_.quic_transport_channel()->SetRemoteFingerprint( | |
260 peer1_fingerprint->algorithm, | |
261 reinterpret_cast<const uint8_t*>(peer1_fingerprint->digest.data()), | |
262 peer1_fingerprint->digest.size()); | |
263 } | |
264 | |
265 protected: | |
266 QuicDataChannelPeer peer1_; | |
267 QuicDataChannelPeer peer2_; | |
268 }; | |
269 | |
270 // Tests that a QuicDataChannel transitions from connecting to open when | |
271 // the QuicTransportChannel becomes writable for the first time. | |
272 TEST_F(QuicDataChannelTest, DataChannelOpensWhenTransportChannelConnects) { | |
273 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
274 peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); | |
275 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | |
276 ConnectTransportChannels(); | |
277 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, data_channel->state(), | |
278 kTimeoutMs); | |
279 } | |
280 | |
281 // Tests that a QuicDataChannel transitions from connecting to open when | |
282 // SetTransportChannel is called with a QuicTransportChannel that is already | |
283 // writable. | |
284 TEST_F(QuicDataChannelTest, DataChannelOpensWhenTransportChannelWritable) { | |
285 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
286 peer1_.CreateDataChannelWithoutTransportChannel(4, "label", "protocol"); | |
287 ConnectTransportChannels(); | |
288 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | |
289 data_channel->SetTransportChannel(peer1_.quic_transport_channel()); | |
290 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | |
291 } | |
292 | |
293 // Tests that the QuicDataChannel transfers messages small enough to fit into a | |
294 // single QUIC stream frame. | |
295 TEST_F(QuicDataChannelTest, TransferSmallMessage) { | |
296 ConnectTransportChannels(); | |
297 int data_channel_id = 2; | |
298 std::string label = "label"; | |
299 std::string protocol = "protocol"; | |
300 rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | |
301 peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
302 protocol); | |
303 ASSERT_TRUE(peer1_data_channel->state() == | |
304 webrtc::DataChannelInterface::kOpen); | |
305 rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | |
306 peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
307 protocol); | |
308 ASSERT_TRUE(peer2_data_channel->state() == | |
309 webrtc::DataChannelInterface::kOpen); | |
310 | |
311 FakeObserver peer1_observer; | |
312 peer1_data_channel->RegisterObserver(&peer1_observer); | |
313 FakeObserver peer2_observer; | |
314 peer2_data_channel->RegisterObserver(&peer2_observer); | |
315 | |
316 // peer1 -> peer2 | |
317 EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer1)); | |
318 ASSERT_EQ_WAIT(1, peer2_observer.messages_received(), kTimeoutMs); | |
319 EXPECT_EQ(kSmallMessage1, peer2_observer.messages()[0]); | |
320 // peer2 -> peer1 | |
321 EXPECT_TRUE(peer2_data_channel->Send(kSmallBuffer2)); | |
322 ASSERT_EQ_WAIT(1, peer1_observer.messages_received(), kTimeoutMs); | |
323 EXPECT_EQ(kSmallMessage2, peer1_observer.messages()[0]); | |
324 // peer2 -> peer1 | |
325 EXPECT_TRUE(peer2_data_channel->Send(kSmallBuffer3)); | |
326 ASSERT_EQ_WAIT(2, peer1_observer.messages_received(), kTimeoutMs); | |
327 EXPECT_EQ(kSmallMessage3, peer1_observer.messages()[1]); | |
328 // peer1 -> peer2 | |
329 EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer4)); | |
330 ASSERT_EQ_WAIT(2, peer2_observer.messages_received(), kTimeoutMs); | |
331 EXPECT_EQ(kSmallMessage4, peer2_observer.messages()[1]); | |
332 } | |
333 | |
334 // Tests that QuicDataChannel transfers messages large enough to fit into | |
335 // multiple QUIC stream frames, which don't violate the QUIC flow control limit. | |
336 // These require buffering by the QuicDataChannel. | |
337 TEST_F(QuicDataChannelTest, TransferLargeMessage) { | |
338 ConnectTransportChannels(); | |
339 int data_channel_id = 347; | |
340 std::string label = "label"; | |
341 std::string protocol = "protocol"; | |
342 rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | |
343 peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
344 protocol); | |
345 ASSERT_TRUE(peer1_data_channel->state() == | |
346 webrtc::DataChannelInterface::kOpen); | |
347 rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | |
348 peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
349 protocol); | |
350 ASSERT_TRUE(peer2_data_channel->state() == | |
351 webrtc::DataChannelInterface::kOpen); | |
352 | |
353 FakeObserver peer1_observer; | |
354 peer1_data_channel->RegisterObserver(&peer1_observer); | |
355 FakeObserver peer2_observer; | |
356 peer2_data_channel->RegisterObserver(&peer2_observer); | |
357 | |
358 // peer1 -> peer2 | |
359 EXPECT_TRUE(peer1_data_channel->Send(kLargeBuffer1)); | |
360 ASSERT_TRUE_WAIT(peer2_observer.messages_received() == 1, kTimeoutMs); | |
361 EXPECT_EQ(kLargeMessage1, peer2_observer.messages()[0]); | |
362 // peer2 -> peer1 | |
363 EXPECT_TRUE(peer2_data_channel->Send(kLargeBuffer2)); | |
364 ASSERT_EQ_WAIT(1, peer1_observer.messages_received(), kTimeoutMs); | |
365 EXPECT_EQ(kLargeMessage2, peer1_observer.messages()[0]); | |
366 // peer2 -> peer1 | |
367 EXPECT_TRUE(peer2_data_channel->Send(kLargeBuffer3)); | |
368 ASSERT_EQ_WAIT(2, peer1_observer.messages_received(), kTimeoutMs); | |
369 EXPECT_EQ(kLargeMessage3, peer1_observer.messages()[1]); | |
370 // peer1 -> peer2 | |
371 EXPECT_TRUE(peer1_data_channel->Send(kLargeBuffer4)); | |
372 ASSERT_EQ_WAIT(2, peer2_observer.messages_received(), kTimeoutMs); | |
373 EXPECT_EQ(kLargeMessage4, peer2_observer.messages()[1]); | |
374 } | |
375 | |
376 // Tests that when a message size exceeds the flow control limit (> 16KB), the | |
377 // QuicDataChannel can queue the data and send it after receiving window update | |
378 // frames from the remote peer. | |
379 TEST_F(QuicDataChannelTest, TransferOversizedMessage) { | |
380 ConnectTransportChannels(); | |
381 int data_channel_id = 189; | |
382 std::string label = "label"; | |
383 std::string protocol = "protocol"; | |
384 rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | |
385 peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
386 protocol); | |
387 rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | |
388 peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
389 protocol); | |
390 ASSERT_TRUE(peer2_data_channel->state() == | |
391 webrtc::DataChannelInterface::kOpen); | |
392 | |
393 FakeObserver peer1_observer; | |
394 peer1_data_channel->RegisterObserver(&peer1_observer); | |
395 FakeObserver peer2_observer; | |
396 peer2_data_channel->RegisterObserver(&peer2_observer); | |
397 | |
398 EXPECT_TRUE(peer1_data_channel->Send(kOversizedBuffer)); | |
399 EXPECT_EQ(1, peer1_data_channel->GetNumWriteBlockedStreams()); | |
400 EXPECT_EQ_WAIT(1, peer2_data_channel->GetNumIncomingStreams(), kTimeoutMs); | |
401 ASSERT_EQ_WAIT(1, peer2_observer.messages_received(), kTimeoutMs); | |
402 EXPECT_EQ(kOversizedMessage, peer2_observer.messages()[0]); | |
403 EXPECT_EQ(0, peer1_data_channel->GetNumWriteBlockedStreams()); | |
404 EXPECT_EQ(0, peer2_data_channel->GetNumIncomingStreams()); | |
405 } | |
406 | |
407 // Tests that empty messages can be sent. | |
408 TEST_F(QuicDataChannelTest, TransferEmptyMessage) { | |
409 ConnectTransportChannels(); | |
410 int data_channel_id = 69; | |
411 std::string label = "label"; | |
412 std::string protocol = "protocol"; | |
413 rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | |
414 peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
415 protocol); | |
416 rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | |
417 peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
418 protocol); | |
419 ASSERT_TRUE(peer2_data_channel->state() == | |
420 webrtc::DataChannelInterface::kOpen); | |
421 | |
422 FakeObserver peer1_observer; | |
423 peer1_data_channel->RegisterObserver(&peer1_observer); | |
424 FakeObserver peer2_observer; | |
425 peer2_data_channel->RegisterObserver(&peer2_observer); | |
426 | |
427 EXPECT_TRUE(peer1_data_channel->Send(DataBuffer(""))); | |
428 ASSERT_EQ_WAIT(1, peer2_observer.messages_received(), kTimeoutMs); | |
429 EXPECT_EQ("", peer2_observer.messages()[0]); | |
430 } | |
431 | |
432 // Tests that when the QuicDataChannel is open and sends a message while the | |
433 // QuicTransportChannel is unwritable, it gets buffered then received once the | |
434 // QuicTransportChannel becomes writable again. | |
435 TEST_F(QuicDataChannelTest, MessagesReceivedWhenTransportChannelReconnects) { | |
436 ConnectTransportChannels(); | |
437 int data_channel_id = 401; | |
438 std::string label = "label"; | |
439 std::string protocol = "protocol"; | |
440 rtc::scoped_refptr<QuicDataChannel> peer1_data_channel = | |
441 peer1_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
442 protocol); | |
443 ASSERT_TRUE(peer1_data_channel->state() == | |
444 webrtc::DataChannelInterface::kOpen); | |
445 rtc::scoped_refptr<QuicDataChannel> peer2_data_channel = | |
446 peer2_.CreateDataChannelWithTransportChannel(data_channel_id, label, | |
447 protocol); | |
448 ASSERT_TRUE(peer2_data_channel->state() == | |
449 webrtc::DataChannelInterface::kOpen); | |
450 | |
451 FakeObserver peer1_observer; | |
452 peer1_data_channel->RegisterObserver(&peer1_observer); | |
453 FakeObserver peer2_observer; | |
454 peer2_data_channel->RegisterObserver(&peer2_observer); | |
455 // writable => unwritable | |
456 peer1_.ice_transport_channel()->SetWritable(false); | |
457 ASSERT_FALSE(peer1_.quic_transport_channel()->writable()); | |
458 // Verify that sent data is buffered. | |
459 EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer1)); | |
460 EXPECT_EQ(1, peer1_data_channel->GetNumWriteBlockedStreams()); | |
461 EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer2)); | |
462 EXPECT_EQ(2, peer1_data_channel->GetNumWriteBlockedStreams()); | |
463 EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer3)); | |
464 EXPECT_EQ(3, peer1_data_channel->GetNumWriteBlockedStreams()); | |
465 EXPECT_TRUE(peer1_data_channel->Send(kSmallBuffer4)); | |
466 EXPECT_EQ(4, peer1_data_channel->GetNumWriteBlockedStreams()); | |
467 // unwritable => writable | |
468 peer1_.ice_transport_channel()->SetWritable(true); | |
469 ASSERT_TRUE(peer1_.quic_transport_channel()->writable()); | |
470 ASSERT_EQ_WAIT(4, peer2_observer.messages_received(), kTimeoutMs); | |
471 EXPECT_EQ(0, peer1_data_channel->GetNumWriteBlockedStreams()); | |
472 EXPECT_EQ(0, peer2_data_channel->GetNumIncomingStreams()); | |
473 } | |
474 | |
475 // Tests that the QuicDataChannel does not send before it is open. | |
476 TEST_F(QuicDataChannelTest, TransferMessageBeforeChannelOpens) { | |
477 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
478 peer1_.CreateDataChannelWithTransportChannel(6, "label", "protocol"); | |
479 ASSERT_TRUE(data_channel->state() == | |
480 webrtc::DataChannelInterface::kConnecting); | |
481 EXPECT_FALSE(data_channel->Send(kSmallBuffer1)); | |
482 } | |
483 | |
484 // Tests that the QuicDataChannel does not send after it is closed. | |
485 TEST_F(QuicDataChannelTest, TransferDataAfterChannelClosed) { | |
486 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
487 peer1_.CreateDataChannelWithTransportChannel(42, "label", "protocol"); | |
488 data_channel->Close(); | |
489 ASSERT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | |
490 kTimeoutMs); | |
491 EXPECT_FALSE(data_channel->Send(kSmallBuffer1)); | |
492 } | |
493 | |
494 // Tests that QuicDataChannel state changes fire OnStateChanged() for the | |
495 // observer, with the correct data channel states, when the data channel | |
496 // transitions from kConnecting => kOpen => kClosing => kClosed. | |
497 TEST_F(QuicDataChannelTest, OnStateChangedFired) { | |
498 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
499 peer1_.CreateDataChannelWithTransportChannel(7, "label", "protocol"); | |
500 FakeObserver observer; | |
501 data_channel->RegisterObserver(&observer); | |
502 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | |
503 EXPECT_EQ(0, observer.on_state_change_count()); | |
504 ConnectTransportChannels(); | |
505 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, data_channel->state(), | |
506 kTimeoutMs); | |
507 EXPECT_EQ(1, observer.on_state_change_count()); | |
508 data_channel->Close(); | |
509 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | |
510 kTimeoutMs); | |
511 // 2 state changes due to kClosing and kClosed. | |
512 EXPECT_EQ(3, observer.on_state_change_count()); | |
513 } | |
514 | |
515 // Tests that a QuicTransportChannel can be closed without being opened when it | |
516 // is connected to a transprot chanenl. | |
517 TEST_F(QuicDataChannelTest, NeverOpenedWithTransportChannel) { | |
518 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
519 peer1_.CreateDataChannelWithTransportChannel(7, "label", "protocol"); | |
520 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | |
521 data_channel->Close(); | |
522 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | |
523 kTimeoutMs); | |
524 } | |
525 | |
526 // Tests that a QuicTransportChannel can be closed without being opened or | |
527 // connected to a transport channel. | |
528 TEST_F(QuicDataChannelTest, NeverOpenedWithoutTransportChannel) { | |
529 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
530 peer1_.CreateDataChannelWithoutTransportChannel(7, "label", "protocol"); | |
531 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, data_channel->state()); | |
532 data_channel->Close(); | |
533 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | |
534 kTimeoutMs); | |
535 } | |
536 | |
537 // Tests that the QuicDataChannel is closed when the QUIC connection closes. | |
538 TEST_F(QuicDataChannelTest, ClosedOnTransportError) { | |
539 ConnectTransportChannels(); | |
540 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
541 peer1_.CreateDataChannelWithTransportChannel(1, "label", "protocol"); | |
542 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | |
543 ReliableQuicStream* stream = | |
544 peer1_.quic_transport_channel()->CreateQuicStream(); | |
545 ASSERT_NE(nullptr, stream); | |
546 stream->CloseConnectionWithDetails(net::QuicErrorCode::QUIC_NO_ERROR, | |
547 "Closing QUIC for testing"); | |
548 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | |
549 kTimeoutMs); | |
550 } | |
551 | |
552 // Tests that an already closed QuicDataChannel does not fire onStateChange and | |
553 // remains closed. | |
554 TEST_F(QuicDataChannelTest, DoesNotChangeStateWhenClosed) { | |
555 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
556 peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); | |
557 FakeObserver observer; | |
558 data_channel->RegisterObserver(&observer); | |
559 data_channel->Close(); | |
560 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, data_channel->state(), | |
561 kTimeoutMs); | |
562 // OnStateChange called for kClosing and kClosed. | |
563 EXPECT_EQ(2, observer.on_state_change_count()); | |
564 // Call Close() again to verify that the state cannot be kClosing. | |
565 data_channel->Close(); | |
566 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); | |
567 EXPECT_EQ(2, observer.on_state_change_count()); | |
568 ConnectTransportChannels(); | |
569 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); | |
570 EXPECT_EQ(2, observer.on_state_change_count()); | |
571 // writable => unwritable | |
572 peer1_.ice_transport_channel()->SetWritable(false); | |
573 ASSERT_FALSE(peer1_.quic_transport_channel()->writable()); | |
574 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); | |
575 EXPECT_EQ(2, observer.on_state_change_count()); | |
576 // unwritable => writable | |
577 peer1_.ice_transport_channel()->SetWritable(true); | |
578 ASSERT_TRUE(peer1_.quic_transport_channel()->writable()); | |
579 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, data_channel->state()); | |
580 EXPECT_EQ(2, observer.on_state_change_count()); | |
581 } | |
582 | |
583 // Tests that when the QuicDataChannel is open and the QuicTransportChannel | |
584 // transitions between writable and unwritable, it does not fire onStateChange | |
585 // and remains open. | |
586 TEST_F(QuicDataChannelTest, DoesNotChangeStateWhenTransportChannelReconnects) { | |
587 ConnectTransportChannels(); | |
588 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
589 peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); | |
590 FakeObserver observer; | |
591 data_channel->RegisterObserver(&observer); | |
592 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | |
593 EXPECT_EQ(0, observer.on_state_change_count()); | |
594 // writable => unwritable | |
595 peer1_.ice_transport_channel()->SetWritable(false); | |
596 ASSERT_FALSE(peer1_.quic_transport_channel()->writable()); | |
597 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | |
598 EXPECT_EQ(0, observer.on_state_change_count()); | |
599 // unwritable => writable | |
600 peer1_.ice_transport_channel()->SetWritable(true); | |
601 ASSERT_TRUE(peer1_.quic_transport_channel()->writable()); | |
602 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, data_channel->state()); | |
603 EXPECT_EQ(0, observer.on_state_change_count()); | |
604 } | |
605 | |
606 // Tests that SetTransportChannel returns false when setting a NULL transport | |
607 // channel or a transport channel that is not equivalent to the one already set. | |
608 TEST_F(QuicDataChannelTest, SetTransportChannelReturnValue) { | |
609 rtc::scoped_refptr<QuicDataChannel> data_channel = | |
610 peer1_.CreateDataChannelWithTransportChannel(4, "label", "protocol"); | |
611 EXPECT_FALSE(data_channel->SetTransportChannel(nullptr)); | |
612 QuicTransportChannel* transport_channel = peer1_.quic_transport_channel(); | |
613 EXPECT_TRUE(data_channel->SetTransportChannel(transport_channel)); | |
614 EXPECT_TRUE(data_channel->SetTransportChannel(transport_channel)); | |
615 QuicTransportChannel* other_transport_channel = | |
616 peer2_.quic_transport_channel(); | |
617 EXPECT_FALSE(data_channel->SetTransportChannel(other_transport_channel)); | |
618 } | |
619 | |
620 // Tests that the QUIC message header is encoded with the correct number of | |
621 // bytes and is properly decoded. | |
622 TEST_F(QuicDataChannelTest, EncodeParseQuicDataMessageHeader) { | |
623 int data_channel_id1 = 127; // 1 byte | |
624 uint64_t message_id1 = 0; // 1 byte | |
625 rtc::CopyOnWriteBuffer header1; | |
626 webrtc::WriteQuicDataChannelMessageHeader(data_channel_id1, message_id1, | |
627 &header1); | |
628 EXPECT_EQ(2u, header1.size()); | |
629 | |
630 int decoded_data_channel_id1; | |
631 uint64_t decoded_message_id1; | |
632 size_t bytes_read1; | |
633 ASSERT_TRUE(webrtc::ParseQuicDataMessageHeader( | |
634 header1.data<char>(), header1.size(), &decoded_data_channel_id1, | |
635 &decoded_message_id1, &bytes_read1)); | |
636 EXPECT_EQ(data_channel_id1, decoded_data_channel_id1); | |
637 EXPECT_EQ(message_id1, decoded_message_id1); | |
638 EXPECT_EQ(2u, bytes_read1); | |
639 | |
640 int data_channel_id2 = 4178; // 2 bytes | |
641 uint64_t message_id2 = 1324921792003; // 6 bytes | |
642 rtc::CopyOnWriteBuffer header2; | |
643 webrtc::WriteQuicDataChannelMessageHeader(data_channel_id2, message_id2, | |
644 &header2); | |
645 EXPECT_EQ(8u, header2.size()); | |
646 | |
647 int decoded_data_channel_id2; | |
648 uint64_t decoded_message_id2; | |
649 size_t bytes_read2; | |
650 ASSERT_TRUE(webrtc::ParseQuicDataMessageHeader( | |
651 header2.data<char>(), header2.size(), &decoded_data_channel_id2, | |
652 &decoded_message_id2, &bytes_read2)); | |
653 EXPECT_EQ(data_channel_id2, decoded_data_channel_id2); | |
654 EXPECT_EQ(message_id2, decoded_message_id2); | |
655 EXPECT_EQ(8u, bytes_read2); | |
656 } | |
657 | |
658 } // namespace | |
OLD | NEW |