| Index: webrtc/p2p/rawudp/rawudptransportchannel_unittest.cc
|
| diff --git a/webrtc/p2p/rawudp/rawudptransportchannel_unittest.cc b/webrtc/p2p/rawudp/rawudptransportchannel_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..b42b7b3d3aaab90b58e4d0120ee5135c578a5cf4
|
| --- /dev/null
|
| +++ b/webrtc/p2p/rawudp/rawudptransportchannel_unittest.cc
|
| @@ -0,0 +1,193 @@
|
| +/*
|
| + * 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/transportchannelimpl.h"
|
| +#include "webrtc/p2p/rawudp/rawudptransportchannel.h"
|
| +
|
| +namespace cricket {
|
| +
|
| +class RawUdpTransPortChannelTest : 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 RawUdpTransportChannel(std::move(tch_name), component));
|
| + ch_->SignalReadPacket.connect(this, &Endpoint::OnReadPacket);
|
| + ch_->SignalCandidateGathered.connect(this,
|
| + &Endpoint::OnCandidateGathered);
|
| + ch_->SignalGatheringState.connect(this,
|
| + &Endpoint::OnGatheringStateChanged);
|
| + ResetStats();
|
| + }
|
| +
|
| + void Reset() {
|
| + ch_.reset();
|
| + local_candidates_.clear();
|
| + remote_candidates_.clear();
|
| + gathering_state_changes_.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_send_packets_ = 0;
|
| + count_signal_tch_state_changed_ = 0;
|
| + }
|
| +
|
| + void OnReadPacket(TransportChannel* channel,
|
| + 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));
|
| + }
|
| +
|
| + int SendData(const char* data, size_t len) {
|
| + rtc::PacketOptions options;
|
| + return ch_->SendPacket(data, len, options, 0);
|
| + }
|
| +
|
| + void OnCandidateGathered(TransportChannelImpl* channel,
|
| + const Candidate& cand) {
|
| + ASSERT_EQ(ch_.get(), channel);
|
| + local_candidates_.push_back(cand);
|
| + }
|
| +
|
| + // for now this function is only well defined, if there is exact one local
|
| + // candidate.
|
| + void GetLocalPort(uint16_t* local_port) {
|
| + ASSERT_EQ(1u, local_candidates_.size());
|
| + const cricket::Candidate& cand = local_candidates_[0];
|
| + const rtc::SocketAddress& addr = cand.address();
|
| + *local_port = addr.port();
|
| + }
|
| +
|
| + void OnGatheringStateChanged(TransportChannelImpl* channel) {
|
| + ASSERT_EQ(ch_.get(), channel);
|
| + IceGatheringState gathering_state = channel->gathering_state();
|
| + gathering_state_changes_.push_back(gathering_state);
|
| + }
|
| +
|
| + std::list<std::string> ch_packets_;
|
| + std::unique_ptr<RawUdpTransportChannel> ch_;
|
| + uint32_t num_received_packets_; // increases on SignalReadPacket.
|
| + uint32_t num_send_packets_;
|
| + std::vector<IceGatheringState> gathering_state_changes_;
|
| + uint32_t count_signal_tch_state_changed_;
|
| + // TODO(johan) check if it makes sense to have a vector of
|
| + // std::shared<cricket::Candidate> to simplify memory cleanup.
|
| + Candidates local_candidates_;
|
| + 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(RawUdpTransPortChannelTest, SendRecvBasic) {
|
| + ep1_.ch_->MaybeStartGathering();
|
| + ep2_.ch_->MaybeStartGathering();
|
| + 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(RawUdpTransPortChannelTest, MaybeStartGatheringTwice) {
|
| + ep1_.ch_->MaybeStartGathering();
|
| + ep1_.ch_->MaybeStartGathering();
|
| + EXPECT_EQ_WAIT(1u, ep1_.local_candidates_.size(), 1000);
|
| +}
|
| +
|
| +TEST_F(RawUdpTransPortChannelTest, StatusAndSignals) {
|
| + // TransportController connects to several signals of a TransportChannel.
|
| + // RawUdpTransportChannel should emit all this signals.
|
| + // Exception to this rule is SignalDtlsHandshakeError.
|
| + EXPECT_EQ(kIceGatheringNew, ep1_.ch_->gathering_state());
|
| + ep1_.ch_->MaybeStartGathering();
|
| + ASSERT_EQ_WAIT(2u, ep1_.gathering_state_changes_.size(), 100);
|
| + EXPECT_EQ(kIceGatheringGathering, ep1_.gathering_state_changes_[0]);
|
| + EXPECT_EQ(kIceGatheringComplete, ep1_.gathering_state_changes_[1]);
|
| + // Loopback
|
| + EXPECT_EQ_WAIT(1u, ep1_.local_candidates_.size(), 100);
|
| + Candidate cand = ep1_.local_candidates_[0];
|
| + 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());
|
| + // TODO(johan): test for SignalWritableState.
|
| + const char data[] = "abc";
|
| + ep1_.SendData(data, sizeof(data));
|
| + EXPECT_EQ_WAIT(1u, ep1_.ch_packets_.size(), 100);
|
| +}
|
| +} // namespace cricket
|
|
|