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", 1), |
| 44 ep2_("name2", 2) { |
| 45 // Setup IP Address for outgoing packets from sockets bound to |
| 46 // IPV4 INADDR_ANY ("0.0.0.0."). Virtual socket server sends packets these |
| 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 Endpoint(std::string tch_name, int component) { |
| 55 ch_.reset(new UdpTransportChannel(std::move(tch_name), component)); |
| 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 const rtc::SocketAddress& addr = ch_->local_parameters(); |
| 102 *local_port = addr.port(); |
| 103 } |
| 104 |
| 105 std::list<std::string> ch_packets_; |
| 106 std::unique_ptr<UdpTransportChannel> ch_; |
| 107 uint32_t num_received_packets_ = 0; // Increases on SignalReadPacket. |
| 108 uint32_t num_sig_sent_packets_ = 0; // Increases on SignalSentPacket. |
| 109 uint32_t num_sig_writable_ = 0; // Increases on SignalWritable. |
| 110 uint32_t num_sig_ready_to_send_ = 0; // Increases on SignalReadyToSend. |
| 111 }; |
| 112 |
| 113 rtc::Thread* network_thread_ = nullptr; |
| 114 std::unique_ptr<rtc::PhysicalSocketServer> physical_socket_server_; |
| 115 std::unique_ptr<rtc::VirtualSocketServer> virtual_socket_server_; |
| 116 rtc::SocketServerScope ss_scope_; |
| 117 |
| 118 Endpoint ep1_; |
| 119 Endpoint ep2_; |
| 120 |
| 121 void TestSendRecv() { |
| 122 for (uint32_t i = 0; i < 5; ++i) { |
| 123 static const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
| 124 int len = static_cast<int>(strlen(data)); |
| 125 // local_channel <==> remote_channel |
| 126 EXPECT_EQ_WAIT(len, ep1_.SendData(data, len), kTimeoutMs); |
| 127 EXPECT_TRUE_WAIT(ep2_.CheckData(data, len), kTimeoutMs); |
| 128 EXPECT_EQ_WAIT(i + 1u, ep2_.num_received_packets_, kTimeoutMs); |
| 129 EXPECT_EQ_WAIT(len, ep2_.SendData(data, len), kTimeoutMs); |
| 130 EXPECT_TRUE_WAIT(ep1_.CheckData(data, len), kTimeoutMs); |
| 131 EXPECT_EQ_WAIT(i + 1u, ep1_.num_received_packets_, kTimeoutMs); |
| 132 } |
| 133 } |
| 134 }; |
| 135 |
| 136 TEST_F(UdpTransportChannelTest, SendRecvBasic) { |
| 137 ep1_.ch_->CreateSocket(); |
| 138 ep2_.ch_->CreateSocket(); |
| 139 uint16_t port; |
| 140 ep2_.GetLocalPort(&port); |
| 141 rtc::SocketAddress addr2 = rtc::SocketAddress("127.0.0.1", port); |
| 142 ep1_.ch_->SetRemoteParameters(addr2); |
| 143 ep1_.GetLocalPort(&port); |
| 144 rtc::SocketAddress addr1 = rtc::SocketAddress("127.0.0.1", port); |
| 145 ep2_.ch_->SetRemoteParameters(addr1); |
| 146 TestSendRecv(); |
| 147 } |
| 148 |
| 149 TEST_F(UdpTransportChannelTest, DefaultLocalParameters) { |
| 150 EXPECT_TRUE(ep1_.ch_->local_parameters().IsNil()); |
| 151 } |
| 152 |
| 153 TEST_F(UdpTransportChannelTest, CreateSocketTwice) { |
| 154 ep1_.ch_->CreateSocket(); |
| 155 EXPECT_EQ(UDPTRANSPORT_STATE_CONNECTING, ep1_.ch_->GetState()); |
| 156 ep1_.ch_->CreateSocket(); |
| 157 EXPECT_EQ(UDPTRANSPORT_STATE_CONNECTING, ep1_.ch_->GetState()); |
| 158 } |
| 159 |
| 160 TEST_F(UdpTransportChannelTest, StatusAndSignals) { |
| 161 EXPECT_EQ(UDPTRANSPORT_STATE_INIT, ep1_.ch_->GetState()); |
| 162 ep1_.ch_->CreateSocket(); |
| 163 EXPECT_EQ(UDPTRANSPORT_STATE_CONNECTING, ep1_.ch_->GetState()); |
| 164 EXPECT_EQ(0u, ep1_.num_sig_writable_); |
| 165 EXPECT_EQ(0u, ep1_.num_sig_ready_to_send_); |
| 166 // Loopback |
| 167 EXPECT_TRUE(!ep1_.ch_->writable()); |
| 168 rtc::SocketAddress addr = ep1_.ch_->local_parameters(); |
| 169 // Keep port, but explicitly set IP. |
| 170 addr.SetIP("127.0.0.1"); |
| 171 ep1_.ch_->SetRemoteParameters(addr); |
| 172 EXPECT_TRUE(ep1_.ch_->writable()); |
| 173 EXPECT_EQ(1u, ep1_.num_sig_writable_); |
| 174 EXPECT_EQ(1u, ep1_.num_sig_ready_to_send_); |
| 175 const char data[] = "abc"; |
| 176 ep1_.SendData(data, sizeof(data)); |
| 177 EXPECT_EQ_WAIT(1u, ep1_.ch_packets_.size(), kTimeoutMs); |
| 178 EXPECT_EQ_WAIT(1u, ep1_.num_sig_sent_packets_, kTimeoutMs); |
| 179 } |
| 180 } // namespace cricket |
OLD | NEW |