henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/test_client.h" |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <utility> |
| 15 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/gunit.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/time_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | // DESIGN: Each packet received is put it into a list of packets. |
| 24 | // Callers can retrieve received packets from any thread by calling |
| 25 | // NextPacket. |
| 26 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 27 | TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket) |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 28 | : TestClient(std::move(socket), nullptr) {} |
| 29 | |
| 30 | TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket, |
| 31 | FakeClock* fake_clock) |
| 32 | : fake_clock_(fake_clock), |
| 33 | socket_(std::move(socket)), |
| 34 | prev_packet_timestamp_(-1) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | socket_->SignalReadPacket.connect(this, &TestClient::OnPacket); |
| 36 | socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend); |
| 37 | } |
| 38 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 39 | TestClient::~TestClient() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | |
| 41 | bool TestClient::CheckConnState(AsyncPacketSocket::State state) { |
| 42 | // Wait for our timeout value until the socket reaches the desired state. |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 43 | int64_t end = TimeAfter(kTimeoutMs); |
| 44 | while (socket_->GetState() != state && TimeUntil(end) > 0) { |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 45 | AdvanceTime(1); |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 46 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | return (socket_->GetState() == state); |
| 48 | } |
| 49 | |
| 50 | int TestClient::Send(const char* buf, size_t size) { |
| 51 | rtc::PacketOptions options; |
| 52 | return socket_->Send(buf, size, options); |
| 53 | } |
| 54 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 55 | int TestClient::SendTo(const char* buf, |
| 56 | size_t size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 57 | const SocketAddress& dest) { |
| 58 | rtc::PacketOptions options; |
| 59 | return socket_->SendTo(buf, size, dest, options); |
| 60 | } |
| 61 | |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 62 | std::unique_ptr<TestClient::Packet> TestClient::NextPacket(int timeout_ms) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | // If no packets are currently available, we go into a get/dispatch loop for |
jlmiller@webrtc.org | ec499be | 2015-02-07 22:37:59 +0000 | [diff] [blame] | 64 | // at most timeout_ms. If, during the loop, a packet arrives, then we can |
| 65 | // stop early and return it. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | |
| 67 | // Note that the case where no packet arrives is important. We often want to |
| 68 | // test that a packet does not arrive. |
| 69 | |
| 70 | // Note also that we only try to pump our current thread's message queue. |
| 71 | // Pumping another thread's queue could lead to messages being dispatched from |
| 72 | // the wrong thread to non-thread-safe objects. |
| 73 | |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 74 | int64_t end = TimeAfter(timeout_ms); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | while (TimeUntil(end) > 0) { |
| 76 | { |
| 77 | CritScope cs(&crit_); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 78 | if (packets_.size() != 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | } |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 82 | AdvanceTime(1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Return the first packet placed in the queue. |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 86 | std::unique_ptr<Packet> packet; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | CritScope cs(&crit_); |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 88 | if (packets_.size() > 0) { |
| 89 | packet = std::move(packets_.front()); |
| 90 | packets_.erase(packets_.begin()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return packet; |
| 94 | } |
| 95 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 96 | bool TestClient::CheckNextPacket(const char* buf, |
| 97 | size_t size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | SocketAddress* addr) { |
| 99 | bool res = false; |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 100 | std::unique_ptr<Packet> packet = NextPacket(kTimeoutMs); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 101 | if (packet) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 102 | res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 && |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 103 | CheckTimestamp(packet->packet_time_us)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | if (addr) |
| 105 | *addr = packet->addr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | } |
| 107 | return res; |
| 108 | } |
| 109 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 110 | bool TestClient::CheckTimestamp(int64_t packet_timestamp) { |
| 111 | bool res = true; |
| 112 | if (packet_timestamp == -1) { |
| 113 | res = false; |
| 114 | } |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 115 | if (prev_packet_timestamp_ != -1) { |
| 116 | if (packet_timestamp < prev_packet_timestamp_) { |
| 117 | res = false; |
| 118 | } |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 119 | } |
| 120 | prev_packet_timestamp_ = packet_timestamp; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 121 | return res; |
| 122 | } |
| 123 | |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 124 | void TestClient::AdvanceTime(int ms) { |
| 125 | // If the test is using a fake clock, we must advance the fake clock to |
| 126 | // advance time. Otherwise, ProcessMessages will work. |
| 127 | if (fake_clock_) { |
| 128 | SIMULATED_WAIT(false, ms, *fake_clock_); |
| 129 | } else { |
| 130 | Thread::Current()->ProcessMessages(1); |
| 131 | } |
| 132 | } |
| 133 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 134 | bool TestClient::CheckNoPacket() { |
nisse | 32f2505 | 2017-05-08 01:57:18 -0700 | [diff] [blame] | 135 | return NextPacket(kNoPacketTimeoutMs) == nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | int TestClient::GetError() { |
| 139 | return socket_->GetError(); |
| 140 | } |
| 141 | |
| 142 | int TestClient::SetOption(Socket::Option opt, int value) { |
| 143 | return socket_->SetOption(opt, value); |
| 144 | } |
| 145 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | void TestClient::OnPacket(AsyncPacketSocket* socket, |
| 147 | const char* buf, |
| 148 | size_t size, |
| 149 | const SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 150 | const int64_t& packet_time_us) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 151 | CritScope cs(&crit_); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 152 | packets_.push_back( |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 153 | absl::make_unique<Packet>(remote_addr, buf, size, packet_time_us)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void TestClient::OnReadyToSend(AsyncPacketSocket* socket) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 157 | ++ready_to_send_count_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 160 | TestClient::Packet::Packet(const SocketAddress& a, |
| 161 | const char* b, |
| 162 | size_t s, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 163 | int64_t packet_time_us) |
| 164 | : addr(a), buf(0), size(s), packet_time_us(packet_time_us) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 165 | buf = new char[size]; |
| 166 | memcpy(buf, b, size); |
| 167 | } |
| 168 | |
| 169 | TestClient::Packet::Packet(const Packet& p) |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 170 | : addr(p.addr), buf(0), size(p.size), packet_time_us(p.packet_time_us) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 171 | buf = new char[size]; |
| 172 | memcpy(buf, p.buf, size); |
| 173 | } |
| 174 | |
| 175 | TestClient::Packet::~Packet() { |
| 176 | delete[] buf; |
| 177 | } |
| 178 | |
| 179 | } // namespace rtc |