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