blob: c0dbe65bc8db398d05dd8cea811e391e748af09a [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_TESTCLIENT_H_
12#define RTC_BASE_TESTCLIENT_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <memory>
15#include <vector>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/asyncudpsocket.h"
17#include "rtc_base/constructormagic.h"
18#include "rtc_base/criticalsection.h"
19#include "rtc_base/fakeclock.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,
32 const PacketTime& packet_time);
33 Packet(const Packet& p);
34 virtual ~Packet();
35
36 SocketAddress addr;
37 char* buf;
38 size_t size;
39 PacketTime packet_time;
40 };
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.
51 TestClient(std::unique_ptr<AsyncPacketSocket> socket, FakeClock* fake_clock);
52 ~TestClient() override;
53
54 SocketAddress address() const { return socket_->GetLocalAddress(); }
55 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
56
57 // Checks that the socket moves to the specified connect state.
58 bool CheckConnState(AsyncPacketSocket::State state);
59
60 // Checks that the socket is connected to the remote side.
61 bool CheckConnected() {
62 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
63 }
64
65 // Sends using the clients socket.
66 int Send(const char* buf, size_t size);
67
68 // Sends using the clients socket to the given destination.
69 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
70
71 // Returns the next packet received by the client or null if none is received
72 // within the specified timeout.
73 std::unique_ptr<Packet> NextPacket(int timeout_ms);
74
75 // Checks that the next packet has the given contents. Returns the remote
76 // address that the packet was sent from.
77 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
78
79 // Checks that no packets have arrived or will arrive in the next second.
80 bool CheckNoPacket();
81
82 int GetError();
83 int SetOption(Socket::Option opt, int value);
84
85 bool ready_to_send() const { return ready_to_send_count() > 0; }
86
87 // How many times SignalReadyToSend has been fired.
88 int ready_to_send_count() const { return ready_to_send_count_; }
89
90 private:
91 // Timeout for reads when no packet is expected.
92 static const int kNoPacketTimeoutMs = 1000;
93 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
94 Socket::ConnState GetState();
95 // Slot for packets read on the socket.
96 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len,
97 const SocketAddress& remote_addr,
98 const PacketTime& packet_time);
99 void OnReadyToSend(AsyncPacketSocket* socket);
100 bool CheckTimestamp(int64_t packet_timestamp);
101 void AdvanceTime(int ms);
102
103 FakeClock* fake_clock_ = nullptr;
104 CriticalSection crit_;
105 std::unique_ptr<AsyncPacketSocket> socket_;
106 std::vector<std::unique_ptr<Packet>> packets_;
107 int ready_to_send_count_ = 0;
108 int64_t prev_packet_timestamp_;
109 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient);
110};
111
112} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200114#endif // RTC_BASE_TESTCLIENT_H_