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" |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame^] | 31 | #include "testing/base/public/gmock.h" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 32 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 33 | using webrtc::DataChannel; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 34 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame^] | 35 | class FakeDataChannelObserver : public webrtc::DataChannelObserver { |
| 36 | public: |
| 37 | MOCK_METHOD0(OnStateChange, void()); |
| 38 | MOCK_METHOD1(OnMessage, void(const webrtc::DataBuffer& buffer)); |
| 39 | }; |
| 40 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 41 | class SctpDataChannelTest : public testing::Test { |
| 42 | protected: |
| 43 | SctpDataChannelTest() |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 44 | : webrtc_data_channel_( |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 45 | DataChannel::Create(&provider_, cricket::DCT_SCTP, "test", &init_)) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 46 | } |
| 47 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 48 | void SetChannelReady() { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 49 | provider_.set_transport_available(true); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 50 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 51 | if (webrtc_data_channel_->id() < 0) { |
| 52 | webrtc_data_channel_->SetSctpSid(0); |
| 53 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 54 | provider_.set_ready_to_send(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 55 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 56 | |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame^] | 57 | void AddObserver() { |
| 58 | observer_.reset(new FakeDataChannelObserver()); |
| 59 | webrtc_data_channel_->RegisterObserver(observer_.get()); |
| 60 | } |
| 61 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 62 | webrtc::DataChannelInit init_; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 63 | FakeDataChannelProvider provider_; |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame^] | 64 | talk_base::scoped_ptr<FakeDataChannelObserver> observer_; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 65 | talk_base::scoped_refptr<DataChannel> webrtc_data_channel_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 68 | // Verifies that the data channel is connected to the transport after creation. |
| 69 | TEST_F(SctpDataChannelTest, ConnectedToTransportOnCreated) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 70 | provider_.set_transport_available(true); |
| 71 | talk_base::scoped_refptr<DataChannel> dc = DataChannel::Create( |
| 72 | &provider_, cricket::DCT_SCTP, "test1", &init_); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 73 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 74 | EXPECT_TRUE(provider_.IsConnected(dc.get())); |
| 75 | // The sid is not set yet, so it should not have added the streams. |
| 76 | EXPECT_FALSE(provider_.IsSendStreamAdded(dc->id())); |
| 77 | EXPECT_FALSE(provider_.IsRecvStreamAdded(dc->id())); |
| 78 | |
| 79 | dc->SetSctpSid(0); |
| 80 | EXPECT_TRUE(provider_.IsSendStreamAdded(dc->id())); |
| 81 | EXPECT_TRUE(provider_.IsRecvStreamAdded(dc->id())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Verifies that the data channel is connected to the transport if the transport |
| 85 | // is not available initially and becomes available later. |
| 86 | TEST_F(SctpDataChannelTest, ConnectedAfterTransportBecomesAvailable) { |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 87 | EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 88 | |
| 89 | provider_.set_transport_available(true); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 90 | webrtc_data_channel_->OnTransportChannelCreated(); |
| 91 | EXPECT_TRUE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 92 | } |
| 93 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 94 | // Tests the state of the data channel. |
| 95 | TEST_F(SctpDataChannelTest, StateTransition) { |
| 96 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, |
| 97 | webrtc_data_channel_->state()); |
| 98 | SetChannelReady(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 99 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 100 | EXPECT_EQ(webrtc::DataChannelInterface::kOpen, webrtc_data_channel_->state()); |
| 101 | webrtc_data_channel_->Close(); |
| 102 | EXPECT_EQ(webrtc::DataChannelInterface::kClosed, |
| 103 | webrtc_data_channel_->state()); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 104 | // Verifies that it's disconnected from the transport. |
| 105 | EXPECT_FALSE(provider_.IsConnected(webrtc_data_channel_.get())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 106 | } |
| 107 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 108 | // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 109 | // blocked. |
| 110 | TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 111 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 112 | webrtc::DataBuffer buffer("abcd"); |
| 113 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 114 | |
| 115 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 116 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 117 | provider_.set_send_blocked(true); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 118 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 119 | const int number_of_packets = 3; |
| 120 | for (int i = 0; i < number_of_packets; ++i) { |
| 121 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 122 | } |
| 123 | EXPECT_EQ(buffer.data.length() * number_of_packets, |
| 124 | webrtc_data_channel_->buffered_amount()); |
| 125 | } |
| 126 | |
| 127 | // Tests that the queued data are sent when the channel transitions from blocked |
| 128 | // to unblocked. |
| 129 | TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 130 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 131 | webrtc::DataBuffer buffer("abcd"); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 132 | provider_.set_send_blocked(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 133 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 134 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 135 | provider_.set_send_blocked(false); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 136 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 137 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 138 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 139 | |
| 140 | // 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] | 141 | TEST_F(SctpDataChannelTest, OpenMessageSent) { |
| 142 | // Initially the id is unassigned. |
| 143 | EXPECT_EQ(-1, webrtc_data_channel_->id()); |
| 144 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 145 | SetChannelReady(); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 146 | EXPECT_GE(webrtc_data_channel_->id(), 0); |
| 147 | EXPECT_EQ(cricket::DMT_CONTROL, provider_.last_send_data_params().type); |
| 148 | EXPECT_EQ(provider_.last_send_data_params().ssrc, |
| 149 | static_cast<uint32>(webrtc_data_channel_->id())); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 150 | } |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 151 | |
| 152 | // Tests that the DataChannel created after transport gets ready can enter OPEN |
| 153 | // state. |
| 154 | TEST_F(SctpDataChannelTest, LateCreatedChannelTransitionToOpen) { |
| 155 | SetChannelReady(); |
| 156 | webrtc::DataChannelInit init; |
| 157 | init.id = 1; |
| 158 | talk_base::scoped_refptr<DataChannel> dc = |
| 159 | DataChannel::Create(&provider_, cricket::DCT_SCTP, "test1", &init); |
| 160 | EXPECT_EQ(webrtc::DataChannelInterface::kConnecting, dc->state()); |
| 161 | EXPECT_TRUE_WAIT(webrtc::DataChannelInterface::kOpen == dc->state(), |
| 162 | 1000); |
| 163 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame^] | 164 | |
| 165 | // Tests that messages are sent with the right ssrc. |
| 166 | TEST_F(SctpDataChannelTest, SendDataSsrc) { |
| 167 | webrtc_data_channel_->SetSctpSid(1); |
| 168 | SetChannelReady(); |
| 169 | webrtc::DataBuffer buffer("data"); |
| 170 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 171 | EXPECT_EQ(1U, provider_.last_send_data_params().ssrc); |
| 172 | } |
| 173 | |
| 174 | // Tests that the incoming messages with wrong ssrcs are rejected. |
| 175 | TEST_F(SctpDataChannelTest, ReceiveDataWithInvalidSsrc) { |
| 176 | webrtc_data_channel_->SetSctpSid(1); |
| 177 | SetChannelReady(); |
| 178 | |
| 179 | AddObserver(); |
| 180 | EXPECT_CALL(*(observer_.get()), OnMessage(testing::_)).Times(0); |
| 181 | |
| 182 | cricket::ReceiveDataParams params; |
| 183 | params.ssrc = 0; |
| 184 | webrtc::DataBuffer buffer("abcd"); |
| 185 | webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data); |
| 186 | } |
| 187 | |
| 188 | // Tests that the incoming messages with right ssrcs are acceted. |
| 189 | TEST_F(SctpDataChannelTest, ReceiveDataWithValidSsrc) { |
| 190 | webrtc_data_channel_->SetSctpSid(1); |
| 191 | SetChannelReady(); |
| 192 | |
| 193 | AddObserver(); |
| 194 | EXPECT_CALL(*(observer_.get()), OnMessage(testing::_)).Times(1); |
| 195 | |
| 196 | cricket::ReceiveDataParams params; |
| 197 | params.ssrc = 1; |
| 198 | webrtc::DataBuffer buffer("abcd"); |
| 199 | |
| 200 | webrtc_data_channel_->OnDataReceived(NULL, params, buffer.data); |
| 201 | } |