blob: a2f898ed9449fd940b33c233de9de980494a5fe6 [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"
29#include "talk/app/webrtc/mediastreamsignaling.h"
30#include "talk/app/webrtc/test/fakeconstraints.h"
31#include "talk/app/webrtc/webrtcsession.h"
32#include "talk/base/gunit.h"
33#include "talk/media/base/fakemediaengine.h"
34#include "talk/media/devices/fakedevicemanager.h"
35#include "talk/session/media/channelmanager.h"
36
37using webrtc::MediaConstraintsInterface;
38
39const uint32 kFakeSsrc = 1;
40
41class SctpDataChannelTest : public testing::Test {
42 protected:
43 SctpDataChannelTest()
44 : media_engine_(new cricket::FakeMediaEngine),
45 data_engine_(new cricket::FakeDataEngine),
46 channel_manager_(
47 new cricket::ChannelManager(media_engine_,
48 data_engine_,
49 new cricket::FakeDeviceManager(),
50 new cricket::CaptureManager(),
51 talk_base::Thread::Current())),
wu@webrtc.orga0545692013-08-01 22:08:14 +000052 ms_signaling_(new webrtc::MediaStreamSignaling(
53 talk_base::Thread::Current(), NULL)),
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000054 session_(channel_manager_.get(),
55 talk_base::Thread::Current(),
56 talk_base::Thread::Current(),
57 NULL,
wu@webrtc.orga0545692013-08-01 22:08:14 +000058 ms_signaling_.get()),
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000059 webrtc_data_channel_(NULL) {}
60
61 virtual void SetUp() {
62 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) {
63 return;
64 }
65 channel_manager_->Init();
66 webrtc::FakeConstraints constraints;
67 constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, true);
68 constraints.AddMandatory(MediaConstraintsInterface::kEnableSctpDataChannels,
69 true);
70 ASSERT_TRUE(session_.Initialize(&constraints));
71 webrtc::SessionDescriptionInterface* offer = session_.CreateOffer(NULL);
72 ASSERT_TRUE(offer != NULL);
73 ASSERT_TRUE(session_.SetLocalDescription(offer, NULL));
74
75 webrtc_data_channel_ = webrtc::DataChannel::Create(&session_, "test", NULL);
76 // Connect to the media channel.
77 webrtc_data_channel_->SetSendSsrc(kFakeSsrc);
78 webrtc_data_channel_->SetReceiveSsrc(kFakeSsrc);
79
80 session_.data_channel()->SignalReadyToSendData(true);
81 }
82
83 void SetSendBlocked(bool blocked) {
84 bool was_blocked = data_engine_->GetChannel(0)->is_send_blocked();
85 data_engine_->GetChannel(0)->set_send_blocked(blocked);
86 if (!blocked && was_blocked) {
87 session_.data_channel()->SignalReadyToSendData(true);
88 }
89 }
90
91 cricket::FakeMediaEngine* media_engine_;
92 cricket::FakeDataEngine* data_engine_;
93 talk_base::scoped_ptr<cricket::ChannelManager> channel_manager_;
wu@webrtc.orga0545692013-08-01 22:08:14 +000094 talk_base::scoped_ptr<webrtc::MediaStreamSignaling> ms_signaling_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000095 webrtc::WebRtcSession session_;
96 talk_base::scoped_refptr<webrtc::DataChannel> webrtc_data_channel_;
97};
98
99// Tests that DataChannel::buffered_amount() is correct after the channel is
100// blocked.
101TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
102 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) {
103 return;
104 }
105 webrtc::DataBuffer buffer("abcd");
106 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
107
108 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
109
110 SetSendBlocked(true);
111 const int number_of_packets = 3;
112 for (int i = 0; i < number_of_packets; ++i) {
113 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
114 }
115 EXPECT_EQ(buffer.data.length() * number_of_packets,
116 webrtc_data_channel_->buffered_amount());
117}
118
119// Tests that the queued data are sent when the channel transitions from blocked
120// to unblocked.
121TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
122 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) {
123 return;
124 }
125 webrtc::DataBuffer buffer("abcd");
126 SetSendBlocked(true);
127 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
128
129 SetSendBlocked(false);
130 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
131}