You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

570 lines
19 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef TALK_BASE_TESTUTILS_H__
  28. #define TALK_BASE_TESTUTILS_H__
  29. // Utilities for testing talk_base infrastructure in unittests
  30. #include <map>
  31. #include <vector>
  32. #include "talk/base/asyncsocket.h"
  33. #include "talk/base/common.h"
  34. #include "talk/base/gunit.h"
  35. #include "talk/base/nethelpers.h"
  36. #include "talk/base/stream.h"
  37. #include "talk/base/stringencode.h"
  38. #include "talk/base/stringutils.h"
  39. #include "talk/base/thread.h"
  40. namespace testing {
  41. using namespace talk_base;
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // StreamSink - Monitor asynchronously signalled events from StreamInterface
  44. // or AsyncSocket (which should probably be a StreamInterface.
  45. ///////////////////////////////////////////////////////////////////////////////
  46. // Note: Any event that is an error is treaded as SSE_ERROR instead of that
  47. // event.
  48. enum StreamSinkEvent {
  49. SSE_OPEN = SE_OPEN,
  50. SSE_READ = SE_READ,
  51. SSE_WRITE = SE_WRITE,
  52. SSE_CLOSE = SE_CLOSE,
  53. SSE_ERROR = 16
  54. };
  55. class StreamSink : public sigslot::has_slots<> {
  56. public:
  57. void Monitor(StreamInterface* stream) {
  58. stream->SignalEvent.connect(this, &StreamSink::OnEvent);
  59. events_.erase(stream);
  60. }
  61. void Unmonitor(StreamInterface* stream) {
  62. stream->SignalEvent.disconnect(this);
  63. // In case you forgot to unmonitor a previous object with this address
  64. events_.erase(stream);
  65. }
  66. bool Check(StreamInterface* stream, StreamSinkEvent event, bool reset = true) {
  67. return DoCheck(stream, event, reset);
  68. }
  69. int Events(StreamInterface* stream, bool reset = true) {
  70. return DoEvents(stream, reset);
  71. }
  72. void Monitor(AsyncSocket* socket) {
  73. socket->SignalConnectEvent.connect(this, &StreamSink::OnConnectEvent);
  74. socket->SignalReadEvent.connect(this, &StreamSink::OnReadEvent);
  75. socket->SignalWriteEvent.connect(this, &StreamSink::OnWriteEvent);
  76. socket->SignalCloseEvent.connect(this, &StreamSink::OnCloseEvent);
  77. // In case you forgot to unmonitor a previous object with this address
  78. events_.erase(socket);
  79. }
  80. void Unmonitor(AsyncSocket* socket) {
  81. socket->SignalConnectEvent.disconnect(this);
  82. socket->SignalReadEvent.disconnect(this);
  83. socket->SignalWriteEvent.disconnect(this);
  84. socket->SignalCloseEvent.disconnect(this);
  85. events_.erase(socket);
  86. }
  87. bool Check(AsyncSocket* socket, StreamSinkEvent event, bool reset = true) {
  88. return DoCheck(socket, event, reset);
  89. }
  90. int Events(AsyncSocket* socket, bool reset = true) {
  91. return DoEvents(socket, reset);
  92. }
  93. private:
  94. typedef std::map<void*,int> EventMap;
  95. void OnEvent(StreamInterface* stream, int events, int error) {
  96. if (error) {
  97. events = SSE_ERROR;
  98. }
  99. AddEvents(stream, events);
  100. }
  101. void OnConnectEvent(AsyncSocket* socket) {
  102. AddEvents(socket, SSE_OPEN);
  103. }
  104. void OnReadEvent(AsyncSocket* socket) {
  105. AddEvents(socket, SSE_READ);
  106. }
  107. void OnWriteEvent(AsyncSocket* socket) {
  108. AddEvents(socket, SSE_WRITE);
  109. }
  110. void OnCloseEvent(AsyncSocket* socket, int error) {
  111. AddEvents(socket, (0 == error) ? SSE_CLOSE : SSE_ERROR);
  112. }
  113. void AddEvents(void* obj, int events) {
  114. EventMap::iterator it = events_.find(obj);
  115. if (events_.end() == it) {
  116. events_.insert(EventMap::value_type(obj, events));
  117. } else {
  118. it->second |= events;
  119. }
  120. }
  121. bool DoCheck(void* obj, StreamSinkEvent event, bool reset) {
  122. EventMap::iterator it = events_.find(obj);
  123. if ((events_.end() == it) || (0 == (it->second & event))) {
  124. return false;
  125. }
  126. if (reset) {
  127. it->second &= ~event;
  128. }
  129. return true;
  130. }
  131. int DoEvents(void* obj, bool reset) {
  132. EventMap::iterator it = events_.find(obj);
  133. if (events_.end() == it)
  134. return 0;
  135. int events = it->second;
  136. if (reset) {
  137. it->second = 0;
  138. }
  139. return events;
  140. }
  141. EventMap events_;
  142. };
  143. ///////////////////////////////////////////////////////////////////////////////
  144. // StreamSource - Implements stream interface and simulates asynchronous
  145. // events on the stream, without a network. Also buffers written data.
  146. ///////////////////////////////////////////////////////////////////////////////
  147. class StreamSource : public StreamInterface {
  148. public:
  149. StreamSource() {
  150. Clear();
  151. }
  152. void Clear() {
  153. readable_data_.clear();
  154. written_data_.clear();
  155. state_ = SS_CLOSED;
  156. read_block_ = 0;
  157. write_block_ = SIZE_UNKNOWN;
  158. }
  159. void QueueString(const char* data) {
  160. QueueData(data, strlen(data));
  161. }
  162. void QueueStringF(const char* format, ...) {
  163. va_list args;
  164. va_start(args, format);
  165. char buffer[1024];
  166. size_t len = vsprintfn(buffer, sizeof(buffer), format, args);
  167. ASSERT(len < sizeof(buffer) - 1);
  168. va_end(args);
  169. QueueData(buffer, len);
  170. }
  171. void QueueData(const char* data, size_t len) {
  172. readable_data_.insert(readable_data_.end(), data, data + len);
  173. if ((SS_OPEN == state_) && (readable_data_.size() == len)) {
  174. SignalEvent(this, SE_READ, 0);
  175. }
  176. }
  177. std::string ReadData() {
  178. std::string data;
  179. // avoid accessing written_data_[0] if it is undefined
  180. if (written_data_.size() > 0) {
  181. data.insert(0, &written_data_[0], written_data_.size());
  182. }
  183. written_data_.clear();
  184. return data;
  185. }
  186. void SetState(StreamState state) {
  187. int events = 0;
  188. if ((SS_OPENING == state_) && (SS_OPEN == state)) {
  189. events |= SE_OPEN;
  190. if (!readable_data_.empty()) {
  191. events |= SE_READ;
  192. }
  193. } else if ((SS_CLOSED != state_) && (SS_CLOSED == state)) {
  194. events |= SE_CLOSE;
  195. }
  196. state_ = state;
  197. if (events) {
  198. SignalEvent(this, events, 0);
  199. }
  200. }
  201. // Will cause Read to block when there are pos bytes in the read queue.
  202. void SetReadBlock(size_t pos) { read_block_ = pos; }
  203. // Will cause Write to block when there are pos bytes in the write queue.
  204. void SetWriteBlock(size_t pos) { write_block_ = pos; }
  205. virtual StreamState GetState() const { return state_; }
  206. virtual StreamResult Read(void* buffer, size_t buffer_len,
  207. size_t* read, int* error) {
  208. if (SS_CLOSED == state_) {
  209. if (error) *error = -1;
  210. return SR_ERROR;
  211. }
  212. if ((SS_OPENING == state_) || (readable_data_.size() <= read_block_)) {
  213. return SR_BLOCK;
  214. }
  215. size_t count = _min(buffer_len, readable_data_.size() - read_block_);
  216. memcpy(buffer, &readable_data_[0], count);
  217. size_t new_size = readable_data_.size() - count;
  218. // Avoid undefined access beyond the last element of the vector.
  219. // This only happens when new_size is 0.
  220. if (count < readable_data_.size()) {
  221. memmove(&readable_data_[0], &readable_data_[count], new_size);
  222. }
  223. readable_data_.resize(new_size);
  224. if (read) *read = count;
  225. return SR_SUCCESS;
  226. }
  227. virtual StreamResult Write(const void* data, size_t data_len,
  228. size_t* written, int* error) {
  229. if (SS_CLOSED == state_) {
  230. if (error) *error = -1;
  231. return SR_ERROR;
  232. }
  233. if (SS_OPENING == state_) {
  234. return SR_BLOCK;
  235. }
  236. if (SIZE_UNKNOWN != write_block_) {
  237. if (written_data_.size() >= write_block_) {
  238. return SR_BLOCK;
  239. }
  240. if (data_len > (write_block_ - written_data_.size())) {
  241. data_len = write_block_ - written_data_.size();
  242. }
  243. }
  244. if (written) *written = data_len;
  245. const char* cdata = static_cast<const char*>(data);
  246. written_data_.insert(written_data_.end(), cdata, cdata + data_len);
  247. return SR_SUCCESS;
  248. }
  249. virtual void Close() { state_ = SS_CLOSED; }
  250. private:
  251. typedef std::vector<char> Buffer;
  252. Buffer readable_data_, written_data_;
  253. StreamState state_;
  254. size_t read_block_, write_block_;
  255. };
  256. ///////////////////////////////////////////////////////////////////////////////
  257. // SocketTestClient
  258. // Creates a simulated client for testing. Works on real and virtual networks.
  259. ///////////////////////////////////////////////////////////////////////////////
  260. class SocketTestClient : public sigslot::has_slots<> {
  261. public:
  262. SocketTestClient() {
  263. Init(NULL, AF_INET);
  264. }
  265. SocketTestClient(AsyncSocket* socket) {
  266. Init(socket, socket->GetLocalAddress().family());
  267. }
  268. SocketTestClient(const SocketAddress& address) {
  269. Init(NULL, address.family());
  270. socket_->Connect(address);
  271. }
  272. AsyncSocket* socket() { return socket_.get(); }
  273. void QueueString(const char* data) {
  274. QueueData(data, strlen(data));
  275. }
  276. void QueueStringF(const char* format, ...) {
  277. va_list args;
  278. va_start(args, format);
  279. char buffer[1024];
  280. size_t len = vsprintfn(buffer, sizeof(buffer), format, args);
  281. ASSERT(len < sizeof(buffer) - 1);
  282. va_end(args);
  283. QueueData(buffer, len);
  284. }
  285. void QueueData(const char* data, size_t len) {
  286. send_buffer_.insert(send_buffer_.end(), data, data + len);
  287. if (Socket::CS_CONNECTED == socket_->GetState()) {
  288. Flush();
  289. }
  290. }
  291. std::string ReadData() {
  292. std::string data(&recv_buffer_[0], recv_buffer_.size());
  293. recv_buffer_.clear();
  294. return data;
  295. }
  296. bool IsConnected() const {
  297. return (Socket::CS_CONNECTED == socket_->GetState());
  298. }
  299. bool IsClosed() const {
  300. return (Socket::CS_CLOSED == socket_->GetState());
  301. }
  302. private:
  303. typedef std::vector<char> Buffer;
  304. void Init(AsyncSocket* socket, int family) {
  305. if (!socket) {
  306. socket = Thread::Current()->socketserver()
  307. ->CreateAsyncSocket(family, SOCK_STREAM);
  308. }
  309. socket_.reset(socket);
  310. socket_->SignalConnectEvent.connect(this,
  311. &SocketTestClient::OnConnectEvent);
  312. socket_->SignalReadEvent.connect(this, &SocketTestClient::OnReadEvent);
  313. socket_->SignalWriteEvent.connect(this, &SocketTestClient::OnWriteEvent);
  314. socket_->SignalCloseEvent.connect(this, &SocketTestClient::OnCloseEvent);
  315. }
  316. void Flush() {
  317. size_t sent = 0;
  318. while (sent < send_buffer_.size()) {
  319. int result = socket_->Send(&send_buffer_[sent],
  320. send_buffer_.size() - sent);
  321. if (result > 0) {
  322. sent += result;
  323. } else {
  324. break;
  325. }
  326. }
  327. size_t new_size = send_buffer_.size() - sent;
  328. memmove(&send_buffer_[0], &send_buffer_[sent], new_size);
  329. send_buffer_.resize(new_size);
  330. }
  331. void OnConnectEvent(AsyncSocket* socket) {
  332. if (!send_buffer_.empty()) {
  333. Flush();
  334. }
  335. }
  336. void OnReadEvent(AsyncSocket* socket) {
  337. char data[64 * 1024];
  338. int result = socket_->Recv(data, ARRAY_SIZE(data));
  339. if (result > 0) {
  340. recv_buffer_.insert(recv_buffer_.end(), data, data + result);
  341. }
  342. }
  343. void OnWriteEvent(AsyncSocket* socket) {
  344. if (!send_buffer_.empty()) {
  345. Flush();
  346. }
  347. }
  348. void OnCloseEvent(AsyncSocket* socket, int error) {
  349. }
  350. scoped_ptr<AsyncSocket> socket_;
  351. Buffer send_buffer_, recv_buffer_;
  352. };
  353. ///////////////////////////////////////////////////////////////////////////////
  354. // SocketTestServer
  355. // Creates a simulated server for testing. Works on real and virtual networks.
  356. ///////////////////////////////////////////////////////////////////////////////
  357. class SocketTestServer : public sigslot::has_slots<> {
  358. public:
  359. SocketTestServer(const SocketAddress& address)
  360. : socket_(Thread::Current()->socketserver()
  361. ->CreateAsyncSocket(address.family(), SOCK_STREAM))
  362. {
  363. socket_->SignalReadEvent.connect(this, &SocketTestServer::OnReadEvent);
  364. socket_->Bind(address);
  365. socket_->Listen(5);
  366. }
  367. virtual ~SocketTestServer() {
  368. clear();
  369. }
  370. size_t size() const { return clients_.size(); }
  371. SocketTestClient* client(size_t index) const { return clients_[index]; }
  372. SocketTestClient* operator[](size_t index) const { return client(index); }
  373. void clear() {
  374. for (size_t i=0; i<clients_.size(); ++i) {
  375. delete clients_[i];
  376. }
  377. clients_.clear();
  378. }
  379. private:
  380. void OnReadEvent(AsyncSocket* socket) {
  381. AsyncSocket* accepted =
  382. static_cast<AsyncSocket*>(socket_->Accept(NULL));
  383. if (!accepted)
  384. return;
  385. clients_.push_back(new SocketTestClient(accepted));
  386. }
  387. scoped_ptr<AsyncSocket> socket_;
  388. std::vector<SocketTestClient*> clients_;
  389. };
  390. ///////////////////////////////////////////////////////////////////////////////
  391. // Generic Utilities
  392. ///////////////////////////////////////////////////////////////////////////////
  393. inline bool ReadFile(const char* filename, std::string* contents) {
  394. FILE* fp = fopen(filename, "rb");
  395. if (!fp)
  396. return false;
  397. char buffer[1024*64];
  398. size_t read;
  399. contents->clear();
  400. while ((read = fread(buffer, 1, sizeof(buffer), fp))) {
  401. contents->append(buffer, read);
  402. }
  403. bool success = (0 != feof(fp));
  404. fclose(fp);
  405. return success;
  406. }
  407. ///////////////////////////////////////////////////////////////////////////////
  408. // Unittest predicates which are similar to STREQ, but for raw memory
  409. ///////////////////////////////////////////////////////////////////////////////
  410. inline AssertionResult CmpHelperMemEq(const char* expected_expression,
  411. const char* expected_length_expression,
  412. const char* actual_expression,
  413. const char* actual_length_expression,
  414. const void* expected,
  415. size_t expected_length,
  416. const void* actual,
  417. size_t actual_length)
  418. {
  419. if ((expected_length == actual_length)
  420. && (0 == memcmp(expected, actual, expected_length))) {
  421. return AssertionSuccess();
  422. }
  423. Message msg;
  424. msg << "Value of: " << actual_expression
  425. << " [" << actual_length_expression << "]";
  426. if (true) { //!actual_value.Equals(actual_expression)) {
  427. size_t buffer_size = actual_length * 2 + 1;
  428. char* buffer = STACK_ARRAY(char, buffer_size);
  429. hex_encode(buffer, buffer_size,
  430. reinterpret_cast<const char*>(actual), actual_length);
  431. msg << "\n Actual: " << buffer << " [" << actual_length << "]";
  432. }
  433. msg << "\nExpected: " << expected_expression
  434. << " [" << expected_length_expression << "]";
  435. if (true) { //!expected_value.Equals(expected_expression)) {
  436. size_t buffer_size = expected_length * 2 + 1;
  437. char* buffer = STACK_ARRAY(char, buffer_size);
  438. hex_encode(buffer, buffer_size,
  439. reinterpret_cast<const char*>(expected), expected_length);
  440. msg << "\nWhich is: " << buffer << " [" << expected_length << "]";
  441. }
  442. return AssertionFailure(msg);
  443. }
  444. inline AssertionResult CmpHelperFileEq(const char* expected_expression,
  445. const char* expected_length_expression,
  446. const char* actual_filename,
  447. const void* expected,
  448. size_t expected_length,
  449. const char* filename)
  450. {
  451. std::string contents;
  452. if (!ReadFile(filename, &contents)) {
  453. Message msg;
  454. msg << "File '" << filename << "' could not be read.";
  455. return AssertionFailure(msg);
  456. }
  457. return CmpHelperMemEq(expected_expression, expected_length_expression,
  458. actual_filename, "",
  459. expected, expected_length,
  460. contents.c_str(), contents.size());
  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. #define ASSERT_MEMEQ(expected, expected_length, actual, actual_length) \
  466. ASSERT_PRED_FORMAT4(::testing::CmpHelperMemEq, expected, expected_length, \
  467. actual, actual_length)
  468. #define EXPECT_FILEEQ(expected, expected_length, filename) \
  469. EXPECT_PRED_FORMAT3(::testing::CmpHelperFileEq, expected, expected_length, \
  470. filename)
  471. #define ASSERT_FILEEQ(expected, expected_length, filename) \
  472. ASSERT_PRED_FORMAT3(::testing::CmpHelperFileEq, expected, expected_length, \
  473. filename)
  474. ///////////////////////////////////////////////////////////////////////////////
  475. // Helpers for initializing constant memory with integers in a particular byte
  476. // order
  477. ///////////////////////////////////////////////////////////////////////////////
  478. #define BYTE_CAST(x) static_cast<uint8>((x) & 0xFF)
  479. // Declare a N-bit integer as a little-endian sequence of bytes
  480. #define LE16(x) BYTE_CAST(((uint16)x) >> 0), BYTE_CAST(((uint16)x) >> 8)
  481. #define LE32(x) BYTE_CAST(((uint32)x) >> 0), BYTE_CAST(((uint32)x) >> 8), \
  482. BYTE_CAST(((uint32)x) >> 16), BYTE_CAST(((uint32)x) >> 24)
  483. #define LE64(x) BYTE_CAST(((uint64)x) >> 0), BYTE_CAST(((uint64)x) >> 8), \
  484. BYTE_CAST(((uint64)x) >> 16), BYTE_CAST(((uint64)x) >> 24), \
  485. BYTE_CAST(((uint64)x) >> 32), BYTE_CAST(((uint64)x) >> 40), \
  486. BYTE_CAST(((uint64)x) >> 48), BYTE_CAST(((uint64)x) >> 56)
  487. // Declare a N-bit integer as a big-endian (Internet) sequence of bytes
  488. #define BE16(x) BYTE_CAST(((uint16)x) >> 8), BYTE_CAST(((uint16)x) >> 0)
  489. #define BE32(x) BYTE_CAST(((uint32)x) >> 24), BYTE_CAST(((uint32)x) >> 16), \
  490. BYTE_CAST(((uint32)x) >> 8), BYTE_CAST(((uint32)x) >> 0)
  491. #define BE64(x) BYTE_CAST(((uint64)x) >> 56), BYTE_CAST(((uint64)x) >> 48), \
  492. BYTE_CAST(((uint64)x) >> 40), BYTE_CAST(((uint64)x) >> 32), \
  493. BYTE_CAST(((uint64)x) >> 24), BYTE_CAST(((uint64)x) >> 16), \
  494. BYTE_CAST(((uint64)x) >> 8), BYTE_CAST(((uint64)x) >> 0)
  495. // Declare a N-bit integer as a this-endian (local machine) sequence of bytes
  496. #ifndef BIG_ENDIAN
  497. #define BIG_ENDIAN 1
  498. #endif // BIG_ENDIAN
  499. #if BIG_ENDIAN
  500. #define TE16 BE16
  501. #define TE32 BE32
  502. #define TE64 BE64
  503. #else // !BIG_ENDIAN
  504. #define TE16 LE16
  505. #define TE32 LE32
  506. #define TE64 LE64
  507. #endif // !BIG_ENDIAN
  508. ///////////////////////////////////////////////////////////////////////////////
  509. } // namespace testing
  510. #endif // TALK_BASE_TESTUTILS_H__