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.

228 lines
7.2 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. #include "talk/base/bytebuffer.h"
  28. #include "talk/base/byteorder.h"
  29. #include "talk/base/common.h"
  30. #include "talk/base/gunit.h"
  31. namespace talk_base {
  32. TEST(ByteBufferTest, TestByteOrder) {
  33. uint16 n16 = 1;
  34. uint32 n32 = 1;
  35. uint64 n64 = 1;
  36. EXPECT_EQ(n16, NetworkToHost16(HostToNetwork16(n16)));
  37. EXPECT_EQ(n32, NetworkToHost32(HostToNetwork32(n32)));
  38. EXPECT_EQ(n64, NetworkToHost64(HostToNetwork64(n64)));
  39. if (IsHostBigEndian()) {
  40. // The host is the network (big) endian.
  41. EXPECT_EQ(n16, HostToNetwork16(n16));
  42. EXPECT_EQ(n32, HostToNetwork32(n32));
  43. EXPECT_EQ(n64, HostToNetwork64(n64));
  44. // GetBE converts big endian to little endian here.
  45. EXPECT_EQ(n16 >> 8, GetBE16(&n16));
  46. EXPECT_EQ(n32 >> 24, GetBE32(&n32));
  47. EXPECT_EQ(n64 >> 56, GetBE64(&n64));
  48. } else {
  49. // The host is little endian.
  50. EXPECT_NE(n16, HostToNetwork16(n16));
  51. EXPECT_NE(n32, HostToNetwork32(n32));
  52. EXPECT_NE(n64, HostToNetwork64(n64));
  53. // GetBE converts little endian to big endian here.
  54. EXPECT_EQ(GetBE16(&n16), HostToNetwork16(n16));
  55. EXPECT_EQ(GetBE32(&n32), HostToNetwork32(n32));
  56. EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64));
  57. // GetBE converts little endian to big endian here.
  58. EXPECT_EQ(n16 << 8, GetBE16(&n16));
  59. EXPECT_EQ(n32 << 24, GetBE32(&n32));
  60. EXPECT_EQ(n64 << 56, GetBE64(&n64));
  61. }
  62. }
  63. TEST(ByteBufferTest, TestBufferLength) {
  64. ByteBuffer buffer;
  65. size_t size = 0;
  66. EXPECT_EQ(size, buffer.Length());
  67. buffer.WriteUInt8(1);
  68. ++size;
  69. EXPECT_EQ(size, buffer.Length());
  70. buffer.WriteUInt16(1);
  71. size += 2;
  72. EXPECT_EQ(size, buffer.Length());
  73. buffer.WriteUInt24(1);
  74. size += 3;
  75. EXPECT_EQ(size, buffer.Length());
  76. buffer.WriteUInt32(1);
  77. size += 4;
  78. EXPECT_EQ(size, buffer.Length());
  79. buffer.WriteUInt64(1);
  80. size += 8;
  81. EXPECT_EQ(size, buffer.Length());
  82. EXPECT_TRUE(buffer.Consume(0));
  83. EXPECT_EQ(size, buffer.Length());
  84. EXPECT_TRUE(buffer.Consume(4));
  85. size -= 4;
  86. EXPECT_EQ(size, buffer.Length());
  87. }
  88. TEST(ByteBufferTest, TestGetSetReadPosition) {
  89. ByteBuffer buffer("ABCDEF", 6);
  90. EXPECT_EQ(6U, buffer.Length());
  91. ByteBuffer::ReadPosition pos(buffer.GetReadPosition());
  92. EXPECT_TRUE(buffer.SetReadPosition(pos));
  93. EXPECT_EQ(6U, buffer.Length());
  94. std::string read;
  95. EXPECT_TRUE(buffer.ReadString(&read, 3));
  96. EXPECT_EQ("ABC", read);
  97. EXPECT_EQ(3U, buffer.Length());
  98. EXPECT_TRUE(buffer.SetReadPosition(pos));
  99. EXPECT_EQ(6U, buffer.Length());
  100. read.clear();
  101. EXPECT_TRUE(buffer.ReadString(&read, 3));
  102. EXPECT_EQ("ABC", read);
  103. EXPECT_EQ(3U, buffer.Length());
  104. // For a resize by writing Capacity() number of bytes.
  105. size_t capacity = buffer.Capacity();
  106. buffer.ReserveWriteBuffer(buffer.Capacity());
  107. EXPECT_EQ(capacity + 3U, buffer.Length());
  108. EXPECT_FALSE(buffer.SetReadPosition(pos));
  109. read.clear();
  110. EXPECT_TRUE(buffer.ReadString(&read, 3));
  111. EXPECT_EQ("DEF", read);
  112. }
  113. TEST(ByteBufferTest, TestReadWriteBuffer) {
  114. ByteBuffer::ByteOrder orders[2] = { ByteBuffer::ORDER_HOST,
  115. ByteBuffer::ORDER_NETWORK };
  116. for (size_t i = 0; i < ARRAY_SIZE(orders); i++) {
  117. ByteBuffer buffer(orders[i]);
  118. EXPECT_EQ(orders[i], buffer.Order());
  119. uint8 ru8;
  120. EXPECT_FALSE(buffer.ReadUInt8(&ru8));
  121. // Write and read uint8.
  122. uint8 wu8 = 1;
  123. buffer.WriteUInt8(wu8);
  124. EXPECT_TRUE(buffer.ReadUInt8(&ru8));
  125. EXPECT_EQ(wu8, ru8);
  126. EXPECT_EQ(0U, buffer.Length());
  127. // Write and read uint16.
  128. uint16 wu16 = (1 << 8) + 1;
  129. buffer.WriteUInt16(wu16);
  130. uint16 ru16;
  131. EXPECT_TRUE(buffer.ReadUInt16(&ru16));
  132. EXPECT_EQ(wu16, ru16);
  133. EXPECT_EQ(0U, buffer.Length());
  134. // Write and read uint24.
  135. uint32 wu24 = (3 << 16) + (2 << 8) + 1;
  136. buffer.WriteUInt24(wu24);
  137. uint32 ru24;
  138. EXPECT_TRUE(buffer.ReadUInt24(&ru24));
  139. EXPECT_EQ(wu24, ru24);
  140. EXPECT_EQ(0U, buffer.Length());
  141. // Write and read uint32.
  142. uint32 wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
  143. buffer.WriteUInt32(wu32);
  144. uint32 ru32;
  145. EXPECT_TRUE(buffer.ReadUInt32(&ru32));
  146. EXPECT_EQ(wu32, ru32);
  147. EXPECT_EQ(0U, buffer.Length());
  148. // Write and read uint64.
  149. uint32 another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
  150. uint64 wu64 = (static_cast<uint64>(another32) << 32) + wu32;
  151. buffer.WriteUInt64(wu64);
  152. uint64 ru64;
  153. EXPECT_TRUE(buffer.ReadUInt64(&ru64));
  154. EXPECT_EQ(wu64, ru64);
  155. EXPECT_EQ(0U, buffer.Length());
  156. // Write and read string.
  157. std::string write_string("hello");
  158. buffer.WriteString(write_string);
  159. std::string read_string;
  160. EXPECT_TRUE(buffer.ReadString(&read_string, write_string.size()));
  161. EXPECT_EQ(write_string, read_string);
  162. EXPECT_EQ(0U, buffer.Length());
  163. // Write and read bytes
  164. char write_bytes[] = "foo";
  165. buffer.WriteBytes(write_bytes, 3);
  166. char read_bytes[3];
  167. EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3));
  168. for (int i = 0; i < 3; ++i) {
  169. EXPECT_EQ(write_bytes[i], read_bytes[i]);
  170. }
  171. EXPECT_EQ(0U, buffer.Length());
  172. // Write and read reserved buffer space
  173. char* write_dst = buffer.ReserveWriteBuffer(3);
  174. memcpy(write_dst, write_bytes, 3);
  175. memset(read_bytes, 0, 3);
  176. EXPECT_TRUE(buffer.ReadBytes(read_bytes, 3));
  177. for (int i = 0; i < 3; ++i) {
  178. EXPECT_EQ(write_bytes[i], read_bytes[i]);
  179. }
  180. EXPECT_EQ(0U, buffer.Length());
  181. // Write and read in order.
  182. buffer.WriteUInt8(wu8);
  183. buffer.WriteUInt16(wu16);
  184. buffer.WriteUInt24(wu24);
  185. buffer.WriteUInt32(wu32);
  186. buffer.WriteUInt64(wu64);
  187. EXPECT_TRUE(buffer.ReadUInt8(&ru8));
  188. EXPECT_EQ(wu8, ru8);
  189. EXPECT_TRUE(buffer.ReadUInt16(&ru16));
  190. EXPECT_EQ(wu16, ru16);
  191. EXPECT_TRUE(buffer.ReadUInt24(&ru24));
  192. EXPECT_EQ(wu24, ru24);
  193. EXPECT_TRUE(buffer.ReadUInt32(&ru32));
  194. EXPECT_EQ(wu32, ru32);
  195. EXPECT_TRUE(buffer.ReadUInt64(&ru64));
  196. EXPECT_EQ(wu64, ru64);
  197. EXPECT_EQ(0U, buffer.Length());
  198. }
  199. }
  200. } // namespace talk_base