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" 29 #include "talk/app/webrtc/jsep.h" 30 #include "talk/app/webrtc/mediastreamsignaling.h" 31 #include "talk/app/webrtc/test/fakeconstraints.h" 32 #include "talk/app/webrtc/webrtcsession.h" 33 #include "talk/base/gunit.h" 34 #include "talk/media/base/fakemediaengine.h" 35 #include "talk/media/devices/fakedevicemanager.h" 36 #include "talk/session/media/channelmanager.h" 37 38 using webrtc::CreateSessionDescriptionObserver; 39 using webrtc::MediaConstraintsInterface; 40 using webrtc::SessionDescriptionInterface; 41 42 const uint32 kFakeSsrc = 1; 43 44 class CreateSessionDescriptionObserverForTest 45 : public talk_base::RefCountedObject<CreateSessionDescriptionObserver> { 46 public: 47 virtual void OnSuccess(SessionDescriptionInterface* desc) { 48 description_.reset(desc); 49 } 50 virtual void OnFailure(const std::string& error) {} 51 52 SessionDescriptionInterface* description() { return description_.get(); } 53 54 SessionDescriptionInterface* ReleaseDescription() { 55 return description_.release(); 56 } 57 58 protected: 59 ~CreateSessionDescriptionObserverForTest() {} 60 61 private: 62 talk_base::scoped_ptr<SessionDescriptionInterface> description_; 63 }; 64 65 class SctpDataChannelTest : public testing::Test { 66 protected: 67 SctpDataChannelTest() 68 : media_engine_(new cricket::FakeMediaEngine), 69 data_engine_(new cricket::FakeDataEngine), 70 channel_manager_( 71 new cricket::ChannelManager(media_engine_, 72 data_engine_, 73 new cricket::FakeDeviceManager(), 74 new cricket::CaptureManager(), 75 talk_base::Thread::Current())), 76 media_stream_signaling_( 77 new webrtc::MediaStreamSignaling(talk_base::Thread::Current(), 78 NULL)), 79 session_(channel_manager_.get(), 80 talk_base::Thread::Current(), 81 talk_base::Thread::Current(), 82 NULL, 83 media_stream_signaling_.get()), 84 webrtc_data_channel_(NULL) {} 85 86 virtual void SetUp() { 87 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) { 88 return; 89 } 90 channel_manager_->Init(); 91 webrtc::FakeConstraints constraints; 92 constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, true); 93 constraints.AddMandatory(MediaConstraintsInterface::kEnableSctpDataChannels, 94 true); 95 ASSERT_TRUE(session_.Initialize(&constraints, NULL)); 96 talk_base::scoped_refptr<CreateSessionDescriptionObserverForTest> observer 97 = new CreateSessionDescriptionObserverForTest(); 98 session_.CreateOffer(observer.get(), NULL); 99 EXPECT_TRUE_WAIT(observer->description() != NULL, 1000); 100 ASSERT_TRUE(observer->description() != NULL); 101 ASSERT_TRUE(session_.SetLocalDescription(observer->ReleaseDescription(), 102 NULL)); 103 104 webrtc_data_channel_ = webrtc::DataChannel::Create(&session_, "test", NULL); 105 // Connect to the media channel. 106 webrtc_data_channel_->SetSendSsrc(kFakeSsrc); 107 webrtc_data_channel_->SetReceiveSsrc(kFakeSsrc); 108 109 session_.data_channel()->SignalReadyToSendData(true); 110 } 111 112 void SetSendBlocked(bool blocked) { 113 bool was_blocked = data_engine_->GetChannel(0)->is_send_blocked(); 114 data_engine_->GetChannel(0)->set_send_blocked(blocked); 115 if (!blocked && was_blocked) { 116 session_.data_channel()->SignalReadyToSendData(true); 117 } 118 } 119 120 cricket::FakeMediaEngine* media_engine_; 121 cricket::FakeDataEngine* data_engine_; 122 talk_base::scoped_ptr<cricket::ChannelManager> channel_manager_; 123 talk_base::scoped_ptr<webrtc::MediaStreamSignaling> media_stream_signaling_; 124 webrtc::WebRtcSession session_; 125 talk_base::scoped_refptr<webrtc::DataChannel> webrtc_data_channel_; 126 }; 127 128 // Tests that DataChannel::buffered_amount() is correct after the channel is 129 // blocked. 130 TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) { 131 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) { 132 return; 133 } 134 webrtc::DataBuffer buffer("abcd"); 135 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); 136 137 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); 138 139 SetSendBlocked(true); 140 const int number_of_packets = 3; 141 for (int i = 0; i < number_of_packets; ++i) { 142 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); 143 } 144 EXPECT_EQ(buffer.data.length() * number_of_packets, 145 webrtc_data_channel_->buffered_amount()); 146 } 147 148 // Tests that the queued data are sent when the channel transitions from blocked 149 // to unblocked. 150 TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) { 151 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) { 152 return; 153 } 154 webrtc::DataBuffer buffer("abcd"); 155 SetSendBlocked(true); 156 EXPECT_TRUE(webrtc_data_channel_->Send(buffer)); 157 158 SetSendBlocked(false); 159 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount()); 160 } 161