blob: bbae12cb1ceaed85ef51bedfb881cfd87ad3a4e4 [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>
Steve Anton10542f22019-01-11 09:11:00 -080016#include "rtc_base/async_udp_socket.h"
17#include "rtc_base/constructor_magic.h"
18#include "rtc_base/critical_section.h"
19#include "rtc_base/fake_clock.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
55 SocketAddress address() const { return socket_->GetLocalAddress(); }
56 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
57
58 // Checks that the socket moves to the specified connect state.
59 bool CheckConnState(AsyncPacketSocket::State state);
60
61 // Checks that the socket is connected to the remote side.
62 bool CheckConnected() {
63 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
64 }
65
66 // Sends using the clients socket.
67 int Send(const char* buf, size_t size);
68
69 // Sends using the clients socket to the given destination.
70 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
71
72 // Returns the next packet received by the client or null if none is received
73 // within the specified timeout.
74 std::unique_ptr<Packet> NextPacket(int timeout_ms);
75
76 // Checks that the next packet has the given contents. Returns the remote
77 // address that the packet was sent from.
78 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
79
80 // Checks that no packets have arrived or will arrive in the next second.
81 bool CheckNoPacket();
82
83 int GetError();
84 int SetOption(Socket::Option opt, int value);
85
86 bool ready_to_send() const { return ready_to_send_count() > 0; }
87
88 // How many times SignalReadyToSend has been fired.
89 int ready_to_send_count() const { return ready_to_send_count_; }
90
91 private:
92 // Timeout for reads when no packet is expected.
93 static const int kNoPacketTimeoutMs = 1000;
94 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
95 Socket::ConnState GetState();
96 // Slot for packets read on the socket.
Yves Gerey665174f2018-06-19 15:03:05 +020097 void OnPacket(AsyncPacketSocket* socket,
98 const char* buf,
99 size_t len,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100101 const int64_t& packet_time_us);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102 void OnReadyToSend(AsyncPacketSocket* socket);
103 bool CheckTimestamp(int64_t packet_timestamp);
104 void AdvanceTime(int ms);
105
Sebastian Janssond624c392019-04-17 10:36:03 +0200106 ThreadProcessingFakeClock* fake_clock_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 CriticalSection crit_;
108 std::unique_ptr<AsyncPacketSocket> socket_;
109 std::vector<std::unique_ptr<Packet>> packets_;
110 int ready_to_send_count_ = 0;
111 int64_t prev_packet_timestamp_;
112 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient);
113};
114
115} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116
Steve Anton10542f22019-01-11 09:11:00 -0800117#endif // RTC_BASE_TEST_CLIENT_H_