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