| Index: webrtc/p2p/base/udptransportchannel_unittest.cc
|
| diff --git a/webrtc/p2p/base/udptransportchannel_unittest.cc b/webrtc/p2p/base/udptransportchannel_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a3aeb29528e48b55153c3ae5cd2b5ee20b981174
|
| --- /dev/null
|
| +++ b/webrtc/p2p/base/udptransportchannel_unittest.cc
|
| @@ -0,0 +1,188 @@
|
| +/*
|
| + * Copyright 2016 The WebRTC Project Authors. All rights reserved.
|
| + *
|
| + * Use of this source code is governed by a BSD-style license
|
| + * that can be found in the LICENSE file in the root of the source
|
| + * tree. An additional intellectual property rights grant can be found
|
| + * in the file PATENTS. All contributing project authors may
|
| + * be found in the AUTHORS file in the root of the source tree.
|
| + */
|
| +
|
| +#include <algorithm>
|
| +#include <list>
|
| +#include <memory>
|
| +#include <utility>
|
| +#include <vector>
|
| +
|
| +#include "webrtc/base/gunit.h"
|
| +#include "webrtc/base/thread.h"
|
| +#include "webrtc/base/socketserver.h"
|
| +#include "webrtc/p2p/base/packettransportinterface.h"
|
| +#include "webrtc/p2p/base/transportchannelimpl.h"
|
| +#include "webrtc/p2p/base/udptransportchannel.h"
|
| +
|
| +namespace cricket {
|
| +
|
| +class UdpTransPortChannelTest : public testing::Test,
|
| + public sigslot::has_slots<> {
|
| + public:
|
| + void SetUp() {
|
| + // TODO(johan) investigate whether an AutoThread should be instantiated.
|
| + network_thread_ = rtc::Thread::Current();
|
| + ASSERT_NE(nullptr, network_thread_);
|
| + network_thread_->set_socketserver(
|
| + rtc::SocketServer::CreateDefault().release());
|
| + ep1_.Init("name1", 1);
|
| + ep2_.Init("name2", 2);
|
| + }
|
| +
|
| + void TearDown() {
|
| + ep1_.Reset();
|
| + ep2_.Reset();
|
| + network_thread_ = nullptr;
|
| + }
|
| +
|
| + struct Endpoint : public sigslot::has_slots<> {
|
| + void Init(std::string tch_name, int component) {
|
| + ch_.reset(new UdpTransportChannel(std::move(tch_name), component));
|
| + ch_->SignalReadPacket.connect(this, &Endpoint::OnReadPacket);
|
| + ch_->SignalSentPacket.connect(this, &Endpoint::OnSentPacket);
|
| + ch_->SignalReadyToSend.connect(this, &Endpoint::OnReadyToSend);
|
| + ch_->SignalWritableState.connect(this, &Endpoint::OnWritableState);
|
| + ResetStats();
|
| + }
|
| +
|
| + void Reset() {
|
| + ch_.reset();
|
| + remote_candidates_.clear();
|
| + }
|
| +
|
| + bool CheckData(const char* data, int len) {
|
| + bool ret = false;
|
| + if (!ch_packets_.empty()) {
|
| + std::string packet = ch_packets_.front();
|
| + ret = (packet == std::string(data, len));
|
| + ch_packets_.pop_front();
|
| + }
|
| + return ret;
|
| + }
|
| +
|
| + void ResetStats() {
|
| + num_received_packets_ = 0;
|
| + num_sig_sent_packets_ = 0;
|
| + num_sig_writable_ = 0;
|
| + num_sig_ready_to_send_ = 0;
|
| + }
|
| +
|
| + void OnWritableState(rtc::PacketTransportInterface* transport) {
|
| + num_sig_writable_++;
|
| + }
|
| +
|
| + void OnReadyToSend(rtc::PacketTransportInterface* transport) {
|
| + num_sig_ready_to_send_++;
|
| + }
|
| +
|
| + void OnReadPacket(rtc::PacketTransportInterface* transport,
|
| + const char* data,
|
| + size_t len,
|
| + const rtc::PacketTime& packet_time,
|
| + int flags) {
|
| + num_received_packets_++;
|
| + LOG(LS_VERBOSE) << "OnReadPacket (unittest)";
|
| + ch_packets_.push_front(std::string(data, len));
|
| + }
|
| +
|
| + void OnSentPacket(rtc::PacketTransportInterface* transport,
|
| + const rtc::SentPacket&) {
|
| + num_sig_sent_packets_++;
|
| + }
|
| +
|
| + int SendData(const char* data, size_t len) {
|
| + rtc::PacketOptions options;
|
| + return ch_->SendPacket(data, len, options, 0);
|
| + }
|
| +
|
| + // for now this function is only well defined, if there is exact one local
|
| + // candidate.
|
| + void GetLocalPort(uint16_t* local_port) {
|
| + const cricket::Candidate& cand = ch_->GetLocalCandidate();
|
| + const rtc::SocketAddress& addr = cand.address();
|
| + *local_port = addr.port();
|
| + }
|
| +
|
| + std::list<std::string> ch_packets_;
|
| + std::unique_ptr<UdpTransportChannel> ch_;
|
| + uint32_t num_received_packets_ = 0; // Increases on SignalReadPacket.
|
| + uint32_t num_sig_sent_packets_ = 0; // Increases on SignalSentPacket.
|
| + uint32_t num_sig_writable_ = 0; // Increases on SignalWritable.
|
| + uint32_t num_sig_ready_to_send_ = 0; // Increases on SignalReadyToSend.
|
| + Candidates remote_candidates_;
|
| + };
|
| +
|
| + Endpoint ep1_;
|
| + Endpoint ep2_;
|
| + rtc::Thread* network_thread_;
|
| +
|
| + void TestSendRecv() {
|
| + for (uint32_t i = 0; i < 5; ++i) {
|
| + static const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
| + int len = static_cast<int>(strlen(data));
|
| + // local_channel1 <==> remote_chanel1
|
| + EXPECT_EQ_WAIT(len, ep1_.SendData(data, len), 1000);
|
| + // rtc::Thread::Current()->ProcessMessages(1000);
|
| + EXPECT_TRUE_WAIT(ep2_.CheckData(data, len), 1000);
|
| + EXPECT_EQ_WAIT(i + 1u, ep2_.num_received_packets_, 100);
|
| + EXPECT_EQ_WAIT(len, ep2_.SendData(data, len), 1000);
|
| + // rtc::Thread::Current()->ProcessMessages(1000);
|
| + EXPECT_TRUE_WAIT(ep1_.CheckData(data, len), 1000);
|
| + EXPECT_EQ_WAIT(i + 1u, ep1_.num_received_packets_, 100);
|
| + }
|
| + }
|
| +};
|
| +
|
| +TEST_F(UdpTransPortChannelTest, SendRecvBasic) {
|
| + ep1_.ch_->TryAllocateSocket();
|
| + ep2_.ch_->TryAllocateSocket();
|
| + uint16_t port;
|
| + ep2_.GetLocalPort(&port);
|
| + rtc::SocketAddress addr2 = rtc::SocketAddress("127.0.0.1", port);
|
| + cricket::Candidate cand;
|
| + cand.set_address(addr2);
|
| + ep1_.ch_->AddRemoteCandidate(cand);
|
| + ep1_.GetLocalPort(&port);
|
| + rtc::SocketAddress addr1 = rtc::SocketAddress("127.0.0.1", port);
|
| + cand.set_address(addr1);
|
| + ep2_.ch_->AddRemoteCandidate(cand);
|
| + TestSendRecv();
|
| +}
|
| +
|
| +TEST_F(UdpTransPortChannelTest, TryAllocateSocketTwice) {
|
| + ep1_.ch_->TryAllocateSocket();
|
| + EXPECT_EQ(STATE_CONNECTING, ep1_.ch_->GetState());
|
| + ep1_.ch_->TryAllocateSocket();
|
| + EXPECT_EQ(STATE_CONNECTING, ep1_.ch_->GetState());
|
| +}
|
| +
|
| +TEST_F(UdpTransPortChannelTest, StatusAndSignals) {
|
| + EXPECT_EQ(STATE_INIT, ep1_.ch_->GetState());
|
| + ep1_.ch_->TryAllocateSocket();
|
| + EXPECT_EQ(STATE_CONNECTING, ep1_.ch_->GetState());
|
| + EXPECT_EQ(0u, ep1_.num_sig_writable_);
|
| + EXPECT_EQ(0u, ep1_.num_sig_ready_to_send_);
|
| + // Loopback
|
| + Candidate cand = ep1_.ch_->GetLocalCandidate();
|
| + EXPECT_TRUE(!ep1_.ch_->writable());
|
| + rtc::SocketAddress cand_addr = cand.address();
|
| + // keep port, but explicit set IP.
|
| + cand_addr.SetIP("127.0.0.1");
|
| + cand.set_address(cand_addr);
|
| + ep1_.ch_->AddRemoteCandidate(cand);
|
| + EXPECT_TRUE(ep1_.ch_->writable());
|
| + EXPECT_EQ(1u, ep1_.num_sig_writable_);
|
| + EXPECT_EQ(1u, ep1_.num_sig_ready_to_send_);
|
| + const char data[] = "abc";
|
| + ep1_.SendData(data, sizeof(data));
|
| + EXPECT_EQ_WAIT(1u, ep1_.ch_packets_.size(), 100);
|
| + EXPECT_EQ_WAIT(1u, ep1_.num_sig_sent_packets_, 200);
|
| +}
|
| +} // namespace cricket
|
|
|