test_client.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifndef RTC_BASE_TEST_CLIENT_H_
  11. #define RTC_BASE_TEST_CLIENT_H_
  12. #include <memory>
  13. #include <vector>
  14. #include "rtc_base/async_udp_socket.h"
  15. #include "rtc_base/constructor_magic.h"
  16. #include "rtc_base/fake_clock.h"
  17. #include "rtc_base/synchronization/mutex.h"
  18. namespace rtc {
  19. // A simple client that can send TCP or UDP data and check that it receives
  20. // what it expects to receive. Useful for testing server functionality.
  21. class TestClient : public sigslot::has_slots<> {
  22. public:
  23. // Records the contents of a packet that was received.
  24. struct Packet {
  25. Packet(const SocketAddress& a,
  26. const char* b,
  27. size_t s,
  28. int64_t packet_time_us);
  29. Packet(const Packet& p);
  30. virtual ~Packet();
  31. SocketAddress addr;
  32. char* buf;
  33. size_t size;
  34. int64_t packet_time_us;
  35. };
  36. // Default timeout for NextPacket reads.
  37. static const int kTimeoutMs = 5000;
  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(std::unique_ptr<AsyncPacketSocket> socket);
  41. // Create a test client that will use a fake clock. NextPacket needs to wait
  42. // for a packet to be received, and thus it needs to advance the fake clock
  43. // if the test is using one, rather than just sleeping.
  44. TestClient(std::unique_ptr<AsyncPacketSocket> socket,
  45. ThreadProcessingFakeClock* fake_clock);
  46. ~TestClient() override;
  47. SocketAddress address() const { return socket_->GetLocalAddress(); }
  48. SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
  49. // Checks that the socket moves to the specified connect state.
  50. bool CheckConnState(AsyncPacketSocket::State state);
  51. // Checks that the socket is connected to the remote side.
  52. bool CheckConnected() {
  53. return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
  54. }
  55. // Sends using the clients socket.
  56. int Send(const char* buf, size_t size);
  57. // Sends using the clients socket to the given destination.
  58. int SendTo(const char* buf, size_t size, const SocketAddress& dest);
  59. // Returns the next packet received by the client or null if none is received
  60. // within the specified timeout.
  61. std::unique_ptr<Packet> NextPacket(int timeout_ms);
  62. // Checks that the next packet has the given contents. Returns the remote
  63. // address that the packet was sent from.
  64. bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
  65. // Checks that no packets have arrived or will arrive in the next second.
  66. bool CheckNoPacket();
  67. int GetError();
  68. int SetOption(Socket::Option opt, int value);
  69. bool ready_to_send() const { return ready_to_send_count() > 0; }
  70. // How many times SignalReadyToSend has been fired.
  71. int ready_to_send_count() const { return ready_to_send_count_; }
  72. private:
  73. // Timeout for reads when no packet is expected.
  74. static const int kNoPacketTimeoutMs = 1000;
  75. // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
  76. Socket::ConnState GetState();
  77. // Slot for packets read on the socket.
  78. void OnPacket(AsyncPacketSocket* socket,
  79. const char* buf,
  80. size_t len,
  81. const SocketAddress& remote_addr,
  82. const int64_t& packet_time_us);
  83. void OnReadyToSend(AsyncPacketSocket* socket);
  84. bool CheckTimestamp(int64_t packet_timestamp);
  85. void AdvanceTime(int ms);
  86. ThreadProcessingFakeClock* fake_clock_ = nullptr;
  87. webrtc::Mutex mutex_;
  88. std::unique_ptr<AsyncPacketSocket> socket_;
  89. std::vector<std::unique_ptr<Packet>> packets_;
  90. int ready_to_send_count_ = 0;
  91. int64_t prev_packet_timestamp_;
  92. RTC_DISALLOW_COPY_AND_ASSIGN(TestClient);
  93. };
  94. } // namespace rtc
  95. #endif // RTC_BASE_TEST_CLIENT_H_