henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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" |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 16 | #include "webrtc/base/constructormagic.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | #include "webrtc/base/criticalsection.h" |
| 18 | |
| 19 | namespace 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. |
| 23 | class TestClient : public sigslot::has_slots<> { |
| 24 | public: |
| 25 | // Records the contents of a packet that was received. |
| 26 | struct Packet { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 27 | Packet(const SocketAddress& a, |
| 28 | const char* b, |
| 29 | size_t s, |
| 30 | const PacketTime& packet_time); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | Packet(const Packet& p); |
| 32 | virtual ~Packet(); |
| 33 | |
| 34 | SocketAddress addr; |
| 35 | char* buf; |
| 36 | size_t size; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 37 | PacketTime packet_time; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 40 | // Default timeout for NextPacket reads. |
| 41 | static const int kTimeoutMs = 5000; |
| 42 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | // 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.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 46 | ~TestClient() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | |
| 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.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 66 | // within the specified timeout. The caller must delete the packet |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | // when done with it. |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 68 | Packet* NextPacket(int timeout_ms); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | |
| 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.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 83 | // Timeout for reads when no packet is expected. |
| 84 | static const int kNoPacketTimeoutMs = 1000; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | // 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 Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 92 | bool CheckTimestamp(int64_t packet_timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | |
| 94 | CriticalSection crit_; |
| 95 | AsyncPacketSocket* socket_; |
| 96 | std::vector<Packet*>* packets_; |
| 97 | bool ready_to_send_; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame^] | 98 | int64_t prev_packet_timestamp_; |
| 99 | int64_t prev_time_us_; |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 100 | RTC_DISALLOW_COPY_AND_ASSIGN(TestClient); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | } // namespace rtc |
| 104 | |
| 105 | #endif // WEBRTC_BASE_TESTCLIENT_H_ |