blob: 69060b751d85cbd7f0cc8d4a8de6e7a82581cbc7 [file] [log] [blame]
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001/*
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.org91053e72013-08-10 07:18:04 +000029#include "talk/app/webrtc/jsep.h"
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000030#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
wu@webrtc.org91053e72013-08-10 07:18:04 +000038using webrtc::CreateSessionDescriptionObserver;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000039using webrtc::MediaConstraintsInterface;
wu@webrtc.org91053e72013-08-10 07:18:04 +000040using webrtc::SessionDescriptionInterface;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000041
42const uint32 kFakeSsrc = 1;
43
wu@webrtc.org91053e72013-08-10 07:18:04 +000044class CreateSessionDescriptionObserverForTest
45 : public talk_base::RefCountedObject<CreateSessionDescriptionObserver> {
46 public:
wu@webrtc.org91053e72013-08-10 07:18:04 +000047 virtual void OnSuccess(SessionDescriptionInterface* desc) {
mallinath@webrtc.orga5506692013-08-12 21:18:15 +000048 description_.reset(desc);
wu@webrtc.org91053e72013-08-10 07:18:04 +000049 }
50 virtual void OnFailure(const std::string& error) {}
51
mallinath@webrtc.orga5506692013-08-12 21:18:15 +000052 SessionDescriptionInterface* description() { return description_.get(); }
53
54 SessionDescriptionInterface* ReleaseDescription() {
55 return description_.release();
56 }
wu@webrtc.org91053e72013-08-10 07:18:04 +000057
58 protected:
59 ~CreateSessionDescriptionObserverForTest() {}
60
61 private:
mallinath@webrtc.orga5506692013-08-12 21:18:15 +000062 talk_base::scoped_ptr<SessionDescriptionInterface> description_;
wu@webrtc.org91053e72013-08-10 07:18:04 +000063};
64
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000065class 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())),
wu@webrtc.org91053e72013-08-10 07:18:04 +000076 media_stream_signaling_(
77 new webrtc::MediaStreamSignaling(talk_base::Thread::Current(),
78 NULL)),
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000079 session_(channel_manager_.get(),
80 talk_base::Thread::Current(),
81 talk_base::Thread::Current(),
82 NULL,
wu@webrtc.org91053e72013-08-10 07:18:04 +000083 media_stream_signaling_.get()),
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000084 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);
wu@webrtc.org91053e72013-08-10 07:18:04 +000095 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);
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000101 ASSERT_TRUE(session_.SetLocalDescription(observer->ReleaseDescription(),
102 NULL));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000103
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_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000123 talk_base::scoped_ptr<webrtc::MediaStreamSignaling> media_stream_signaling_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000124 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.
130TEST_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.
150TEST_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}