blob: d3faf177aaf3d472817cb8e966d67b5395b74b0f [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())),
52 session_(channel_manager_.get(),
53 talk_base::Thread::Current(),
54 talk_base::Thread::Current(),
55 NULL,
56 new webrtc::MediaStreamSignaling(talk_base::Thread::Current(),
57 NULL)),
58 webrtc_data_channel_(NULL) {}
59
60 virtual void SetUp() {
61 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) {
62 return;
63 }
64 channel_manager_->Init();
65 webrtc::FakeConstraints constraints;
66 constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, true);
67 constraints.AddMandatory(MediaConstraintsInterface::kEnableSctpDataChannels,
68 true);
69 ASSERT_TRUE(session_.Initialize(&constraints));
70 webrtc::SessionDescriptionInterface* offer = session_.CreateOffer(NULL);
71 ASSERT_TRUE(offer != NULL);
72 ASSERT_TRUE(session_.SetLocalDescription(offer, NULL));
73
74 webrtc_data_channel_ = webrtc::DataChannel::Create(&session_, "test", NULL);
75 // Connect to the media channel.
76 webrtc_data_channel_->SetSendSsrc(kFakeSsrc);
77 webrtc_data_channel_->SetReceiveSsrc(kFakeSsrc);
78
79 session_.data_channel()->SignalReadyToSendData(true);
80 }
81
82 void SetSendBlocked(bool blocked) {
83 bool was_blocked = data_engine_->GetChannel(0)->is_send_blocked();
84 data_engine_->GetChannel(0)->set_send_blocked(blocked);
85 if (!blocked && was_blocked) {
86 session_.data_channel()->SignalReadyToSendData(true);
87 }
88 }
89
90 cricket::FakeMediaEngine* media_engine_;
91 cricket::FakeDataEngine* data_engine_;
92 talk_base::scoped_ptr<cricket::ChannelManager> channel_manager_;
93 webrtc::WebRtcSession session_;
94 talk_base::scoped_refptr<webrtc::DataChannel> webrtc_data_channel_;
95};
96
97// Tests that DataChannel::buffered_amount() is correct after the channel is
98// blocked.
99TEST_F(SctpDataChannelTest, BufferedAmountWhenBlocked) {
100 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) {
101 return;
102 }
103 webrtc::DataBuffer buffer("abcd");
104 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
105
106 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
107
108 SetSendBlocked(true);
109 const int number_of_packets = 3;
110 for (int i = 0; i < number_of_packets; ++i) {
111 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
112 }
113 EXPECT_EQ(buffer.data.length() * number_of_packets,
114 webrtc_data_channel_->buffered_amount());
115}
116
117// Tests that the queued data are sent when the channel transitions from blocked
118// to unblocked.
119TEST_F(SctpDataChannelTest, QueuedDataSentWhenUnblocked) {
120 if (!talk_base::SSLStreamAdapter::HaveDtlsSrtp()) {
121 return;
122 }
123 webrtc::DataBuffer buffer("abcd");
124 SetSendBlocked(true);
125 EXPECT_TRUE(webrtc_data_channel_->Send(buffer));
126
127 SetSendBlocked(false);
128 EXPECT_EQ(0U, webrtc_data_channel_->buffered_amount());
129}