xref: /aosp_15_r20/external/webrtc/rtc_base/test_client.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2004 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_TEST_CLIENT_H_
12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_TEST_CLIENT_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <memory>
15*d9f75844SAndroid Build Coastguard Worker #include <vector>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/async_udp_socket.h"
18*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/fake_clock.h"
19*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/synchronization/mutex.h"
20*d9f75844SAndroid Build Coastguard Worker 
21*d9f75844SAndroid Build Coastguard Worker namespace rtc {
22*d9f75844SAndroid Build Coastguard Worker 
23*d9f75844SAndroid Build Coastguard Worker // A simple client that can send TCP or UDP data and check that it receives
24*d9f75844SAndroid Build Coastguard Worker // what it expects to receive. Useful for testing server functionality.
25*d9f75844SAndroid Build Coastguard Worker class TestClient : public sigslot::has_slots<> {
26*d9f75844SAndroid Build Coastguard Worker  public:
27*d9f75844SAndroid Build Coastguard Worker   // Records the contents of a packet that was received.
28*d9f75844SAndroid Build Coastguard Worker   struct Packet {
29*d9f75844SAndroid Build Coastguard Worker     Packet(const SocketAddress& a,
30*d9f75844SAndroid Build Coastguard Worker            const char* b,
31*d9f75844SAndroid Build Coastguard Worker            size_t s,
32*d9f75844SAndroid Build Coastguard Worker            int64_t packet_time_us);
33*d9f75844SAndroid Build Coastguard Worker     Packet(const Packet& p);
34*d9f75844SAndroid Build Coastguard Worker     virtual ~Packet();
35*d9f75844SAndroid Build Coastguard Worker 
36*d9f75844SAndroid Build Coastguard Worker     SocketAddress addr;
37*d9f75844SAndroid Build Coastguard Worker     char* buf;
38*d9f75844SAndroid Build Coastguard Worker     size_t size;
39*d9f75844SAndroid Build Coastguard Worker     int64_t packet_time_us;
40*d9f75844SAndroid Build Coastguard Worker   };
41*d9f75844SAndroid Build Coastguard Worker 
42*d9f75844SAndroid Build Coastguard Worker   // Default timeout for NextPacket reads.
43*d9f75844SAndroid Build Coastguard Worker   static const int kTimeoutMs = 5000;
44*d9f75844SAndroid Build Coastguard Worker 
45*d9f75844SAndroid Build Coastguard Worker   // Creates a client that will send and receive with the given socket and
46*d9f75844SAndroid Build Coastguard Worker   // will post itself messages with the given thread.
47*d9f75844SAndroid Build Coastguard Worker   explicit TestClient(std::unique_ptr<AsyncPacketSocket> socket);
48*d9f75844SAndroid Build Coastguard Worker   // Create a test client that will use a fake clock. NextPacket needs to wait
49*d9f75844SAndroid Build Coastguard Worker   // for a packet to be received, and thus it needs to advance the fake clock
50*d9f75844SAndroid Build Coastguard Worker   // if the test is using one, rather than just sleeping.
51*d9f75844SAndroid Build Coastguard Worker   TestClient(std::unique_ptr<AsyncPacketSocket> socket,
52*d9f75844SAndroid Build Coastguard Worker              ThreadProcessingFakeClock* fake_clock);
53*d9f75844SAndroid Build Coastguard Worker   ~TestClient() override;
54*d9f75844SAndroid Build Coastguard Worker 
55*d9f75844SAndroid Build Coastguard Worker   TestClient(const TestClient&) = delete;
56*d9f75844SAndroid Build Coastguard Worker   TestClient& operator=(const TestClient&) = delete;
57*d9f75844SAndroid Build Coastguard Worker 
address()58*d9f75844SAndroid Build Coastguard Worker   SocketAddress address() const { return socket_->GetLocalAddress(); }
remote_address()59*d9f75844SAndroid Build Coastguard Worker   SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
60*d9f75844SAndroid Build Coastguard Worker 
61*d9f75844SAndroid Build Coastguard Worker   // Checks that the socket moves to the specified connect state.
62*d9f75844SAndroid Build Coastguard Worker   bool CheckConnState(AsyncPacketSocket::State state);
63*d9f75844SAndroid Build Coastguard Worker 
64*d9f75844SAndroid Build Coastguard Worker   // Checks that the socket is connected to the remote side.
CheckConnected()65*d9f75844SAndroid Build Coastguard Worker   bool CheckConnected() {
66*d9f75844SAndroid Build Coastguard Worker     return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
67*d9f75844SAndroid Build Coastguard Worker   }
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker   // Sends using the clients socket.
70*d9f75844SAndroid Build Coastguard Worker   int Send(const char* buf, size_t size);
71*d9f75844SAndroid Build Coastguard Worker 
72*d9f75844SAndroid Build Coastguard Worker   // Sends using the clients socket to the given destination.
73*d9f75844SAndroid Build Coastguard Worker   int SendTo(const char* buf, size_t size, const SocketAddress& dest);
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker   // Returns the next packet received by the client or null if none is received
76*d9f75844SAndroid Build Coastguard Worker   // within the specified timeout.
77*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<Packet> NextPacket(int timeout_ms);
78*d9f75844SAndroid Build Coastguard Worker 
79*d9f75844SAndroid Build Coastguard Worker   // Checks that the next packet has the given contents. Returns the remote
80*d9f75844SAndroid Build Coastguard Worker   // address that the packet was sent from.
81*d9f75844SAndroid Build Coastguard Worker   bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
82*d9f75844SAndroid Build Coastguard Worker 
83*d9f75844SAndroid Build Coastguard Worker   // Checks that no packets have arrived or will arrive in the next second.
84*d9f75844SAndroid Build Coastguard Worker   bool CheckNoPacket();
85*d9f75844SAndroid Build Coastguard Worker 
86*d9f75844SAndroid Build Coastguard Worker   int GetError();
87*d9f75844SAndroid Build Coastguard Worker   int SetOption(Socket::Option opt, int value);
88*d9f75844SAndroid Build Coastguard Worker 
ready_to_send()89*d9f75844SAndroid Build Coastguard Worker   bool ready_to_send() const { return ready_to_send_count() > 0; }
90*d9f75844SAndroid Build Coastguard Worker 
91*d9f75844SAndroid Build Coastguard Worker   // How many times SignalReadyToSend has been fired.
ready_to_send_count()92*d9f75844SAndroid Build Coastguard Worker   int ready_to_send_count() const { return ready_to_send_count_; }
93*d9f75844SAndroid Build Coastguard Worker 
94*d9f75844SAndroid Build Coastguard Worker  private:
95*d9f75844SAndroid Build Coastguard Worker   // Timeout for reads when no packet is expected.
96*d9f75844SAndroid Build Coastguard Worker   static const int kNoPacketTimeoutMs = 1000;
97*d9f75844SAndroid Build Coastguard Worker   // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
98*d9f75844SAndroid Build Coastguard Worker   Socket::ConnState GetState();
99*d9f75844SAndroid Build Coastguard Worker   // Slot for packets read on the socket.
100*d9f75844SAndroid Build Coastguard Worker   void OnPacket(AsyncPacketSocket* socket,
101*d9f75844SAndroid Build Coastguard Worker                 const char* buf,
102*d9f75844SAndroid Build Coastguard Worker                 size_t len,
103*d9f75844SAndroid Build Coastguard Worker                 const SocketAddress& remote_addr,
104*d9f75844SAndroid Build Coastguard Worker                 const int64_t& packet_time_us);
105*d9f75844SAndroid Build Coastguard Worker   void OnReadyToSend(AsyncPacketSocket* socket);
106*d9f75844SAndroid Build Coastguard Worker   bool CheckTimestamp(int64_t packet_timestamp);
107*d9f75844SAndroid Build Coastguard Worker   void AdvanceTime(int ms);
108*d9f75844SAndroid Build Coastguard Worker 
109*d9f75844SAndroid Build Coastguard Worker   ThreadProcessingFakeClock* fake_clock_ = nullptr;
110*d9f75844SAndroid Build Coastguard Worker   webrtc::Mutex mutex_;
111*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<AsyncPacketSocket> socket_;
112*d9f75844SAndroid Build Coastguard Worker   std::vector<std::unique_ptr<Packet>> packets_;
113*d9f75844SAndroid Build Coastguard Worker   int ready_to_send_count_ = 0;
114*d9f75844SAndroid Build Coastguard Worker   int64_t prev_packet_timestamp_;
115*d9f75844SAndroid Build Coastguard Worker };
116*d9f75844SAndroid Build Coastguard Worker 
117*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
118*d9f75844SAndroid Build Coastguard Worker 
119*d9f75844SAndroid Build Coastguard Worker #endif  // RTC_BASE_TEST_CLIENT_H_
120