blob: 5d8ee98a6fb42d643cd614f0a53ec518850ea863 [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"
16#include "webrtc/base/criticalsection.h"
17
18namespace rtc {
19
20// A simple client that can send TCP or UDP data and check that it receives
21// what it expects to receive. Useful for testing server functionality.
22class TestClient : public sigslot::has_slots<> {
23 public:
24 // Records the contents of a packet that was received.
25 struct Packet {
26 Packet(const SocketAddress& a, const char* b, size_t s);
27 Packet(const Packet& p);
28 virtual ~Packet();
29
30 SocketAddress addr;
31 char* buf;
32 size_t size;
33 };
34
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000035 // Default timeout for NextPacket reads.
36 static const int kTimeoutMs = 5000;
37
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038 // Creates a client that will send and receive with the given socket and
39 // will post itself messages with the given thread.
40 explicit TestClient(AsyncPacketSocket* socket);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000041 ~TestClient() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042
43 SocketAddress address() const { return socket_->GetLocalAddress(); }
44 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
45
46 // Checks that the socket moves to the specified connect state.
47 bool CheckConnState(AsyncPacketSocket::State state);
48
49 // Checks that the socket is connected to the remote side.
50 bool CheckConnected() {
51 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
52 }
53
54 // Sends using the clients socket.
55 int Send(const char* buf, size_t size);
56
57 // Sends using the clients socket to the given destination.
58 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
59
60 // Returns the next packet received by the client or 0 if none is received
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000061 // within the specified timeout. The caller must delete the packet
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 // when done with it.
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000063 Packet* NextPacket(int timeout_ms);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064
65 // Checks that the next packet has the given contents. Returns the remote
66 // address that the packet was sent from.
67 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
68
69 // Checks that no packets have arrived or will arrive in the next second.
70 bool CheckNoPacket();
71
72 int GetError();
73 int SetOption(Socket::Option opt, int value);
74
75 bool ready_to_send() const;
76
77 private:
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000078 // Timeout for reads when no packet is expected.
79 static const int kNoPacketTimeoutMs = 1000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
81 Socket::ConnState GetState();
82 // Slot for packets read on the socket.
83 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len,
84 const SocketAddress& remote_addr,
85 const PacketTime& packet_time);
86 void OnReadyToSend(AsyncPacketSocket* socket);
87
88 CriticalSection crit_;
89 AsyncPacketSocket* socket_;
90 std::vector<Packet*>* packets_;
91 bool ready_to_send_;
henrikg3c089d72015-09-16 05:37:44 -070092 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093};
94
95} // namespace rtc
96
97#endif // WEBRTC_BASE_TESTCLIENT_H_