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> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 16 | |
Per K | d07900c | 2023-11-17 09:18:25 | [diff] [blame] | 17 | #include "api/units/timestamp.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 18 | #include "rtc_base/async_udp_socket.h" |
Per K | d07900c | 2023-11-17 09:18:25 | [diff] [blame] | 19 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "rtc_base/fake_clock.h" |
Per K | d07900c | 2023-11-17 09:18:25 | [diff] [blame] | 21 | #include "rtc_base/network/received_packet.h" |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 22 | #include "rtc_base/synchronization/mutex.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 23 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 24 | namespace rtc { |
| 25 | |
| 26 | // A simple client that can send TCP or UDP data and check that it receives |
| 27 | // what it expects to receive. Useful for testing server functionality. |
| 28 | class TestClient : public sigslot::has_slots<> { |
| 29 | public: |
| 30 | // Records the contents of a packet that was received. |
| 31 | struct Packet { |
Per K | d07900c | 2023-11-17 09:18:25 | [diff] [blame] | 32 | Packet(const rtc::ReceivedPacket& received_packet); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 33 | Packet(const Packet& p); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 34 | |
| 35 | SocketAddress addr; |
Per K | d07900c | 2023-11-17 09:18:25 | [diff] [blame] | 36 | Buffer buf; |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 37 | std::optional<webrtc::Timestamp> packet_time; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | // Default timeout for NextPacket reads. |
| 41 | static const int kTimeoutMs = 5000; |
| 42 | |
| 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(std::unique_ptr<AsyncPacketSocket> socket); |
| 46 | // Create a test client that will use a fake clock. NextPacket needs to wait |
| 47 | // for a packet to be received, and thus it needs to advance the fake clock |
| 48 | // if the test is using one, rather than just sleeping. |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 49 | TestClient(std::unique_ptr<AsyncPacketSocket> socket, |
| 50 | ThreadProcessingFakeClock* fake_clock); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 51 | ~TestClient() override; |
| 52 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 53 | TestClient(const TestClient&) = delete; |
| 54 | TestClient& operator=(const TestClient&) = delete; |
| 55 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 56 | SocketAddress address() const { return socket_->GetLocalAddress(); } |
| 57 | SocketAddress remote_address() const { return socket_->GetRemoteAddress(); } |
| 58 | |
| 59 | // Checks that the socket moves to the specified connect state. |
| 60 | bool CheckConnState(AsyncPacketSocket::State state); |
| 61 | |
| 62 | // Checks that the socket is connected to the remote side. |
| 63 | bool CheckConnected() { |
| 64 | return CheckConnState(AsyncPacketSocket::STATE_CONNECTED); |
| 65 | } |
| 66 | |
| 67 | // Sends using the clients socket. |
| 68 | int Send(const char* buf, size_t size); |
| 69 | |
| 70 | // Sends using the clients socket to the given destination. |
| 71 | int SendTo(const char* buf, size_t size, const SocketAddress& dest); |
| 72 | |
| 73 | // Returns the next packet received by the client or null if none is received |
| 74 | // within the specified timeout. |
| 75 | std::unique_ptr<Packet> NextPacket(int timeout_ms); |
| 76 | |
| 77 | // Checks that the next packet has the given contents. Returns the remote |
| 78 | // address that the packet was sent from. |
| 79 | bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr); |
| 80 | |
| 81 | // Checks that no packets have arrived or will arrive in the next second. |
| 82 | bool CheckNoPacket(); |
| 83 | |
| 84 | int GetError(); |
| 85 | int SetOption(Socket::Option opt, int value); |
| 86 | |
| 87 | bool ready_to_send() const { return ready_to_send_count() > 0; } |
| 88 | |
| 89 | // How many times SignalReadyToSend has been fired. |
| 90 | int ready_to_send_count() const { return ready_to_send_count_; } |
| 91 | |
| 92 | private: |
| 93 | // Timeout for reads when no packet is expected. |
| 94 | static const int kNoPacketTimeoutMs = 1000; |
| 95 | // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist. |
| 96 | Socket::ConnState GetState(); |
Per K | d07900c | 2023-11-17 09:18:25 | [diff] [blame] | 97 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 98 | void OnPacket(AsyncPacketSocket* socket, |
Per K | d07900c | 2023-11-17 09:18:25 | [diff] [blame] | 99 | const rtc::ReceivedPacket& received_packet); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 100 | void OnReadyToSend(AsyncPacketSocket* socket); |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 101 | bool CheckTimestamp(std::optional<webrtc::Timestamp> packet_timestamp); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 102 | void AdvanceTime(int ms); |
| 103 | |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 104 | ThreadProcessingFakeClock* fake_clock_ = nullptr; |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 105 | webrtc::Mutex mutex_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 106 | std::unique_ptr<AsyncPacketSocket> socket_; |
| 107 | std::vector<std::unique_ptr<Packet>> packets_; |
| 108 | int ready_to_send_count_ = 0; |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 109 | std::optional<webrtc::Timestamp> prev_packet_timestamp_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 113 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 114 | #endif // RTC_BASE_TEST_CLIENT_H_ |