blob: d56f948b0497d98b2d2a72bd0ca95e77e8186bfc [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
35 // Creates a client that will send and receive with the given socket and
36 // will post itself messages with the given thread.
37 explicit TestClient(AsyncPacketSocket* socket);
38 ~TestClient();
39
40 SocketAddress address() const { return socket_->GetLocalAddress(); }
41 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
42
43 // Checks that the socket moves to the specified connect state.
44 bool CheckConnState(AsyncPacketSocket::State state);
45
46 // Checks that the socket is connected to the remote side.
47 bool CheckConnected() {
48 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
49 }
50
51 // Sends using the clients socket.
52 int Send(const char* buf, size_t size);
53
54 // Sends using the clients socket to the given destination.
55 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
56
57 // Returns the next packet received by the client or 0 if none is received
58 // within a reasonable amount of time. The caller must delete the packet
59 // when done with it.
60 Packet* NextPacket();
61
62 // Checks that the next packet has the given contents. Returns the remote
63 // address that the packet was sent from.
64 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
65
66 // Checks that no packets have arrived or will arrive in the next second.
67 bool CheckNoPacket();
68
69 int GetError();
70 int SetOption(Socket::Option opt, int value);
71
72 bool ready_to_send() const;
73
74 private:
75 static const int kTimeout = 1000;
76 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
77 Socket::ConnState GetState();
78 // Slot for packets read on the socket.
79 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len,
80 const SocketAddress& remote_addr,
81 const PacketTime& packet_time);
82 void OnReadyToSend(AsyncPacketSocket* socket);
83
84 CriticalSection crit_;
85 AsyncPacketSocket* socket_;
86 std::vector<Packet*>* packets_;
87 bool ready_to_send_;
88 DISALLOW_EVIL_CONSTRUCTORS(TestClient);
89};
90
91} // namespace rtc
92
93#endif // WEBRTC_BASE_TESTCLIENT_H_