blob: d78e142a2f6c1c7a3f5252ffcd3c83d9a71236f2 [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
11#ifndef WEBRTC_BASE_TESTCLIENT_H_
12#define WEBRTC_BASE_TESTCLIENT_H_
13
14#include <vector>
15#include "webrtc/base/asyncudpsocket.h"
kwiberg4485ffb2016-04-26 08:14:39 -070016#include "webrtc/base/constructormagic.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/criticalsection.h"
18
19namespace rtc {
20
21// A simple client that can send TCP or UDP data and check that it receives
22// what it expects to receive. Useful for testing server functionality.
23class TestClient : public sigslot::has_slots<> {
24 public:
25 // Records the contents of a packet that was received.
26 struct Packet {
Stefan Holmer9131efd2016-05-23 18:19:26 +020027 Packet(const SocketAddress& a,
28 const char* b,
29 size_t s,
30 const PacketTime& packet_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031 Packet(const Packet& p);
32 virtual ~Packet();
33
34 SocketAddress addr;
35 char* buf;
36 size_t size;
Stefan Holmer9131efd2016-05-23 18:19:26 +020037 PacketTime packet_time;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038 };
39
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000040 // Default timeout for NextPacket reads.
41 static const int kTimeoutMs = 5000;
42
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 // Creates a client that will send and receive with the given socket and
44 // will post itself messages with the given thread.
45 explicit TestClient(AsyncPacketSocket* socket);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000046 ~TestClient() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047
48 SocketAddress address() const { return socket_->GetLocalAddress(); }
49 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
50
51 // Checks that the socket moves to the specified connect state.
52 bool CheckConnState(AsyncPacketSocket::State state);
53
54 // Checks that the socket is connected to the remote side.
55 bool CheckConnected() {
56 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
57 }
58
59 // Sends using the clients socket.
60 int Send(const char* buf, size_t size);
61
62 // Sends using the clients socket to the given destination.
63 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
64
65 // Returns the next packet received by the client or 0 if none is received
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000066 // within the specified timeout. The caller must delete the packet
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067 // when done with it.
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000068 Packet* NextPacket(int timeout_ms);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069
70 // Checks that the next packet has the given contents. Returns the remote
71 // address that the packet was sent from.
72 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
73
74 // Checks that no packets have arrived or will arrive in the next second.
75 bool CheckNoPacket();
76
77 int GetError();
78 int SetOption(Socket::Option opt, int value);
79
80 bool ready_to_send() const;
81
82 private:
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000083 // Timeout for reads when no packet is expected.
84 static const int kNoPacketTimeoutMs = 1000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
86 Socket::ConnState GetState();
87 // Slot for packets read on the socket.
88 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len,
89 const SocketAddress& remote_addr,
90 const PacketTime& packet_time);
91 void OnReadyToSend(AsyncPacketSocket* socket);
Stefan Holmer9131efd2016-05-23 18:19:26 +020092 bool CheckTimestamp(int64_t packet_timestamp);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093
94 CriticalSection crit_;
95 AsyncPacketSocket* socket_;
96 std::vector<Packet*>* packets_;
97 bool ready_to_send_;
Stefan Holmer9131efd2016-05-23 18:19:26 +020098 int64_t prev_packet_timestamp_;
99 int64_t prev_time_us_;
henrikg3c089d72015-09-16 05:37:44 -0700100 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101};
102
103} // namespace rtc
104
105#endif // WEBRTC_BASE_TESTCLIENT_H_