sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
| 11 | #include <limits> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/rtp_rtcp/source/byte_io.h" |
| 14 | #include "test/gtest.h" |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace { |
| 18 | |
| 19 | class ByteIoTest : public ::testing::Test { |
| 20 | protected: |
Danil Chapovalov | dd7e284 | 2018-03-09 15:37:03 +0000 | [diff] [blame^] | 21 | ByteIoTest() = default; |
| 22 | ~ByteIoTest() override = default; |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 23 | |
| 24 | enum { kAlignments = sizeof(uint64_t) - 1 }; |
| 25 | |
| 26 | // Method to create a test value that is not the same when byte reversed. |
| 27 | template <typename T> |
| 28 | T CreateTestValue(bool negative, uint8_t num_bytes) { |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 29 | // Examples of output: |
| 30 | // T = int32_t, negative = false, num_bytes = 4: 0x00010203 |
| 31 | // T = int32_t, negative = true, num_bytes = 4: 0xFFFEFDFC |
| 32 | // T = int32_t, negative = false, num_bytes = 3: 0x000102 |
| 33 | // * T = int32_t, negative = true, num_bytes = 3: 0xFFFEFD |
| 34 | |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 35 | T val = 0; |
| 36 | for (uint8_t i = 0; i != num_bytes; ++i) { |
| 37 | val = (val << 8) + (negative ? (0xFF - i) : (i + 1)); |
| 38 | } |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 39 | |
| 40 | // This loop will create a sign extend mask if num_bytes if necessary. |
| 41 | // For the last example (marked * above), the number needs to be sign |
| 42 | // extended to be a valid int32_t. The sign extend mask is 0xFF000000. |
| 43 | // Comments for each step with this example below. |
| 44 | if (std::numeric_limits<T>::is_signed && negative && |
| 45 | num_bytes < sizeof(T)) { |
| 46 | // Start with mask = 0xFFFFFFFF. |
| 47 | T mask = static_cast<T>(-1); |
| 48 | // Create a temporary for the lowest byte (0x000000FF). |
| 49 | const T neg_byte = static_cast<T>(0xFF); |
| 50 | for (int i = 0; i < num_bytes; ++i) { |
| 51 | // And the inverse of the temporary and the mask: |
| 52 | // 0xFFFFFFFF & 0xFFFFFF00 = 0xFFFFFF00. |
| 53 | // 0xFFFFFF00 & 0xFFFF00FF = 0xFFFF0000. |
| 54 | // 0xFFFF0000 & 0xFF00FFFF = 0xFF000000. |
| 55 | mask &= ~(neg_byte << (i * 8)); |
| 56 | } |
| 57 | // Add the sign extension mask to the actual value. |
| 58 | val |= mask; |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 59 | } |
| 60 | return val; |
| 61 | } |
| 62 | |
| 63 | // Populate byte buffer with value, in big endian format. |
| 64 | template <typename T> |
| 65 | void PopulateTestData(uint8_t* data, T value, int num_bytes, bool bigendian) { |
| 66 | if (bigendian) { |
| 67 | for (int i = 0; i < num_bytes; ++i) { |
| 68 | data[i] = (value >> ((num_bytes - i - 1) * 8)) & 0xFF; |
| 69 | } |
| 70 | } else { |
| 71 | for (int i = 0; i < num_bytes; ++i) { |
| 72 | data[i] = (value >> (i * 8)) & 0xFF; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Test reading big endian numbers. |
| 78 | // Template arguments: Type T, read method RM(buffer), B bytes of data. |
sprang@webrtc.org | 2a6558c | 2015-01-28 12:37:36 +0000 | [diff] [blame] | 79 | template <typename T, T (*RM)(const uint8_t*), int B> |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 80 | void TestRead(bool big_endian) { |
| 81 | // Test both for values that are positive and negative (if signed) |
| 82 | for (int neg = 0; neg < 2; ++neg) { |
| 83 | bool negative = neg > 0; |
| 84 | |
| 85 | // Write test value to byte buffer, in big endian format. |
| 86 | T test_value = CreateTestValue<T>(negative, B); |
| 87 | uint8_t bytes[B + kAlignments]; |
| 88 | |
| 89 | // Make one test for each alignment. |
| 90 | for (int i = 0; i < kAlignments; ++i) { |
| 91 | PopulateTestData(bytes + i, test_value, B, big_endian); |
| 92 | |
| 93 | // Check that test value is retrieved from buffer when used read method. |
| 94 | EXPECT_EQ(test_value, RM(bytes + i)); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Test writing big endian numbers. |
| 100 | // Template arguments: Type T, write method WM(buffer, value), B bytes of data |
| 101 | template <typename T, void (*WM)(uint8_t*, T), int B> |
| 102 | void TestWrite(bool big_endian) { |
| 103 | // Test both for values that are positive and negative (if signed). |
| 104 | for (int neg = 0; neg < 2; ++neg) { |
| 105 | bool negative = neg > 0; |
| 106 | |
| 107 | // Write test value to byte buffer, in big endian format. |
| 108 | T test_value = CreateTestValue<T>(negative, B); |
| 109 | uint8_t expected_bytes[B + kAlignments]; |
| 110 | uint8_t bytes[B + kAlignments]; |
| 111 | |
| 112 | // Make one test for each alignment. |
| 113 | for (int i = 0; i < kAlignments; ++i) { |
| 114 | PopulateTestData(expected_bytes + i, test_value, B, big_endian); |
| 115 | |
| 116 | // Zero initialize buffer and let WM populate it. |
| 117 | memset(bytes, 0, B + kAlignments); |
| 118 | WM(bytes + i, test_value); |
| 119 | |
| 120 | // Check that data produced by WM is big endian as expected. |
| 121 | for (int j = 0; j < B; ++j) { |
| 122 | EXPECT_EQ(expected_bytes[i + j], bytes[i + j]); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | TEST_F(ByteIoTest, Test16UBitBigEndian) { |
| 130 | TestRead<uint16_t, ByteReader<uint16_t>::ReadBigEndian, |
| 131 | sizeof(uint16_t)>(true); |
| 132 | TestWrite<uint16_t, ByteWriter<uint16_t>::WriteBigEndian, |
| 133 | sizeof(uint16_t)>(true); |
| 134 | } |
| 135 | |
| 136 | TEST_F(ByteIoTest, Test24UBitBigEndian) { |
| 137 | TestRead<uint32_t, ByteReader<uint32_t, 3>::ReadBigEndian, 3>(true); |
| 138 | TestWrite<uint32_t, ByteWriter<uint32_t, 3>::WriteBigEndian, 3>(true); |
| 139 | } |
| 140 | |
| 141 | TEST_F(ByteIoTest, Test32UBitBigEndian) { |
| 142 | TestRead<uint32_t, ByteReader<uint32_t>::ReadBigEndian, |
| 143 | sizeof(uint32_t)>(true); |
| 144 | TestWrite<uint32_t, ByteWriter<uint32_t>::WriteBigEndian, |
| 145 | sizeof(uint32_t)>(true); |
| 146 | } |
| 147 | |
| 148 | TEST_F(ByteIoTest, Test64UBitBigEndian) { |
| 149 | TestRead<uint64_t, ByteReader<uint64_t>::ReadBigEndian, |
| 150 | sizeof(uint64_t)>(true); |
| 151 | TestWrite<uint64_t, ByteWriter<uint64_t>::WriteBigEndian, |
| 152 | sizeof(uint64_t)>(true); |
| 153 | } |
| 154 | |
| 155 | TEST_F(ByteIoTest, Test16SBitBigEndian) { |
| 156 | TestRead<int16_t, ByteReader<int16_t>::ReadBigEndian, |
| 157 | sizeof(int16_t)>(true); |
| 158 | TestWrite<int16_t, ByteWriter<int16_t>::WriteBigEndian, |
| 159 | sizeof(int16_t)>(true); |
| 160 | } |
| 161 | |
| 162 | TEST_F(ByteIoTest, Test24SBitBigEndian) { |
| 163 | TestRead<int32_t, ByteReader<int32_t, 3>::ReadBigEndian, 3>(true); |
| 164 | TestWrite<int32_t, ByteWriter<int32_t, 3>::WriteBigEndian, 3>(true); |
| 165 | } |
| 166 | |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 167 | TEST_F(ByteIoTest, Test32SBitBigEndian) { |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 168 | TestRead<int32_t, ByteReader<int32_t>::ReadBigEndian, |
| 169 | sizeof(int32_t)>(true); |
| 170 | TestWrite<int32_t, ByteWriter<int32_t>::WriteBigEndian, |
| 171 | sizeof(int32_t)>(true); |
| 172 | } |
| 173 | |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 174 | TEST_F(ByteIoTest, Test64SBitBigEndian) { |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 175 | TestRead<int64_t, ByteReader<int64_t>::ReadBigEndian, |
| 176 | sizeof(int64_t)>(true); |
| 177 | TestWrite<int64_t, ByteWriter<int64_t>::WriteBigEndian, |
| 178 | sizeof(int64_t)>(true); |
| 179 | } |
| 180 | |
| 181 | TEST_F(ByteIoTest, Test16UBitLittleEndian) { |
| 182 | TestRead<uint16_t, ByteReader<uint16_t>::ReadLittleEndian, |
| 183 | sizeof(uint16_t)>(false); |
| 184 | TestWrite<uint16_t, ByteWriter<uint16_t>::WriteLittleEndian, |
| 185 | sizeof(uint16_t)>(false); |
| 186 | } |
| 187 | |
| 188 | TEST_F(ByteIoTest, Test24UBitLittleEndian) { |
| 189 | TestRead<uint32_t, ByteReader<uint32_t, 3>::ReadLittleEndian, 3>(false); |
| 190 | TestWrite<uint32_t, ByteWriter<uint32_t, 3>::WriteLittleEndian, 3>(false); |
| 191 | } |
| 192 | |
| 193 | TEST_F(ByteIoTest, Test32UBitLittleEndian) { |
| 194 | TestRead<uint32_t, ByteReader<uint32_t>::ReadLittleEndian, |
| 195 | sizeof(uint32_t)>(false); |
| 196 | TestWrite<uint32_t, ByteWriter<uint32_t>::WriteLittleEndian, |
| 197 | sizeof(uint32_t)>(false); |
| 198 | } |
| 199 | |
| 200 | TEST_F(ByteIoTest, Test64UBitLittleEndian) { |
| 201 | TestRead<uint64_t, ByteReader<uint64_t>::ReadLittleEndian, |
| 202 | sizeof(uint64_t)>(false); |
| 203 | TestWrite<uint64_t, ByteWriter<uint64_t>::WriteLittleEndian, |
| 204 | sizeof(uint64_t)>(false); |
| 205 | } |
| 206 | |
| 207 | TEST_F(ByteIoTest, Test16SBitLittleEndian) { |
| 208 | TestRead<int16_t, ByteReader<int16_t>::ReadLittleEndian, |
| 209 | sizeof(int16_t)>(false); |
| 210 | TestWrite<int16_t, ByteWriter<int16_t>::WriteLittleEndian, |
| 211 | sizeof(int16_t)>(false); |
| 212 | } |
| 213 | |
| 214 | TEST_F(ByteIoTest, Test24SBitLittleEndian) { |
| 215 | TestRead<int32_t, ByteReader<int32_t, 3>::ReadLittleEndian, 3>(false); |
| 216 | TestWrite<int32_t, ByteWriter<int32_t, 3>::WriteLittleEndian, 3>(false); |
| 217 | } |
| 218 | |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 219 | TEST_F(ByteIoTest, Test32SBitLittleEndian) { |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 220 | TestRead<int32_t, ByteReader<int32_t>::ReadLittleEndian, |
| 221 | sizeof(int32_t)>(false); |
| 222 | TestWrite<int32_t, ByteWriter<int32_t>::WriteLittleEndian, |
| 223 | sizeof(int32_t)>(false); |
| 224 | } |
| 225 | |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 226 | TEST_F(ByteIoTest, Test64SBitLittleEndian) { |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 227 | TestRead<int64_t, ByteReader<int64_t>::ReadLittleEndian, |
| 228 | sizeof(int64_t)>(false); |
| 229 | TestWrite<int64_t, ByteWriter<int64_t>::WriteLittleEndian, |
| 230 | sizeof(int64_t)>(false); |
| 231 | } |
| 232 | |
henrik.lundin | 7a83951 | 2016-01-24 23:47:51 -0800 | [diff] [blame] | 233 | // Sets up a fixed byte array and converts N bytes from the array into a |
| 234 | // uint64_t. Verifies the value with hard-coded reference. |
| 235 | TEST(ByteIo, SanityCheckFixedByteArrayUnsignedReadBigEndian) { |
| 236 | uint8_t data[8] = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88}; |
| 237 | uint64_t value = ByteReader<uint64_t, 2>::ReadBigEndian(data); |
| 238 | EXPECT_EQ(static_cast<uint64_t>(0xFFEE), value); |
| 239 | value = ByteReader<uint64_t, 3>::ReadBigEndian(data); |
| 240 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDD), value); |
| 241 | value = ByteReader<uint64_t, 4>::ReadBigEndian(data); |
| 242 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCC), value); |
| 243 | value = ByteReader<uint64_t, 5>::ReadBigEndian(data); |
| 244 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCCBB), value); |
| 245 | value = ByteReader<uint64_t, 6>::ReadBigEndian(data); |
| 246 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCCBBAA), value); |
| 247 | value = ByteReader<uint64_t, 7>::ReadBigEndian(data); |
| 248 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCCBBAA99), value); |
| 249 | value = ByteReader<uint64_t, 8>::ReadBigEndian(data); |
| 250 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCCBBAA9988), value); |
| 251 | } |
| 252 | |
| 253 | // Same as above, but for little-endian reading. |
| 254 | TEST(ByteIo, SanityCheckFixedByteArrayUnsignedReadLittleEndian) { |
| 255 | uint8_t data[8] = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88}; |
| 256 | uint64_t value = ByteReader<uint64_t, 2>::ReadLittleEndian(data); |
| 257 | EXPECT_EQ(static_cast<uint64_t>(0xEEFF), value); |
| 258 | value = ByteReader<uint64_t, 3>::ReadLittleEndian(data); |
| 259 | EXPECT_EQ(static_cast<uint64_t>(0xDDEEFF), value); |
| 260 | value = ByteReader<uint64_t, 4>::ReadLittleEndian(data); |
| 261 | EXPECT_EQ(static_cast<uint64_t>(0xCCDDEEFF), value); |
| 262 | value = ByteReader<uint64_t, 5>::ReadLittleEndian(data); |
| 263 | EXPECT_EQ(static_cast<uint64_t>(0xBBCCDDEEFF), value); |
| 264 | value = ByteReader<uint64_t, 6>::ReadLittleEndian(data); |
| 265 | EXPECT_EQ(static_cast<uint64_t>(0xAABBCCDDEEFF), value); |
| 266 | value = ByteReader<uint64_t, 7>::ReadLittleEndian(data); |
| 267 | EXPECT_EQ(static_cast<uint64_t>(0x99AABBCCDDEEFF), value); |
| 268 | value = ByteReader<uint64_t, 8>::ReadLittleEndian(data); |
| 269 | EXPECT_EQ(static_cast<uint64_t>(0x8899AABBCCDDEEFF), value); |
| 270 | } |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 271 | } // namespace |
| 272 | } // namespace webrtc |