blob: dba24a2dbbba62c92becd867d5a31e47844de049 [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.org07a6fbe2013-11-04 18:41:34 +000042 provider_.set_transport_available(true);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000043 webrtc_data_channel_->OnTransportChannelCreated();
44 if (webrtc_data_channel_->id() < 0) {
45 webrtc_data_channel_->SetSctpSid(0);
46 }
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000047 provider_.set_ready_to_send(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000048 }
wu@webrtc.org78187522013-10-07 23:32:02 +000049
50 webrtc::DataChannelInit init_;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000051 FakeDataChannelProvider provider_;
wu@webrtc.org78187522013-10-07 23:32:02 +000052 talk_base::scoped_refptr<DataChannel> webrtc_data_channel_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000053};
54
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000055// Verifies that the data channel is connected to the transport after creation.
56TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000057 provider_.set_transport_available(true);
58 talk_base::scoped_refptr<DataChannel> dc = DataChannel::Create(
59 &provider_, cricket::DCT_SCTP, "test1", &init_);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000060
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000061 EXPECT_TRUE(provider_.IsConnected(dc.get()));
62 // The sid is not set yet, so it should not have added the streams.
63 EXPECT_FALSE(provider_.IsSendStreamAdded(dc->id()));
64 EXPECT_FALSE(provider_.IsRecvStreamAdded(dc->id()));
65
66 dc->SetSctpSid(0);
67 EXPECT_TRUE(provider_.IsSendStreamAdded(dc->id()));
68 EXPECT_TRUE(provider_.IsRecvStreamAdded(dc->id()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000069}
70
71// Verifies that the data channel is connected to the transport if the transport
72// is not available initially and becomes available later.
73TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) {
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000074 EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000075
76 provider_.set_transport_available(true);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000077 webrtc_data_channel_->OnTransportChannelCreated();
78 EXPECT_TRUE(provider_.IsConnected(webrtc_data_channel_.get()));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000079}
80
wu@webrtc.org78187522013-10-07 23:32:02 +000081// Tests the state of the data channel.
82TEST_F(SctpDataChannelTest, StateTransition) {
83 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting,
84 webrtc_data_channel_->state());
85 SetChannelReady();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000086
wu@webrtc.org78187522013-10-07 23:32:02 +000087 EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state());
88 webrtc_data_channel_->Close();
89 EXPECT_EQ(webrtc::DataChannelInterface::kClosed,
90 webrtc_data_channel_->state());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000091 // Verifies that it's disconnected from the transport.
92 EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get()));
wu@webrtc.org78187522013-10-07 23:32:02 +000093}
94
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000095// Tests that DataChannel::buffered_amount() is correct after the channel is
96// blocked.
97TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
wu@webrtc.org78187522013-10-07 23:32:02 +000098 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000099 webrtc::DataBuffer buffer("abcd");
100 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
101
102 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
103
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000104 provider_.set_send_blocked(true);
wu@webrtc.org78187522013-10-07 23:32:02 +0000105
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000106 const int number_of_packets = 3;
107 for (int i = 0; i < number_of_packets; ++i) {
108 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
109 }
110 EXPECT_EQ(buffer.data.length() * number_of_packets,
111 webrtc_data_channel_->buffered_amount());
112}
113
114// Tests that the queued data are sent when the channel transitions from blocked
115// to unblocked.
116TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000117 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000118 webrtc::DataBuffer buffer("abcd");
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000119 provider_.set_send_blocked(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000120 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
121
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000122 provider_.set_send_blocked(false);
wu@webrtc.org78187522013-10-07 23:32:02 +0000123 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000124 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
125}
wu@webrtc.org78187522013-10-07 23:32:02 +0000126
127// Tests that the queued control message is sent when channel is ready.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000128TEST_F(SctpDataChannelTest, OpenMessageSent) {
129 // Initially the id is unassigned.
130 EXPECT_EQ(-1, webrtc_data_channel_->id());
131
wu@webrtc.org78187522013-10-07 23:32:02 +0000132 SetChannelReady();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000133 EXPECT_GE(webrtc_data_channel_->id(), 0);
134 EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type);
135 EXPECT_EQ(provider_.last_send_data_params().ssrc,
136 static_cast<uint32>(webrtc_data_channel_->id()));
wu@webrtc.org78187522013-10-07 23:32:02 +0000137}
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000138
139// Tests that the DataChannel created after transport gets ready can enter OPEN
140// state.
141TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) {
142 SetChannelReady();
143 webrtc::DataChannelInit init;
144 init.id = 1;
145 talk_base::scoped_refptr<DataChannel> dc =
146 DataChannel::Create(&provider_, cricket::DCT_SCTP, "test1", &init);
147 EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state());
148 EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(),
149 1000);
150}