xref: /aosp_15_r20/external/webrtc/rtc_base/socket_unittest.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2009 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 #ifndef RTC_BASE_SOCKET_UNITTEST_H_
12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_SOCKET_UNITTEST_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
15*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/gunit.h"
16*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread.h"
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker namespace rtc {
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker // Generic socket tests, to be used when testing individual socketservers.
21*d9f75844SAndroid Build Coastguard Worker // Derive your specific test class from SocketTest, install your
22*d9f75844SAndroid Build Coastguard Worker // socketserver, and call the SocketTest test methods.
23*d9f75844SAndroid Build Coastguard Worker class SocketTest : public ::testing::Test {
24*d9f75844SAndroid Build Coastguard Worker  protected:
SocketTest(rtc::SocketFactory * socket_factory)25*d9f75844SAndroid Build Coastguard Worker   explicit SocketTest(rtc::SocketFactory* socket_factory)
26*d9f75844SAndroid Build Coastguard Worker       : kIPv4Loopback(INADDR_LOOPBACK),
27*d9f75844SAndroid Build Coastguard Worker         kIPv6Loopback(in6addr_loopback),
28*d9f75844SAndroid Build Coastguard Worker         socket_factory_(socket_factory) {}
29*d9f75844SAndroid Build Coastguard Worker   void TestConnectIPv4();
30*d9f75844SAndroid Build Coastguard Worker   void TestConnectIPv6();
31*d9f75844SAndroid Build Coastguard Worker   void TestConnectWithDnsLookupIPv4();
32*d9f75844SAndroid Build Coastguard Worker   void TestConnectWithDnsLookupIPv6();
33*d9f75844SAndroid Build Coastguard Worker   void TestConnectFailIPv4();
34*d9f75844SAndroid Build Coastguard Worker   void TestConnectFailIPv6();
35*d9f75844SAndroid Build Coastguard Worker   void TestConnectWithDnsLookupFailIPv4();
36*d9f75844SAndroid Build Coastguard Worker   void TestConnectWithDnsLookupFailIPv6();
37*d9f75844SAndroid Build Coastguard Worker   void TestConnectWithClosedSocketIPv4();
38*d9f75844SAndroid Build Coastguard Worker   void TestConnectWithClosedSocketIPv6();
39*d9f75844SAndroid Build Coastguard Worker   void TestConnectWhileNotClosedIPv4();
40*d9f75844SAndroid Build Coastguard Worker   void TestConnectWhileNotClosedIPv6();
41*d9f75844SAndroid Build Coastguard Worker   void TestServerCloseDuringConnectIPv4();
42*d9f75844SAndroid Build Coastguard Worker   void TestServerCloseDuringConnectIPv6();
43*d9f75844SAndroid Build Coastguard Worker   void TestClientCloseDuringConnectIPv4();
44*d9f75844SAndroid Build Coastguard Worker   void TestClientCloseDuringConnectIPv6();
45*d9f75844SAndroid Build Coastguard Worker   void TestServerCloseIPv4();
46*d9f75844SAndroid Build Coastguard Worker   void TestServerCloseIPv6();
47*d9f75844SAndroid Build Coastguard Worker   void TestCloseInClosedCallbackIPv4();
48*d9f75844SAndroid Build Coastguard Worker   void TestCloseInClosedCallbackIPv6();
49*d9f75844SAndroid Build Coastguard Worker   void TestDeleteInReadCallbackIPv4();
50*d9f75844SAndroid Build Coastguard Worker   void TestDeleteInReadCallbackIPv6();
51*d9f75844SAndroid Build Coastguard Worker   void TestSocketServerWaitIPv4();
52*d9f75844SAndroid Build Coastguard Worker   void TestSocketServerWaitIPv6();
53*d9f75844SAndroid Build Coastguard Worker   void TestTcpIPv4();
54*d9f75844SAndroid Build Coastguard Worker   void TestTcpIPv6();
55*d9f75844SAndroid Build Coastguard Worker   void TestSingleFlowControlCallbackIPv4();
56*d9f75844SAndroid Build Coastguard Worker   void TestSingleFlowControlCallbackIPv6();
57*d9f75844SAndroid Build Coastguard Worker   void TestUdpIPv4();
58*d9f75844SAndroid Build Coastguard Worker   void TestUdpIPv6();
59*d9f75844SAndroid Build Coastguard Worker   void TestUdpReadyToSendIPv4();
60*d9f75844SAndroid Build Coastguard Worker   void TestUdpReadyToSendIPv6();
61*d9f75844SAndroid Build Coastguard Worker   void TestGetSetOptionsIPv4();
62*d9f75844SAndroid Build Coastguard Worker   void TestGetSetOptionsIPv6();
63*d9f75844SAndroid Build Coastguard Worker   void TestSocketRecvTimestampIPv4();
64*d9f75844SAndroid Build Coastguard Worker   void TestSocketRecvTimestampIPv6();
65*d9f75844SAndroid Build Coastguard Worker   void TestUdpSocketRecvTimestampUseRtcEpochIPv4();
66*d9f75844SAndroid Build Coastguard Worker   void TestUdpSocketRecvTimestampUseRtcEpochIPv6();
67*d9f75844SAndroid Build Coastguard Worker 
68*d9f75844SAndroid Build Coastguard Worker   static const int kTimeout = 5000;  // ms
69*d9f75844SAndroid Build Coastguard Worker   const IPAddress kIPv4Loopback;
70*d9f75844SAndroid Build Coastguard Worker   const IPAddress kIPv6Loopback;
71*d9f75844SAndroid Build Coastguard Worker 
72*d9f75844SAndroid Build Coastguard Worker  protected:
73*d9f75844SAndroid Build Coastguard Worker   void TcpInternal(const IPAddress& loopback,
74*d9f75844SAndroid Build Coastguard Worker                    size_t data_size,
75*d9f75844SAndroid Build Coastguard Worker                    ptrdiff_t max_send_size);
76*d9f75844SAndroid Build Coastguard Worker 
77*d9f75844SAndroid Build Coastguard Worker  private:
78*d9f75844SAndroid Build Coastguard Worker   void ConnectInternal(const IPAddress& loopback);
79*d9f75844SAndroid Build Coastguard Worker   void ConnectWithDnsLookupInternal(const IPAddress& loopback,
80*d9f75844SAndroid Build Coastguard Worker                                     absl::string_view host);
81*d9f75844SAndroid Build Coastguard Worker   void ConnectFailInternal(const IPAddress& loopback);
82*d9f75844SAndroid Build Coastguard Worker 
83*d9f75844SAndroid Build Coastguard Worker   void ConnectWithDnsLookupFailInternal(const IPAddress& loopback);
84*d9f75844SAndroid Build Coastguard Worker   void ConnectWithClosedSocketInternal(const IPAddress& loopback);
85*d9f75844SAndroid Build Coastguard Worker   void ConnectWhileNotClosedInternal(const IPAddress& loopback);
86*d9f75844SAndroid Build Coastguard Worker   void ServerCloseDuringConnectInternal(const IPAddress& loopback);
87*d9f75844SAndroid Build Coastguard Worker   void ClientCloseDuringConnectInternal(const IPAddress& loopback);
88*d9f75844SAndroid Build Coastguard Worker   void ServerCloseInternal(const IPAddress& loopback);
89*d9f75844SAndroid Build Coastguard Worker   void CloseInClosedCallbackInternal(const IPAddress& loopback);
90*d9f75844SAndroid Build Coastguard Worker   void DeleteInReadCallbackInternal(const IPAddress& loopback);
91*d9f75844SAndroid Build Coastguard Worker   void SocketServerWaitInternal(const IPAddress& loopback);
92*d9f75844SAndroid Build Coastguard Worker   void SingleFlowControlCallbackInternal(const IPAddress& loopback);
93*d9f75844SAndroid Build Coastguard Worker   void UdpInternal(const IPAddress& loopback);
94*d9f75844SAndroid Build Coastguard Worker   void UdpReadyToSend(const IPAddress& loopback);
95*d9f75844SAndroid Build Coastguard Worker   void GetSetOptionsInternal(const IPAddress& loopback);
96*d9f75844SAndroid Build Coastguard Worker   void SocketRecvTimestamp(const IPAddress& loopback);
97*d9f75844SAndroid Build Coastguard Worker   void UdpSocketRecvTimestampUseRtcEpoch(const IPAddress& loopback);
98*d9f75844SAndroid Build Coastguard Worker 
99*d9f75844SAndroid Build Coastguard Worker   SocketFactory* socket_factory_;
100*d9f75844SAndroid Build Coastguard Worker };
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker // For unbound sockets, GetLocalAddress / GetRemoteAddress return AF_UNSPEC
103*d9f75844SAndroid Build Coastguard Worker // values on Windows, but an empty address of the same family on Linux/MacOS X.
104*d9f75844SAndroid Build Coastguard Worker bool IsUnspecOrEmptyIP(const IPAddress& address);
105*d9f75844SAndroid Build Coastguard Worker 
106*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
107*d9f75844SAndroid Build Coastguard Worker 
108*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_SOCKET_UNITTEST_H_
109