1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/byte_buffer.h"
#include <string.h>
#include <cstdint>
#include <string>
#include <utility>
#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "rtc_base/buffer.h"
#include "rtc_base/byte_order.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace webrtc {
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
TEST(ByteBufferTest, WriterAccessors) {
// To be changed into ByteBufferWriter when base type is converted.
ByteBufferWriterT<BufferT<uint8_t>> buffer;
buffer.WriteString("abc");
EXPECT_EQ(buffer.Length(), 3U);
EXPECT_THAT(buffer.DataView(), ElementsAre('a', 'b', 'c'));
EXPECT_EQ(absl::string_view("abc"), buffer.DataAsStringView());
buffer.WriteUInt8(0);
EXPECT_STREQ(buffer.DataAsCharPointer(), "abc");
EXPECT_STREQ(reinterpret_cast<const char*>(buffer.Data()), "abc");
}
TEST(ByteBufferTest, TestByteOrder) {
uint16_t n16 = 1;
uint32_t n32 = 1;
uint64_t n64 = 1;
EXPECT_EQ(n16, NetworkToHost16(HostToNetwork16(n16)));
EXPECT_EQ(n32, NetworkToHost32(HostToNetwork32(n32)));
EXPECT_EQ(n64, NetworkToHost64(HostToNetwork64(n64)));
if (IsHostBigEndian()) {
// The host is the network (big) endian.
EXPECT_EQ(n16, HostToNetwork16(n16));
EXPECT_EQ(n32, HostToNetwork32(n32));
EXPECT_EQ(n64, HostToNetwork64(n64));
// GetBE converts big endian to little endian here.
EXPECT_EQ(n16 >> 8, GetBE16(&n16));
EXPECT_EQ(n32 >> 24, GetBE32(&n32));
EXPECT_EQ(n64 >> 56, GetBE64(&n64));
} else {
// The host is little endian.
EXPECT_NE(n16, HostToNetwork16(n16));
EXPECT_NE(n32, HostToNetwork32(n32));
EXPECT_NE(n64, HostToNetwork64(n64));
// GetBE converts little endian to big endian here.
EXPECT_EQ(GetBE16(&n16), HostToNetwork16(n16));
EXPECT_EQ(GetBE32(&n32), HostToNetwork32(n32));
EXPECT_EQ(GetBE64(&n64), HostToNetwork64(n64));
// GetBE converts little endian to big endian here.
EXPECT_EQ(n16 << 8, GetBE16(&n16));
EXPECT_EQ(n32 << 24, GetBE32(&n32));
EXPECT_EQ(n64 << 56, GetBE64(&n64));
}
}
TEST(ByteBufferTest, TestBufferLength) {
ByteBufferWriter buffer;
size_t size = 0;
EXPECT_EQ(size, buffer.Length());
buffer.WriteUInt8(1);
++size;
EXPECT_EQ(size, buffer.Length());
buffer.WriteUInt16(1);
size += 2;
EXPECT_EQ(size, buffer.Length());
buffer.WriteUInt24(1);
size += 3;
EXPECT_EQ(size, buffer.Length());
buffer.WriteUInt32(1);
size += 4;
EXPECT_EQ(size, buffer.Length());
buffer.WriteUInt64(1);
size += 8;
EXPECT_EQ(size, buffer.Length());
}
TEST(ByteBufferTest, TestReadWriteBuffer) {
ByteBufferWriter buffer;
ByteBufferReader read_buf(ArrayView<const uint8_t>(nullptr, 0));
uint8_t ru8;
EXPECT_FALSE(read_buf.ReadUInt8(&ru8));
// Write and read uint8_t.
uint8_t wu8 = 1;
buffer.WriteUInt8(wu8);
ByteBufferReader read_buf1(buffer);
EXPECT_TRUE(read_buf1.ReadUInt8(&ru8));
EXPECT_EQ(wu8, ru8);
EXPECT_EQ(read_buf1.Length(), 0U);
buffer.Clear();
// Write and read uint16_t.
uint16_t wu16 = (1 << 8) + 1;
buffer.WriteUInt16(wu16);
ByteBufferReader read_buf2(buffer);
uint16_t ru16;
EXPECT_TRUE(read_buf2.ReadUInt16(&ru16));
EXPECT_EQ(wu16, ru16);
EXPECT_EQ(read_buf2.Length(), 0U);
buffer.Clear();
// Write and read uint24.
uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
buffer.WriteUInt24(wu24);
ByteBufferReader read_buf3(buffer);
uint32_t ru24;
EXPECT_TRUE(read_buf3.ReadUInt24(&ru24));
EXPECT_EQ(wu24, ru24);
EXPECT_EQ(read_buf3.Length(), 0U);
buffer.Clear();
// Write and read uint32_t.
uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
buffer.WriteUInt32(wu32);
ByteBufferReader read_buf4(buffer);
uint32_t ru32;
EXPECT_TRUE(read_buf4.ReadUInt32(&ru32));
EXPECT_EQ(wu32, ru32);
EXPECT_EQ(read_buf3.Length(), 0U);
buffer.Clear();
// Write and read uint64_t.
uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32;
buffer.WriteUInt64(wu64);
ByteBufferReader read_buf5(buffer);
uint64_t ru64;
EXPECT_TRUE(read_buf5.ReadUInt64(&ru64));
EXPECT_EQ(wu64, ru64);
EXPECT_EQ(read_buf5.Length(), 0U);
buffer.Clear();
// Write and read string.
std::string write_string("hello");
buffer.WriteString(write_string);
ByteBufferReader read_buf6(buffer);
std::string read_string;
EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size()));
EXPECT_EQ(write_string, read_string);
EXPECT_EQ(read_buf6.Length(), 0U);
buffer.Clear();
// Write and read bytes
uint8_t write_bytes[] = {3, 2, 1};
buffer.Write(ArrayView<const uint8_t>(write_bytes, 3));
ByteBufferReader read_buf7(buffer);
uint8_t read_bytes[3];
EXPECT_TRUE(read_buf7.ReadBytes(read_bytes));
EXPECT_THAT(read_bytes, ElementsAreArray(write_bytes));
EXPECT_EQ(read_buf7.Length(), 0U);
buffer.Clear();
// Write and read bytes with deprecated function
// TODO: issues.webrtc.org/42225170 - delete
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
buffer.WriteBytes(write_bytes, 3);
#pragma clang diagnostic pop
ByteBufferReader read_buf75(buffer);
EXPECT_TRUE(read_buf75.ReadBytes(read_bytes));
EXPECT_THAT(read_bytes, ElementsAreArray(write_bytes));
EXPECT_EQ(read_buf75.Length(), 0U);
buffer.Clear();
// Write and read reserved buffer space
uint8_t* write_dst = buffer.ReserveWriteBuffer(3);
memcpy(write_dst, write_bytes, 3);
ByteBufferReader read_buf8(buffer);
memset(read_bytes, 0, 3);
EXPECT_TRUE(read_buf8.ReadBytes(read_bytes));
EXPECT_THAT(read_bytes, ElementsAreArray(write_dst, 3));
EXPECT_EQ(read_buf8.Length(), 0U);
buffer.Clear();
// Write and read in order.
buffer.WriteUInt8(wu8);
buffer.WriteUInt16(wu16);
buffer.WriteUInt24(wu24);
buffer.WriteUInt32(wu32);
buffer.WriteUInt64(wu64);
ByteBufferReader read_buf9(buffer);
EXPECT_TRUE(read_buf9.ReadUInt8(&ru8));
EXPECT_EQ(wu8, ru8);
EXPECT_TRUE(read_buf9.ReadUInt16(&ru16));
EXPECT_EQ(wu16, ru16);
EXPECT_TRUE(read_buf9.ReadUInt24(&ru24));
EXPECT_EQ(wu24, ru24);
EXPECT_TRUE(read_buf9.ReadUInt32(&ru32));
EXPECT_EQ(wu32, ru32);
EXPECT_TRUE(read_buf9.ReadUInt64(&ru64));
EXPECT_EQ(wu64, ru64);
EXPECT_EQ(read_buf9.Length(), 0U);
buffer.Clear();
}
TEST(ByteBufferTest, TestWriteCArray) {
// Write and read data
const uint8_t write_data[3] = {3, 2, 1};
ByteBufferWriter buffer;
buffer.Write(write_data);
EXPECT_EQ(buffer.Length(), 3U);
ByteBufferReader read_buf10(buffer);
uint8_t read_bytes[3] = {};
EXPECT_TRUE(read_buf10.ReadBytes(read_bytes));
EXPECT_THAT(read_bytes, ElementsAreArray(write_data));
EXPECT_EQ(read_buf10.Length(), 0U);
}
TEST(ByteBufferTest, TestWriteBuffer) {
const uint8_t write_data[3] = {3, 2, 1};
// Write and read buffer
Buffer write_buffer(write_data);
ByteBufferWriter buffer;
buffer.Write(write_buffer);
ByteBufferReader read_buf11(buffer);
uint8_t read_bytes[3] = {};
EXPECT_TRUE(read_buf11.ReadBytes(read_bytes));
EXPECT_THAT(read_bytes, ElementsAreArray(write_buffer));
EXPECT_EQ(read_buf11.Length(), 0U);
}
TEST(ByteBufferTest, TestWriteArrayView) {
const uint8_t write_data[3] = {3, 2, 1};
// Write and read arrayview
ArrayView<const uint8_t> write_view(write_data);
ByteBufferWriter buffer;
buffer.Write(write_view);
ByteBufferReader read_buf12(buffer);
uint8_t read_bytes[3] = {};
EXPECT_TRUE(read_buf12.ReadBytes(read_bytes));
EXPECT_THAT(read_bytes, ElementsAreArray(write_view));
EXPECT_EQ(read_buf12.Length(), 0U);
}
TEST(ByteBufferTest, TestWriteConsume) {
ByteBufferWriter writer;
// Write and read uint8_t.
uint8_t wu8 = 1;
writer.WriteUInt8(wu8);
Buffer consumed = std::move(writer).Extract();
EXPECT_THAT(consumed, ElementsAre(wu8));
}
TEST(ByteBufferTest, TestReadStringView) {
const absl::string_view tests[] = {"hello", " ", "string_view"};
std::string buffer;
for (const auto& test : tests)
buffer += test;
ArrayView<const uint8_t> bytes(reinterpret_cast<const uint8_t*>(&buffer[0]),
buffer.size());
ByteBufferReader read_buf(bytes);
size_t consumed = 0;
for (const auto& test : tests) {
absl::string_view sv;
EXPECT_TRUE(read_buf.ReadStringView(&sv, test.length()));
EXPECT_EQ(sv.compare(test), 0);
// The returned string view should point directly into the original
// string.
EXPECT_EQ(&sv[0], &buffer[0 + consumed]);
consumed += sv.size();
}
}
TEST(ByteBufferTest, TestReadWriteUVarint) {
ByteBufferWriter write_buffer;
size_t size = 0;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(1u);
++size;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(2u);
++size;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(27u);
++size;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(149u);
size += 2;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(68719476736u);
size += 6;
EXPECT_EQ(size, write_buffer.Length());
ByteBufferReader read_buffer(write_buffer);
EXPECT_EQ(size, read_buffer.Length());
uint64_t val1, val2, val3, val4, val5;
ASSERT_TRUE(read_buffer.ReadUVarint(&val1));
EXPECT_EQ(val1, 1U);
--size;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val2));
EXPECT_EQ(val2, 2U);
--size;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val3));
EXPECT_EQ(val3, 27U);
--size;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val4));
EXPECT_EQ(val4, 149U);
size -= 2;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val5));
EXPECT_EQ(val5, 68719476736U);
size -= 6;
EXPECT_EQ(size, read_buffer.Length());
}
TEST(ByteBufferTest, ReadFromArrayView) {
const uint8_t buf[] = {'a', 'b', 'c'};
ArrayView<const uint8_t> view(buf, 3);
ByteBufferReader read_buffer(view);
uint8_t val;
EXPECT_TRUE(read_buffer.ReadUInt8(&val));
EXPECT_EQ(val, 'a');
EXPECT_TRUE(read_buffer.ReadUInt8(&val));
EXPECT_EQ(val, 'b');
EXPECT_TRUE(read_buffer.ReadUInt8(&val));
EXPECT_EQ(val, 'c');
EXPECT_FALSE(read_buffer.ReadUInt8(&val));
}
TEST(ByteBufferTest, ReadToArrayView) {
const uint8_t buf[] = {'a', 'b', 'c'};
ArrayView<const uint8_t> stored_view(buf, 3);
ByteBufferReader read_buffer(stored_view);
uint8_t result[] = {'1', '2', '3'};
EXPECT_TRUE(read_buffer.ReadBytes(MakeArrayView(result, 2)));
EXPECT_EQ(result[0], 'a');
EXPECT_EQ(result[1], 'b');
EXPECT_EQ(result[2], '3');
EXPECT_TRUE(read_buffer.ReadBytes(MakeArrayView(&result[2], 1)));
EXPECT_EQ(result[2], 'c');
EXPECT_FALSE(read_buffer.ReadBytes(MakeArrayView(result, 1)));
}
} // namespace webrtc
|