1 /*
2 * Copyright 2015 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 "p2p/stunprober/stun_prober.h"
12
13 #include <stdint.h>
14
15 #include <memory>
16 #include <utility>
17
18 #include "p2p/base/basic_packet_socket_factory.h"
19 #include "p2p/base/test_stun_server.h"
20 #include "rtc_base/gunit.h"
21 #include "rtc_base/ip_address.h"
22 #include "rtc_base/ssl_adapter.h"
23 #include "rtc_base/virtual_socket_server.h"
24 #include "test/gtest.h"
25
26 using stunprober::AsyncCallback;
27 using stunprober::StunProber;
28
29 namespace stunprober {
30
31 namespace {
32
33 const rtc::SocketAddress kLocalAddr("192.168.0.1", 0);
34 const rtc::SocketAddress kStunAddr1("1.1.1.1", 3478);
35 const rtc::SocketAddress kStunAddr2("1.1.1.2", 3478);
36 const rtc::SocketAddress kFailedStunAddr("1.1.1.3", 3478);
37 const rtc::SocketAddress kStunMappedAddr("77.77.77.77", 0);
38
39 } // namespace
40
41 class StunProberTest : public ::testing::Test {
42 public:
StunProberTest()43 StunProberTest()
44 : ss_(std::make_unique<rtc::VirtualSocketServer>()),
45 main_(ss_.get()),
46 result_(StunProber::SUCCESS),
47 stun_server_1_(cricket::TestStunServer::Create(ss_.get(), kStunAddr1)),
48 stun_server_2_(cricket::TestStunServer::Create(ss_.get(), kStunAddr2)) {
49 stun_server_1_->set_fake_stun_addr(kStunMappedAddr);
50 stun_server_2_->set_fake_stun_addr(kStunMappedAddr);
51 rtc::InitializeSSL();
52 }
53
set_expected_result(int result)54 void set_expected_result(int result) { result_ = result; }
55
StartProbing(rtc::PacketSocketFactory * socket_factory,const std::vector<rtc::SocketAddress> & addrs,std::vector<const rtc::Network * > networks,bool shared_socket,uint16_t interval,uint16_t pings_per_ip)56 void StartProbing(rtc::PacketSocketFactory* socket_factory,
57 const std::vector<rtc::SocketAddress>& addrs,
58 std::vector<const rtc::Network*> networks,
59 bool shared_socket,
60 uint16_t interval,
61 uint16_t pings_per_ip) {
62 prober_ = std::make_unique<StunProber>(
63 socket_factory, rtc::Thread::Current(), std::move(networks));
64 prober_->Start(addrs, shared_socket, interval, pings_per_ip,
65 100 /* timeout_ms */,
66 [this](StunProber* prober, int result) {
67 StopCallback(prober, result);
68 });
69 }
70
RunProber(bool shared_mode)71 void RunProber(bool shared_mode) {
72 const int pings_per_ip = 3;
73 std::vector<rtc::SocketAddress> addrs;
74 addrs.push_back(kStunAddr1);
75 addrs.push_back(kStunAddr2);
76 // Add a non-existing server. This shouldn't pollute the result.
77 addrs.push_back(kFailedStunAddr);
78
79 rtc::Network ipv4_network1("test_eth0", "Test Network Adapter 1",
80 rtc::IPAddress(0x12345600U), 24);
81 ipv4_network1.AddIP(rtc::IPAddress(0x12345678));
82 std::vector<const rtc::Network*> networks;
83 networks.push_back(&ipv4_network1);
84
85 auto socket_factory =
86 std::make_unique<rtc::BasicPacketSocketFactory>(ss_.get());
87
88 // Set up the expected results for verification.
89 std::set<std::string> srflx_addresses;
90 srflx_addresses.insert(kStunMappedAddr.ToString());
91 const uint32_t total_pings_tried =
92 static_cast<uint32_t>(pings_per_ip * addrs.size());
93
94 // The reported total_pings should not count for pings sent to the
95 // kFailedStunAddr.
96 const uint32_t total_pings_reported = total_pings_tried - pings_per_ip;
97
98 StartProbing(socket_factory.get(), addrs, std::move(networks), shared_mode,
99 3, pings_per_ip);
100
101 WAIT(stopped_, 1000);
102
103 StunProber::Stats stats;
104 EXPECT_TRUE(prober_->GetStats(&stats));
105 EXPECT_EQ(stats.success_percent, 100);
106 EXPECT_TRUE(stats.nat_type > stunprober::NATTYPE_NONE);
107 EXPECT_EQ(stats.srflx_addrs, srflx_addresses);
108 EXPECT_EQ(static_cast<uint32_t>(stats.num_request_sent),
109 total_pings_reported);
110 EXPECT_EQ(static_cast<uint32_t>(stats.num_response_received),
111 total_pings_reported);
112 }
113
114 private:
StopCallback(StunProber * prober,int result)115 void StopCallback(StunProber* prober, int result) {
116 EXPECT_EQ(result, result_);
117 stopped_ = true;
118 }
119
120 std::unique_ptr<rtc::VirtualSocketServer> ss_;
121 rtc::AutoSocketServerThread main_;
122 std::unique_ptr<StunProber> prober_;
123 int result_ = 0;
124 bool stopped_ = false;
125 std::unique_ptr<cricket::TestStunServer> stun_server_1_;
126 std::unique_ptr<cricket::TestStunServer> stun_server_2_;
127 };
128
TEST_F(StunProberTest,NonSharedMode)129 TEST_F(StunProberTest, NonSharedMode) {
130 RunProber(false);
131 }
132
TEST_F(StunProberTest,SharedMode)133 TEST_F(StunProberTest, SharedMode) {
134 RunProber(true);
135 }
136
137 } // namespace stunprober
138