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