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