/* * Copyright (c) 2012 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 "modules/rtp_rtcp/source/byte_io.h" #include #include "test/gtest.h" namespace webrtc { namespace { class ByteIoTest : public ::testing::Test { protected: ByteIoTest() = default; ~ByteIoTest() override = default; enum { kAlignments = sizeof(uint64_t) - 1 }; // Method to create a test value that is not the same when byte reversed. template T CreateTestValue(bool negative, uint8_t num_bytes) { // Examples of output: // T = int32_t, negative = false, num_bytes = 4: 0x00010203 // T = int32_t, negative = true, num_bytes = 4: 0xFFFEFDFC // T = int32_t, negative = false, num_bytes = 3: 0x000102 // * T = int32_t, negative = true, num_bytes = 3: 0xFFFEFD T val = 0; for (uint8_t i = 0; i != num_bytes; ++i) { val = (val << 8) + (negative ? (0xFF - i) : (i + 1)); } // This loop will create a sign extend mask if num_bytes if necessary. // For the last example (marked * above), the number needs to be sign // extended to be a valid int32_t. The sign extend mask is 0xFF000000. // Comments for each step with this example below. if (std::numeric_limits::is_signed && negative && num_bytes < sizeof(T)) { // Start with mask = 0xFFFFFFFF. T mask = static_cast(-1); // Create a temporary for the lowest byte (0x000000FF). const T neg_byte = static_cast(0xFF); for (int i = 0; i < num_bytes; ++i) { // And the inverse of the temporary and the mask: // 0xFFFFFFFF & 0xFFFFFF00 = 0xFFFFFF00. // 0xFFFFFF00 & 0xFFFF00FF = 0xFFFF0000. // 0xFFFF0000 & 0xFF00FFFF = 0xFF000000. mask &= ~(neg_byte << (i * 8)); } // Add the sign extension mask to the actual value. val |= mask; } return val; } // Populate byte buffer with value, in big endian format. template void PopulateTestData(uint8_t* data, T value, int num_bytes, bool bigendian) { if (bigendian) { for (int i = 0; i < num_bytes; ++i) { data[i] = (value >> ((num_bytes - i - 1) * 8)) & 0xFF; } } else { for (int i = 0; i < num_bytes; ++i) { data[i] = (value >> (i * 8)) & 0xFF; } } } // Test reading big endian numbers. // Template arguments: Type T, read method RM(buffer), B bytes of data. template void TestRead(bool big_endian) { // Test both for values that are positive and negative (if signed) for (int neg = 0; neg < 2; ++neg) { bool negative = neg > 0; // Write test value to byte buffer, in big endian format. T test_value = CreateTestValue(negative, B); uint8_t bytes[B + kAlignments]; // Make one test for each alignment. for (int i = 0; i < kAlignments; ++i) { PopulateTestData(bytes + i, test_value, B, big_endian); // Check that test value is retrieved from buffer when used read method. EXPECT_EQ(test_value, RM(bytes + i)); } } } // Test writing big endian numbers. // Template arguments: Type T, write method WM(buffer, value), B bytes of data template void TestWrite(bool big_endian) { // Test both for values that are positive and negative (if signed). for (int neg = 0; neg < 2; ++neg) { bool negative = neg > 0; // Write test value to byte buffer, in big endian format. T test_value = CreateTestValue(negative, B); uint8_t expected_bytes[B + kAlignments]; uint8_t bytes[B + kAlignments]; // Make one test for each alignment. for (int i = 0; i < kAlignments; ++i) { PopulateTestData(expected_bytes + i, test_value, B, big_endian); // Zero initialize buffer and let WM populate it. memset(bytes, 0, B + kAlignments); WM(bytes + i, test_value); // Check that data produced by WM is big endian as expected. for (int j = 0; j < B; ++j) { EXPECT_EQ(expected_bytes[i + j], bytes[i + j]); } } } } }; TEST_F(ByteIoTest, Test16UBitBigEndian) { TestRead::ReadBigEndian, sizeof(uint16_t)>( true); TestWrite::WriteBigEndian, sizeof(uint16_t)>( true); } TEST_F(ByteIoTest, Test24UBitBigEndian) { TestRead::ReadBigEndian, 3>(true); TestWrite::WriteBigEndian, 3>(true); } TEST_F(ByteIoTest, Test32UBitBigEndian) { TestRead::ReadBigEndian, sizeof(uint32_t)>( true); TestWrite::WriteBigEndian, sizeof(uint32_t)>( true); } TEST_F(ByteIoTest, Test64UBitBigEndian) { TestRead::ReadBigEndian, sizeof(uint64_t)>( true); TestWrite::WriteBigEndian, sizeof(uint64_t)>( true); } TEST_F(ByteIoTest, Test16SBitBigEndian) { TestRead::ReadBigEndian, sizeof(int16_t)>(true); TestWrite::WriteBigEndian, sizeof(int16_t)>( true); } TEST_F(ByteIoTest, Test24SBitBigEndian) { TestRead::ReadBigEndian, 3>(true); TestWrite::WriteBigEndian, 3>(true); } TEST_F(ByteIoTest, Test32SBitBigEndian) { TestRead::ReadBigEndian, sizeof(int32_t)>(true); TestWrite::WriteBigEndian, sizeof(int32_t)>( true); } TEST_F(ByteIoTest, Test64SBitBigEndian) { TestRead::ReadBigEndian, sizeof(int64_t)>(true); TestWrite::WriteBigEndian, sizeof(int64_t)>( true); } TEST_F(ByteIoTest, Test16UBitLittleEndian) { TestRead::ReadLittleEndian, sizeof(uint16_t)>( false); TestWrite::WriteLittleEndian, sizeof(uint16_t)>(false); } TEST_F(ByteIoTest, Test24UBitLittleEndian) { TestRead::ReadLittleEndian, 3>(false); TestWrite::WriteLittleEndian, 3>(false); } TEST_F(ByteIoTest, Test32UBitLittleEndian) { TestRead::ReadLittleEndian, sizeof(uint32_t)>( false); TestWrite::WriteLittleEndian, sizeof(uint32_t)>(false); } TEST_F(ByteIoTest, Test64UBitLittleEndian) { TestRead::ReadLittleEndian, sizeof(uint64_t)>( false); TestWrite::WriteLittleEndian, sizeof(uint64_t)>(false); } TEST_F(ByteIoTest, Test16SBitLittleEndian) { TestRead::ReadLittleEndian, sizeof(int16_t)>( false); TestWrite::WriteLittleEndian, sizeof(int16_t)>( false); } TEST_F(ByteIoTest, Test24SBitLittleEndian) { TestRead::ReadLittleEndian, 3>(false); TestWrite::WriteLittleEndian, 3>(false); } TEST_F(ByteIoTest, Test32SBitLittleEndian) { TestRead::ReadLittleEndian, sizeof(int32_t)>( false); TestWrite::WriteLittleEndian, sizeof(int32_t)>( false); } TEST_F(ByteIoTest, Test64SBitLittleEndian) { TestRead::ReadLittleEndian, sizeof(int64_t)>( false); TestWrite::WriteLittleEndian, sizeof(int64_t)>( false); } // Sets up a fixed byte array and converts N bytes from the array into a // uint64_t. Verifies the value with hard-coded reference. TEST(ByteIo, SanityCheckFixedByteArrayUnsignedReadBigEndian) { uint8_t data[8] = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88}; uint64_t value = ByteReader::ReadBigEndian(data); EXPECT_EQ(static_cast(0xFFEE), value); value = ByteReader::ReadBigEndian(data); EXPECT_EQ(static_cast(0xFFEEDD), value); value = ByteReader::ReadBigEndian(data); EXPECT_EQ(static_cast(0xFFEEDDCC), value); value = ByteReader::ReadBigEndian(data); EXPECT_EQ(static_cast(0xFFEEDDCCBB), value); value = ByteReader::ReadBigEndian(data); EXPECT_EQ(static_cast(0xFFEEDDCCBBAA), value); value = ByteReader::ReadBigEndian(data); EXPECT_EQ(static_cast(0xFFEEDDCCBBAA99), value); value = ByteReader::ReadBigEndian(data); EXPECT_EQ(static_cast(0xFFEEDDCCBBAA9988), value); } // Same as above, but for little-endian reading. TEST(ByteIo, SanityCheckFixedByteArrayUnsignedReadLittleEndian) { uint8_t data[8] = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88}; uint64_t value = ByteReader::ReadLittleEndian(data); EXPECT_EQ(static_cast(0xEEFF), value); value = ByteReader::ReadLittleEndian(data); EXPECT_EQ(static_cast(0xDDEEFF), value); value = ByteReader::ReadLittleEndian(data); EXPECT_EQ(static_cast(0xCCDDEEFF), value); value = ByteReader::ReadLittleEndian(data); EXPECT_EQ(static_cast(0xBBCCDDEEFF), value); value = ByteReader::ReadLittleEndian(data); EXPECT_EQ(static_cast(0xAABBCCDDEEFF), value); value = ByteReader::ReadLittleEndian(data); EXPECT_EQ(static_cast(0x99AABBCCDDEEFF), value); value = ByteReader::ReadLittleEndian(data); EXPECT_EQ(static_cast(0x8899AABBCCDDEEFF), value); } } // namespace } // namespace webrtc