henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 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 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 14 | #include "webrtc/p2p/base/relayport.h" |
| 15 | #include "webrtc/p2p/base/relayserver.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame^] | 16 | #include "webrtc/rtc_base/gunit.h" |
| 17 | #include "webrtc/rtc_base/helpers.h" |
| 18 | #include "webrtc/rtc_base/logging.h" |
| 19 | #include "webrtc/rtc_base/socketadapters.h" |
| 20 | #include "webrtc/rtc_base/socketaddress.h" |
| 21 | #include "webrtc/rtc_base/ssladapter.h" |
| 22 | #include "webrtc/rtc_base/thread.h" |
| 23 | #include "webrtc/rtc_base/virtualsocketserver.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 24 | |
| 25 | using rtc::SocketAddress; |
| 26 | |
| 27 | static const SocketAddress kLocalAddress = SocketAddress("192.168.1.2", 0); |
| 28 | static const SocketAddress kRelayUdpAddr = SocketAddress("99.99.99.1", 5000); |
| 29 | static const SocketAddress kRelayTcpAddr = SocketAddress("99.99.99.2", 5001); |
| 30 | static const SocketAddress kRelaySslAddr = SocketAddress("99.99.99.3", 443); |
| 31 | static const SocketAddress kRelayExtAddr = SocketAddress("99.99.99.3", 5002); |
| 32 | |
| 33 | static const int kTimeoutMs = 1000; |
| 34 | static const int kMaxTimeoutMs = 5000; |
| 35 | |
| 36 | // Tests connecting a RelayPort to a fake relay server |
| 37 | // (cricket::RelayServer) using all currently available protocols. The |
| 38 | // network layer is faked out by using a VirtualSocketServer for |
| 39 | // creating sockets. The test will monitor the current state of the |
| 40 | // RelayPort and created sockets by listening for signals such as, |
| 41 | // SignalConnectFailure, SignalConnectTimeout, SignalSocketClosed and |
| 42 | // SignalReadPacket. |
| 43 | class RelayPortTest : public testing::Test, |
| 44 | public sigslot::has_slots<> { |
| 45 | public: |
| 46 | RelayPortTest() |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 47 | : virtual_socket_server_(new rtc::VirtualSocketServer()), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 48 | main_(virtual_socket_server_.get()), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 49 | network_("unittest", "unittest", rtc::IPAddress(INADDR_ANY), 32), |
| 50 | socket_factory_(rtc::Thread::Current()), |
| 51 | username_(rtc::CreateRandomString(16)), |
| 52 | password_(rtc::CreateRandomString(16)), |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 53 | relay_port_(cricket::RelayPort::Create(&main_, |
| 54 | &socket_factory_, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 55 | &network_, |
| 56 | kLocalAddress.ipaddr(), |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 57 | 0, |
| 58 | 0, |
| 59 | username_, |
| 60 | password_)), |
| 61 | relay_server_(new cricket::RelayServer(&main_)) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | |
| 63 | void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 64 | const char* data, size_t size, |
| 65 | const rtc::SocketAddress& remote_addr, |
| 66 | const rtc::PacketTime& packet_time) { |
| 67 | received_packet_count_[socket]++; |
| 68 | } |
| 69 | |
| 70 | void OnConnectFailure(const cricket::ProtocolAddress* addr) { |
| 71 | failed_connections_.push_back(*addr); |
| 72 | } |
| 73 | |
| 74 | void OnSoftTimeout(const cricket::ProtocolAddress* addr) { |
| 75 | soft_timedout_connections_.push_back(*addr); |
| 76 | } |
| 77 | |
| 78 | protected: |
| 79 | virtual void SetUp() { |
| 80 | // The relay server needs an external socket to work properly. |
| 81 | rtc::AsyncUDPSocket* ext_socket = |
| 82 | CreateAsyncUdpSocket(kRelayExtAddr); |
| 83 | relay_server_->AddExternalSocket(ext_socket); |
| 84 | |
| 85 | // Listen for failures. |
| 86 | relay_port_->SignalConnectFailure. |
| 87 | connect(this, &RelayPortTest::OnConnectFailure); |
| 88 | |
| 89 | // Listen for soft timeouts. |
| 90 | relay_port_->SignalSoftTimeout. |
| 91 | connect(this, &RelayPortTest::OnSoftTimeout); |
| 92 | } |
| 93 | |
| 94 | // Udp has the highest 'goodness' value of the three different |
| 95 | // protocols used for connecting to the relay server. As soon as |
| 96 | // PrepareAddress is called, the RelayPort will start trying to |
| 97 | // connect to the given UDP address. As soon as a response to the |
| 98 | // sent STUN allocate request message has been received, the |
| 99 | // RelayPort will consider the connection to be complete and will |
| 100 | // abort any other connection attempts. |
| 101 | void TestConnectUdp() { |
| 102 | // Add a UDP socket to the relay server. |
| 103 | rtc::AsyncUDPSocket* internal_udp_socket = |
| 104 | CreateAsyncUdpSocket(kRelayUdpAddr); |
| 105 | rtc::AsyncSocket* server_socket = CreateServerSocket(kRelayTcpAddr); |
| 106 | |
| 107 | relay_server_->AddInternalSocket(internal_udp_socket); |
| 108 | relay_server_->AddInternalServerSocket(server_socket, cricket::PROTO_TCP); |
| 109 | |
| 110 | // Now add our relay addresses to the relay port and let it start. |
| 111 | relay_port_->AddServerAddress( |
| 112 | cricket::ProtocolAddress(kRelayUdpAddr, cricket::PROTO_UDP)); |
| 113 | relay_port_->AddServerAddress( |
| 114 | cricket::ProtocolAddress(kRelayTcpAddr, cricket::PROTO_TCP)); |
| 115 | relay_port_->PrepareAddress(); |
| 116 | |
| 117 | // Should be connected. |
| 118 | EXPECT_TRUE_WAIT(relay_port_->IsReady(), kTimeoutMs); |
| 119 | |
| 120 | // Make sure that we are happy with UDP, ie. not continuing with |
| 121 | // TCP, SSLTCP, etc. |
| 122 | WAIT(relay_server_->HasConnection(kRelayTcpAddr), kTimeoutMs); |
| 123 | |
| 124 | // Should have only one connection. |
| 125 | EXPECT_EQ(1, relay_server_->GetConnectionCount()); |
| 126 | |
| 127 | // Should be the UDP address. |
| 128 | EXPECT_TRUE(relay_server_->HasConnection(kRelayUdpAddr)); |
| 129 | } |
| 130 | |
| 131 | // TCP has the second best 'goodness' value, and as soon as UDP |
| 132 | // connection has failed, the RelayPort will attempt to connect via |
| 133 | // TCP. Here we add a fake UDP address together with a real TCP |
| 134 | // address to simulate an UDP failure. As soon as UDP has failed the |
| 135 | // RelayPort will try the TCP adress and succed. |
| 136 | void TestConnectTcp() { |
| 137 | // Create a fake UDP address for relay port to simulate a failure. |
| 138 | cricket::ProtocolAddress fake_protocol_address = |
| 139 | cricket::ProtocolAddress(kRelayUdpAddr, cricket::PROTO_UDP); |
| 140 | |
| 141 | // Create a server socket for the RelayServer. |
| 142 | rtc::AsyncSocket* server_socket = CreateServerSocket(kRelayTcpAddr); |
| 143 | relay_server_->AddInternalServerSocket(server_socket, cricket::PROTO_TCP); |
| 144 | |
| 145 | // Add server addresses to the relay port and let it start. |
| 146 | relay_port_->AddServerAddress( |
| 147 | cricket::ProtocolAddress(fake_protocol_address)); |
| 148 | relay_port_->AddServerAddress( |
| 149 | cricket::ProtocolAddress(kRelayTcpAddr, cricket::PROTO_TCP)); |
| 150 | relay_port_->PrepareAddress(); |
| 151 | |
| 152 | EXPECT_FALSE(relay_port_->IsReady()); |
| 153 | |
deadbeef | 4c2db41 | 2016-05-26 14:34:09 -0700 | [diff] [blame] | 154 | // Should have timed out in 200 + 200 + 400 + 800 + 1600 ms = 3200ms. |
| 155 | // Add some margin of error for slow bots. |
| 156 | // TODO(deadbeef): Use simulated clock instead of just increasing timeouts |
| 157 | // to fix flaky tests. |
| 158 | EXPECT_TRUE_WAIT(HasFailed(&fake_protocol_address), 5000); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | |
| 160 | // Wait until relayport is ready. |
| 161 | EXPECT_TRUE_WAIT(relay_port_->IsReady(), kMaxTimeoutMs); |
| 162 | |
| 163 | // Should have only one connection. |
| 164 | EXPECT_EQ(1, relay_server_->GetConnectionCount()); |
| 165 | |
| 166 | // Should be the TCP address. |
| 167 | EXPECT_TRUE(relay_server_->HasConnection(kRelayTcpAddr)); |
| 168 | } |
| 169 | |
| 170 | void TestConnectSslTcp() { |
| 171 | // Create a fake TCP address for relay port to simulate a failure. |
| 172 | // We skip UDP here since transition from UDP to TCP has been |
| 173 | // tested above. |
| 174 | cricket::ProtocolAddress fake_protocol_address = |
| 175 | cricket::ProtocolAddress(kRelayTcpAddr, cricket::PROTO_TCP); |
| 176 | |
| 177 | // Create a ssl server socket for the RelayServer. |
| 178 | rtc::AsyncSocket* ssl_server_socket = |
| 179 | CreateServerSocket(kRelaySslAddr); |
| 180 | relay_server_->AddInternalServerSocket(ssl_server_socket, |
| 181 | cricket::PROTO_SSLTCP); |
| 182 | |
| 183 | // Create a tcp server socket that listens on the fake address so |
| 184 | // the relay port can attempt to connect to it. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 185 | std::unique_ptr<rtc::AsyncSocket> tcp_server_socket( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 186 | CreateServerSocket(kRelayTcpAddr)); |
| 187 | |
| 188 | // Add server addresses to the relay port and let it start. |
| 189 | relay_port_->AddServerAddress(fake_protocol_address); |
| 190 | relay_port_->AddServerAddress( |
| 191 | cricket::ProtocolAddress(kRelaySslAddr, cricket::PROTO_SSLTCP)); |
| 192 | relay_port_->PrepareAddress(); |
| 193 | EXPECT_FALSE(relay_port_->IsReady()); |
| 194 | |
| 195 | // Should have timed out in 3000 ms(relayport.cc, kSoftConnectTimeoutMs). |
| 196 | EXPECT_TRUE_WAIT_MARGIN(HasTimedOut(&fake_protocol_address), 3000, 100); |
| 197 | |
| 198 | // Wait until relayport is ready. |
| 199 | EXPECT_TRUE_WAIT(relay_port_->IsReady(), kMaxTimeoutMs); |
| 200 | |
| 201 | // Should have only one connection. |
| 202 | EXPECT_EQ(1, relay_server_->GetConnectionCount()); |
| 203 | |
| 204 | // Should be the SSLTCP address. |
| 205 | EXPECT_TRUE(relay_server_->HasConnection(kRelaySslAddr)); |
| 206 | } |
| 207 | |
| 208 | private: |
| 209 | rtc::AsyncUDPSocket* CreateAsyncUdpSocket(const SocketAddress addr) { |
| 210 | rtc::AsyncSocket* socket = |
| 211 | virtual_socket_server_->CreateAsyncSocket(SOCK_DGRAM); |
| 212 | rtc::AsyncUDPSocket* packet_socket = |
| 213 | rtc::AsyncUDPSocket::Create(socket, addr); |
| 214 | EXPECT_TRUE(packet_socket != NULL); |
| 215 | packet_socket->SignalReadPacket.connect(this, &RelayPortTest::OnReadPacket); |
| 216 | return packet_socket; |
| 217 | } |
| 218 | |
| 219 | rtc::AsyncSocket* CreateServerSocket(const SocketAddress addr) { |
| 220 | rtc::AsyncSocket* socket = |
| 221 | virtual_socket_server_->CreateAsyncSocket(SOCK_STREAM); |
| 222 | EXPECT_GE(socket->Bind(addr), 0); |
| 223 | EXPECT_GE(socket->Listen(5), 0); |
| 224 | return socket; |
| 225 | } |
| 226 | |
| 227 | bool HasFailed(cricket::ProtocolAddress* addr) { |
| 228 | for (size_t i = 0; i < failed_connections_.size(); i++) { |
| 229 | if (failed_connections_[i].address == addr->address && |
| 230 | failed_connections_[i].proto == addr->proto) { |
| 231 | return true; |
| 232 | } |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | bool HasTimedOut(cricket::ProtocolAddress* addr) { |
| 238 | for (size_t i = 0; i < soft_timedout_connections_.size(); i++) { |
| 239 | if (soft_timedout_connections_[i].address == addr->address && |
| 240 | soft_timedout_connections_[i].proto == addr->proto) { |
| 241 | return true; |
| 242 | } |
| 243 | } |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | typedef std::map<rtc::AsyncPacketSocket*, int> PacketMap; |
| 248 | |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 249 | std::unique_ptr<rtc::VirtualSocketServer> virtual_socket_server_; |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 250 | rtc::AutoSocketServerThread main_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 251 | rtc::Network network_; |
| 252 | rtc::BasicPacketSocketFactory socket_factory_; |
| 253 | std::string username_; |
| 254 | std::string password_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 255 | std::unique_ptr<cricket::RelayPort> relay_port_; |
| 256 | std::unique_ptr<cricket::RelayServer> relay_server_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 257 | std::vector<cricket::ProtocolAddress> failed_connections_; |
| 258 | std::vector<cricket::ProtocolAddress> soft_timedout_connections_; |
| 259 | PacketMap received_packet_count_; |
| 260 | }; |
| 261 | |
| 262 | TEST_F(RelayPortTest, ConnectUdp) { |
| 263 | TestConnectUdp(); |
| 264 | } |
| 265 | |
| 266 | TEST_F(RelayPortTest, ConnectTcp) { |
| 267 | TestConnectTcp(); |
| 268 | } |
| 269 | |
| 270 | TEST_F(RelayPortTest, ConnectSslTcp) { |
| 271 | TestConnectSslTcp(); |
| 272 | } |