OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include <algorithm> |
| 12 #include <list> |
| 13 #include <memory> |
| 14 #include <utility> |
| 15 #include <vector> |
| 16 |
| 17 #include "webrtc/base/gunit.h" |
| 18 #include "webrtc/base/thread.h" |
| 19 #include "webrtc/base/socketserver.h" |
| 20 #include "webrtc/p2p/base/packettransportinterface.h" |
| 21 #include "webrtc/p2p/base/transportchannelimpl.h" |
| 22 #include "webrtc/p2p/base/udptransportchannel.h" |
| 23 |
| 24 namespace cricket { |
| 25 |
| 26 class UdpTransPortChannelTest : public testing::Test, |
| 27 public sigslot::has_slots<> { |
| 28 public: |
| 29 void SetUp() { |
| 30 // TODO(johan) investigate whether an AutoThread should be instantiated. |
| 31 network_thread_ = rtc::Thread::Current(); |
| 32 ASSERT_NE(nullptr, network_thread_); |
| 33 network_thread_->set_socketserver( |
| 34 rtc::SocketServer::CreateDefault().release()); |
| 35 ep1_.Init("name1", 1); |
| 36 ep2_.Init("name2", 2); |
| 37 } |
| 38 |
| 39 void TearDown() { |
| 40 ep1_.Reset(); |
| 41 ep2_.Reset(); |
| 42 network_thread_ = nullptr; |
| 43 } |
| 44 |
| 45 struct Endpoint : public sigslot::has_slots<> { |
| 46 void Init(std::string tch_name, int component) { |
| 47 ch_.reset(new UdpTransportChannel(std::move(tch_name), component)); |
| 48 ch_->SignalReadPacket.connect(this, &Endpoint::OnReadPacket); |
| 49 ch_->SignalSentPacket.connect(this, &Endpoint::OnSentPacket); |
| 50 ch_->SignalReadyToSend.connect(this, &Endpoint::OnReadyToSend); |
| 51 ch_->SignalWritableState.connect(this, &Endpoint::OnWritableState); |
| 52 ResetStats(); |
| 53 } |
| 54 |
| 55 void Reset() { |
| 56 ch_.reset(); |
| 57 remote_candidates_.clear(); |
| 58 } |
| 59 |
| 60 bool CheckData(const char* data, int len) { |
| 61 bool ret = false; |
| 62 if (!ch_packets_.empty()) { |
| 63 std::string packet = ch_packets_.front(); |
| 64 ret = (packet == std::string(data, len)); |
| 65 ch_packets_.pop_front(); |
| 66 } |
| 67 return ret; |
| 68 } |
| 69 |
| 70 void ResetStats() { |
| 71 num_received_packets_ = 0; |
| 72 num_sig_sent_packets_ = 0; |
| 73 num_sig_writable_ = 0; |
| 74 num_sig_ready_to_send_ = 0; |
| 75 } |
| 76 |
| 77 void OnWritableState(rtc::PacketTransportInterface* transport) { |
| 78 num_sig_writable_++; |
| 79 } |
| 80 |
| 81 void OnReadyToSend(rtc::PacketTransportInterface* transport) { |
| 82 num_sig_ready_to_send_++; |
| 83 } |
| 84 |
| 85 void OnReadPacket(rtc::PacketTransportInterface* transport, |
| 86 const char* data, |
| 87 size_t len, |
| 88 const rtc::PacketTime& packet_time, |
| 89 int flags) { |
| 90 num_received_packets_++; |
| 91 LOG(LS_VERBOSE) << "OnReadPacket (unittest)"; |
| 92 ch_packets_.push_front(std::string(data, len)); |
| 93 } |
| 94 |
| 95 void OnSentPacket(rtc::PacketTransportInterface* transport, |
| 96 const rtc::SentPacket&) { |
| 97 num_sig_sent_packets_++; |
| 98 } |
| 99 |
| 100 int SendData(const char* data, size_t len) { |
| 101 rtc::PacketOptions options; |
| 102 return ch_->SendPacket(data, len, options, 0); |
| 103 } |
| 104 |
| 105 // for now this function is only well defined, if there is exact one local |
| 106 // candidate. |
| 107 void GetLocalPort(uint16_t* local_port) { |
| 108 const cricket::Candidate& cand = ch_->GetLocalCandidate(); |
| 109 const rtc::SocketAddress& addr = cand.address(); |
| 110 *local_port = addr.port(); |
| 111 } |
| 112 |
| 113 std::list<std::string> ch_packets_; |
| 114 std::unique_ptr<UdpTransportChannel> ch_; |
| 115 uint32_t num_received_packets_ = 0; // Increases on SignalReadPacket. |
| 116 uint32_t num_sig_sent_packets_ = 0; // Increases on SignalSentPacket. |
| 117 uint32_t num_sig_writable_ = 0; // Increases on SignalWritable. |
| 118 uint32_t num_sig_ready_to_send_ = 0; // Increases on SignalReadyToSend. |
| 119 Candidates remote_candidates_; |
| 120 }; |
| 121 |
| 122 Endpoint ep1_; |
| 123 Endpoint ep2_; |
| 124 rtc::Thread* network_thread_; |
| 125 |
| 126 void TestSendRecv() { |
| 127 for (uint32_t i = 0; i < 5; ++i) { |
| 128 static const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
| 129 int len = static_cast<int>(strlen(data)); |
| 130 // local_channel1 <==> remote_chanel1 |
| 131 EXPECT_EQ_WAIT(len, ep1_.SendData(data, len), 1000); |
| 132 // rtc::Thread::Current()->ProcessMessages(1000); |
| 133 EXPECT_TRUE_WAIT(ep2_.CheckData(data, len), 1000); |
| 134 EXPECT_EQ_WAIT(i + 1u, ep2_.num_received_packets_, 100); |
| 135 EXPECT_EQ_WAIT(len, ep2_.SendData(data, len), 1000); |
| 136 // rtc::Thread::Current()->ProcessMessages(1000); |
| 137 EXPECT_TRUE_WAIT(ep1_.CheckData(data, len), 1000); |
| 138 EXPECT_EQ_WAIT(i + 1u, ep1_.num_received_packets_, 100); |
| 139 } |
| 140 } |
| 141 }; |
| 142 |
| 143 TEST_F(UdpTransPortChannelTest, SendRecvBasic) { |
| 144 ep1_.ch_->TryAllocateSocket(); |
| 145 ep2_.ch_->TryAllocateSocket(); |
| 146 uint16_t port; |
| 147 ep2_.GetLocalPort(&port); |
| 148 rtc::SocketAddress addr2 = rtc::SocketAddress("127.0.0.1", port); |
| 149 cricket::Candidate cand; |
| 150 cand.set_address(addr2); |
| 151 ep1_.ch_->AddRemoteCandidate(cand); |
| 152 ep1_.GetLocalPort(&port); |
| 153 rtc::SocketAddress addr1 = rtc::SocketAddress("127.0.0.1", port); |
| 154 cand.set_address(addr1); |
| 155 ep2_.ch_->AddRemoteCandidate(cand); |
| 156 TestSendRecv(); |
| 157 } |
| 158 |
| 159 TEST_F(UdpTransPortChannelTest, TryAllocateSocketTwice) { |
| 160 ep1_.ch_->TryAllocateSocket(); |
| 161 EXPECT_EQ(STATE_CONNECTING, ep1_.ch_->GetState()); |
| 162 ep1_.ch_->TryAllocateSocket(); |
| 163 EXPECT_EQ(STATE_CONNECTING, ep1_.ch_->GetState()); |
| 164 } |
| 165 |
| 166 TEST_F(UdpTransPortChannelTest, StatusAndSignals) { |
| 167 EXPECT_EQ(STATE_INIT, ep1_.ch_->GetState()); |
| 168 ep1_.ch_->TryAllocateSocket(); |
| 169 EXPECT_EQ(STATE_CONNECTING, ep1_.ch_->GetState()); |
| 170 EXPECT_EQ(0u, ep1_.num_sig_writable_); |
| 171 EXPECT_EQ(0u, ep1_.num_sig_ready_to_send_); |
| 172 // Loopback |
| 173 Candidate cand = ep1_.ch_->GetLocalCandidate(); |
| 174 EXPECT_TRUE(!ep1_.ch_->writable()); |
| 175 rtc::SocketAddress cand_addr = cand.address(); |
| 176 // keep port, but explicit set IP. |
| 177 cand_addr.SetIP("127.0.0.1"); |
| 178 cand.set_address(cand_addr); |
| 179 ep1_.ch_->AddRemoteCandidate(cand); |
| 180 EXPECT_TRUE(ep1_.ch_->writable()); |
| 181 EXPECT_EQ(1u, ep1_.num_sig_writable_); |
| 182 EXPECT_EQ(1u, ep1_.num_sig_ready_to_send_); |
| 183 const char data[] = "abc"; |
| 184 ep1_.SendData(data, sizeof(data)); |
| 185 EXPECT_EQ_WAIT(1u, ep1_.ch_packets_.size(), 100); |
| 186 EXPECT_EQ_WAIT(1u, ep1_.num_sig_sent_packets_, 200); |
| 187 } |
| 188 } // namespace cricket |
OLD | NEW |