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