| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 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 "webrtc/base/asyncresolverinterface.h" |
| 11 #include "webrtc/base/basictypes.h" | 12 #include "webrtc/base/basictypes.h" |
| 12 #include "webrtc/base/bind.h" | 13 #include "webrtc/base/bind.h" |
| 13 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/gunit.h" | 15 #include "webrtc/base/gunit.h" |
| 15 #include "webrtc/base/physicalsocketserver.h" | 16 #include "webrtc/base/physicalsocketserver.h" |
| 16 #include "webrtc/base/scoped_ptr.h" | 17 #include "webrtc/base/scoped_ptr.h" |
| 17 #include "webrtc/base/ssladapter.h" | 18 #include "webrtc/base/ssladapter.h" |
| 18 #include "webrtc/base/virtualsocketserver.h" | 19 #include "webrtc/base/virtualsocketserver.h" |
| 19 | 20 #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
| 20 #include "webrtc/p2p/base/teststunserver.h" | 21 #include "webrtc/p2p/base/teststunserver.h" |
| 21 #include "webrtc/p2p/stunprober/stunprober.h" | 22 #include "webrtc/p2p/stunprober/stunprober.h" |
| 22 #include "webrtc/p2p/stunprober/stunprober_dependencies.h" | |
| 23 | 23 |
| 24 using stunprober::HostNameResolverInterface; | |
| 25 using stunprober::TaskRunner; | |
| 26 using stunprober::SocketFactory; | |
| 27 using stunprober::StunProber; | 24 using stunprober::StunProber; |
| 28 using stunprober::AsyncCallback; | 25 using stunprober::AsyncCallback; |
| 29 using stunprober::ClientSocketInterface; | |
| 30 using stunprober::ServerSocketInterface; | |
| 31 using stunprober::SocketFactory; | |
| 32 using stunprober::TaskRunner; | |
| 33 | 26 |
| 34 namespace stunprober { | 27 namespace stunprober { |
| 35 | 28 |
| 36 namespace { | 29 namespace { |
| 37 | 30 |
| 38 const rtc::SocketAddress kLocalAddr("192.168.0.1", 0); | 31 const rtc::SocketAddress kLocalAddr("192.168.0.1", 0); |
| 39 const rtc::SocketAddress kStunAddr1("1.1.1.1", 3478); | 32 const rtc::SocketAddress kStunAddr1("1.1.1.1", 3478); |
| 40 const rtc::SocketAddress kStunAddr2("1.1.1.2", 3478); | 33 const rtc::SocketAddress kStunAddr2("1.1.1.2", 3478); |
| 41 const rtc::SocketAddress kFailedStunAddr("1.1.1.3", 3478); | 34 const rtc::SocketAddress kFailedStunAddr("1.1.1.3", 3478); |
| 42 const rtc::SocketAddress kStunMappedAddr("77.77.77.77", 0); | 35 const rtc::SocketAddress kStunMappedAddr("77.77.77.77", 0); |
| 43 | 36 |
| 44 class TestSocketServer : public rtc::VirtualSocketServer { | |
| 45 public: | |
| 46 using rtc::VirtualSocketServer::CreateAsyncSocket; | |
| 47 explicit TestSocketServer(SocketServer* ss) : rtc::VirtualSocketServer(ss) {} | |
| 48 void SetLocalAddress(const rtc::SocketAddress& addr) { addr_ = addr; } | |
| 49 | |
| 50 // CreateAsyncSocket is used by StunProber to create both client and server | |
| 51 // sockets. The first socket is used to retrieve local address which will be | |
| 52 // used later for Bind(). | |
| 53 rtc::AsyncSocket* CreateAsyncSocket(int type) override { | |
| 54 rtc::VirtualSocket* socket = static_cast<rtc::VirtualSocket*>( | |
| 55 rtc::VirtualSocketServer::CreateAsyncSocket(type)); | |
| 56 if (!local_addr_set_) { | |
| 57 // Only the first socket can SetLocalAddress. For others, Bind will fail | |
| 58 // if local address is set. | |
| 59 socket->SetLocalAddress(addr_); | |
| 60 local_addr_set_ = true; | |
| 61 } else { | |
| 62 sockets_.push_back(socket); | |
| 63 } | |
| 64 return socket; | |
| 65 } | |
| 66 | |
| 67 size_t num_socket() { return sockets_.size(); } | |
| 68 | |
| 69 private: | |
| 70 bool local_addr_set_ = false; | |
| 71 std::vector<rtc::VirtualSocket*> sockets_; | |
| 72 rtc::SocketAddress addr_; | |
| 73 }; | |
| 74 | |
| 75 class FakeHostNameResolver : public HostNameResolverInterface { | |
| 76 public: | |
| 77 FakeHostNameResolver() {} | |
| 78 void set_result(int ret) { ret_ = ret; } | |
| 79 void set_addresses(const std::vector<rtc::SocketAddress>& addresses) { | |
| 80 server_ips_ = addresses; | |
| 81 } | |
| 82 const std::vector<rtc::SocketAddress>& get_addresses() { return server_ips_; } | |
| 83 void add_address(const rtc::SocketAddress& ip) { server_ips_.push_back(ip); } | |
| 84 void Resolve(const rtc::SocketAddress& addr, | |
| 85 std::vector<rtc::SocketAddress>* addresses, | |
| 86 stunprober::AsyncCallback callback) override { | |
| 87 *addresses = server_ips_; | |
| 88 callback(ret_); | |
| 89 } | |
| 90 | |
| 91 private: | |
| 92 int ret_ = 0; | |
| 93 std::vector<rtc::SocketAddress> server_ips_; | |
| 94 }; | |
| 95 | |
| 96 } // namespace | 37 } // namespace |
| 97 | 38 |
| 98 class StunProberTest : public testing::Test { | 39 class StunProberTest : public testing::Test { |
| 99 public: | 40 public: |
| 100 StunProberTest() | 41 StunProberTest() |
| 101 : main_(rtc::Thread::Current()), | 42 : main_(rtc::Thread::Current()), |
| 102 pss_(new rtc::PhysicalSocketServer), | 43 pss_(new rtc::PhysicalSocketServer), |
| 103 ss_(new TestSocketServer(pss_.get())), | 44 ss_(new rtc::VirtualSocketServer(pss_.get())), |
| 104 ss_scope_(ss_.get()), | 45 ss_scope_(ss_.get()), |
| 105 result_(StunProber::SUCCESS), | 46 result_(StunProber::SUCCESS), |
| 106 stun_server_1_(cricket::TestStunServer::Create(rtc::Thread::Current(), | 47 stun_server_1_(cricket::TestStunServer::Create(rtc::Thread::Current(), |
| 107 kStunAddr1)), | 48 kStunAddr1)), |
| 108 stun_server_2_(cricket::TestStunServer::Create(rtc::Thread::Current(), | 49 stun_server_2_(cricket::TestStunServer::Create(rtc::Thread::Current(), |
| 109 kStunAddr2)) { | 50 kStunAddr2)) { |
| 110 stun_server_1_->set_fake_stun_addr(kStunMappedAddr); | 51 stun_server_1_->set_fake_stun_addr(kStunMappedAddr); |
| 111 stun_server_2_->set_fake_stun_addr(kStunMappedAddr); | 52 stun_server_2_->set_fake_stun_addr(kStunMappedAddr); |
| 112 rtc::InitializeSSL(); | 53 rtc::InitializeSSL(); |
| 113 } | 54 } |
| 114 | 55 |
| 115 void set_expected_result(int result) { result_ = result; } | 56 void set_expected_result(int result) { result_ = result; } |
| 116 | 57 |
| 117 void StartProbing(HostNameResolverInterface* resolver, | 58 void StartProbing(rtc::PacketSocketFactory* socket_factory, |
| 118 SocketFactoryInterface* socket_factory, | 59 const std::vector<rtc::SocketAddress>& addrs, |
| 119 const rtc::SocketAddress& addr, | 60 const rtc::NetworkManager::NetworkList& networks, |
| 120 bool shared_socket, | 61 bool shared_socket, |
| 121 uint16 interval, | 62 uint16 interval, |
| 122 uint16 pings_per_ip) { | 63 uint16 pings_per_ip) { |
| 123 std::vector<rtc::SocketAddress> addrs; | 64 prober.reset( |
| 124 addrs.push_back(addr); | 65 new StunProber(socket_factory, rtc::Thread::Current(), networks)); |
| 125 prober.reset(new StunProber(resolver, socket_factory, new TaskRunner())); | |
| 126 prober->Start(addrs, shared_socket, interval, pings_per_ip, | 66 prober->Start(addrs, shared_socket, interval, pings_per_ip, |
| 127 100 /* timeout_ms */, | 67 100 /* timeout_ms */, [this](StunProber* prober, int result) { |
| 128 [this](int result) { this->StopCallback(result); }); | 68 this->StopCallback(prober, result); |
| 69 }); |
| 129 } | 70 } |
| 130 | 71 |
| 131 void RunProber(bool shared_mode) { | 72 void RunProber(bool shared_mode) { |
| 132 const int pings_per_ip = 3; | 73 const int pings_per_ip = 3; |
| 133 const uint16 port = kStunAddr1.port(); | |
| 134 rtc::SocketAddress addr("stun.l.google.com", port); | |
| 135 std::vector<rtc::SocketAddress> addrs; | 74 std::vector<rtc::SocketAddress> addrs; |
| 75 addrs.push_back(kStunAddr1); |
| 76 addrs.push_back(kStunAddr2); |
| 77 // Add a non-existing server. This shouldn't pollute the result. |
| 78 addrs.push_back(kFailedStunAddr); |
| 136 | 79 |
| 137 // Set up the resolver for 2 stun server addresses. | 80 rtc::Network ipv4_network1("test_eth0", "Test Network Adapter 1", |
| 138 rtc::scoped_ptr<FakeHostNameResolver> resolver(new FakeHostNameResolver()); | 81 rtc::IPAddress(0x12345600U), 24); |
| 139 resolver->add_address(kStunAddr1); | 82 ipv4_network1.AddIP(rtc::IPAddress(0x12345678)); |
| 140 resolver->add_address(kStunAddr2); | 83 rtc::NetworkManager::NetworkList networks; |
| 141 // Add a non-existing server. This shouldn't pollute the result. | 84 networks.push_back(&ipv4_network1); |
| 142 resolver->add_address(kFailedStunAddr); | |
| 143 | 85 |
| 144 rtc::scoped_ptr<SocketFactory> socket_factory(new SocketFactory()); | 86 rtc::scoped_ptr<rtc::BasicPacketSocketFactory> socket_factory( |
| 145 | 87 new rtc::BasicPacketSocketFactory()); |
| 146 // Set local address in socketserver so getsockname will return kLocalAddr | |
| 147 // instead of 0.0.0.0 for the first socket. | |
| 148 ss_->SetLocalAddress(kLocalAddr); | |
| 149 | 88 |
| 150 // Set up the expected results for verification. | 89 // Set up the expected results for verification. |
| 151 std::set<std::string> srflx_addresses; | 90 std::set<std::string> srflx_addresses; |
| 152 srflx_addresses.insert(kStunMappedAddr.ToString()); | 91 srflx_addresses.insert(kStunMappedAddr.ToString()); |
| 153 const uint32 total_pings_tried = | 92 const uint32 total_pings_tried = |
| 154 static_cast<uint32>(pings_per_ip * resolver->get_addresses().size()); | 93 static_cast<uint32>(pings_per_ip * addrs.size()); |
| 155 | 94 |
| 156 // The reported total_pings should not count for pings sent to the | 95 // The reported total_pings should not count for pings sent to the |
| 157 // kFailedStunAddr. | 96 // kFailedStunAddr. |
| 158 const uint32 total_pings_reported = total_pings_tried - pings_per_ip; | 97 const uint32 total_pings_reported = total_pings_tried - pings_per_ip; |
| 159 | 98 |
| 160 size_t total_sockets = shared_mode ? pings_per_ip : total_pings_tried; | 99 StartProbing(socket_factory.get(), addrs, networks, shared_mode, 3, |
| 161 | 100 pings_per_ip); |
| 162 StartProbing(resolver.release(), socket_factory.release(), addr, | |
| 163 shared_mode, 3, pings_per_ip); | |
| 164 | 101 |
| 165 WAIT(stopped_, 1000); | 102 WAIT(stopped_, 1000); |
| 166 | 103 |
| 167 StunProber::Stats stats; | 104 StunProber::Stats stats; |
| 168 EXPECT_EQ(ss_->num_socket(), total_sockets); | |
| 169 EXPECT_TRUE(prober->GetStats(&stats)); | 105 EXPECT_TRUE(prober->GetStats(&stats)); |
| 170 EXPECT_EQ(stats.success_percent, 100); | 106 EXPECT_EQ(stats.success_percent, 100); |
| 171 EXPECT_TRUE(stats.nat_type > stunprober::NATTYPE_NONE); | 107 EXPECT_TRUE(stats.nat_type > stunprober::NATTYPE_NONE); |
| 172 EXPECT_EQ(stats.host_ip, kLocalAddr.ipaddr().ToString()); | |
| 173 EXPECT_EQ(stats.srflx_addrs, srflx_addresses); | 108 EXPECT_EQ(stats.srflx_addrs, srflx_addresses); |
| 174 EXPECT_EQ(static_cast<uint32>(stats.num_request_sent), | 109 EXPECT_EQ(static_cast<uint32>(stats.num_request_sent), |
| 175 total_pings_reported); | 110 total_pings_reported); |
| 176 EXPECT_EQ(static_cast<uint32>(stats.num_response_received), | 111 EXPECT_EQ(static_cast<uint32>(stats.num_response_received), |
| 177 total_pings_reported); | 112 total_pings_reported); |
| 178 } | 113 } |
| 179 | 114 |
| 180 private: | 115 private: |
| 181 void StopCallback(int result) { | 116 void StopCallback(StunProber* prober, int result) { |
| 182 EXPECT_EQ(result, result_); | 117 EXPECT_EQ(result, result_); |
| 183 stopped_ = true; | 118 stopped_ = true; |
| 184 } | 119 } |
| 185 | 120 |
| 186 rtc::Thread* main_; | 121 rtc::Thread* main_; |
| 187 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; | 122 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_; |
| 188 rtc::scoped_ptr<TestSocketServer> ss_; | 123 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_; |
| 189 rtc::SocketServerScope ss_scope_; | 124 rtc::SocketServerScope ss_scope_; |
| 190 rtc::scoped_ptr<StunProber> prober; | 125 rtc::scoped_ptr<StunProber> prober; |
| 191 int result_ = 0; | 126 int result_ = 0; |
| 192 bool stopped_ = false; | 127 bool stopped_ = false; |
| 193 rtc::scoped_ptr<cricket::TestStunServer> stun_server_1_; | 128 rtc::scoped_ptr<cricket::TestStunServer> stun_server_1_; |
| 194 rtc::scoped_ptr<cricket::TestStunServer> stun_server_2_; | 129 rtc::scoped_ptr<cricket::TestStunServer> stun_server_2_; |
| 195 }; | 130 }; |
| 196 | 131 |
| 197 TEST_F(StunProberTest, DNSFailure) { | |
| 198 rtc::SocketAddress addr("stun.l.google.com", 19302); | |
| 199 rtc::scoped_ptr<FakeHostNameResolver> resolver(new FakeHostNameResolver()); | |
| 200 rtc::scoped_ptr<SocketFactory> socket_factory(new SocketFactory()); | |
| 201 | |
| 202 set_expected_result(StunProber::RESOLVE_FAILED); | |
| 203 | |
| 204 // Non-0 value is treated as failure. | |
| 205 resolver->set_result(1); | |
| 206 StartProbing(resolver.release(), socket_factory.release(), addr, false, 10, | |
| 207 30); | |
| 208 } | |
| 209 | |
| 210 TEST_F(StunProberTest, NonSharedMode) { | 132 TEST_F(StunProberTest, NonSharedMode) { |
| 211 RunProber(false); | 133 RunProber(false); |
| 212 } | 134 } |
| 213 | 135 |
| 214 TEST_F(StunProberTest, SharedMode) { | 136 TEST_F(StunProberTest, SharedMode) { |
| 215 RunProber(true); | 137 RunProber(true); |
| 216 } | 138 } |
| 217 | 139 |
| 218 } // namespace stunprober | 140 } // namespace stunprober |
| OLD | NEW |