henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_TEST_CLIENT_H_ |
| 12 | #define RTC_BASE_TEST_CLIENT_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <vector> |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 16 | #include "rtc_base/async_udp_socket.h" |
| 17 | #include "rtc_base/constructor_magic.h" |
| 18 | #include "rtc_base/critical_section.h" |
| 19 | #include "rtc_base/fake_clock.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 20 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 21 | namespace 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. |
| 25 | class 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, |
Niels Möller | e693381 | 2018-11-05 12:01:41 | [diff] [blame] | 32 | int64_t packet_time_us); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 33 | Packet(const Packet& p); |
| 34 | virtual ~Packet(); |
| 35 | |
| 36 | SocketAddress addr; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 37 | char* buf; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 38 | size_t size; |
Niels Möller | e693381 | 2018-11-05 12:01:41 | [diff] [blame] | 39 | int64_t packet_time_us; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 96 | void OnPacket(AsyncPacketSocket* socket, |
| 97 | const char* buf, |
| 98 | size_t len, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 99 | const SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 12:01:41 | [diff] [blame] | 100 | const int64_t& packet_time_us); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 101 | void OnReadyToSend(AsyncPacketSocket* socket); |
| 102 | bool CheckTimestamp(int64_t packet_timestamp); |
| 103 | void AdvanceTime(int ms); |
| 104 | |
| 105 | FakeClock* fake_clock_ = nullptr; |
| 106 | CriticalSection crit_; |
| 107 | std::unique_ptr<AsyncPacketSocket> socket_; |
| 108 | std::vector<std::unique_ptr<Packet>> packets_; |
| 109 | int ready_to_send_count_ = 0; |
| 110 | int64_t prev_packet_timestamp_; |
| 111 | RTC_DISALLOW_COPY_AND_ASSIGN(TestClient); |
| 112 | }; |
| 113 | |
| 114 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 115 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 116 | #endif // RTC_BASE_TEST_CLIENT_H_ |