blob: e05488052bbd13f82d5400db4a234d1487d47311 [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#ifndef RTC_BASE_TESTUTILS_H_
12#define RTC_BASE_TESTUTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014// Utilities for testing rtc infrastructure in unittests
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
17#include <X11/Xlib.h>
18#include <X11/extensions/Xrandr.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020// X defines a few macros that stomp on types that gunit.h uses.
21#undef None
22#undef Bool
23#endif
24
25#include <algorithm>
26#include <map>
27#include <memory>
28#include <vector>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/arraysize.h"
30#include "rtc_base/asyncsocket.h"
31#include "rtc_base/checks.h"
32#include "rtc_base/gunit.h"
33#include "rtc_base/nethelpers.h"
34#include "rtc_base/pathutils.h"
35#include "rtc_base/stream.h"
36#include "rtc_base/stringencode.h"
37#include "rtc_base/stringutils.h"
38#include "rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039
40namespace webrtc {
41namespace testing {
42
43using namespace rtc;
44
45///////////////////////////////////////////////////////////////////////////////
46// StreamSink - Monitor asynchronously signalled events from StreamInterface
47// or AsyncSocket (which should probably be a StreamInterface.
48///////////////////////////////////////////////////////////////////////////////
49
50// Note: Any event that is an error is treaded as SSE_ERROR instead of that
51// event.
52
53enum StreamSinkEvent {
54 SSE_OPEN = SE_OPEN,
55 SSE_READ = SE_READ,
56 SSE_WRITE = SE_WRITE,
57 SSE_CLOSE = SE_CLOSE,
58 SSE_ERROR = 16
59};
60
61class StreamSink : public sigslot::has_slots<> {
62 public:
63 void Monitor(StreamInterface* stream) {
64 stream->SignalEvent.connect(this, &StreamSink::OnEvent);
65 events_.erase(stream);
66 }
67 void Unmonitor(StreamInterface* stream) {
68 stream->SignalEvent.disconnect(this);
69 // In case you forgot to unmonitor a previous object with this address
70 events_.erase(stream);
71 }
72 bool Check(StreamInterface* stream, StreamSinkEvent event, bool reset = true) {
73 return DoCheck(stream, event, reset);
74 }
75 int Events(StreamInterface* stream, bool reset = true) {
76 return DoEvents(stream, reset);
77 }
78
79 void Monitor(AsyncSocket* socket) {
80 socket->SignalConnectEvent.connect(this, &StreamSink::OnConnectEvent);
81 socket->SignalReadEvent.connect(this, &StreamSink::OnReadEvent);
82 socket->SignalWriteEvent.connect(this, &StreamSink::OnWriteEvent);
83 socket->SignalCloseEvent.connect(this, &StreamSink::OnCloseEvent);
84 // In case you forgot to unmonitor a previous object with this address
85 events_.erase(socket);
86 }
87 void Unmonitor(AsyncSocket* socket) {
88 socket->SignalConnectEvent.disconnect(this);
89 socket->SignalReadEvent.disconnect(this);
90 socket->SignalWriteEvent.disconnect(this);
91 socket->SignalCloseEvent.disconnect(this);
92 events_.erase(socket);
93 }
94 bool Check(AsyncSocket* socket, StreamSinkEvent event, bool reset = true) {
95 return DoCheck(socket, event, reset);
96 }
97 int Events(AsyncSocket* socket, bool reset = true) {
98 return DoEvents(socket, reset);
99 }
100
101 private:
102 typedef std::map<void*,int> EventMap;
103
104 void OnEvent(StreamInterface* stream, int events, int error) {
105 if (error) {
106 events = SSE_ERROR;
107 }
108 AddEvents(stream, events);
109 }
110 void OnConnectEvent(AsyncSocket* socket) {
111 AddEvents(socket, SSE_OPEN);
112 }
113 void OnReadEvent(AsyncSocket* socket) {
114 AddEvents(socket, SSE_READ);
115 }
116 void OnWriteEvent(AsyncSocket* socket) {
117 AddEvents(socket, SSE_WRITE);
118 }
119 void OnCloseEvent(AsyncSocket* socket, int error) {
120 AddEvents(socket, (0 == error) ? SSE_CLOSE : SSE_ERROR);
121 }
122
123 void AddEvents(void* obj, int events) {
124 EventMap::iterator it = events_.find(obj);
125 if (events_.end() == it) {
126 events_.insert(EventMap::value_type(obj, events));
127 } else {
128 it->second |= events;
129 }
130 }
131 bool DoCheck(void* obj, StreamSinkEvent event, bool reset) {
132 EventMap::iterator it = events_.find(obj);
133 if ((events_.end() == it) || (0 == (it->second & event))) {
134 return false;
135 }
136 if (reset) {
137 it->second &= ~event;
138 }
139 return true;
140 }
141 int DoEvents(void* obj, bool reset) {
142 EventMap::iterator it = events_.find(obj);
143 if (events_.end() == it)
144 return 0;
145 int events = it->second;
146 if (reset) {
147 it->second = 0;
148 }
149 return events;
150 }
151
152 EventMap events_;
153};
154
155///////////////////////////////////////////////////////////////////////////////
156// StreamSource - Implements stream interface and simulates asynchronous
157// events on the stream, without a network. Also buffers written data.
158///////////////////////////////////////////////////////////////////////////////
159
160class StreamSource : public StreamInterface {
161public:
162 StreamSource() {
163 Clear();
164 }
165
166 void Clear() {
167 readable_data_.clear();
168 written_data_.clear();
169 state_ = SS_CLOSED;
170 read_block_ = 0;
171 write_block_ = SIZE_UNKNOWN;
172 }
173 void QueueString(const char* data) {
174 QueueData(data, strlen(data));
175 }
176 void QueueStringF(const char* format, ...) {
177 va_list args;
178 va_start(args, format);
179 char buffer[1024];
180 size_t len = vsprintfn(buffer, sizeof(buffer), format, args);
181 RTC_CHECK(len < sizeof(buffer) - 1);
182 va_end(args);
183 QueueData(buffer, len);
184 }
185 void QueueData(const char* data, size_t len) {
186 readable_data_.insert(readable_data_.end(), data, data + len);
187 if ((SS_OPEN == state_) && (readable_data_.size() == len)) {
188 SignalEvent(this, SE_READ, 0);
189 }
190 }
191 std::string ReadData() {
192 std::string data;
193 // avoid accessing written_data_[0] if it is undefined
194 if (written_data_.size() > 0) {
195 data.insert(0, &written_data_[0], written_data_.size());
196 }
197 written_data_.clear();
198 return data;
199 }
200 void SetState(StreamState state) {
201 int events = 0;
202 if ((SS_OPENING == state_) && (SS_OPEN == state)) {
203 events |= SE_OPEN;
204 if (!readable_data_.empty()) {
205 events |= SE_READ;
206 }
207 } else if ((SS_CLOSED != state_) && (SS_CLOSED == state)) {
208 events |= SE_CLOSE;
209 }
210 state_ = state;
211 if (events) {
212 SignalEvent(this, events, 0);
213 }
214 }
215 // Will cause Read to block when there are pos bytes in the read queue.
216 void SetReadBlock(size_t pos) { read_block_ = pos; }
217 // Will cause Write to block when there are pos bytes in the write queue.
218 void SetWriteBlock(size_t pos) { write_block_ = pos; }
219
220 virtual StreamState GetState() const { return state_; }
221 virtual StreamResult Read(void* buffer, size_t buffer_len,
222 size_t* read, int* error) {
223 if (SS_CLOSED == state_) {
224 if (error) *error = -1;
225 return SR_ERROR;
226 }
227 if ((SS_OPENING == state_) || (readable_data_.size() <= read_block_)) {
228 return SR_BLOCK;
229 }
230 size_t count = std::min(buffer_len, readable_data_.size() - read_block_);
231 memcpy(buffer, &readable_data_[0], count);
232 size_t new_size = readable_data_.size() - count;
233 // Avoid undefined access beyond the last element of the vector.
234 // This only happens when new_size is 0.
235 if (count < readable_data_.size()) {
236 memmove(&readable_data_[0], &readable_data_[count], new_size);
237 }
238 readable_data_.resize(new_size);
239 if (read) *read = count;
240 return SR_SUCCESS;
241 }
242 virtual StreamResult Write(const void* data, size_t data_len,
243 size_t* written, int* error) {
244 if (SS_CLOSED == state_) {
245 if (error) *error = -1;
246 return SR_ERROR;
247 }
248 if (SS_OPENING == state_) {
249 return SR_BLOCK;
250 }
251 if (SIZE_UNKNOWN != write_block_) {
252 if (written_data_.size() >= write_block_) {
253 return SR_BLOCK;
254 }
255 if (data_len > (write_block_ - written_data_.size())) {
256 data_len = write_block_ - written_data_.size();
257 }
258 }
259 if (written) *written = data_len;
260 const char* cdata = static_cast<const char*>(data);
261 written_data_.insert(written_data_.end(), cdata, cdata + data_len);
262 return SR_SUCCESS;
263 }
264 virtual void Close() { state_ = SS_CLOSED; }
265
266private:
267 typedef std::vector<char> Buffer;
268 Buffer readable_data_, written_data_;
269 StreamState state_;
270 size_t read_block_, write_block_;
271};
272
273///////////////////////////////////////////////////////////////////////////////
274// SocketTestClient
275// Creates a simulated client for testing. Works on real and virtual networks.
276///////////////////////////////////////////////////////////////////////////////
277
278class SocketTestClient : public sigslot::has_slots<> {
279public:
280 SocketTestClient() { Init(nullptr, AF_INET); }
281 SocketTestClient(AsyncSocket* socket) {
282 Init(socket, socket->GetLocalAddress().family());
283 }
284 SocketTestClient(const SocketAddress& address) {
285 Init(nullptr, address.family());
286 socket_->Connect(address);
287 }
288
289 AsyncSocket* socket() { return socket_.get(); }
290
291 void QueueString(const char* data) {
292 QueueData(data, strlen(data));
293 }
294 void QueueStringF(const char* format, ...) {
295 va_list args;
296 va_start(args, format);
297 char buffer[1024];
298 size_t len = vsprintfn(buffer, sizeof(buffer), format, args);
299 RTC_CHECK(len < sizeof(buffer) - 1);
300 va_end(args);
301 QueueData(buffer, len);
302 }
303 void QueueData(const char* data, size_t len) {
304 send_buffer_.insert(send_buffer_.end(), data, data + len);
305 if (Socket::CS_CONNECTED == socket_->GetState()) {
306 Flush();
307 }
308 }
309 std::string ReadData() {
310 std::string data(&recv_buffer_[0], recv_buffer_.size());
311 recv_buffer_.clear();
312 return data;
313 }
314
315 bool IsConnected() const {
316 return (Socket::CS_CONNECTED == socket_->GetState());
317 }
318 bool IsClosed() const {
319 return (Socket::CS_CLOSED == socket_->GetState());
320 }
321
322private:
323 typedef std::vector<char> Buffer;
324
325 void Init(AsyncSocket* socket, int family) {
326 if (!socket) {
327 socket = Thread::Current()->socketserver()
328 ->CreateAsyncSocket(family, SOCK_STREAM);
329 }
330 socket_.reset(socket);
331 socket_->SignalConnectEvent.connect(this,
332 &SocketTestClient::OnConnectEvent);
333 socket_->SignalReadEvent.connect(this, &SocketTestClient::OnReadEvent);
334 socket_->SignalWriteEvent.connect(this, &SocketTestClient::OnWriteEvent);
335 socket_->SignalCloseEvent.connect(this, &SocketTestClient::OnCloseEvent);
336 }
337
338 void Flush() {
339 size_t sent = 0;
340 while (sent < send_buffer_.size()) {
341 int result = socket_->Send(&send_buffer_[sent],
342 send_buffer_.size() - sent);
343 if (result > 0) {
344 sent += result;
345 } else {
346 break;
347 }
348 }
349 size_t new_size = send_buffer_.size() - sent;
350 memmove(&send_buffer_[0], &send_buffer_[sent], new_size);
351 send_buffer_.resize(new_size);
352 }
353
354 void OnConnectEvent(AsyncSocket* socket) {
355 if (!send_buffer_.empty()) {
356 Flush();
357 }
358 }
359 void OnReadEvent(AsyncSocket* socket) {
360 char data[64 * 1024];
361 int result = socket_->Recv(data, arraysize(data), nullptr);
362 if (result > 0) {
363 recv_buffer_.insert(recv_buffer_.end(), data, data + result);
364 }
365 }
366 void OnWriteEvent(AsyncSocket* socket) {
367 if (!send_buffer_.empty()) {
368 Flush();
369 }
370 }
371 void OnCloseEvent(AsyncSocket* socket, int error) {
372 }
373
374 std::unique_ptr<AsyncSocket> socket_;
375 Buffer send_buffer_, recv_buffer_;
376};
377
378///////////////////////////////////////////////////////////////////////////////
379// SocketTestServer
380// Creates a simulated server for testing. Works on real and virtual networks.
381///////////////////////////////////////////////////////////////////////////////
382
383class SocketTestServer : public sigslot::has_slots<> {
384 public:
385 SocketTestServer(const SocketAddress& address)
386 : socket_(Thread::Current()->socketserver()
387 ->CreateAsyncSocket(address.family(), SOCK_STREAM))
388 {
389 socket_->SignalReadEvent.connect(this, &SocketTestServer::OnReadEvent);
390 socket_->Bind(address);
391 socket_->Listen(5);
392 }
393 virtual ~SocketTestServer() {
394 clear();
395 }
396
397 size_t size() const { return clients_.size(); }
398 SocketTestClient* client(size_t index) const { return clients_[index]; }
399 SocketTestClient* operator[](size_t index) const { return client(index); }
400
401 void clear() {
402 for (size_t i=0; i<clients_.size(); ++i) {
403 delete clients_[i];
404 }
405 clients_.clear();
406 }
407
408 private:
409 void OnReadEvent(AsyncSocket* socket) {
410 AsyncSocket* accepted = static_cast<AsyncSocket*>(socket_->Accept(nullptr));
411 if (!accepted)
412 return;
413 clients_.push_back(new SocketTestClient(accepted));
414 }
415
416 std::unique_ptr<AsyncSocket> socket_;
417 std::vector<SocketTestClient*> clients_;
418};
419
420///////////////////////////////////////////////////////////////////////////////
421// Unittest predicates which are similar to STREQ, but for raw memory
422///////////////////////////////////////////////////////////////////////////////
423
424inline ::testing::AssertionResult CmpHelperMemEq(
425 const char* expected_expression,
426 const char* expected_length_expression,
427 const char* actual_expression,
428 const char* actual_length_expression,
429 const void* expected,
430 size_t expected_length,
431 const void* actual,
432 size_t actual_length) {
433 if ((expected_length == actual_length)
434 && (0 == memcmp(expected, actual, expected_length))) {
435 return ::testing::AssertionSuccess();
436 }
437
438 ::testing::Message msg;
439 msg << "Value of: " << actual_expression
440 << " [" << actual_length_expression << "]";
441 if (true) { //!actual_value.Equals(actual_expression)) {
442 size_t buffer_size = actual_length * 2 + 1;
443 char* buffer = STACK_ARRAY(char, buffer_size);
444 hex_encode(buffer, buffer_size,
445 reinterpret_cast<const char*>(actual), actual_length);
446 msg << "\n Actual: " << buffer << " [" << actual_length << "]";
447 }
448
449 msg << "\nExpected: " << expected_expression
450 << " [" << expected_length_expression << "]";
451 if (true) { //!expected_value.Equals(expected_expression)) {
452 size_t buffer_size = expected_length * 2 + 1;
453 char* buffer = STACK_ARRAY(char, buffer_size);
454 hex_encode(buffer, buffer_size,
455 reinterpret_cast<const char*>(expected), expected_length);
456 msg << "\nWhich is: " << buffer << " [" << expected_length << "]";
457 }
458
459 return AssertionFailure(msg);
460}
461
462#define EXPECT_MEMEQ(expected, expected_length, actual, actual_length) \
463 EXPECT_PRED_FORMAT4(::testing::CmpHelperMemEq, expected, expected_length, \
464 actual, actual_length)
465
466#define ASSERT_MEMEQ(expected, expected_length, actual, actual_length) \
467 ASSERT_PRED_FORMAT4(::testing::CmpHelperMemEq, expected, expected_length, \
468 actual, actual_length)
469
470///////////////////////////////////////////////////////////////////////////////
471// Helpers for initializing constant memory with integers in a particular byte
472// order
473///////////////////////////////////////////////////////////////////////////////
474
475#define BYTE_CAST(x) static_cast<uint8_t>((x)&0xFF)
476
477// Declare a N-bit integer as a little-endian sequence of bytes
478#define LE16(x) BYTE_CAST(((uint16_t)x) >> 0), BYTE_CAST(((uint16_t)x) >> 8)
479
480#define LE32(x) \
481 BYTE_CAST(((uint32_t)x) >> 0), BYTE_CAST(((uint32_t)x) >> 8), \
482 BYTE_CAST(((uint32_t)x) >> 16), BYTE_CAST(((uint32_t)x) >> 24)
483
484#define LE64(x) \
485 BYTE_CAST(((uint64_t)x) >> 0), BYTE_CAST(((uint64_t)x) >> 8), \
486 BYTE_CAST(((uint64_t)x) >> 16), BYTE_CAST(((uint64_t)x) >> 24), \
487 BYTE_CAST(((uint64_t)x) >> 32), BYTE_CAST(((uint64_t)x) >> 40), \
488 BYTE_CAST(((uint64_t)x) >> 48), BYTE_CAST(((uint64_t)x) >> 56)
489
490// Declare a N-bit integer as a big-endian (Internet) sequence of bytes
491#define BE16(x) BYTE_CAST(((uint16_t)x) >> 8), BYTE_CAST(((uint16_t)x) >> 0)
492
493#define BE32(x) \
494 BYTE_CAST(((uint32_t)x) >> 24), BYTE_CAST(((uint32_t)x) >> 16), \
495 BYTE_CAST(((uint32_t)x) >> 8), BYTE_CAST(((uint32_t)x) >> 0)
496
497#define BE64(x) \
498 BYTE_CAST(((uint64_t)x) >> 56), BYTE_CAST(((uint64_t)x) >> 48), \
499 BYTE_CAST(((uint64_t)x) >> 40), BYTE_CAST(((uint64_t)x) >> 32), \
500 BYTE_CAST(((uint64_t)x) >> 24), BYTE_CAST(((uint64_t)x) >> 16), \
501 BYTE_CAST(((uint64_t)x) >> 8), BYTE_CAST(((uint64_t)x) >> 0)
502
503// Declare a N-bit integer as a this-endian (local machine) sequence of bytes
504#ifndef BIG_ENDIAN
505#define BIG_ENDIAN 1
506#endif // BIG_ENDIAN
507
508#if BIG_ENDIAN
509#define TE16 BE16
510#define TE32 BE32
511#define TE64 BE64
512#else // !BIG_ENDIAN
513#define TE16 LE16
514#define TE32 LE32
515#define TE64 LE64
516#endif // !BIG_ENDIAN
517
518///////////////////////////////////////////////////////////////////////////////
519
520// Helpers for determining if X/screencasting is available (on linux).
521
522#define MAYBE_SKIP_SCREENCAST_TEST() \
523 if (!testing::IsScreencastingAvailable()) { \
524 LOG(LS_WARNING) << "Skipping test, since it doesn't have the requisite " \
525 << "X environment for screen capture."; \
526 return; \
527 } \
528
529#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
530struct XDisplay {
531 XDisplay() : display_(XOpenDisplay(nullptr)) {}
532 ~XDisplay() { if (display_) XCloseDisplay(display_); }
533 bool IsValid() const { return display_ != nullptr; }
534 operator Display*() { return display_; }
535 private:
536 Display* display_;
537};
538#endif
539
540// Returns true if screencasting is available. When false, anything that uses
541// screencasting features may fail.
542inline bool IsScreencastingAvailable() {
543#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
544 XDisplay display;
545 if (!display.IsValid()) {
546 LOG(LS_WARNING) << "No X Display available.";
547 return false;
548 }
549 int ignored_int, major_version, minor_version;
550 if (!XRRQueryExtension(display, &ignored_int, &ignored_int) ||
551 !XRRQueryVersion(display, &major_version, &minor_version) ||
552 major_version < 1 ||
553 (major_version < 2 && minor_version < 3)) {
554 LOG(LS_WARNING) << "XRandr version: " << major_version << "."
555 << minor_version;
556 LOG(LS_WARNING) << "XRandr is not supported or is too old (pre 1.3).";
557 return false;
558 }
559#endif
560 return true;
561}
562
563} // namespace testing
564} // namespace webrtc
565
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200566#endif // RTC_BASE_TESTUTILS_H_