blob: 4d669078db3edeb5288095a800899b4a5dadc9f6 [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.orgd64719d2013-08-01 00:00:07 +000029#include "talk/base/gunit.h"
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000030
wu@webrtc.org78187522013-10-07 23:32:02 +000031using webrtc::DataChannel;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000032
wu@webrtc.org78187522013-10-07 23:32:02 +000033class FakeDataChannelClient : public webrtc::DataChannelProviderInterface {
wu@webrtc.org91053e72013-08-10 07:18:04 +000034 public:
wu@webrtc.org78187522013-10-07 23:32:02 +000035 FakeDataChannelClient() : send_blocked_(false) {}
36 virtual ~FakeDataChannelClient() {}
37
38 virtual bool SendData(const cricket::SendDataParams& params,
39 const talk_base::Buffer& payload,
40 cricket::SendDataResult* result) OVERRIDE {
41 if (send_blocked_) {
42 *result = cricket::SDR_BLOCK;
43 return false;
44 }
45 last_send_data_params_ = params;
46 return true;
wu@webrtc.org91053e72013-08-10 07:18:04 +000047 }
wu@webrtc.org78187522013-10-07 23:32:02 +000048 virtual bool ConnectDataChannel(DataChannel* data_channel) OVERRIDE {
49 return true;
mallinath@webrtc.orga5506692013-08-12 21:18:15 +000050 }
wu@webrtc.org78187522013-10-07 23:32:02 +000051 virtual void DisconnectDataChannel(DataChannel* data_channel) OVERRIDE {}
wu@webrtc.org91053e72013-08-10 07:18:04 +000052
wu@webrtc.org78187522013-10-07 23:32:02 +000053 void set_send_blocked(bool blocked) { send_blocked_ = blocked; }
54 cricket::SendDataParams last_send_data_params() {
55 return last_send_data_params_;
56 }
wu@webrtc.org91053e72013-08-10 07:18:04 +000057
58 private:
wu@webrtc.org78187522013-10-07 23:32:02 +000059 cricket::SendDataParams last_send_data_params_;
60 bool send_blocked_;
wu@webrtc.org91053e72013-08-10 07:18:04 +000061};
62
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000063class SctpDataChannelTest : public testing::Test {
64 protected:
65 SctpDataChannelTest()
wu@webrtc.org78187522013-10-07 23:32:02 +000066 : webrtc_data_channel_(
67 DataChannel::Create(&client_, cricket::DCT_SCTP, "test", &init_)) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000068 }
69
wu@webrtc.org78187522013-10-07 23:32:02 +000070 void SetChannelReady() {
71 webrtc_data_channel_->OnChannelReady(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000072 }
wu@webrtc.org78187522013-10-07 23:32:02 +000073
74 webrtc::DataChannelInit init_;
75 FakeDataChannelClient client_;
76 talk_base::scoped_refptr<DataChannel> webrtc_data_channel_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000077};
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());
88}
89
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000090// Tests that DataChannel::buffered_amount() is correct after the channel is
91// blocked.
92TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
wu@webrtc.org78187522013-10-07 23:32:02 +000093 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000094 webrtc::DataBuffer buffer("abcd");
95 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
96
97 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
98
wu@webrtc.org78187522013-10-07 23:32:02 +000099 client_.set_send_blocked(true);
100
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000101 const int number_of_packets = 3;
102 for (int i = 0; i < number_of_packets; ++i) {
103 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
104 }
105 EXPECT_EQ(buffer.data.length() * number_of_packets,
106 webrtc_data_channel_->buffered_amount());
107}
108
109// Tests that the queued data are sent when the channel transitions from blocked
110// to unblocked.
111TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000112 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000113 webrtc::DataBuffer buffer("abcd");
wu@webrtc.org78187522013-10-07 23:32:02 +0000114 client_.set_send_blocked(true);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000115 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
116
wu@webrtc.org78187522013-10-07 23:32:02 +0000117 client_.set_send_blocked(false);
118 SetChannelReady();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000119 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
120}
wu@webrtc.org78187522013-10-07 23:32:02 +0000121
122// Tests that the queued control message is sent when channel is ready.
123TEST_F(SctpDataChannelTest, QueuedControlMessageSent) {
124 talk_base::Buffer* buffer = new talk_base::Buffer("abcd", 4);
125 EXPECT_TRUE(webrtc_data_channel_->SendControl(buffer));
126 SetChannelReady();
127 EXPECT_EQ(cricket::DMT_CONTROL, client_.last_send_data_params().type);
128}