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.

261 lines
7.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/byte_buffer.h"
  11. #include <string.h>
  12. #include "rtc_base/arraysize.h"
  13. #include "rtc_base/byte_order.h"
  14. #include "test/gtest.h"
  15. namespace rtc {
  16. TEST(ByteBufferTest, TestByteOrder) {
  17. uint16_t n16 = 1;
  18. uint32_t n32 = 1;
  19. uint64_t n64 = 1;
  20. EXPECT_EQ(n16, NetworkToHost16(HostToNetwork16(n16)));
  21. EXPECT_EQ(n32, NetworkToHost32(HostToNetwork32(n32)));
  22. EXPECT_EQ(n64, NetworkToHost64(HostToNetwork64(n64)));
  23. if (IsHostBigEndian()) {
  24. // The host is the network (big) endian.
  25. EXPECT_EQ(n16, HostToNetwork16(n16));
  26. EXPECT_EQ(n32, HostToNetwork32(n32));
  27. EXPECT_EQ(n64, HostToNetwork64(n64));
  28. // GetBE converts big endian to little endian here.
  29. EXPECT_EQ(n16 >> 8, GetBE16(&n16));
  30. EXPECT_EQ(n32 >> 24, GetBE32(&n32));
  31. EXPECT_EQ(n64 >> 56, GetBE64(&n64));
  32. } else {
  33. // The host is little endian.
  34. EXPECT_NE(n16, HostToNetwork16(n16));
  35. EXPECT_NE(n32, HostToNetwork32(n32));
  36. EXPECT_NE(n64, HostToNetwork64(n64));
  37. // GetBE converts little endian to big endian here.
  38. EXPECT_EQ(GetBE16(&n16), HostToNetwork16(n16));
  39. EXPECT_EQ(GetBE32(&n32), HostToNetwork32(n32));
  40. EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64));
  41. // GetBE converts little endian to big endian here.
  42. EXPECT_EQ(n16 << 8, GetBE16(&n16));
  43. EXPECT_EQ(n32 << 24, GetBE32(&n32));
  44. EXPECT_EQ(n64 << 56, GetBE64(&n64));
  45. }
  46. }
  47. TEST(ByteBufferTest, TestBufferLength) {
  48. ByteBufferWriter buffer;
  49. size_t size = 0;
  50. EXPECT_EQ(size, buffer.Length());
  51. buffer.WriteUInt8(1);
  52. ++size;
  53. EXPECT_EQ(size, buffer.Length());
  54. buffer.WriteUInt16(1);
  55. size += 2;
  56. EXPECT_EQ(size, buffer.Length());
  57. buffer.WriteUInt24(1);
  58. size += 3;
  59. EXPECT_EQ(size, buffer.Length());
  60. buffer.WriteUInt32(1);
  61. size += 4;
  62. EXPECT_EQ(size, buffer.Length());
  63. buffer.WriteUInt64(1);
  64. size += 8;
  65. EXPECT_EQ(size, buffer.Length());
  66. }
  67. TEST(ByteBufferTest, TestReadWriteBuffer) {
  68. ByteBufferWriter::ByteOrder orders[2] = {ByteBufferWriter::ORDER_HOST,
  69. ByteBufferWriter::ORDER_NETWORK};
  70. for (size_t i = 0; i < arraysize(orders); i++) {
  71. ByteBufferWriter buffer(orders[i]);
  72. EXPECT_EQ(orders[i], buffer.Order());
  73. ByteBufferReader read_buf(nullptr, 0, orders[i]);
  74. EXPECT_EQ(orders[i], read_buf.Order());
  75. uint8_t ru8;
  76. EXPECT_FALSE(read_buf.ReadUInt8(&ru8));
  77. // Write and read uint8_t.
  78. uint8_t wu8 = 1;
  79. buffer.WriteUInt8(wu8);
  80. ByteBufferReader read_buf1(buffer.Data(), buffer.Length(), orders[i]);
  81. EXPECT_TRUE(read_buf1.ReadUInt8(&ru8));
  82. EXPECT_EQ(wu8, ru8);
  83. EXPECT_EQ(0U, read_buf1.Length());
  84. buffer.Clear();
  85. // Write and read uint16_t.
  86. uint16_t wu16 = (1 << 8) + 1;
  87. buffer.WriteUInt16(wu16);
  88. ByteBufferReader read_buf2(buffer.Data(), buffer.Length(), orders[i]);
  89. uint16_t ru16;
  90. EXPECT_TRUE(read_buf2.ReadUInt16(&ru16));
  91. EXPECT_EQ(wu16, ru16);
  92. EXPECT_EQ(0U, read_buf2.Length());
  93. buffer.Clear();
  94. // Write and read uint24.
  95. uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
  96. buffer.WriteUInt24(wu24);
  97. ByteBufferReader read_buf3(buffer.Data(), buffer.Length(), orders[i]);
  98. uint32_t ru24;
  99. EXPECT_TRUE(read_buf3.ReadUInt24(&ru24));
  100. EXPECT_EQ(wu24, ru24);
  101. EXPECT_EQ(0U, read_buf3.Length());
  102. buffer.Clear();
  103. // Write and read uint32_t.
  104. uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
  105. buffer.WriteUInt32(wu32);
  106. ByteBufferReader read_buf4(buffer.Data(), buffer.Length(), orders[i]);
  107. uint32_t ru32;
  108. EXPECT_TRUE(read_buf4.ReadUInt32(&ru32));
  109. EXPECT_EQ(wu32, ru32);
  110. EXPECT_EQ(0U, read_buf3.Length());
  111. buffer.Clear();
  112. // Write and read uint64_t.
  113. uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
  114. uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32;
  115. buffer.WriteUInt64(wu64);
  116. ByteBufferReader read_buf5(buffer.Data(), buffer.Length(), orders[i]);
  117. uint64_t ru64;
  118. EXPECT_TRUE(read_buf5.ReadUInt64(&ru64));
  119. EXPECT_EQ(wu64, ru64);
  120. EXPECT_EQ(0U, read_buf5.Length());
  121. buffer.Clear();
  122. // Write and read string.
  123. std::string write_string("hello");
  124. buffer.WriteString(write_string);
  125. ByteBufferReader read_buf6(buffer.Data(), buffer.Length(), orders[i]);
  126. std::string read_string;
  127. EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size()));
  128. EXPECT_EQ(write_string, read_string);
  129. EXPECT_EQ(0U, read_buf6.Length());
  130. buffer.Clear();
  131. // Write and read bytes
  132. char write_bytes[] = "foo";
  133. buffer.WriteBytes(write_bytes, 3);
  134. ByteBufferReader read_buf7(buffer.Data(), buffer.Length(), orders[i]);
  135. char read_bytes[3];
  136. EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3));
  137. for (int i = 0; i < 3; ++i) {
  138. EXPECT_EQ(write_bytes[i], read_bytes[i]);
  139. }
  140. EXPECT_EQ(0U, read_buf7.Length());
  141. buffer.Clear();
  142. // Write and read reserved buffer space
  143. char* write_dst = buffer.ReserveWriteBuffer(3);
  144. memcpy(write_dst, write_bytes, 3);
  145. ByteBufferReader read_buf8(buffer.Data(), buffer.Length(), orders[i]);
  146. memset(read_bytes, 0, 3);
  147. EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3));
  148. for (int i = 0; i < 3; ++i) {
  149. EXPECT_EQ(write_bytes[i], read_bytes[i]);
  150. }
  151. EXPECT_EQ(0U, read_buf8.Length());
  152. buffer.Clear();
  153. // Write and read in order.
  154. buffer.WriteUInt8(wu8);
  155. buffer.WriteUInt16(wu16);
  156. buffer.WriteUInt24(wu24);
  157. buffer.WriteUInt32(wu32);
  158. buffer.WriteUInt64(wu64);
  159. ByteBufferReader read_buf9(buffer.Data(), buffer.Length(), orders[i]);
  160. EXPECT_TRUE(read_buf9.ReadUInt8(&ru8));
  161. EXPECT_EQ(wu8, ru8);
  162. EXPECT_TRUE(read_buf9.ReadUInt16(&ru16));
  163. EXPECT_EQ(wu16, ru16);
  164. EXPECT_TRUE(read_buf9.ReadUInt24(&ru24));
  165. EXPECT_EQ(wu24, ru24);
  166. EXPECT_TRUE(read_buf9.ReadUInt32(&ru32));
  167. EXPECT_EQ(wu32, ru32);
  168. EXPECT_TRUE(read_buf9.ReadUInt64(&ru64));
  169. EXPECT_EQ(wu64, ru64);
  170. EXPECT_EQ(0U, read_buf9.Length());
  171. buffer.Clear();
  172. }
  173. }
  174. TEST(ByteBufferTest, TestReadWriteUVarint) {
  175. ByteBufferWriter::ByteOrder orders[2] = {ByteBufferWriter::ORDER_HOST,
  176. ByteBufferWriter::ORDER_NETWORK};
  177. for (ByteBufferWriter::ByteOrder& order : orders) {
  178. ByteBufferWriter write_buffer(order);
  179. size_t size = 0;
  180. EXPECT_EQ(size, write_buffer.Length());
  181. write_buffer.WriteUVarint(1u);
  182. ++size;
  183. EXPECT_EQ(size, write_buffer.Length());
  184. write_buffer.WriteUVarint(2u);
  185. ++size;
  186. EXPECT_EQ(size, write_buffer.Length());
  187. write_buffer.WriteUVarint(27u);
  188. ++size;
  189. EXPECT_EQ(size, write_buffer.Length());
  190. write_buffer.WriteUVarint(149u);
  191. size += 2;
  192. EXPECT_EQ(size, write_buffer.Length());
  193. write_buffer.WriteUVarint(68719476736u);
  194. size += 6;
  195. EXPECT_EQ(size, write_buffer.Length());
  196. ByteBufferReader read_buffer(write_buffer.Data(), write_buffer.Length(),
  197. order);
  198. EXPECT_EQ(size, read_buffer.Length());
  199. uint64_t val1, val2, val3, val4, val5;
  200. ASSERT_TRUE(read_buffer.ReadUVarint(&val1));
  201. EXPECT_EQ(1u, val1);
  202. --size;
  203. EXPECT_EQ(size, read_buffer.Length());
  204. ASSERT_TRUE(read_buffer.ReadUVarint(&val2));
  205. EXPECT_EQ(2u, val2);
  206. --size;
  207. EXPECT_EQ(size, read_buffer.Length());
  208. ASSERT_TRUE(read_buffer.ReadUVarint(&val3));
  209. EXPECT_EQ(27u, val3);
  210. --size;
  211. EXPECT_EQ(size, read_buffer.Length());
  212. ASSERT_TRUE(read_buffer.ReadUVarint(&val4));
  213. EXPECT_EQ(149u, val4);
  214. size -= 2;
  215. EXPECT_EQ(size, read_buffer.Length());
  216. ASSERT_TRUE(read_buffer.ReadUVarint(&val5));
  217. EXPECT_EQ(68719476736u, val5);
  218. size -= 6;
  219. EXPECT_EQ(size, read_buffer.Length());
  220. }
  221. }
  222. } // namespace rtc