blob: 6989fe1d5707376f832a0e1977f9c3bc5c06244e [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"
18#include "rtc_base/constructor_magic.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/fake_clock.h"
Markus Handell18523c32020-07-08 17:55:58 +020020#include "rtc_base/synchronization/mutex.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022namespace rtc {
23
24// A simple client that can send TCP or UDP data and check that it receives
25// what it expects to receive. Useful for testing server functionality.
26class TestClient : public sigslot::has_slots<> {
27 public:
28 // Records the contents of a packet that was received.
29 struct Packet {
30 Packet(const SocketAddress& a,
31 const char* b,
32 size_t s,
Niels Möllere6933812018-11-05 13:01:41 +010033 int64_t packet_time_us);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034 Packet(const Packet& p);
35 virtual ~Packet();
36
37 SocketAddress addr;
Yves Gerey665174f2018-06-19 15:03:05 +020038 char* buf;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039 size_t size;
Niels Möllere6933812018-11-05 13:01:41 +010040 int64_t packet_time_us;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041 };
42
43 // Default timeout for NextPacket reads.
44 static const int kTimeoutMs = 5000;
45
46 // Creates a client that will send and receive with the given socket and
47 // will post itself messages with the given thread.
48 explicit TestClient(std::unique_ptr<AsyncPacketSocket> socket);
49 // Create a test client that will use a fake clock. NextPacket needs to wait
50 // for a packet to be received, and thus it needs to advance the fake clock
51 // if the test is using one, rather than just sleeping.
Sebastian Janssond624c392019-04-17 10:36:03 +020052 TestClient(std::unique_ptr<AsyncPacketSocket> socket,
53 ThreadProcessingFakeClock* fake_clock);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054 ~TestClient() override;
55
56 SocketAddress address() const { return socket_->GetLocalAddress(); }
57 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
58
59 // Checks that the socket moves to the specified connect state.
60 bool CheckConnState(AsyncPacketSocket::State state);
61
62 // Checks that the socket is connected to the remote side.
63 bool CheckConnected() {
64 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
65 }
66
67 // Sends using the clients socket.
68 int Send(const char* buf, size_t size);
69
70 // Sends using the clients socket to the given destination.
71 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
72
73 // Returns the next packet received by the client or null if none is received
74 // within the specified timeout.
75 std::unique_ptr<Packet> NextPacket(int timeout_ms);
76
77 // Checks that the next packet has the given contents. Returns the remote
78 // address that the packet was sent from.
79 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
80
81 // Checks that no packets have arrived or will arrive in the next second.
82 bool CheckNoPacket();
83
84 int GetError();
85 int SetOption(Socket::Option opt, int value);
86
87 bool ready_to_send() const { return ready_to_send_count() > 0; }
88
89 // How many times SignalReadyToSend has been fired.
90 int ready_to_send_count() const { return ready_to_send_count_; }
91
92 private:
93 // Timeout for reads when no packet is expected.
94 static const int kNoPacketTimeoutMs = 1000;
95 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
96 Socket::ConnState GetState();
97 // Slot for packets read on the socket.
Yves Gerey665174f2018-06-19 15:03:05 +020098 void OnPacket(AsyncPacketSocket* socket,
99 const char* buf,
100 size_t len,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100102 const int64_t& packet_time_us);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 void OnReadyToSend(AsyncPacketSocket* socket);
104 bool CheckTimestamp(int64_t packet_timestamp);
105 void AdvanceTime(int ms);
106
Sebastian Janssond624c392019-04-17 10:36:03 +0200107 ThreadProcessingFakeClock* fake_clock_ = nullptr;
Markus Handell18523c32020-07-08 17:55:58 +0200108 webrtc::Mutex mutex_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109 std::unique_ptr<AsyncPacketSocket> socket_;
110 std::vector<std::unique_ptr<Packet>> packets_;
111 int ready_to_send_count_ = 0;
112 int64_t prev_packet_timestamp_;
113 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient);
114};
115
116} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117
Steve Anton10542f22019-01-11 09:11:00 -0800118#endif // RTC_BASE_TEST_CLIENT_H_