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