OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2013 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 <memory> | |
12 | |
13 #include "webrtc/api/datachannel.h" | |
14 #include "webrtc/api/sctputils.h" | |
15 #include "webrtc/api/test/fakedatachannelprovider.h" | |
16 #include "webrtc/base/gunit.h" | |
17 | |
18 using webrtc::DataChannel; | |
19 using webrtc::SctpSidAllocator; | |
20 | |
21 static constexpr int kDefaultTimeout = 10000; | |
22 | |
23 class FakeDataChannelObserver : public webrtc::DataChannelObserver { | |
24 public: | |
25 FakeDataChannelObserver() | |
26 : messages_received_(0), | |
27 on_state_change_count_(0), | |
28 on_buffered_amount_change_count_(0) {} | |
29 | |
30 void OnStateChange() { | |
31 ++on_state_change_count_; | |
32 } | |
33 | |
34 void OnBufferedAmountChange(uint64_t previous_amount) { | |
35 ++on_buffered_amount_change_count_; | |
36 } | |
37 | |
38 void OnMessage(const webrtc::DataBuffer& buffer) { | |
39 ++messages_received_; | |
40 } | |
41 | |
42 size_t messages_received() const { | |
43 return messages_received_; | |
44 } | |
45 | |
46 void ResetOnStateChangeCount() { | |
47 on_state_change_count_ = 0; | |
48 } | |
49 | |
50 void ResetOnBufferedAmountChangeCount() { | |
51 on_buffered_amount_change_count_ = 0; | |
52 } | |
53 | |
54 size_t on_state_change_count() const { | |
55 return on_state_change_count_; | |
56 } | |
57 | |
58 size_t on_buffered_amount_change_count() const { | |
59 return on_buffered_amount_change_count_; | |
60 } | |
61 | |
62 private: | |
63 size_t messages_received_; | |
64 size_t on_state_change_count_; | |
65 size_t on_buffered_amount_change_count_; | |
66 }; | |
67 | |
68 class SctpDataChannelTest : public testing::Test { | |
69 protected: | |
70 SctpDataChannelTest() | |
71 : provider_(new FakeDataChannelProvider()), | |
72 webrtc_data_channel_(DataChannel::Create(provider_.get(), | |
73 cricket::DCT_SCTP, | |
74 "test", | |
75 init_)) {} | |
76 | |
77 void SetChannelReady() { | |
78 provider_->set_transport_available(true); | |
79 webrtc_data_channel_->OnTransportChannelCreated(); | |
80 if (webrtc_data_channel_->id() < 0) { | |
81 webrtc_data_channel_->SetSctpSid(0); | |
82 } | |
83 provider_->set_ready_to_send(true); | |
84 } | |
85 | |
86 void AddObserver() { | |
87 observer_.reset(new FakeDataChannelObserver()); | |
88 webrtc_data_channel_->RegisterObserver(observer_.get()); | |
89 } | |
90 | |
91 webrtc::InternalDataChannelInit init_; | |
92 std::unique_ptr<FakeDataChannelProvider> provider_; | |
93 std::unique_ptr<FakeDataChannelObserver> observer_; | |
94 rtc::scoped_refptr<DataChannel> webrtc_data_channel_; | |
95 }; | |
96 | |
97 class StateSignalsListener : public sigslot::has_slots<> { | |
98 public: | |
99 int opened_count() const { return opened_count_; } | |
100 int closed_count() const { return closed_count_; } | |
101 | |
102 void OnSignalOpened(DataChannel* data_channel) { | |
103 ++opened_count_; | |
104 } | |
105 | |
106 void OnSignalClosed(DataChannel* data_channel) { | |
107 ++closed_count_; | |
108 } | |
109 | |
110 private: | |
111 int opened_count_ = 0; | |
112 int closed_count_ = 0; | |
113 }; | |
114 | |
115 // Verifies that the data channel is connected to the transport after creation. | |
116 TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { | |
117 provider_->set_transport_available(true); | |
118 rtc::scoped_refptr<DataChannel> dc = | |
119 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init_); | |
120 | |
121 EXPECT_TRUE(provider_->IsConnected(dc.get())); | |
122 // The sid is not set yet, so it should not have added the streams. | |
123 EXPECT_FALSE(provider_->IsSendStreamAdded(dc->id())); | |
124 EXPECT_FALSE(provider_->IsRecvStreamAdded(dc->id())); | |
125 | |
126 dc->SetSctpSid(0); | |
127 EXPECT_TRUE(provider_->IsSendStreamAdded(dc->id())); | |
128 EXPECT_TRUE(provider_->IsRecvStreamAdded(dc->id())); | |
129 } | |
130 | |
131 // Verifies that the data channel is connected to the transport if the transport | |
132 // is not available initially and becomes available later. | |
133 TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) { | |
134 EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get())); | |
135 | |
136 provider_->set_transport_available(true); | |
137 webrtc_data_channel_->OnTransportChannelCreated(); | |
138 EXPECT_TRUE(provider_->IsConnected(webrtc_data_channel_.get())); | |
139 } | |
140 | |
141 // Tests the state of the data channel. | |
142 TEST_F(SctpDataChannelTest, StateTransition) { | |
143 StateSignalsListener state_signals_listener; | |
144 webrtc_data_channel_->SignalOpened.connect( | |
145 &state_signals_listener, &StateSignalsListener::OnSignalOpened); | |
146 webrtc_data_channel_->SignalClosed.connect( | |
147 &state_signals_listener, &StateSignalsListener::OnSignalClosed); | |
148 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, | |
149 webrtc_data_channel_->state()); | |
150 EXPECT_EQ(state_signals_listener.opened_count(), 0); | |
151 EXPECT_EQ(state_signals_listener.closed_count(), 0); | |
152 SetChannelReady(); | |
153 | |
154 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); | |
155 EXPECT_EQ(state_signals_listener.opened_count(), 1); | |
156 EXPECT_EQ(state_signals_listener.closed_count(), 0); | |
157 webrtc_data_channel_->Close(); | |
158 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, | |
159 webrtc_data_channel_->state()); | |
160 EXPECT_EQ(state_signals_listener.opened_count(), 1); | |
161 EXPECT_EQ(state_signals_listener.closed_count(), 1); | |
162 // Verifies that it's disconnected from the transport. | |
163 EXPECT_FALSE(provider_->IsConnected(webrtc_data_channel_.get())); | |
164 } | |
165 | |
166 // Tests that DataChannel::buffered_amount() is correct after the channel is | |
167 // blocked. | |
168 TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { | |
169 AddObserver(); | |
170 SetChannelReady(); | |
171 webrtc::DataBuffer buffer("abcd"); | |
172 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | |
173 | |
174 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); | |
175 EXPECT_EQ(0U, observer_->on_buffered_amount_change_count()); | |
176 | |
177 provider_->set_send_blocked(true); | |
178 | |
179 const int number_of_packets = 3; | |
180 for (int i = 0; i < number_of_packets; ++i) { | |
181 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | |
182 } | |
183 EXPECT_EQ(buffer.data.size() * number_of_packets, | |
184 webrtc_data_channel_->buffered_amount()); | |
185 EXPECT_EQ(number_of_packets, observer_->on_buffered_amount_change_count()); | |
186 } | |
187 | |
188 // Tests that the queued data are sent when the channel transitions from blocked | |
189 // to unblocked. | |
190 TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { | |
191 AddObserver(); | |
192 SetChannelReady(); | |
193 webrtc::DataBuffer buffer("abcd"); | |
194 provider_->set_send_blocked(true); | |
195 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | |
196 | |
197 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); | |
198 | |
199 provider_->set_send_blocked(false); | |
200 SetChannelReady(); | |
201 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); | |
202 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); | |
203 } | |
204 | |
205 // Tests that no crash when the channel is blocked right away while trying to | |
206 // send queued data. | |
207 TEST_F(SctpDataChannelTest, BlockedWhenSendQueuedDataNoCrash) { | |
208 AddObserver(); | |
209 SetChannelReady(); | |
210 webrtc::DataBuffer buffer("abcd"); | |
211 provider_->set_send_blocked(true); | |
212 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | |
213 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); | |
214 | |
215 // Set channel ready while it is still blocked. | |
216 SetChannelReady(); | |
217 EXPECT_EQ(buffer.size(), webrtc_data_channel_->buffered_amount()); | |
218 EXPECT_EQ(1U, observer_->on_buffered_amount_change_count()); | |
219 | |
220 // Unblock the channel to send queued data again, there should be no crash. | |
221 provider_->set_send_blocked(false); | |
222 SetChannelReady(); | |
223 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); | |
224 EXPECT_EQ(2U, observer_->on_buffered_amount_change_count()); | |
225 } | |
226 | |
227 // Tests that DataChannel::messages_sent() and DataChannel::bytes_sent() are | |
228 // correct, sending data both while unblocked and while blocked. | |
229 TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesSent) { | |
230 AddObserver(); | |
231 SetChannelReady(); | |
232 std::vector<webrtc::DataBuffer> buffers({ | |
233 webrtc::DataBuffer("message 1"), | |
234 webrtc::DataBuffer("msg 2"), | |
235 webrtc::DataBuffer("message three"), | |
236 webrtc::DataBuffer("quadra message"), | |
237 webrtc::DataBuffer("fifthmsg"), | |
238 webrtc::DataBuffer("message of the beast"), | |
239 }); | |
240 | |
241 // Default values. | |
242 EXPECT_EQ(0U, webrtc_data_channel_->messages_sent()); | |
243 EXPECT_EQ(0U, webrtc_data_channel_->bytes_sent()); | |
244 | |
245 // Send three buffers while not blocked. | |
246 provider_->set_send_blocked(false); | |
247 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[0])); | |
248 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[1])); | |
249 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[2])); | |
250 size_t bytes_sent = buffers[0].size() + buffers[1].size() + buffers[2].size(); | |
251 EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout); | |
252 EXPECT_EQ(3U, webrtc_data_channel_->messages_sent()); | |
253 EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent()); | |
254 | |
255 // Send three buffers while blocked, queuing the buffers. | |
256 provider_->set_send_blocked(true); | |
257 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[3])); | |
258 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[4])); | |
259 EXPECT_TRUE(webrtc_data_channel_->Send(buffers[5])); | |
260 size_t bytes_queued = | |
261 buffers[3].size() + buffers[4].size() + buffers[5].size(); | |
262 EXPECT_EQ(bytes_queued, webrtc_data_channel_->buffered_amount()); | |
263 EXPECT_EQ(3U, webrtc_data_channel_->messages_sent()); | |
264 EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent()); | |
265 | |
266 // Unblock and make sure everything was sent. | |
267 provider_->set_send_blocked(false); | |
268 EXPECT_EQ_WAIT(0U, webrtc_data_channel_->buffered_amount(), kDefaultTimeout); | |
269 bytes_sent += bytes_queued; | |
270 EXPECT_EQ(6U, webrtc_data_channel_->messages_sent()); | |
271 EXPECT_EQ(bytes_sent, webrtc_data_channel_->bytes_sent()); | |
272 } | |
273 | |
274 // Tests that the queued control message is sent when channel is ready. | |
275 TEST_F(SctpDataChannelTest, OpenMessageSent) { | |
276 // Initially the id is unassigned. | |
277 EXPECT_EQ(-1, webrtc_data_channel_->id()); | |
278 | |
279 SetChannelReady(); | |
280 EXPECT_GE(webrtc_data_channel_->id(), 0); | |
281 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); | |
282 EXPECT_EQ(provider_->last_send_data_params().ssrc, | |
283 static_cast<uint32_t>(webrtc_data_channel_->id())); | |
284 } | |
285 | |
286 TEST_F(SctpDataChannelTest, QueuedOpenMessageSent) { | |
287 provider_->set_send_blocked(true); | |
288 SetChannelReady(); | |
289 provider_->set_send_blocked(false); | |
290 | |
291 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); | |
292 EXPECT_EQ(provider_->last_send_data_params().ssrc, | |
293 static_cast<uint32_t>(webrtc_data_channel_->id())); | |
294 } | |
295 | |
296 // Tests that the DataChannel created after transport gets ready can enter OPEN | |
297 // state. | |
298 TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) { | |
299 SetChannelReady(); | |
300 webrtc::InternalDataChannelInit init; | |
301 init.id = 1; | |
302 rtc::scoped_refptr<DataChannel> dc = | |
303 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init); | |
304 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state()); | |
305 EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(), | |
306 1000); | |
307 } | |
308 | |
309 // Tests that an unordered DataChannel sends data as ordered until the OPEN_ACK | |
310 // message is received. | |
311 TEST_F(SctpDataChannelTest, SendUnorderedAfterReceivesOpenAck) { | |
312 SetChannelReady(); | |
313 webrtc::InternalDataChannelInit init; | |
314 init.id = 1; | |
315 init.ordered = false; | |
316 rtc::scoped_refptr<DataChannel> dc = | |
317 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init); | |
318 | |
319 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); | |
320 | |
321 // Sends a message and verifies it's ordered. | |
322 webrtc::DataBuffer buffer("some data"); | |
323 ASSERT_TRUE(dc->Send(buffer)); | |
324 EXPECT_TRUE(provider_->last_send_data_params().ordered); | |
325 | |
326 // Emulates receiving an OPEN_ACK message. | |
327 cricket::ReceiveDataParams params; | |
328 params.ssrc = init.id; | |
329 params.type = cricket::DMT_CONTROL; | |
330 rtc::CopyOnWriteBuffer payload; | |
331 webrtc::WriteDataChannelOpenAckMessage(&payload); | |
332 dc->OnDataReceived(params, payload); | |
333 | |
334 // Sends another message and verifies it's unordered. | |
335 ASSERT_TRUE(dc->Send(buffer)); | |
336 EXPECT_FALSE(provider_->last_send_data_params().ordered); | |
337 } | |
338 | |
339 // Tests that an unordered DataChannel sends unordered data after any DATA | |
340 // message is received. | |
341 TEST_F(SctpDataChannelTest, SendUnorderedAfterReceiveData) { | |
342 SetChannelReady(); | |
343 webrtc::InternalDataChannelInit init; | |
344 init.id = 1; | |
345 init.ordered = false; | |
346 rtc::scoped_refptr<DataChannel> dc = | |
347 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", init); | |
348 | |
349 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); | |
350 | |
351 // Emulates receiving a DATA message. | |
352 cricket::ReceiveDataParams params; | |
353 params.ssrc = init.id; | |
354 params.type = cricket::DMT_TEXT; | |
355 webrtc::DataBuffer buffer("data"); | |
356 dc->OnDataReceived(params, buffer.data); | |
357 | |
358 // Sends a message and verifies it's unordered. | |
359 ASSERT_TRUE(dc->Send(buffer)); | |
360 EXPECT_FALSE(provider_->last_send_data_params().ordered); | |
361 } | |
362 | |
363 // Tests that the channel can't open until it's successfully sent the OPEN | |
364 // message. | |
365 TEST_F(SctpDataChannelTest, OpenWaitsForOpenMesssage) { | |
366 webrtc::DataBuffer buffer("foo"); | |
367 | |
368 provider_->set_send_blocked(true); | |
369 SetChannelReady(); | |
370 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, | |
371 webrtc_data_channel_->state()); | |
372 provider_->set_send_blocked(false); | |
373 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, | |
374 webrtc_data_channel_->state(), 1000); | |
375 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); | |
376 } | |
377 | |
378 // Tests that close first makes sure all queued data gets sent. | |
379 TEST_F(SctpDataChannelTest, QueuedCloseFlushes) { | |
380 webrtc::DataBuffer buffer("foo"); | |
381 | |
382 provider_->set_send_blocked(true); | |
383 SetChannelReady(); | |
384 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, | |
385 webrtc_data_channel_->state()); | |
386 provider_->set_send_blocked(false); | |
387 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, | |
388 webrtc_data_channel_->state(), 1000); | |
389 provider_->set_send_blocked(true); | |
390 webrtc_data_channel_->Send(buffer); | |
391 webrtc_data_channel_->Close(); | |
392 provider_->set_send_blocked(false); | |
393 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, | |
394 webrtc_data_channel_->state(), 1000); | |
395 EXPECT_EQ(cricket::DMT_TEXT, provider_->last_send_data_params().type); | |
396 } | |
397 | |
398 // Tests that messages are sent with the right ssrc. | |
399 TEST_F(SctpDataChannelTest, SendDataSsrc) { | |
400 webrtc_data_channel_->SetSctpSid(1); | |
401 SetChannelReady(); | |
402 webrtc::DataBuffer buffer("data"); | |
403 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | |
404 EXPECT_EQ(1U, provider_->last_send_data_params().ssrc); | |
405 } | |
406 | |
407 // Tests that the incoming messages with wrong ssrcs are rejected. | |
408 TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) { | |
409 webrtc_data_channel_->SetSctpSid(1); | |
410 SetChannelReady(); | |
411 | |
412 AddObserver(); | |
413 | |
414 cricket::ReceiveDataParams params; | |
415 params.ssrc = 0; | |
416 webrtc::DataBuffer buffer("abcd"); | |
417 webrtc_data_channel_->OnDataReceived(params, buffer.data); | |
418 | |
419 EXPECT_EQ(0U, observer_->messages_received()); | |
420 } | |
421 | |
422 // Tests that the incoming messages with right ssrcs are acceted. | |
423 TEST_F(SctpDataChannelTest, ReceiveDataWithValidSsrc) { | |
424 webrtc_data_channel_->SetSctpSid(1); | |
425 SetChannelReady(); | |
426 | |
427 AddObserver(); | |
428 | |
429 cricket::ReceiveDataParams params; | |
430 params.ssrc = 1; | |
431 webrtc::DataBuffer buffer("abcd"); | |
432 | |
433 webrtc_data_channel_->OnDataReceived(params, buffer.data); | |
434 EXPECT_EQ(1U, observer_->messages_received()); | |
435 } | |
436 | |
437 // Tests that no CONTROL message is sent if the datachannel is negotiated and | |
438 // not created from an OPEN message. | |
439 TEST_F(SctpDataChannelTest, NoMsgSentIfNegotiatedAndNotFromOpenMsg) { | |
440 webrtc::InternalDataChannelInit config; | |
441 config.id = 1; | |
442 config.negotiated = true; | |
443 config.open_handshake_role = webrtc::InternalDataChannelInit::kNone; | |
444 | |
445 SetChannelReady(); | |
446 rtc::scoped_refptr<DataChannel> dc = | |
447 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", config); | |
448 | |
449 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); | |
450 EXPECT_EQ(0U, provider_->last_send_data_params().ssrc); | |
451 } | |
452 | |
453 // Tests that DataChannel::messages_received() and DataChannel::bytes_received() | |
454 // are correct, receiving data both while not open and while open. | |
455 TEST_F(SctpDataChannelTest, VerifyMessagesAndBytesReceived) { | |
456 AddObserver(); | |
457 std::vector<webrtc::DataBuffer> buffers({ | |
458 webrtc::DataBuffer("message 1"), | |
459 webrtc::DataBuffer("msg 2"), | |
460 webrtc::DataBuffer("message three"), | |
461 webrtc::DataBuffer("quadra message"), | |
462 webrtc::DataBuffer("fifthmsg"), | |
463 webrtc::DataBuffer("message of the beast"), | |
464 }); | |
465 | |
466 webrtc_data_channel_->SetSctpSid(1); | |
467 cricket::ReceiveDataParams params; | |
468 params.ssrc = 1; | |
469 | |
470 // Default values. | |
471 EXPECT_EQ(0U, webrtc_data_channel_->messages_received()); | |
472 EXPECT_EQ(0U, webrtc_data_channel_->bytes_received()); | |
473 | |
474 // Receive three buffers while data channel isn't open. | |
475 webrtc_data_channel_->OnDataReceived(params, buffers[0].data); | |
476 webrtc_data_channel_->OnDataReceived(params, buffers[1].data); | |
477 webrtc_data_channel_->OnDataReceived(params, buffers[2].data); | |
478 EXPECT_EQ(0U, observer_->messages_received()); | |
479 EXPECT_EQ(0U, webrtc_data_channel_->messages_received()); | |
480 EXPECT_EQ(0U, webrtc_data_channel_->bytes_received()); | |
481 | |
482 // Open channel and make sure everything was received. | |
483 SetChannelReady(); | |
484 size_t bytes_received = | |
485 buffers[0].size() + buffers[1].size() + buffers[2].size(); | |
486 EXPECT_EQ(3U, observer_->messages_received()); | |
487 EXPECT_EQ(3U, webrtc_data_channel_->messages_received()); | |
488 EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received()); | |
489 | |
490 // Receive three buffers while open. | |
491 webrtc_data_channel_->OnDataReceived(params, buffers[3].data); | |
492 webrtc_data_channel_->OnDataReceived(params, buffers[4].data); | |
493 webrtc_data_channel_->OnDataReceived(params, buffers[5].data); | |
494 bytes_received += buffers[3].size() + buffers[4].size() + buffers[5].size(); | |
495 EXPECT_EQ(6U, observer_->messages_received()); | |
496 EXPECT_EQ(6U, webrtc_data_channel_->messages_received()); | |
497 EXPECT_EQ(bytes_received, webrtc_data_channel_->bytes_received()); | |
498 } | |
499 | |
500 // Tests that OPEN_ACK message is sent if the datachannel is created from an | |
501 // OPEN message. | |
502 TEST_F(SctpDataChannelTest, OpenAckSentIfCreatedFromOpenMessage) { | |
503 webrtc::InternalDataChannelInit config; | |
504 config.id = 1; | |
505 config.negotiated = true; | |
506 config.open_handshake_role = webrtc::InternalDataChannelInit::kAcker; | |
507 | |
508 SetChannelReady(); | |
509 rtc::scoped_refptr<DataChannel> dc = | |
510 DataChannel::Create(provider_.get(), cricket::DCT_SCTP, "test1", config); | |
511 | |
512 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kOpen, dc->state(), 1000); | |
513 | |
514 EXPECT_EQ(static_cast<unsigned int>(config.id), | |
515 provider_->last_send_data_params().ssrc); | |
516 EXPECT_EQ(cricket::DMT_CONTROL, provider_->last_send_data_params().type); | |
517 } | |
518 | |
519 // Tests the OPEN_ACK role assigned by InternalDataChannelInit. | |
520 TEST_F(SctpDataChannelTest, OpenAckRoleInitialization) { | |
521 webrtc::InternalDataChannelInit init; | |
522 EXPECT_EQ(webrtc::InternalDataChannelInit::kOpener, init.open_handshake_role); | |
523 EXPECT_FALSE(init.negotiated); | |
524 | |
525 webrtc::DataChannelInit base; | |
526 base.negotiated = true; | |
527 webrtc::InternalDataChannelInit init2(base); | |
528 EXPECT_EQ(webrtc::InternalDataChannelInit::kNone, init2.open_handshake_role); | |
529 } | |
530 | |
531 // Tests that the DataChannel is closed if the sending buffer is full. | |
532 TEST_F(SctpDataChannelTest, ClosedWhenSendBufferFull) { | |
533 SetChannelReady(); | |
534 | |
535 rtc::CopyOnWriteBuffer buffer(1024); | |
536 memset(buffer.data(), 0, buffer.size()); | |
537 | |
538 webrtc::DataBuffer packet(buffer, true); | |
539 provider_->set_send_blocked(true); | |
540 | |
541 for (size_t i = 0; i < 16 * 1024 + 1; ++i) { | |
542 EXPECT_TRUE(webrtc_data_channel_->Send(packet)); | |
543 } | |
544 | |
545 EXPECT_TRUE( | |
546 webrtc::DataChannelInterface::kClosed == webrtc_data_channel_->state() || | |
547 webrtc::DataChannelInterface::kClosing == webrtc_data_channel_->state()); | |
548 } | |
549 | |
550 // Tests that the DataChannel is closed on transport errors. | |
551 TEST_F(SctpDataChannelTest, ClosedOnTransportError) { | |
552 SetChannelReady(); | |
553 webrtc::DataBuffer buffer("abcd"); | |
554 provider_->set_transport_error(); | |
555 | |
556 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | |
557 | |
558 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, | |
559 webrtc_data_channel_->state()); | |
560 } | |
561 | |
562 // Tests that a already closed DataChannel does not fire onStateChange again. | |
563 TEST_F(SctpDataChannelTest, ClosedDataChannelDoesNotFireOnStateChange) { | |
564 AddObserver(); | |
565 webrtc_data_channel_->Close(); | |
566 // OnStateChange called for kClosing and kClosed. | |
567 EXPECT_EQ(2U, observer_->on_state_change_count()); | |
568 | |
569 observer_->ResetOnStateChangeCount(); | |
570 webrtc_data_channel_->RemotePeerRequestClose(); | |
571 EXPECT_EQ(0U, observer_->on_state_change_count()); | |
572 } | |
573 | |
574 // Tests that RemotePeerRequestClose closes the local DataChannel. | |
575 TEST_F(SctpDataChannelTest, RemotePeerRequestClose) { | |
576 AddObserver(); | |
577 webrtc_data_channel_->RemotePeerRequestClose(); | |
578 | |
579 // OnStateChange called for kClosing and kClosed. | |
580 EXPECT_EQ(2U, observer_->on_state_change_count()); | |
581 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, | |
582 webrtc_data_channel_->state()); | |
583 } | |
584 | |
585 // Tests that the DataChannel is closed if the received buffer is full. | |
586 TEST_F(SctpDataChannelTest, ClosedWhenReceivedBufferFull) { | |
587 SetChannelReady(); | |
588 rtc::CopyOnWriteBuffer buffer(1024); | |
589 memset(buffer.data(), 0, buffer.size()); | |
590 | |
591 cricket::ReceiveDataParams params; | |
592 params.ssrc = 0; | |
593 | |
594 // Receiving data without having an observer will overflow the buffer. | |
595 for (size_t i = 0; i < 16 * 1024 + 1; ++i) { | |
596 webrtc_data_channel_->OnDataReceived(params, buffer); | |
597 } | |
598 EXPECT_EQ(webrtc::DataChannelInterface::kClosed, | |
599 webrtc_data_channel_->state()); | |
600 } | |
601 | |
602 // Tests that sending empty data returns no error and keeps the channel open. | |
603 TEST_F(SctpDataChannelTest, SendEmptyData) { | |
604 webrtc_data_channel_->SetSctpSid(1); | |
605 SetChannelReady(); | |
606 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, | |
607 webrtc_data_channel_->state()); | |
608 | |
609 webrtc::DataBuffer buffer(""); | |
610 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); | |
611 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, | |
612 webrtc_data_channel_->state()); | |
613 } | |
614 | |
615 // Tests that a channel can be closed without being opened or assigned an sid. | |
616 TEST_F(SctpDataChannelTest, NeverOpened) { | |
617 provider_->set_transport_available(true); | |
618 webrtc_data_channel_->OnTransportChannelCreated(); | |
619 webrtc_data_channel_->Close(); | |
620 } | |
621 | |
622 // Test that the data channel goes to the "closed" state (and doesn't crash) | |
623 // when its transport goes away, even while data is buffered. | |
624 TEST_F(SctpDataChannelTest, TransportDestroyedWhileDataBuffered) { | |
625 SetChannelReady(); | |
626 | |
627 rtc::CopyOnWriteBuffer buffer(1024); | |
628 memset(buffer.data(), 0, buffer.size()); | |
629 webrtc::DataBuffer packet(buffer, true); | |
630 | |
631 // Send a packet while sending is blocked so it ends up buffered. | |
632 provider_->set_send_blocked(true); | |
633 EXPECT_TRUE(webrtc_data_channel_->Send(packet)); | |
634 | |
635 // Tell the data channel that its tranpsort is being destroyed. | |
636 // It should then stop using the transport (allowing us to delete it) and | |
637 // transition to the "closed" state. | |
638 webrtc_data_channel_->OnTransportChannelDestroyed(); | |
639 provider_.reset(nullptr); | |
640 EXPECT_EQ_WAIT(webrtc::DataChannelInterface::kClosed, | |
641 webrtc_data_channel_->state(), kDefaultTimeout); | |
642 } | |
643 | |
644 class SctpSidAllocatorTest : public testing::Test { | |
645 protected: | |
646 SctpSidAllocator allocator_; | |
647 }; | |
648 | |
649 // Verifies that an even SCTP id is allocated for SSL_CLIENT and an odd id for | |
650 // SSL_SERVER. | |
651 TEST_F(SctpSidAllocatorTest, SctpIdAllocationBasedOnRole) { | |
652 int id; | |
653 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); | |
654 EXPECT_EQ(1, id); | |
655 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id)); | |
656 EXPECT_EQ(0, id); | |
657 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &id)); | |
658 EXPECT_EQ(3, id); | |
659 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &id)); | |
660 EXPECT_EQ(2, id); | |
661 } | |
662 | |
663 // Verifies that SCTP ids of existing DataChannels are not reused. | |
664 TEST_F(SctpSidAllocatorTest, SctpIdAllocationNoReuse) { | |
665 int old_id = 1; | |
666 EXPECT_TRUE(allocator_.ReserveSid(old_id)); | |
667 | |
668 int new_id; | |
669 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &new_id)); | |
670 EXPECT_NE(old_id, new_id); | |
671 | |
672 old_id = 0; | |
673 EXPECT_TRUE(allocator_.ReserveSid(old_id)); | |
674 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &new_id)); | |
675 EXPECT_NE(old_id, new_id); | |
676 } | |
677 | |
678 // Verifies that SCTP ids of removed DataChannels can be reused. | |
679 TEST_F(SctpSidAllocatorTest, SctpIdReusedForRemovedDataChannel) { | |
680 int odd_id = 1; | |
681 int even_id = 0; | |
682 EXPECT_TRUE(allocator_.ReserveSid(odd_id)); | |
683 EXPECT_TRUE(allocator_.ReserveSid(even_id)); | |
684 | |
685 int allocated_id = -1; | |
686 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); | |
687 EXPECT_EQ(odd_id + 2, allocated_id); | |
688 | |
689 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); | |
690 EXPECT_EQ(even_id + 2, allocated_id); | |
691 | |
692 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); | |
693 EXPECT_EQ(odd_id + 4, allocated_id); | |
694 | |
695 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); | |
696 EXPECT_EQ(even_id + 4, allocated_id); | |
697 | |
698 allocator_.ReleaseSid(odd_id); | |
699 allocator_.ReleaseSid(even_id); | |
700 | |
701 // Verifies that removed ids are reused. | |
702 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); | |
703 EXPECT_EQ(odd_id, allocated_id); | |
704 | |
705 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); | |
706 EXPECT_EQ(even_id, allocated_id); | |
707 | |
708 // Verifies that used higher ids are not reused. | |
709 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_SERVER, &allocated_id)); | |
710 EXPECT_EQ(odd_id + 6, allocated_id); | |
711 | |
712 EXPECT_TRUE(allocator_.AllocateSid(rtc::SSL_CLIENT, &allocated_id)); | |
713 EXPECT_EQ(even_id + 6, allocated_id); | |
714 } | |
OLD | NEW |