blob: e5aa9d7987cf19fac73093ede4cb9f57c39f742a [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/test_client.h"
deadbeef22e08142017-06-12 14:30:28 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <utility>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
22namespace rtc {
23
24// DESIGN: Each packet received is put it into a list of packets.
25// Callers can retrieve received packets from any thread by calling
26// NextPacket.
27
nisse32f25052017-05-08 01:57:18 -070028TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket)
deadbeef22e08142017-06-12 14:30:28 -070029 : TestClient(std::move(socket), nullptr) {}
30
31TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket,
Sebastian Janssond624c392019-04-17 10:36:03 +020032 ThreadProcessingFakeClock* fake_clock)
deadbeef22e08142017-06-12 14:30:28 -070033 : fake_clock_(fake_clock),
34 socket_(std::move(socket)),
35 prev_packet_timestamp_(-1) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket);
37 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend);
38}
39
nisse32f25052017-05-08 01:57:18 -070040TestClient::~TestClient() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041
42bool TestClient::CheckConnState(AsyncPacketSocket::State state) {
43 // Wait for our timeout value until the socket reaches the desired state.
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070044 int64_t end = TimeAfter(kTimeoutMs);
45 while (socket_->GetState() != state && TimeUntil(end) > 0) {
deadbeef22e08142017-06-12 14:30:28 -070046 AdvanceTime(1);
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070047 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 return (socket_->GetState() == state);
49}
50
51int TestClient::Send(const char* buf, size_t size) {
52 rtc::PacketOptions options;
53 return socket_->Send(buf, size, options);
54}
55
Yves Gerey665174f2018-06-19 15:03:05 +020056int TestClient::SendTo(const char* buf,
57 size_t size,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058 const SocketAddress& dest) {
59 rtc::PacketOptions options;
60 return socket_->SendTo(buf, size, dest, options);
61}
62
nisse32f25052017-05-08 01:57:18 -070063std::unique_ptr<TestClient::Packet> TestClient::NextPacket(int timeout_ms) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064 // If no packets are currently available, we go into a get/dispatch loop for
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000065 // at most timeout_ms. If, during the loop, a packet arrives, then we can
66 // stop early and return it.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000067
68 // Note that the case where no packet arrives is important. We often want to
69 // test that a packet does not arrive.
70
71 // Note also that we only try to pump our current thread's message queue.
72 // Pumping another thread's queue could lead to messages being dispatched from
73 // the wrong thread to non-thread-safe objects.
74
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070075 int64_t end = TimeAfter(timeout_ms);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 while (TimeUntil(end) > 0) {
77 {
78 CritScope cs(&crit_);
nisse32f25052017-05-08 01:57:18 -070079 if (packets_.size() != 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 break;
81 }
82 }
deadbeef22e08142017-06-12 14:30:28 -070083 AdvanceTime(1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084 }
85
86 // Return the first packet placed in the queue.
nisse32f25052017-05-08 01:57:18 -070087 std::unique_ptr<Packet> packet;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088 CritScope cs(&crit_);
nisse32f25052017-05-08 01:57:18 -070089 if (packets_.size() > 0) {
90 packet = std::move(packets_.front());
91 packets_.erase(packets_.begin());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 }
93
94 return packet;
95}
96
Yves Gerey665174f2018-06-19 15:03:05 +020097bool TestClient::CheckNextPacket(const char* buf,
98 size_t size,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099 SocketAddress* addr) {
100 bool res = false;
nisse32f25052017-05-08 01:57:18 -0700101 std::unique_ptr<Packet> packet = NextPacket(kTimeoutMs);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102 if (packet) {
Stefan Holmer9131efd2016-05-23 18:19:26 +0200103 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 &&
Niels Möllere6933812018-11-05 13:01:41 +0100104 CheckTimestamp(packet->packet_time_us));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 if (addr)
106 *addr = packet->addr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107 }
108 return res;
109}
110
Stefan Holmer9131efd2016-05-23 18:19:26 +0200111bool TestClient::CheckTimestamp(int64_t packet_timestamp) {
112 bool res = true;
113 if (packet_timestamp == -1) {
114 res = false;
115 }
Stefan Holmer9131efd2016-05-23 18:19:26 +0200116 if (prev_packet_timestamp_ != -1) {
117 if (packet_timestamp < prev_packet_timestamp_) {
118 res = false;
119 }
Stefan Holmer9131efd2016-05-23 18:19:26 +0200120 }
121 prev_packet_timestamp_ = packet_timestamp;
Stefan Holmer9131efd2016-05-23 18:19:26 +0200122 return res;
123}
124
deadbeef22e08142017-06-12 14:30:28 -0700125void TestClient::AdvanceTime(int ms) {
126 // If the test is using a fake clock, we must advance the fake clock to
127 // advance time. Otherwise, ProcessMessages will work.
128 if (fake_clock_) {
129 SIMULATED_WAIT(false, ms, *fake_clock_);
130 } else {
131 Thread::Current()->ProcessMessages(1);
132 }
133}
134
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135bool TestClient::CheckNoPacket() {
nisse32f25052017-05-08 01:57:18 -0700136 return NextPacket(kNoPacketTimeoutMs) == nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137}
138
139int TestClient::GetError() {
140 return socket_->GetError();
141}
142
143int TestClient::SetOption(Socket::Option opt, int value) {
144 return socket_->SetOption(opt, value);
145}
146
Yves Gerey665174f2018-06-19 15:03:05 +0200147void TestClient::OnPacket(AsyncPacketSocket* socket,
148 const char* buf,
149 size_t size,
150 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100151 const int64_t& packet_time_us) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 CritScope cs(&crit_);
Karl Wiberg918f50c2018-07-05 11:40:33 +0200153 packets_.push_back(
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200154 std::make_unique<Packet>(remote_addr, buf, size, packet_time_us));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000155}
156
157void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700158 ++ready_to_send_count_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159}
160
Stefan Holmer9131efd2016-05-23 18:19:26 +0200161TestClient::Packet::Packet(const SocketAddress& a,
162 const char* b,
163 size_t s,
Niels Möllere6933812018-11-05 13:01:41 +0100164 int64_t packet_time_us)
165 : addr(a), buf(0), size(s), packet_time_us(packet_time_us) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166 buf = new char[size];
167 memcpy(buf, b, size);
168}
169
170TestClient::Packet::Packet(const Packet& p)
Niels Möllere6933812018-11-05 13:01:41 +0100171 : addr(p.addr), buf(0), size(p.size), packet_time_us(p.packet_time_us) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172 buf = new char[size];
173 memcpy(buf, p.buf, size);
174}
175
176TestClient::Packet::~Packet() {
177 delete[] buf;
178}
179
180} // namespace rtc