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 | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 29 | #include "talk/base/gunit.h" |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 30 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 31 | using webrtc::DataChannel; |
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 | class FakeDataChannelClient : public webrtc::DataChannelProviderInterface { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 34 | public: |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 35 | 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.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 47 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 48 | virtual bool ConnectDataChannel(DataChannel* data_channel) OVERRIDE { |
| 49 | return true; |
mallinath@webrtc.org | a550669 | 2013-08-12 21:18:15 +0000 | [diff] [blame] | 50 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 51 | virtual void DisconnectDataChannel(DataChannel* data_channel) OVERRIDE {} |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 52 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 53 | 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.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 57 | |
| 58 | private: |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 59 | cricket::SendDataParams last_send_data_params_; |
| 60 | bool send_blocked_; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 63 | class SctpDataChannelTest : public testing::Test { |
| 64 | protected: |
| 65 | SctpDataChannelTest() |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 66 | : webrtc_data_channel_( |
| 67 | DataChannel::Create(&client_, cricket::DCT_SCTP, "test", &init_)) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 68 | } |
| 69 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 70 | void SetChannelReady() { |
| 71 | webrtc_data_channel_->OnChannelReady(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 72 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 73 | |
| 74 | webrtc::DataChannelInit init_; |
| 75 | FakeDataChannelClient client_; |
| 76 | talk_base::scoped_refptr<DataChannel> webrtc_data_channel_; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 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()); |
| 88 | } |
| 89 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 90 | // Tests that DataChannel::buffered_amount() is correct after the channel is |
| 91 | // blocked. |
| 92 | TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 93 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 94 | 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.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 99 | client_.set_send_blocked(true); |
| 100 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 101 | 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. |
| 111 | TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 112 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 113 | webrtc::DataBuffer buffer("abcd"); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 114 | client_.set_send_blocked(true); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 115 | EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); |
| 116 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 117 | client_.set_send_blocked(false); |
| 118 | SetChannelReady(); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 119 | EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); |
| 120 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 121 | |
| 122 | // Tests that the queued control message is sent when channel is ready. |
| 123 | TEST_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 | } |