1 /* 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 45 enum { 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. 51 class 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. 113 class 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 class SignalReadyToSendObserver : public sigslot::has_slots<> { 141 public: 142 SignalReadyToSendObserver() : signaled_(false), writable_(false) {} 143 144 void OnSignaled(bool writable) { 145 signaled_ = true; 146 writable_ = writable; 147 } 148 149 bool IsSignaled(bool writable) { 150 return signaled_ && (writable_ == writable); 151 } 152 153 private: 154 bool signaled_; 155 bool writable_; 156 }; 157 158 // SCTP Data Engine testing framework. 159 class SctpDataMediaChannelTest : public testing::Test { 160 protected: 161 virtual void SetUp() { 162 engine_.reset(new cricket::SctpDataEngine()); 163 } 164 165 void SetupConnectedChannels() { 166 net1_.reset(new SctpFakeNetworkInterface(talk_base::Thread::Current())); 167 net2_.reset(new SctpFakeNetworkInterface(talk_base::Thread::Current())); 168 recv1_.reset(new SctpFakeDataReceiver()); 169 recv2_.reset(new SctpFakeDataReceiver()); 170 chan1_.reset(CreateChannel(net1_.get(), recv1_.get())); 171 chan1_->set_debug_name("chan1/connector"); 172 chan2_.reset(CreateChannel(net2_.get(), recv2_.get())); 173 chan2_->set_debug_name("chan2/listener"); 174 // Setup two connected channels ready to send and receive. 175 net1_->SetDestination(chan2_.get()); 176 net2_->SetDestination(chan1_.get()); 177 178 LOG(LS_VERBOSE) << "Channel setup ----------------------------- "; 179 chan1_->AddSendStream(cricket::StreamParams::CreateLegacy(1)); 180 chan2_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)); 181 182 chan2_->AddSendStream(cricket::StreamParams::CreateLegacy(2)); 183 chan1_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)); 184 185 LOG(LS_VERBOSE) << "Connect the channels -----------------------------"; 186 // chan1 wants to setup a data connection. 187 chan1_->SetReceive(true); 188 // chan1 will have sent chan2 a request to setup a data connection. After 189 // chan2 accepts the offer, chan2 connects to chan1 with the following. 190 chan2_->SetReceive(true); 191 chan2_->SetSend(true); 192 // Makes sure that network packets are delivered and simulates a 193 // deterministic and realistic small timing delay between the SetSend calls. 194 ProcessMessagesUntilIdle(); 195 196 // chan1 and chan2 are now connected so chan1 enables sending to complete 197 // the creation of the connection. 198 chan1_->SetSend(true); 199 } 200 201 cricket::SctpDataMediaChannel* CreateChannel( 202 SctpFakeNetworkInterface* net, SctpFakeDataReceiver* recv) { 203 cricket::SctpDataMediaChannel* channel = 204 static_cast<cricket::SctpDataMediaChannel*>(engine_->CreateChannel( 205 cricket::DCT_SCTP)); 206 channel->SetInterface(net); 207 // When data is received, pass it to the SctpFakeDataReceiver. 208 channel->SignalDataReceived.connect( 209 recv, &SctpFakeDataReceiver::OnDataReceived); 210 return channel; 211 } 212 213 bool SendData(cricket::SctpDataMediaChannel* chan, uint32 ssrc, 214 const std::string& msg, 215 cricket::SendDataResult* result) { 216 cricket::SendDataParams params; 217 params.ssrc = ssrc; 218 return chan->SendData(params, talk_base::Buffer(msg.data(), msg.length()), result); 219 } 220 221 bool ReceivedData(const SctpFakeDataReceiver* recv, uint32 ssrc, 222 const std::string& msg ) { 223 return (recv->received() && 224 recv->last_params().ssrc == ssrc && 225 recv->last_data() == msg); 226 } 227 228 bool ProcessMessagesUntilIdle() { 229 talk_base::Thread* thread = talk_base::Thread::Current(); 230 while (!thread->empty()) { 231 talk_base::Message msg; 232 if (thread->Get(&msg, talk_base::kForever)) { 233 thread->Dispatch(&msg); 234 } 235 } 236 return !thread->IsQuitting(); 237 } 238 239 cricket::SctpDataMediaChannel* channel1() { return chan1_.get(); } 240 cricket::SctpDataMediaChannel* channel2() { return chan2_.get(); } 241 SctpFakeDataReceiver* receiver1() { return recv1_.get(); } 242 SctpFakeDataReceiver* receiver2() { return recv2_.get(); } 243 244 private: 245 talk_base::scoped_ptr<cricket::SctpDataEngine> engine_; 246 talk_base::scoped_ptr<SctpFakeNetworkInterface> net1_; 247 talk_base::scoped_ptr<SctpFakeNetworkInterface> net2_; 248 talk_base::scoped_ptr<SctpFakeDataReceiver> recv1_; 249 talk_base::scoped_ptr<SctpFakeDataReceiver> recv2_; 250 talk_base::scoped_ptr<cricket::SctpDataMediaChannel> chan1_; 251 talk_base::scoped_ptr<cricket::SctpDataMediaChannel> chan2_; 252 }; 253 254 // Verifies that SignalReadyToSend is fired. 255 TEST_F(SctpDataMediaChannelTest, SignalReadyToSend) { 256 SetupConnectedChannels(); 257 258 SignalReadyToSendObserver signal_observer_1; 259 SignalReadyToSendObserver signal_observer_2; 260 261 channel1()->SignalReadyToSend.connect(&signal_observer_1, 262 &SignalReadyToSendObserver::OnSignaled); 263 channel2()->SignalReadyToSend.connect(&signal_observer_2, 264 &SignalReadyToSendObserver::OnSignaled); 265 266 cricket::SendDataResult result; 267 ASSERT_TRUE(SendData(channel1(), 1, "hello?", &result)); 268 EXPECT_EQ(cricket::SDR_SUCCESS, result); 269 EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hello?"), 1000); 270 ASSERT_TRUE(SendData(channel2(), 2, "hi chan1", &result)); 271 EXPECT_EQ(cricket::SDR_SUCCESS, result); 272 EXPECT_TRUE_WAIT(ReceivedData(receiver1(), 2, "hi chan1"), 1000); 273 274 EXPECT_TRUE_WAIT(signal_observer_1.IsSignaled(true), 1000); 275 EXPECT_TRUE_WAIT(signal_observer_2.IsSignaled(true), 1000); 276 } 277 278 TEST_F(SctpDataMediaChannelTest, SendData) { 279 SetupConnectedChannels(); 280 281 cricket::SendDataResult result; 282 LOG(LS_VERBOSE) << "chan1 sending: 'hello?' -----------------------------"; 283 ASSERT_TRUE(SendData(channel1(), 1, "hello?", &result)); 284 EXPECT_EQ(cricket::SDR_SUCCESS, result); 285 EXPECT_TRUE_WAIT(ReceivedData(receiver2(), 1, "hello?"), 1000); 286 LOG(LS_VERBOSE) << "recv2.received=" << receiver2()->received() 287 << "recv2.last_params.ssrc=" 288 << receiver2()->last_params().ssrc 289 << "recv2.last_params.timestamp=" 290 << receiver2()->last_params().ssrc 291 << "recv2.last_params.seq_num=" 292 << receiver2()->last_params().seq_num 293 << "recv2.last_data=" << receiver2()->last_data(); 294 295 LOG(LS_VERBOSE) << "chan2 sending: 'hi chan1' -----------------------------"; 296 ASSERT_TRUE(SendData(channel2(), 2, "hi chan1", &result)); 297 EXPECT_EQ(cricket::SDR_SUCCESS, result); 298 EXPECT_TRUE_WAIT(ReceivedData(receiver1(), 2, "hi chan1"), 1000); 299 LOG(LS_VERBOSE) << "recv1.received=" << receiver1()->received() 300 << "recv1.last_params.ssrc=" 301 << receiver1()->last_params().ssrc 302 << "recv1.last_params.timestamp=" 303 << receiver1()->last_params().ssrc 304 << "recv1.last_params.seq_num=" 305 << receiver1()->last_params().seq_num 306 << "recv1.last_data=" << receiver1()->last_data(); 307 308 LOG(LS_VERBOSE) << "Closing down. -----------------------------"; 309 // Disconnects and closes socket, including setting receiving to false. 310 channel1()->SetSend(false); 311 channel2()->SetSend(false); 312 LOG(LS_VERBOSE) << "Cleaning up. -----------------------------"; 313 } 314