blob: 2b2609753a8032c11d9431628b2ce4151e1dde31 [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:
47 CreateSessionDescriptionObserverForTest() : description_(NULL) {}
48
49 virtual void OnSuccess(SessionDescriptionInterface* desc) {
50 description_ = desc;
51 }
52 virtual void OnFailure(const std::string& error) {}
53
54 SessionDescriptionInterface* description() { return description_; }
55
56 protected:
57 ~CreateSessionDescriptionObserverForTest() {}
58
59 private:
60 SessionDescriptionInterface* description_;
61};
62
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000063class SctpDataChannelTest : public testing::Test {
64 protected:
65 SctpDataChannelTest()
66 : media_engine_(new cricket::FakeMediaEngine),
67 data_engine_(new cricket::FakeDataEngine),
68 channel_manager_(
69 new cricket::ChannelManager(media_engine_,
70 data_engine_,
71 new cricket::FakeDeviceManager(),
72 new cricket::CaptureManager(),
73 talk_base::Thread::Current())),
wu@webrtc.org91053e72013-08-10 07:18:04 +000074 media_stream_signaling_(
75 new webrtc::MediaStreamSignaling(talk_base::Thread::Current(),
76 NULL)),
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000077 session_(channel_manager_.get(),
78 talk_base::Thread::Current(),
79 talk_base::Thread::Current(),
80 NULL,
wu@webrtc.org91053e72013-08-10 07:18:04 +000081 media_stream_signaling_.get()),
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000082 webrtc_data_channel_(NULL) {}
83
84 virtual void SetUp() {
85 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) {
86 return;
87 }
88 channel_manager_->Init();
89 webrtc::FakeConstraints constraints;
90 constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, true);
91 constraints.AddMandatory(MediaConstraintsInterface::kEnableSctpDataChannels,
92 true);
wu@webrtc.org91053e72013-08-10 07:18:04 +000093 ASSERT_TRUE(session_.Initialize(&constraints, NULL));
94 talk_base::scoped_refptr<CreateSessionDescriptionObserverForTest> observer
95 = new CreateSessionDescriptionObserverForTest();
96 session_.CreateOffer(observer.get(), NULL);
97 EXPECT_TRUE_WAIT(observer->description() != NULL, 1000);
98 ASSERT_TRUE(observer->description() != NULL);
99 ASSERT_TRUE(session_.SetLocalDescription(observer->description(), NULL));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000100
101 webrtc_data_channel_ = webrtc::DataChannel::Create(&session_, "test", NULL);
102 // Connect to the media channel.
103 webrtc_data_channel_->SetSendSsrc(kFakeSsrc);
104 webrtc_data_channel_->SetReceiveSsrc(kFakeSsrc);
105
106 session_.data_channel()->SignalReadyToSendData(true);
107 }
108
109 void SetSendBlocked(bool blocked) {
110 bool was_blocked = data_engine_->GetChannel(0)->is_send_blocked();
111 data_engine_->GetChannel(0)->set_send_blocked(blocked);
112 if (!blocked && was_blocked) {
113 session_.data_channel()->SignalReadyToSendData(true);
114 }
115 }
116
117 cricket::FakeMediaEngine* media_engine_;
118 cricket::FakeDataEngine* data_engine_;
119 talk_base::scoped_ptr<cricket::ChannelManager> channel_manager_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000120 talk_base::scoped_ptr<webrtc::MediaStreamSignaling> media_stream_signaling_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000121 webrtc::WebRtcSession session_;
122 talk_base::scoped_refptr<webrtc::DataChannel> webrtc_data_channel_;
123};
124
125// Tests that DataChannel::buffered_amount() is correct after the channel is
126// blocked.
127TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
128 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) {
129 return;
130 }
131 webrtc::DataBuffer buffer("abcd");
132 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
133
134 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
135
136 SetSendBlocked(true);
137 const int number_of_packets = 3;
138 for (int i = 0; i < number_of_packets; ++i) {
139 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
140 }
141 EXPECT_EQ(buffer.data.length() * number_of_packets,
142 webrtc_data_channel_->buffered_amount());
143}
144
145// Tests that the queued data are sent when the channel transitions from blocked
146// to unblocked.
147TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
148 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) {
149 return;
150 }
151 webrtc::DataBuffer buffer("abcd");
152 SetSendBlocked(true);
153 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
154
155 SetSendBlocked(false);
156 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
157}