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/asyncpacketsocket.h" |
| 20 #include "webrtc/base/ipaddress.h" |
| 21 #include "webrtc/base/physicalsocketserver.h" |
| 22 #include "webrtc/base/socketaddress.h" |
| 23 #include "webrtc/base/socketserver.h" |
| 24 #include "webrtc/base/virtualsocketserver.h" |
| 25 #include "webrtc/p2p/base/packettransportinterface.h" |
| 26 #include "webrtc/p2p/base/udptransportchannel.h" |
| 27 |
| 28 namespace cricket { |
| 29 |
| 30 constexpr int kTimeoutMs = 10000; |
| 31 static const rtc::IPAddress kIPv4LocalHostAddress = |
| 32 rtc::IPAddress(0x7F000001); // 127.0.0.1 |
| 33 |
| 34 class UdpTransportChannelTest : public testing::Test, |
| 35 public sigslot::has_slots<> { |
| 36 public: |
| 37 UdpTransportChannelTest() |
| 38 : network_thread_(rtc::Thread::Current()), |
| 39 physical_socket_server_(new rtc::PhysicalSocketServer), |
| 40 virtual_socket_server_( |
| 41 new rtc::VirtualSocketServer(physical_socket_server_.get())), |
| 42 ss_scope_(virtual_socket_server_.get()), |
| 43 ep1_("Name1"), |
| 44 ep2_("name2") { |
| 45 // Setup IP Address for outgoing packets from sockets bound to |
| 46 // IPV4 INADDR_ANY ("0.0.0.0."). Virtual socket server sends these packets |
| 47 // only if the default address is explicit set. With a physical socket, the |
| 48 // actual network stack / operating system would set the IP address for |
| 49 // outgoing packets. |
| 50 virtual_socket_server_->SetDefaultRoute(kIPv4LocalHostAddress); |
| 51 } |
| 52 |
| 53 struct Endpoint : public sigslot::has_slots<> { |
| 54 explicit Endpoint(std::string tch_name) { |
| 55 ch_.reset(new UdpTransportChannel(std::move(tch_name))); |
| 56 ch_->SignalReadPacket.connect(this, &Endpoint::OnReadPacket); |
| 57 ch_->SignalSentPacket.connect(this, &Endpoint::OnSentPacket); |
| 58 ch_->SignalReadyToSend.connect(this, &Endpoint::OnReadyToSend); |
| 59 ch_->SignalWritableState.connect(this, &Endpoint::OnWritableState); |
| 60 } |
| 61 |
| 62 bool CheckData(const char* data, int len) { |
| 63 bool ret = false; |
| 64 if (!ch_packets_.empty()) { |
| 65 std::string packet = ch_packets_.front(); |
| 66 ret = (packet == std::string(data, len)); |
| 67 ch_packets_.pop_front(); |
| 68 } |
| 69 return ret; |
| 70 } |
| 71 |
| 72 void OnWritableState(rtc::PacketTransportInterface* transport) { |
| 73 num_sig_writable_++; |
| 74 } |
| 75 |
| 76 void OnReadyToSend(rtc::PacketTransportInterface* transport) { |
| 77 num_sig_ready_to_send_++; |
| 78 } |
| 79 |
| 80 void OnReadPacket(rtc::PacketTransportInterface* transport, |
| 81 const char* data, |
| 82 size_t len, |
| 83 const rtc::PacketTime& packet_time, |
| 84 int flags) { |
| 85 num_received_packets_++; |
| 86 LOG(LS_VERBOSE) << "OnReadPacket (unittest)"; |
| 87 ch_packets_.push_front(std::string(data, len)); |
| 88 } |
| 89 |
| 90 void OnSentPacket(rtc::PacketTransportInterface* transport, |
| 91 const rtc::SentPacket&) { |
| 92 num_sig_sent_packets_++; |
| 93 } |
| 94 |
| 95 int SendData(const char* data, size_t len) { |
| 96 rtc::PacketOptions options; |
| 97 return ch_->SendPacket(data, len, options, 0); |
| 98 } |
| 99 |
| 100 void GetLocalPort(uint16_t* local_port) { |
| 101 rtc::Optional<rtc::SocketAddress> addr = ch_->local_parameters(); |
| 102 if (!addr) { |
| 103 *local_port = 0; |
| 104 return; |
| 105 } |
| 106 *local_port = addr->port(); |
| 107 } |
| 108 |
| 109 std::list<std::string> ch_packets_; |
| 110 std::unique_ptr<UdpTransportChannel> ch_; |
| 111 uint32_t num_received_packets_ = 0; // Increases on SignalReadPacket. |
| 112 uint32_t num_sig_sent_packets_ = 0; // Increases on SignalSentPacket. |
| 113 uint32_t num_sig_writable_ = 0; // Increases on SignalWritable. |
| 114 uint32_t num_sig_ready_to_send_ = 0; // Increases on SignalReadyToSend. |
| 115 }; |
| 116 |
| 117 rtc::Thread* network_thread_ = nullptr; |
| 118 std::unique_ptr<rtc::PhysicalSocketServer> physical_socket_server_; |
| 119 std::unique_ptr<rtc::VirtualSocketServer> virtual_socket_server_; |
| 120 rtc::SocketServerScope ss_scope_; |
| 121 |
| 122 Endpoint ep1_; |
| 123 Endpoint ep2_; |
| 124 |
| 125 void TestSendRecv() { |
| 126 for (uint32_t i = 0; i < 5; ++i) { |
| 127 static const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
| 128 int len = static_cast<int>(strlen(data)); |
| 129 // local_channel <==> remote_channel |
| 130 EXPECT_EQ_WAIT(len, ep1_.SendData(data, len), kTimeoutMs); |
| 131 EXPECT_TRUE_WAIT(ep2_.CheckData(data, len), kTimeoutMs); |
| 132 EXPECT_EQ_WAIT(i + 1u, ep2_.num_received_packets_, kTimeoutMs); |
| 133 EXPECT_EQ_WAIT(len, ep2_.SendData(data, len), kTimeoutMs); |
| 134 EXPECT_TRUE_WAIT(ep1_.CheckData(data, len), kTimeoutMs); |
| 135 EXPECT_EQ_WAIT(i + 1u, ep1_.num_received_packets_, kTimeoutMs); |
| 136 } |
| 137 } |
| 138 }; |
| 139 |
| 140 TEST_F(UdpTransportChannelTest, SendRecvBasic) { |
| 141 ep1_.ch_->Start(); |
| 142 ep2_.ch_->Start(); |
| 143 uint16_t port; |
| 144 ep2_.GetLocalPort(&port); |
| 145 rtc::SocketAddress addr2 = rtc::SocketAddress("127.0.0.1", port); |
| 146 ep1_.ch_->SetRemoteParameters(addr2); |
| 147 ep1_.GetLocalPort(&port); |
| 148 rtc::SocketAddress addr1 = rtc::SocketAddress("127.0.0.1", port); |
| 149 ep2_.ch_->SetRemoteParameters(addr1); |
| 150 TestSendRecv(); |
| 151 } |
| 152 |
| 153 TEST_F(UdpTransportChannelTest, DefaultLocalParameters) { |
| 154 EXPECT_FALSE(ep1_.ch_->local_parameters()); |
| 155 } |
| 156 |
| 157 TEST_F(UdpTransportChannelTest, StartTwice) { |
| 158 ep1_.ch_->Start(); |
| 159 EXPECT_EQ(UdpTransportChannel::State::CONNECTING, ep1_.ch_->state()); |
| 160 ep1_.ch_->Start(); |
| 161 EXPECT_EQ(UdpTransportChannel::State::CONNECTING, ep1_.ch_->state()); |
| 162 } |
| 163 |
| 164 TEST_F(UdpTransportChannelTest, StatusAndSignals) { |
| 165 EXPECT_EQ(UdpTransportChannel::State::INIT, ep1_.ch_->state()); |
| 166 ep1_.ch_->Start(); |
| 167 EXPECT_EQ(UdpTransportChannel::State::CONNECTING, ep1_.ch_->state()); |
| 168 EXPECT_EQ(0u, ep1_.num_sig_writable_); |
| 169 EXPECT_EQ(0u, ep1_.num_sig_ready_to_send_); |
| 170 // Loopback |
| 171 EXPECT_TRUE(!ep1_.ch_->writable()); |
| 172 rtc::Optional<rtc::SocketAddress> addr = ep1_.ch_->local_parameters(); |
| 173 ASSERT_TRUE(addr); |
| 174 // Keep port, but explicitly set IP. |
| 175 addr->SetIP("127.0.0.1"); |
| 176 ep1_.ch_->SetRemoteParameters(*addr); |
| 177 EXPECT_TRUE(ep1_.ch_->writable()); |
| 178 EXPECT_EQ(1u, ep1_.num_sig_writable_); |
| 179 EXPECT_EQ(1u, ep1_.num_sig_ready_to_send_); |
| 180 const char data[] = "abc"; |
| 181 ep1_.SendData(data, sizeof(data)); |
| 182 EXPECT_EQ_WAIT(1u, ep1_.ch_packets_.size(), kTimeoutMs); |
| 183 EXPECT_EQ_WAIT(1u, ep1_.num_sig_sent_packets_, kTimeoutMs); |
| 184 } |
| 185 } // namespace cricket |
OLD | NEW |