wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 1 | /* |
| 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.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 29 | #include "talk/app/webrtc/test/fakedatachannelprovider.h" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 30 | #include "talk/base/gunit.h" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 31 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 32 | using webrtc::DataChannel; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 33 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 34 | class SctpDataChannelTest : public testing::Test { |
| 35 | protected: |
| 36 | SctpDataChannelTest() |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 37 | : webrtc_data_channel_( |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 38 | DataChannel::Create(&provider_, cricket::DCT_SCTP, "test", &init_)) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 39 | } |
| 40 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 41 | void SetChannelReady() { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 42 | provider_.set_transport_available(true); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 43 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 44 | if (webrtc_data_channel_->id() < 0) { |
| 45 | webrtc_data_channel_->SetSctpSid(0); |
| 46 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 47 | provider_.set_ready_to_send(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 48 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 49 | |
| 50 | webrtc::DataChannelInit init_; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 51 | FakeDataChannelProvider provider_; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 52 | talk_base::scoped_refptr<DataChannel> webrtc_data_channel_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 55 | // Verifies that the data channel is connected to the transport after creation. |
| 56 | TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 57 | provider_.set_transport_available(true); |
| 58 | talk_base::scoped_refptr<DataChannel> dc = DataChannel::Create( |
| 59 | &provider_, cricket::DCT_SCTP, "test1", &init_); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 60 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 61 | 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.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Verifies that the data channel is connected to the transport if the transport |
| 72 | // is not available initially and becomes available later. |
| 73 | TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 74 | EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 75 | |
| 76 | provider_.set_transport_available(true); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 77 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 78 | EXPECT_TRUE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 79 | } |
| 80 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 81 | // Tests the state of the data channel. |
| 82 | TEST_F(SctpDataChannelTest, StateTransition) { |
| 83 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 84 | webrtc_data_channel_->state()); |
| 85 | SetChannelReady(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 86 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 87 | 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.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 91 | // Verifies that it's disconnected from the transport. |
| 92 | EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 93 | } |
| 94 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 95 | // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 96 | // blocked. |
| 97 | TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 98 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 99 | 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.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 104 | provider_.set_send_blocked(true); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 105 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 106 | 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. |
| 116 | TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 117 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 118 | webrtc::DataBuffer buffer("abcd"); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 119 | provider_.set_send_blocked(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 120 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 121 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 122 | provider_.set_send_blocked(false); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 123 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 124 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 125 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 126 | |
| 127 | // Tests that the queued control message is sent when channel is ready. |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 128 | TEST_F(SctpDataChannelTest, OpenMessageSent) { |
| 129 | // Initially the id is unassigned. |
| 130 | EXPECT_EQ(-1, webrtc_data_channel_->id()); |
| 131 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 132 | SetChannelReady(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 133 | 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.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 137 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 138 | |
| 139 | // Tests that the DataChannel created after transport gets ready can enter OPEN |
| 140 | // state. |
| 141 | TEST_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 | } |