You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/test_client.h"
  11. #include <string.h>
  12. #include <memory>
  13. #include <utility>
  14. #include "rtc_base/gunit.h"
  15. #include "rtc_base/thread.h"
  16. #include "rtc_base/time_utils.h"
  17. namespace rtc {
  18. // DESIGN: Each packet received is put it into a list of packets.
  19. // Callers can retrieve received packets from any thread by calling
  20. // NextPacket.
  21. TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket)
  22. : TestClient(std::move(socket), nullptr) {}
  23. TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket,
  24. ThreadProcessingFakeClock* fake_clock)
  25. : fake_clock_(fake_clock),
  26. socket_(std::move(socket)),
  27. prev_packet_timestamp_(-1) {
  28. socket_->SignalReadPacket.connect(this, &TestClient::OnPacket);
  29. socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend);
  30. }
  31. TestClient::~TestClient() {}
  32. bool TestClient::CheckConnState(AsyncPacketSocket::State state) {
  33. // Wait for our timeout value until the socket reaches the desired state.
  34. int64_t end = TimeAfter(kTimeoutMs);
  35. while (socket_->GetState() != state && TimeUntil(end) > 0) {
  36. AdvanceTime(1);
  37. }
  38. return (socket_->GetState() == state);
  39. }
  40. int TestClient::Send(const char* buf, size_t size) {
  41. rtc::PacketOptions options;
  42. return socket_->Send(buf, size, options);
  43. }
  44. int TestClient::SendTo(const char* buf,
  45. size_t size,
  46. const SocketAddress& dest) {
  47. rtc::PacketOptions options;
  48. return socket_->SendTo(buf, size, dest, options);
  49. }
  50. std::unique_ptr<TestClient::Packet> TestClient::NextPacket(int timeout_ms) {
  51. // If no packets are currently available, we go into a get/dispatch loop for
  52. // at most timeout_ms. If, during the loop, a packet arrives, then we can
  53. // stop early and return it.
  54. // Note that the case where no packet arrives is important. We often want to
  55. // test that a packet does not arrive.
  56. // Note also that we only try to pump our current thread's message queue.
  57. // Pumping another thread's queue could lead to messages being dispatched from
  58. // the wrong thread to non-thread-safe objects.
  59. int64_t end = TimeAfter(timeout_ms);
  60. while (TimeUntil(end) > 0) {
  61. {
  62. CritScope cs(&crit_);
  63. if (packets_.size() != 0) {
  64. break;
  65. }
  66. }
  67. AdvanceTime(1);
  68. }
  69. // Return the first packet placed in the queue.
  70. std::unique_ptr<Packet> packet;
  71. CritScope cs(&crit_);
  72. if (packets_.size() > 0) {
  73. packet = std::move(packets_.front());
  74. packets_.erase(packets_.begin());
  75. }
  76. return packet;
  77. }
  78. bool TestClient::CheckNextPacket(const char* buf,
  79. size_t size,
  80. SocketAddress* addr) {
  81. bool res = false;
  82. std::unique_ptr<Packet> packet = NextPacket(kTimeoutMs);
  83. if (packet) {
  84. res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 &&
  85. CheckTimestamp(packet->packet_time_us));
  86. if (addr)
  87. *addr = packet->addr;
  88. }
  89. return res;
  90. }
  91. bool TestClient::CheckTimestamp(int64_t packet_timestamp) {
  92. bool res = true;
  93. if (packet_timestamp == -1) {
  94. res = false;
  95. }
  96. if (prev_packet_timestamp_ != -1) {
  97. if (packet_timestamp < prev_packet_timestamp_) {
  98. res = false;
  99. }
  100. }
  101. prev_packet_timestamp_ = packet_timestamp;
  102. return res;
  103. }
  104. void TestClient::AdvanceTime(int ms) {
  105. // If the test is using a fake clock, we must advance the fake clock to
  106. // advance time. Otherwise, ProcessMessages will work.
  107. if (fake_clock_) {
  108. SIMULATED_WAIT(false, ms, *fake_clock_);
  109. } else {
  110. Thread::Current()->ProcessMessages(1);
  111. }
  112. }
  113. bool TestClient::CheckNoPacket() {
  114. return NextPacket(kNoPacketTimeoutMs) == nullptr;
  115. }
  116. int TestClient::GetError() {
  117. return socket_->GetError();
  118. }
  119. int TestClient::SetOption(Socket::Option opt, int value) {
  120. return socket_->SetOption(opt, value);
  121. }
  122. void TestClient::OnPacket(AsyncPacketSocket* socket,
  123. const char* buf,
  124. size_t size,
  125. const SocketAddress& remote_addr,
  126. const int64_t& packet_time_us) {
  127. CritScope cs(&crit_);
  128. packets_.push_back(
  129. std::make_unique<Packet>(remote_addr, buf, size, packet_time_us));
  130. }
  131. void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
  132. ++ready_to_send_count_;
  133. }
  134. TestClient::Packet::Packet(const SocketAddress& a,
  135. const char* b,
  136. size_t s,
  137. int64_t packet_time_us)
  138. : addr(a), buf(0), size(s), packet_time_us(packet_time_us) {
  139. buf = new char[size];
  140. memcpy(buf, b, size);
  141. }
  142. TestClient::Packet::Packet(const Packet& p)
  143. : addr(p.addr), buf(0), size(p.size), packet_time_us(p.packet_time_us) {
  144. buf = new char[size];
  145. memcpy(buf, p.buf, size);
  146. }
  147. TestClient::Packet::~Packet() {
  148. delete[] buf;
  149. }
  150. } // namespace rtc