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 | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 42 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 43 | if (webrtc_data_channel_->id() < 0) { |
| 44 | webrtc_data_channel_->SetSctpSid(0); |
| 45 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 46 | webrtc_data_channel_->OnChannelReady(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 47 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 48 | |
| 49 | webrtc::DataChannelInit init_; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 50 | FakeDataChannelProvider provider_; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 51 | talk_base::scoped_refptr<DataChannel> webrtc_data_channel_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 54 | // Verifies that the data channel is connected to the transport after creation. |
| 55 | TEST_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. |
| 68 | TEST_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.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 79 | // Tests the state of the data channel. |
| 80 | TEST_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.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 88 | // Verifies that it's disconnected from the transport. |
| 89 | EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 90 | } |
| 91 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 92 | // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 93 | // blocked. |
| 94 | TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 95 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 96 | 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.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 101 | provider_.set_send_blocked(true); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 102 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 103 | 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. |
| 113 | TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 114 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 115 | webrtc::DataBuffer buffer("abcd"); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 116 | provider_.set_send_blocked(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 117 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 118 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 119 | provider_.set_send_blocked(false); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 120 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 121 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 122 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 123 | |
| 124 | // 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] | 125 | TEST_F(SctpDataChannelTest, OpenMessageSent) { |
| 126 | // Initially the id is unassigned. |
| 127 | EXPECT_EQ(-1, webrtc_data_channel_->id()); |
| 128 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 129 | SetChannelReady(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 130 | 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.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 134 | } |