OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <list> | 12 #include <list> |
13 #include <memory> | 13 #include <memory> |
14 #include <utility> | 14 #include <utility> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "webrtc/base/gunit.h" | 17 #include "webrtc/base/gunit.h" |
18 #include "webrtc/base/thread.h" | 18 #include "webrtc/base/thread.h" |
19 #include "webrtc/base/asyncpacketsocket.h" | 19 #include "webrtc/base/asyncpacketsocket.h" |
20 #include "webrtc/base/ipaddress.h" | 20 #include "webrtc/base/ipaddress.h" |
21 #include "webrtc/base/physicalsocketserver.h" | 21 #include "webrtc/base/physicalsocketserver.h" |
22 #include "webrtc/base/socketaddress.h" | 22 #include "webrtc/base/socketaddress.h" |
23 #include "webrtc/base/socketserver.h" | 23 #include "webrtc/base/socketserver.h" |
24 #include "webrtc/base/virtualsocketserver.h" | 24 #include "webrtc/base/virtualsocketserver.h" |
| 25 #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
25 #include "webrtc/p2p/base/packettransportinterface.h" | 26 #include "webrtc/p2p/base/packettransportinterface.h" |
26 #include "webrtc/p2p/base/udptransportchannel.h" | 27 #include "webrtc/p2p/base/udptransportchannel.h" |
27 | 28 |
28 namespace cricket { | 29 namespace cricket { |
29 | 30 |
30 constexpr int kTimeoutMs = 10000; | 31 constexpr int kTimeoutMs = 10000; |
31 static const rtc::IPAddress kIPv4LocalHostAddress = | 32 static const rtc::IPAddress kIPv4LocalHostAddress = |
32 rtc::IPAddress(0x7F000001); // 127.0.0.1 | 33 rtc::IPAddress(0x7F000001); // 127.0.0.1 |
33 | 34 |
34 class UdpTransportChannelTest : public testing::Test, | 35 class UdpTransportTest : public testing::Test, public sigslot::has_slots<> { |
35 public sigslot::has_slots<> { | |
36 public: | 36 public: |
37 UdpTransportChannelTest() | 37 UdpTransportTest() |
38 : network_thread_(rtc::Thread::Current()), | 38 : network_thread_(rtc::Thread::Current()), |
39 physical_socket_server_(new rtc::PhysicalSocketServer), | 39 physical_socket_server_(new rtc::PhysicalSocketServer), |
40 virtual_socket_server_( | 40 virtual_socket_server_( |
41 new rtc::VirtualSocketServer(physical_socket_server_.get())), | 41 new rtc::VirtualSocketServer(physical_socket_server_.get())), |
42 ss_scope_(virtual_socket_server_.get()), | 42 ss_scope_(virtual_socket_server_.get()), |
43 ep1_("Name1"), | 43 ep1_("Name1", |
44 ep2_("name2") { | 44 std::unique_ptr<rtc::AsyncPacketSocket>( |
45 // Setup IP Address for outgoing packets from sockets bound to | 45 socket_factory_.CreateUdpSocket( |
46 // IPV4 INADDR_ANY ("0.0.0.0."). Virtual socket server sends these packets | 46 rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), |
47 // only if the default address is explicit set. With a physical socket, the | 47 0, |
48 // actual network stack / operating system would set the IP address for | 48 0))), |
49 // outgoing packets. | 49 ep2_("Name2", |
| 50 std::unique_ptr<rtc::AsyncPacketSocket>( |
| 51 socket_factory_.CreateUdpSocket( |
| 52 rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), |
| 53 0, |
| 54 0))) { |
| 55 // Setup IP Address for outgoing packets from sockets bound to IPV4 |
| 56 // INADDR_ANY ("0.0.0.0."), as used above when creating the virtual |
| 57 // sockets. The virtual socket server sends these packets only if the |
| 58 // default address is explicit set. With a physical socket, the actual |
| 59 // network stack / operating system would set the IP address for outgoing |
| 60 // packets. |
50 virtual_socket_server_->SetDefaultRoute(kIPv4LocalHostAddress); | 61 virtual_socket_server_->SetDefaultRoute(kIPv4LocalHostAddress); |
51 } | 62 } |
52 | 63 |
53 struct Endpoint : public sigslot::has_slots<> { | 64 struct Endpoint : public sigslot::has_slots<> { |
54 explicit Endpoint(std::string tch_name) { | 65 explicit Endpoint(std::string tch_name, |
55 ch_.reset(new UdpTransportChannel(std::move(tch_name))); | 66 std::unique_ptr<rtc::AsyncPacketSocket> socket) { |
| 67 ch_.reset(new UdpTransport(std::move(tch_name), std::move(socket))); |
56 ch_->SignalReadPacket.connect(this, &Endpoint::OnReadPacket); | 68 ch_->SignalReadPacket.connect(this, &Endpoint::OnReadPacket); |
57 ch_->SignalSentPacket.connect(this, &Endpoint::OnSentPacket); | 69 ch_->SignalSentPacket.connect(this, &Endpoint::OnSentPacket); |
58 ch_->SignalReadyToSend.connect(this, &Endpoint::OnReadyToSend); | 70 ch_->SignalReadyToSend.connect(this, &Endpoint::OnReadyToSend); |
59 ch_->SignalWritableState.connect(this, &Endpoint::OnWritableState); | 71 ch_->SignalWritableState.connect(this, &Endpoint::OnWritableState); |
60 } | 72 } |
61 | 73 |
62 bool CheckData(const char* data, int len) { | 74 bool CheckData(const char* data, int len) { |
63 bool ret = false; | 75 bool ret = false; |
64 if (!ch_packets_.empty()) { | 76 if (!ch_packets_.empty()) { |
65 std::string packet = ch_packets_.front(); | 77 std::string packet = ch_packets_.front(); |
(...skipping 25 matching lines...) Expand all Loading... |
91 const rtc::SentPacket&) { | 103 const rtc::SentPacket&) { |
92 num_sig_sent_packets_++; | 104 num_sig_sent_packets_++; |
93 } | 105 } |
94 | 106 |
95 int SendData(const char* data, size_t len) { | 107 int SendData(const char* data, size_t len) { |
96 rtc::PacketOptions options; | 108 rtc::PacketOptions options; |
97 return ch_->SendPacket(data, len, options, 0); | 109 return ch_->SendPacket(data, len, options, 0); |
98 } | 110 } |
99 | 111 |
100 void GetLocalPort(uint16_t* local_port) { | 112 void GetLocalPort(uint16_t* local_port) { |
101 rtc::Optional<rtc::SocketAddress> addr = ch_->local_parameters(); | 113 *local_port = ch_->GetLocalAddress().port(); |
102 if (!addr) { | |
103 *local_port = 0; | |
104 return; | |
105 } | |
106 *local_port = addr->port(); | |
107 } | 114 } |
108 | 115 |
109 std::list<std::string> ch_packets_; | 116 std::list<std::string> ch_packets_; |
110 std::unique_ptr<UdpTransportChannel> ch_; | 117 std::unique_ptr<UdpTransport> ch_; |
111 uint32_t num_received_packets_ = 0; // Increases on SignalReadPacket. | 118 uint32_t num_received_packets_ = 0; // Increases on SignalReadPacket. |
112 uint32_t num_sig_sent_packets_ = 0; // Increases on SignalSentPacket. | 119 uint32_t num_sig_sent_packets_ = 0; // Increases on SignalSentPacket. |
113 uint32_t num_sig_writable_ = 0; // Increases on SignalWritable. | 120 uint32_t num_sig_writable_ = 0; // Increases on SignalWritable. |
114 uint32_t num_sig_ready_to_send_ = 0; // Increases on SignalReadyToSend. | 121 uint32_t num_sig_ready_to_send_ = 0; // Increases on SignalReadyToSend. |
115 }; | 122 }; |
116 | 123 |
117 rtc::Thread* network_thread_ = nullptr; | 124 rtc::Thread* network_thread_ = nullptr; |
118 std::unique_ptr<rtc::PhysicalSocketServer> physical_socket_server_; | 125 std::unique_ptr<rtc::PhysicalSocketServer> physical_socket_server_; |
119 std::unique_ptr<rtc::VirtualSocketServer> virtual_socket_server_; | 126 std::unique_ptr<rtc::VirtualSocketServer> virtual_socket_server_; |
120 rtc::SocketServerScope ss_scope_; | 127 rtc::SocketServerScope ss_scope_; |
| 128 // Uses current thread's socket server, which will be set by ss_scope_. |
| 129 rtc::BasicPacketSocketFactory socket_factory_; |
121 | 130 |
122 Endpoint ep1_; | 131 Endpoint ep1_; |
123 Endpoint ep2_; | 132 Endpoint ep2_; |
124 | 133 |
125 void TestSendRecv() { | 134 void TestSendRecv() { |
126 for (uint32_t i = 0; i < 5; ++i) { | 135 for (uint32_t i = 0; i < 5; ++i) { |
127 static const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; | 136 static const char* data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; |
128 int len = static_cast<int>(strlen(data)); | 137 int len = static_cast<int>(strlen(data)); |
129 // local_channel <==> remote_channel | 138 // local_channel <==> remote_channel |
130 EXPECT_EQ_WAIT(len, ep1_.SendData(data, len), kTimeoutMs); | 139 EXPECT_EQ_WAIT(len, ep1_.SendData(data, len), kTimeoutMs); |
131 EXPECT_TRUE_WAIT(ep2_.CheckData(data, len), kTimeoutMs); | 140 EXPECT_TRUE_WAIT(ep2_.CheckData(data, len), kTimeoutMs); |
132 EXPECT_EQ_WAIT(i + 1u, ep2_.num_received_packets_, kTimeoutMs); | 141 EXPECT_EQ_WAIT(i + 1u, ep2_.num_received_packets_, kTimeoutMs); |
133 EXPECT_EQ_WAIT(len, ep2_.SendData(data, len), kTimeoutMs); | 142 EXPECT_EQ_WAIT(len, ep2_.SendData(data, len), kTimeoutMs); |
134 EXPECT_TRUE_WAIT(ep1_.CheckData(data, len), kTimeoutMs); | 143 EXPECT_TRUE_WAIT(ep1_.CheckData(data, len), kTimeoutMs); |
135 EXPECT_EQ_WAIT(i + 1u, ep1_.num_received_packets_, kTimeoutMs); | 144 EXPECT_EQ_WAIT(i + 1u, ep1_.num_received_packets_, kTimeoutMs); |
136 } | 145 } |
137 } | 146 } |
138 }; | 147 }; |
139 | 148 |
140 TEST_F(UdpTransportChannelTest, SendRecvBasic) { | 149 TEST_F(UdpTransportTest, AddressGetters) { |
141 ep1_.ch_->Start(); | 150 // Initially, remote address should be nil but local address shouldn't be. |
142 ep2_.ch_->Start(); | 151 EXPECT_FALSE(ep1_.ch_->GetLocalAddress().IsNil()); |
| 152 EXPECT_TRUE(ep1_.ch_->GetRemoteAddress().IsNil()); |
| 153 rtc::SocketAddress destination("127.0.0.1", 1337); |
| 154 ASSERT_TRUE(ep1_.ch_->SetRemoteAddress(destination)); |
| 155 EXPECT_EQ(destination, ep1_.ch_->GetRemoteAddress()); |
| 156 } |
| 157 |
| 158 // Setting an invalid address should fail and have no effect. |
| 159 TEST_F(UdpTransportTest, SettingIncompleteRemoteAddressFails) { |
| 160 EXPECT_FALSE(ep1_.ch_->SetRemoteAddress(rtc::SocketAddress("127.0.0.1", 0))); |
| 161 EXPECT_TRUE(ep1_.ch_->GetRemoteAddress().IsNil()); |
| 162 } |
| 163 |
| 164 TEST_F(UdpTransportTest, SendRecvBasic) { |
143 uint16_t port; | 165 uint16_t port; |
144 ep2_.GetLocalPort(&port); | 166 ep2_.GetLocalPort(&port); |
145 rtc::SocketAddress addr2 = rtc::SocketAddress("127.0.0.1", port); | 167 rtc::SocketAddress addr2 = rtc::SocketAddress("127.0.0.1", port); |
146 ep1_.ch_->SetRemoteParameters(addr2); | 168 EXPECT_TRUE(ep1_.ch_->SetRemoteAddress(addr2)); |
147 ep1_.GetLocalPort(&port); | 169 ep1_.GetLocalPort(&port); |
148 rtc::SocketAddress addr1 = rtc::SocketAddress("127.0.0.1", port); | 170 rtc::SocketAddress addr1 = rtc::SocketAddress("127.0.0.1", port); |
149 ep2_.ch_->SetRemoteParameters(addr1); | 171 EXPECT_TRUE(ep2_.ch_->SetRemoteAddress(addr1)); |
150 TestSendRecv(); | 172 TestSendRecv(); |
151 } | 173 } |
152 | 174 |
153 TEST_F(UdpTransportChannelTest, DefaultLocalParameters) { | 175 // Test the signals and state methods used internally by causing a UdpTransport |
154 EXPECT_FALSE(ep1_.ch_->local_parameters()); | 176 // to send a packet to itself. |
155 } | 177 TEST_F(UdpTransportTest, StatusAndSignals) { |
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_); | 178 EXPECT_EQ(0u, ep1_.num_sig_writable_); |
169 EXPECT_EQ(0u, ep1_.num_sig_ready_to_send_); | 179 EXPECT_EQ(0u, ep1_.num_sig_ready_to_send_); |
170 // Loopback | 180 // Loopback |
171 EXPECT_TRUE(!ep1_.ch_->writable()); | 181 EXPECT_TRUE(!ep1_.ch_->writable()); |
172 rtc::Optional<rtc::SocketAddress> addr = ep1_.ch_->local_parameters(); | 182 rtc::SocketAddress addr = ep1_.ch_->GetLocalAddress(); |
173 ASSERT_TRUE(addr); | |
174 // Keep port, but explicitly set IP. | 183 // Keep port, but explicitly set IP. |
175 addr->SetIP("127.0.0.1"); | 184 addr.SetIP("127.0.0.1"); |
176 ep1_.ch_->SetRemoteParameters(*addr); | 185 ep1_.ch_->SetRemoteAddress(addr); |
177 EXPECT_TRUE(ep1_.ch_->writable()); | 186 EXPECT_TRUE(ep1_.ch_->writable()); |
178 EXPECT_EQ(1u, ep1_.num_sig_writable_); | 187 EXPECT_EQ(1u, ep1_.num_sig_writable_); |
179 EXPECT_EQ(1u, ep1_.num_sig_ready_to_send_); | 188 EXPECT_EQ(1u, ep1_.num_sig_ready_to_send_); |
180 const char data[] = "abc"; | 189 const char data[] = "abc"; |
181 ep1_.SendData(data, sizeof(data)); | 190 ep1_.SendData(data, sizeof(data)); |
182 EXPECT_EQ_WAIT(1u, ep1_.ch_packets_.size(), kTimeoutMs); | 191 EXPECT_EQ_WAIT(1u, ep1_.ch_packets_.size(), kTimeoutMs); |
183 EXPECT_EQ_WAIT(1u, ep1_.num_sig_sent_packets_, kTimeoutMs); | 192 EXPECT_EQ_WAIT(1u, ep1_.num_sig_sent_packets_, kTimeoutMs); |
184 } | 193 } |
| 194 |
185 } // namespace cricket | 195 } // namespace cricket |
OLD | NEW |