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 RTC_BASE_TEST_CLIENT_H_ 12 #define RTC_BASE_TEST_CLIENT_H_ 13 14 #include <memory> 15 #include <vector> 16 17 #include "rtc_base/async_udp_socket.h" 18 #include "rtc_base/fake_clock.h" 19 #include "rtc_base/synchronization/mutex.h" 20 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, 32 int64_t packet_time_us); 33 Packet(const Packet& p); 34 virtual ~Packet(); 35 36 SocketAddress addr; 37 char* buf; 38 size_t size; 39 int64_t packet_time_us; 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, 52 ThreadProcessingFakeClock* fake_clock); 53 ~TestClient() override; 54 55 TestClient(const TestClient&) = delete; 56 TestClient& operator=(const TestClient&) = delete; 57 address()58 SocketAddress address() const { return socket_->GetLocalAddress(); } remote_address()59 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); } 60 61 // Checks that the socket moves to the specified connect state. 62 bool CheckConnState(AsyncPacketSocket::State state); 63 64 // Checks that the socket is connected to the remote side. CheckConnected()65 bool CheckConnected() { 66 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED); 67 } 68 69 // Sends using the clients socket. 70 int Send(const char* buf, size_t size); 71 72 // Sends using the clients socket to the given destination. 73 int SendTo(const char* buf, size_t size, const SocketAddress& dest); 74 75 // Returns the next packet received by the client or null if none is received 76 // within the specified timeout. 77 std::unique_ptr<Packet> NextPacket(int timeout_ms); 78 79 // Checks that the next packet has the given contents. Returns the remote 80 // address that the packet was sent from. 81 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr); 82 83 // Checks that no packets have arrived or will arrive in the next second. 84 bool CheckNoPacket(); 85 86 int GetError(); 87 int SetOption(Socket::Option opt, int value); 88 ready_to_send()89 bool ready_to_send() const { return ready_to_send_count() > 0; } 90 91 // How many times SignalReadyToSend has been fired. ready_to_send_count()92 int ready_to_send_count() const { return ready_to_send_count_; } 93 94 private: 95 // Timeout for reads when no packet is expected. 96 static const int kNoPacketTimeoutMs = 1000; 97 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist. 98 Socket::ConnState GetState(); 99 // Slot for packets read on the socket. 100 void OnPacket(AsyncPacketSocket* socket, 101 const char* buf, 102 size_t len, 103 const SocketAddress& remote_addr, 104 const int64_t& packet_time_us); 105 void OnReadyToSend(AsyncPacketSocket* socket); 106 bool CheckTimestamp(int64_t packet_timestamp); 107 void AdvanceTime(int ms); 108 109 ThreadProcessingFakeClock* fake_clock_ = nullptr; 110 webrtc::Mutex mutex_; 111 std::unique_ptr<AsyncPacketSocket> socket_; 112 std::vector<std::unique_ptr<Packet>> packets_; 113 int ready_to_send_count_ = 0; 114 int64_t prev_packet_timestamp_; 115 }; 116 117 } // namespace rtc 118 119 #endif // RTC_BASE_TEST_CLIENT_H_ 120