blob: 5d298110cf9e593dd22fdb9e752391ae111f0025 [file] [log] [blame]
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001/*
2 * libjingle
3 * Copyright 2013, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/datachannel.h"
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000029#include "talk/app/webrtc/test/fakedatachannelprovider.h"
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000030#include "talk/base/gunit.h"
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000031
wu@webrtc.org78187522013-10-07 23:32:02 +000032using webrtc::DataChannel;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000033
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000034class SctpDataChannelTest : public testing::Test {
35 protected:
36 SctpDataChannelTest()
wu@webrtc.org78187522013-10-07 23:32:02 +000037 : webrtc_data_channel_(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000038 DataChannel::Create(&provider_, cricket::DCT_SCTP, "test", &init_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000039 }
40
wu@webrtc.org78187522013-10-07 23:32:02 +000041 void SetChannelReady() {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000042 webrtc_data_channel_->OnTransportChannelCreated();
43 if (webrtc_data_channel_->id() < 0) {
44 webrtc_data_channel_->SetSctpSid(0);
45 }
wu@webrtc.org78187522013-10-07 23:32:02 +000046 webrtc_data_channel_->OnChannelReady(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000047 }
wu@webrtc.org78187522013-10-07 23:32:02 +000048
49 webrtc::DataChannelInit init_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000050 FakeDataChannelProvider provider_;
wu@webrtc.org78187522013-10-07 23:32:02 +000051 talk_base::scoped_refptr<DataChannel> webrtc_data_channel_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000052};
53
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000054// Verifies that the data channel is connected to the transport after creation.
55TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) {
56 EXPECT_TRUE(provider_.IsConnected(webrtc_data_channel_.get()));
57 // The sid is not set yet, so it should not have added the streams.
58 EXPECT_FALSE(provider_.IsSendStreamAdded(webrtc_data_channel_->id()));
59 EXPECT_FALSE(provider_.IsRecvStreamAdded(webrtc_data_channel_->id()));
60
61 webrtc_data_channel_->SetSctpSid(0);
62 EXPECT_TRUE(provider_.IsSendStreamAdded(webrtc_data_channel_->id()));
63 EXPECT_TRUE(provider_.IsRecvStreamAdded(webrtc_data_channel_->id()));
64}
65
66// Verifies that the data channel is connected to the transport if the transport
67// is not available initially and becomes available later.
68TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) {
69 provider_.set_transport_available(false);
70 talk_base::scoped_refptr<DataChannel> dc = DataChannel::Create(
71 &provider_, cricket::DCT_SCTP, "test1", &init_);
72 EXPECT_FALSE(provider_.IsConnected(dc.get()));
73
74 provider_.set_transport_available(true);
75 dc->OnTransportChannelCreated();
76 EXPECT_TRUE(provider_.IsConnected(dc.get()));
77}
78
wu@webrtc.org78187522013-10-07 23:32:02 +000079// Tests the state of the data channel.
80TEST_F(SctpDataChannelTest, StateTransition) {
81 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
82 webrtc_data_channel_->state());
83 SetChannelReady();
84 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state());
85 webrtc_data_channel_->Close();
86 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
87 webrtc_data_channel_->state());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000088 // Verifies that it's disconnected from the transport.
89 EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get()));
wu@webrtc.org78187522013-10-07 23:32:02 +000090}
91
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000092// Tests that DataChannel::buffered_amount() is correct after the channel is
93// blocked.
94TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
wu@webrtc.org78187522013-10-07 23:32:02 +000095 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000096 webrtc::DataBuffer buffer("abcd");
97 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
98
99 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
100
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000101 provider_.set_send_blocked(true);
wu@webrtc.org78187522013-10-07 23:32:02 +0000102
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000103 const int number_of_packets = 3;
104 for (int i = 0; i < number_of_packets; ++i) {
105 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
106 }
107 EXPECT_EQ(buffer.data.length() * number_of_packets,
108 webrtc_data_channel_->buffered_amount());
109}
110
111// Tests that the queued data are sent when the channel transitions from blocked
112// to unblocked.
113TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000114 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000115 webrtc::DataBuffer buffer("abcd");
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000116 provider_.set_send_blocked(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000117 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
118
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000119 provider_.set_send_blocked(false);
wu@webrtc.org78187522013-10-07 23:32:02 +0000120 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000121 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
122}
wu@webrtc.org78187522013-10-07 23:32:02 +0000123
124// Tests that the queued control message is sent when channel is ready.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000125TEST_F(SctpDataChannelTest, OpenMessageSent) {
126 // Initially the id is unassigned.
127 EXPECT_EQ(-1, webrtc_data_channel_->id());
128
wu@webrtc.org78187522013-10-07 23:32:02 +0000129 SetChannelReady();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000130 EXPECT_GE(webrtc_data_channel_->id(), 0);
131 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type);
132 EXPECT_EQ(provider_.last_send_data_params().ssrc,
133 static_cast<uint32>(webrtc_data_channel_->id()));
wu@webrtc.org78187522013-10-07 23:32:02 +0000134}