blob: 071fbbb024dc2b8f0853ee4dcf68cff98ed2b6de [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle SCTP
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 <errno.h>
29#include <stdarg.h>
30#include <stdio.h>
31#include <string>
32
33#include "talk/base/buffer.h"
34#include "talk/base/criticalsection.h"
35#include "talk/base/gunit.h"
36#include "talk/base/helpers.h"
37#include "talk/base/messagehandler.h"
38#include "talk/base/messagequeue.h"
39#include "talk/base/scoped_ptr.h"
40#include "talk/base/thread.h"
41#include "talk/media/base/constants.h"
42#include "talk/media/base/mediachannel.h"
43#include "talk/media/sctp/sctpdataengine.h"
44
45enum {
46 MSG_PACKET = 1,
47};
48
49// Fake NetworkInterface that sends/receives sctp packets. The one in
50// talk/media/base/fakenetworkinterface.h only works with rtp/rtcp.
51class SctpFakeNetworkInterface : public cricket::MediaChannel::NetworkInterface,
52 public talk_base::MessageHandler {
53 public:
54 explicit SctpFakeNetworkInterface(talk_base::Thread* thread)
55 : thread_(thread),
56 dest_(NULL) {
57 }
58
59 void SetDestination(cricket::DataMediaChannel* dest) { dest_ = dest; }
60
61 protected:
62 // Called to send raw packet down the wire (e.g. SCTP an packet).
63 virtual bool SendPacket(talk_base::Buffer* packet) {
64 LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::SendPacket";
65
66 // TODO(ldixon): Can/should we use Buffer.TransferTo here?
67 // Note: this assignment does a deep copy of data from packet.
68 talk_base::Buffer* buffer = new talk_base::Buffer(packet->data(),
69 packet->length());
70 thread_->Post(this, MSG_PACKET, talk_base::WrapMessageData(buffer));
71 LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::SendPacket, Posted message.";
72 return true;
73 }
74
75 // Called when a raw packet has been recieved. This passes the data to the
76 // code that will interpret the packet. e.g. to get the content payload from
77 // an SCTP packet.
78 virtual void OnMessage(talk_base::Message* msg) {
79 LOG(LS_VERBOSE) << "SctpFakeNetworkInterface::OnMessage";
80 talk_base::Buffer* buffer =
81 static_cast<talk_base::TypedMessageData<talk_base::Buffer*>*>(
82 msg->pdata)->data();
83 if (dest_) {
84 dest_->OnPacketReceived(buffer);
85 }
86 delete buffer;
87 }
88
89 // Unsupported functions required to exist by NetworkInterface.
90 // TODO(ldixon): Refactor parent NetworkInterface class so these are not
91 // required. They are RTC specific and should be in an appropriate subclass.
92 virtual bool SendRtcp(talk_base::Buffer* packet) {
93 LOG(LS_WARNING) << "Unsupported: SctpFakeNetworkInterface::SendRtcp.";
94 return false;
95 }
96 virtual int SetOption(SocketType type, talk_base::Socket::Option opt,
97 int option) {
98 LOG(LS_WARNING) << "Unsupported: SctpFakeNetworkInterface::SetOption.";
99 return 0;
100 }
101
102 private:
103 // Not owned by this class.
104 talk_base::Thread* thread_;
105 cricket::DataMediaChannel* dest_;
106};
107
108// This is essentially a buffer to hold recieved data. It stores only the last
109// received data. Calling OnDataReceived twice overwrites old data with the
110// newer one.
111// TODO(ldixon): Implement constraints, and allow new data to be added to old
112// instead of replacing it.
113class SctpFakeDataReceiver : public sigslot::has_slots<> {
114 public:
115 SctpFakeDataReceiver() : received_(false) {}
116
117 void Clear() {
118 received_ = false;
119 last_data_ = "";
120 last_params_ = cricket::ReceiveDataParams();
121 }
122
123 virtual void OnDataReceived(const cricket::ReceiveDataParams& params,
124 const char* data, size_t length) {
125 received_ = true;
126 last_data_ = std::string(data, length);
127 last_params_ = params;
128 }
129
130 bool received() const { return received_; }
131 std::string last_data() const { return last_data_; }
132 cricket::ReceiveDataParams last_params() const { return last_params_; }
133
134 private:
135 bool received_;
136 std::string last_data_;
137 cricket::ReceiveDataParams last_params_;
138};
139
140// SCTP Data Engine testing framework.
141class SctpDataMediaChannelTest : public testing::Test {
142 protected:
143 virtual void SetUp() {
144 engine_.reset(new cricket::SctpDataEngine());
145 }
146
147 cricket::SctpDataMediaChannel* CreateChannel(
148 SctpFakeNetworkInterface* net, SctpFakeDataReceiver* recv) {
149 cricket::SctpDataMediaChannel* channel =
150 static_cast<cricket::SctpDataMediaChannel*>(engine_->CreateChannel(
151 cricket::DCT_SCTP));
152 channel->SetInterface(net);
153 // When data is received, pass it to the SctpFakeDataReceiver.
154 channel->SignalDataReceived.connect(
155 recv, &SctpFakeDataReceiver::OnDataReceived);
156 return channel;
157 }
158
159 bool SendData(cricket::SctpDataMediaChannel* chan, uint32 ssrc,
160 const std::string& msg,
161 cricket::SendDataResult* result) {
162 cricket::SendDataParams params;
163 params.ssrc = ssrc;
164 return chan->SendData(params, talk_base::Buffer(msg.data(), msg.length()), result);
165 }
166
167 bool ReceivedData(const SctpFakeDataReceiver* recv, uint32 ssrc,
168 const std::string& msg ) {
169 return (recv->received() &&
170 recv->last_params().ssrc == ssrc &&
171 recv->last_data() == msg);
172 }
173
174 bool ProcessMessagesUntilIdle() {
175 talk_base::Thread* thread = talk_base::Thread::Current();
176 while (!thread->empty()) {
177 talk_base::Message msg;
178 if (thread->Get(&msg, talk_base::kForever)) {
179 thread->Dispatch(&msg);
180 }
181 }
182 return !thread->IsQuitting();
183 }
184
185 private:
186 talk_base::scoped_ptr<cricket::SctpDataEngine> engine_;
187};
188
189TEST_F(SctpDataMediaChannelTest, SendData) {
190 talk_base::scoped_ptr<SctpFakeNetworkInterface> net1(
191 new SctpFakeNetworkInterface(talk_base::Thread::Current()));
192 talk_base::scoped_ptr<SctpFakeNetworkInterface> net2(
193 new SctpFakeNetworkInterface(talk_base::Thread::Current()));
194 talk_base::scoped_ptr<SctpFakeDataReceiver> recv1(
195 new SctpFakeDataReceiver());
196 talk_base::scoped_ptr<SctpFakeDataReceiver> recv2(
197 new SctpFakeDataReceiver());
198 talk_base::scoped_ptr<cricket::SctpDataMediaChannel> chan1(
199 CreateChannel(net1.get(), recv1.get()));
200 chan1->set_debug_name("chan1/connector");
201 talk_base::scoped_ptr<cricket::SctpDataMediaChannel> chan2(
202 CreateChannel(net2.get(), recv2.get()));
203 chan2->set_debug_name("chan2/listener");
204
205 net1->SetDestination(chan2.get());
206 net2->SetDestination(chan1.get());
207
208 LOG(LS_VERBOSE) << "Channel setup ----------------------------- ";
209 chan1->AddSendStream(cricket::StreamParams::CreateLegacy(1));
210 chan2->AddRecvStream(cricket::StreamParams::CreateLegacy(1));
211
212 chan2->AddSendStream(cricket::StreamParams::CreateLegacy(2));
213 chan1->AddRecvStream(cricket::StreamParams::CreateLegacy(2));
214
215 LOG(LS_VERBOSE) << "Connect the channels -----------------------------";
216 // chan1 wants to setup a data connection.
217 chan1->SetReceive(true);
218 // chan1 will have sent chan2 a request to setup a data connection. After
219 // chan2 accepts the offer, chan2 connects to chan1 with the following.
220 chan2->SetReceive(true);
221 chan2->SetSend(true);
222 // Makes sure that network packets are delivered and simulates a
223 // deterministic and realistic small timing delay between the SetSend calls.
224 ProcessMessagesUntilIdle();
225
226 // chan1 and chan2 are now connected so chan1 enables sending to complete
227 // the creation of the connection.
228 chan1->SetSend(true);
229
230 cricket::SendDataResult result;
231 LOG(LS_VERBOSE) << "chan1 sending: 'hello?' -----------------------------";
232 ASSERT_TRUE(SendData(chan1.get(), 1, "hello?", &result));
233 EXPECT_EQ(cricket::SDR_SUCCESS, result);
234 EXPECT_TRUE_WAIT(ReceivedData(recv2.get(), 1, "hello?"), 1000);
235 LOG(LS_VERBOSE) << "recv2.received=" << recv2->received()
236 << "recv2.last_params.ssrc=" << recv2->last_params().ssrc
237 << "recv2.last_params.timestamp="
238 << recv2->last_params().ssrc
239 << "recv2.last_params.seq_num="
240 << recv2->last_params().seq_num
241 << "recv2.last_data=" << recv2->last_data();
242
243 LOG(LS_VERBOSE) << "chan2 sending: 'hi chan1' -----------------------------";
244 ASSERT_TRUE(SendData(chan2.get(), 2, "hi chan1", &result));
245 EXPECT_EQ(cricket::SDR_SUCCESS, result);
246 EXPECT_TRUE_WAIT(ReceivedData(recv1.get(), 2, "hi chan1"), 1000);
247 LOG(LS_VERBOSE) << "recv1.received=" << recv1->received()
248 << "recv1.last_params.ssrc=" << recv1->last_params().ssrc
249 << "recv1.last_params.timestamp="
250 << recv1->last_params().ssrc
251 << "recv1.last_params.seq_num="
252 << recv1->last_params().seq_num
253 << "recv1.last_data=" << recv1->last_data();
254
255 LOG(LS_VERBOSE) << "Closing down. -----------------------------";
256 // Disconnects and closes socket, including setting receiving to false.
257 chan1->SetSend(false);
258 chan2->SetSend(false);
259 LOG(LS_VERBOSE) << "Cleaning up. -----------------------------";
260}