blob: 585db77785fbf78901f9aa2a2b16fff2f7f9b966 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/testclient.h"
deadbeef22e08142017-06-12 14:30:28 -070012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/gunit.h"
14#include "rtc_base/ptr_util.h"
15#include "rtc_base/thread.h"
16#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
18namespace rtc {
19
20// DESIGN: Each packet received is put it into a list of packets.
21// Callers can retrieve received packets from any thread by calling
22// NextPacket.
23
nisse32f25052017-05-08 01:57:18 -070024TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket)
deadbeef22e08142017-06-12 14:30:28 -070025 : TestClient(std::move(socket), nullptr) {}
26
27TestClient::TestClient(std::unique_ptr<AsyncPacketSocket> socket,
28 FakeClock* fake_clock)
29 : fake_clock_(fake_clock),
30 socket_(std::move(socket)),
31 prev_packet_timestamp_(-1) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032 socket_->SignalReadPacket.connect(this, &TestClient::OnPacket);
33 socket_->SignalReadyToSend.connect(this, &TestClient::OnReadyToSend);
34}
35
nisse32f25052017-05-08 01:57:18 -070036TestClient::~TestClient() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037
38bool TestClient::CheckConnState(AsyncPacketSocket::State state) {
39 // Wait for our timeout value until the socket reaches the desired state.
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070040 int64_t end = TimeAfter(kTimeoutMs);
41 while (socket_->GetState() != state && TimeUntil(end) > 0) {
deadbeef22e08142017-06-12 14:30:28 -070042 AdvanceTime(1);
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070043 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044 return (socket_->GetState() == state);
45}
46
47int TestClient::Send(const char* buf, size_t size) {
48 rtc::PacketOptions options;
49 return socket_->Send(buf, size, options);
50}
51
52int TestClient::SendTo(const char* buf, size_t size,
53 const SocketAddress& dest) {
54 rtc::PacketOptions options;
55 return socket_->SendTo(buf, size, dest, options);
56}
57
nisse32f25052017-05-08 01:57:18 -070058std::unique_ptr<TestClient::Packet> TestClient::NextPacket(int timeout_ms) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059 // If no packets are currently available, we go into a get/dispatch loop for
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000060 // at most timeout_ms. If, during the loop, a packet arrives, then we can
61 // stop early and return it.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062
63 // Note that the case where no packet arrives is important. We often want to
64 // test that a packet does not arrive.
65
66 // Note also that we only try to pump our current thread's message queue.
67 // Pumping another thread's queue could lead to messages being dispatched from
68 // the wrong thread to non-thread-safe objects.
69
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070070 int64_t end = TimeAfter(timeout_ms);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071 while (TimeUntil(end) > 0) {
72 {
73 CritScope cs(&crit_);
nisse32f25052017-05-08 01:57:18 -070074 if (packets_.size() != 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 break;
76 }
77 }
deadbeef22e08142017-06-12 14:30:28 -070078 AdvanceTime(1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 }
80
81 // Return the first packet placed in the queue.
nisse32f25052017-05-08 01:57:18 -070082 std::unique_ptr<Packet> packet;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 CritScope cs(&crit_);
nisse32f25052017-05-08 01:57:18 -070084 if (packets_.size() > 0) {
85 packet = std::move(packets_.front());
86 packets_.erase(packets_.begin());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087 }
88
89 return packet;
90}
91
92bool TestClient::CheckNextPacket(const char* buf, size_t size,
93 SocketAddress* addr) {
94 bool res = false;
nisse32f25052017-05-08 01:57:18 -070095 std::unique_ptr<Packet> packet = NextPacket(kTimeoutMs);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096 if (packet) {
Stefan Holmer9131efd2016-05-23 18:19:26 +020097 res = (packet->size == size && memcmp(packet->buf, buf, size) == 0 &&
98 CheckTimestamp(packet->packet_time.timestamp));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099 if (addr)
100 *addr = packet->addr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101 }
102 return res;
103}
104
Stefan Holmer9131efd2016-05-23 18:19:26 +0200105bool TestClient::CheckTimestamp(int64_t packet_timestamp) {
106 bool res = true;
107 if (packet_timestamp == -1) {
108 res = false;
109 }
Stefan Holmer9131efd2016-05-23 18:19:26 +0200110 if (prev_packet_timestamp_ != -1) {
111 if (packet_timestamp < prev_packet_timestamp_) {
112 res = false;
113 }
Stefan Holmer9131efd2016-05-23 18:19:26 +0200114 }
115 prev_packet_timestamp_ = packet_timestamp;
Stefan Holmer9131efd2016-05-23 18:19:26 +0200116 return res;
117}
118
deadbeef22e08142017-06-12 14:30:28 -0700119void TestClient::AdvanceTime(int ms) {
120 // If the test is using a fake clock, we must advance the fake clock to
121 // advance time. Otherwise, ProcessMessages will work.
122 if (fake_clock_) {
123 SIMULATED_WAIT(false, ms, *fake_clock_);
124 } else {
125 Thread::Current()->ProcessMessages(1);
126 }
127}
128
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129bool TestClient::CheckNoPacket() {
nisse32f25052017-05-08 01:57:18 -0700130 return NextPacket(kNoPacketTimeoutMs) == nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131}
132
133int TestClient::GetError() {
134 return socket_->GetError();
135}
136
137int TestClient::SetOption(Socket::Option opt, int value) {
138 return socket_->SetOption(opt, value);
139}
140
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141void TestClient::OnPacket(AsyncPacketSocket* socket, const char* buf,
142 size_t size, const SocketAddress& remote_addr,
143 const PacketTime& packet_time) {
144 CritScope cs(&crit_);
nisse32f25052017-05-08 01:57:18 -0700145 packets_.push_back(MakeUnique<Packet>(remote_addr, buf, size, packet_time));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146}
147
148void TestClient::OnReadyToSend(AsyncPacketSocket* socket) {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700149 ++ready_to_send_count_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000150}
151
Stefan Holmer9131efd2016-05-23 18:19:26 +0200152TestClient::Packet::Packet(const SocketAddress& a,
153 const char* b,
154 size_t s,
155 const PacketTime& packet_time)
156 : addr(a), buf(0), size(s), packet_time(packet_time) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157 buf = new char[size];
158 memcpy(buf, b, size);
159}
160
161TestClient::Packet::Packet(const Packet& p)
Stefan Holmer9131efd2016-05-23 18:19:26 +0200162 : addr(p.addr), buf(0), size(p.size), packet_time(p.packet_time) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163 buf = new char[size];
164 memcpy(buf, p.buf, size);
165}
166
167TestClient::Packet::~Packet() {
168 delete[] buf;
169}
170
171} // namespace rtc