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