blob: db79be2eb848d1eed19417e4fd6ae538fd4a5715 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_SOCKET_UNITTEST_H_
12#define RTC_BASE_SOCKET_UNITTEST_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Ali Tofigh7fa90572022-03-17 15:47:49 +010014#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/gunit.h"
16#include "rtc_base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018namespace rtc {
19
20// Generic socket tests, to be used when testing individual socketservers.
21// Derive your specific test class from SocketTest, install your
22// socketserver, and call the SocketTest test methods.
Mirko Bonadei6a489f22019-04-09 15:11:12 +020023class SocketTest : public ::testing::Test {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024 protected:
Niels Möller50f7c2c2021-09-08 14:05:16 +020025 explicit SocketTest(rtc::SocketFactory* socket_factory)
Yves Gerey665174f2018-06-19 15:03:05 +020026 : kIPv4Loopback(INADDR_LOOPBACK),
27 kIPv6Loopback(in6addr_loopback),
Niels Möller50f7c2c2021-09-08 14:05:16 +020028 socket_factory_(socket_factory) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029 void TestConnectIPv4();
30 void TestConnectIPv6();
31 void TestConnectWithDnsLookupIPv4();
32 void TestConnectWithDnsLookupIPv6();
33 void TestConnectFailIPv4();
34 void TestConnectFailIPv6();
35 void TestConnectWithDnsLookupFailIPv4();
36 void TestConnectWithDnsLookupFailIPv6();
37 void TestConnectWithClosedSocketIPv4();
38 void TestConnectWithClosedSocketIPv6();
39 void TestConnectWhileNotClosedIPv4();
40 void TestConnectWhileNotClosedIPv6();
41 void TestServerCloseDuringConnectIPv4();
42 void TestServerCloseDuringConnectIPv6();
43 void TestClientCloseDuringConnectIPv4();
44 void TestClientCloseDuringConnectIPv6();
45 void TestServerCloseIPv4();
46 void TestServerCloseIPv6();
47 void TestCloseInClosedCallbackIPv4();
48 void TestCloseInClosedCallbackIPv6();
Taylor Brandstetter7b69a442020-08-20 23:43:13 +000049 void TestDeleteInReadCallbackIPv4();
50 void TestDeleteInReadCallbackIPv6();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051 void TestSocketServerWaitIPv4();
52 void TestSocketServerWaitIPv6();
53 void TestTcpIPv4();
54 void TestTcpIPv6();
55 void TestSingleFlowControlCallbackIPv4();
56 void TestSingleFlowControlCallbackIPv6();
57 void TestUdpIPv4();
58 void TestUdpIPv6();
59 void TestUdpReadyToSendIPv4();
60 void TestUdpReadyToSendIPv6();
61 void TestGetSetOptionsIPv4();
62 void TestGetSetOptionsIPv6();
63 void TestSocketRecvTimestampIPv4();
64 void TestSocketRecvTimestampIPv6();
Per Kjellander3daf6962022-11-08 18:23:43 +010065 void TestUdpSocketRecvTimestampUseRtcEpochIPv4();
66 void TestUdpSocketRecvTimestampUseRtcEpochIPv6();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067
68 static const int kTimeout = 5000; // ms
69 const IPAddress kIPv4Loopback;
70 const IPAddress kIPv6Loopback;
71
72 protected:
Yves Gerey665174f2018-06-19 15:03:05 +020073 void TcpInternal(const IPAddress& loopback,
74 size_t data_size,
75 ptrdiff_t max_send_size);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076
77 private:
78 void ConnectInternal(const IPAddress& loopback);
79 void ConnectWithDnsLookupInternal(const IPAddress& loopback,
Ali Tofigh7fa90572022-03-17 15:47:49 +010080 absl::string_view host);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081 void ConnectFailInternal(const IPAddress& loopback);
82
83 void ConnectWithDnsLookupFailInternal(const IPAddress& loopback);
84 void ConnectWithClosedSocketInternal(const IPAddress& loopback);
85 void ConnectWhileNotClosedInternal(const IPAddress& loopback);
86 void ServerCloseDuringConnectInternal(const IPAddress& loopback);
87 void ClientCloseDuringConnectInternal(const IPAddress& loopback);
88 void ServerCloseInternal(const IPAddress& loopback);
89 void CloseInClosedCallbackInternal(const IPAddress& loopback);
Taylor Brandstetter7b69a442020-08-20 23:43:13 +000090 void DeleteInReadCallbackInternal(const IPAddress& loopback);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091 void SocketServerWaitInternal(const IPAddress& loopback);
92 void SingleFlowControlCallbackInternal(const IPAddress& loopback);
93 void UdpInternal(const IPAddress& loopback);
94 void UdpReadyToSend(const IPAddress& loopback);
95 void GetSetOptionsInternal(const IPAddress& loopback);
96 void SocketRecvTimestamp(const IPAddress& loopback);
Per Kjellander3daf6962022-11-08 18:23:43 +010097 void UdpSocketRecvTimestampUseRtcEpoch(const IPAddress& loopback);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098
Niels Möller50f7c2c2021-09-08 14:05:16 +020099 SocketFactory* socket_factory_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100};
101
102// For unbound sockets, GetLocalAddress / GetRemoteAddress return AF_UNSPEC
103// values on Windows, but an empty address of the same family on Linux/MacOS X.
104bool IsUnspecOrEmptyIP(const IPAddress& address);
105
106} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200108#endif // RTC_BASE_SOCKET_UNITTEST_H_