blob: dd91d37ab926b27681d11e58ba266e0487126972 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_TEST_CLIENT_H_
12#define RTC_BASE_TEST_CLIENT_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <memory>
15#include <vector>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/async_udp_socket.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/fake_clock.h"
Markus Handell18523c32020-07-08 17:55:58 +020019#include "rtc_base/synchronization/mutex.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021namespace rtc {
22
23// A simple client that can send TCP or UDP data and check that it receives
24// what it expects to receive. Useful for testing server functionality.
25class TestClient : public sigslot::has_slots<> {
26 public:
27 // Records the contents of a packet that was received.
28 struct Packet {
29 Packet(const SocketAddress& a,
30 const char* b,
31 size_t s,
Niels Möllere6933812018-11-05 13:01:41 +010032 int64_t packet_time_us);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033 Packet(const Packet& p);
34 virtual ~Packet();
35
36 SocketAddress addr;
Yves Gerey665174f2018-06-19 15:03:05 +020037 char* buf;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038 size_t size;
Niels Möllere6933812018-11-05 13:01:41 +010039 int64_t packet_time_us;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020040 };
41
42 // Default timeout for NextPacket reads.
43 static const int kTimeoutMs = 5000;
44
45 // Creates a client that will send and receive with the given socket and
46 // will post itself messages with the given thread.
47 explicit TestClient(std::unique_ptr<AsyncPacketSocket> socket);
48 // Create a test client that will use a fake clock. NextPacket needs to wait
49 // for a packet to be received, and thus it needs to advance the fake clock
50 // if the test is using one, rather than just sleeping.
Sebastian Janssond624c392019-04-17 10:36:03 +020051 TestClient(std::unique_ptr<AsyncPacketSocket> socket,
52 ThreadProcessingFakeClock* fake_clock);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053 ~TestClient() override;
54
Byoungchan Lee14af7622022-01-12 05:24:58 +090055 TestClient(const TestClient&) = delete;
56 TestClient& operator=(const TestClient&) = delete;
57
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058 SocketAddress address() const { return socket_->GetLocalAddress(); }
59 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
60
61 // Checks that the socket moves to the specified connect state.
62 bool CheckConnState(AsyncPacketSocket::State state);
63
64 // Checks that the socket is connected to the remote side.
65 bool CheckConnected() {
66 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
67 }
68
69 // Sends using the clients socket.
70 int Send(const char* buf, size_t size);
71
72 // Sends using the clients socket to the given destination.
73 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
74
75 // Returns the next packet received by the client or null if none is received
76 // within the specified timeout.
77 std::unique_ptr<Packet> NextPacket(int timeout_ms);
78
79 // Checks that the next packet has the given contents. Returns the remote
80 // address that the packet was sent from.
81 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
82
83 // Checks that no packets have arrived or will arrive in the next second.
84 bool CheckNoPacket();
85
86 int GetError();
87 int SetOption(Socket::Option opt, int value);
88
89 bool ready_to_send() const { return ready_to_send_count() > 0; }
90
91 // How many times SignalReadyToSend has been fired.
92 int ready_to_send_count() const { return ready_to_send_count_; }
93
94 private:
95 // Timeout for reads when no packet is expected.
96 static const int kNoPacketTimeoutMs = 1000;
97 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
98 Socket::ConnState GetState();
99 // Slot for packets read on the socket.
Yves Gerey665174f2018-06-19 15:03:05 +0200100 void OnPacket(AsyncPacketSocket* socket,
101 const char* buf,
102 size_t len,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100104 const int64_t& packet_time_us);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105 void OnReadyToSend(AsyncPacketSocket* socket);
106 bool CheckTimestamp(int64_t packet_timestamp);
107 void AdvanceTime(int ms);
108
Sebastian Janssond624c392019-04-17 10:36:03 +0200109 ThreadProcessingFakeClock* fake_clock_ = nullptr;
Markus Handell18523c32020-07-08 17:55:58 +0200110 webrtc::Mutex mutex_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111 std::unique_ptr<AsyncPacketSocket> socket_;
112 std::vector<std::unique_ptr<Packet>> packets_;
113 int ready_to_send_count_ = 0;
114 int64_t prev_packet_timestamp_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115};
116
117} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118
Steve Anton10542f22019-01-11 09:11:00 -0800119#endif // RTC_BASE_TEST_CLIENT_H_