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) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 130 | TestRead<uint16_t, ByteReader<uint16_t>::ReadBigEndian, sizeof(uint16_t)>( |
| 131 | true); |
| 132 | TestWrite<uint16_t, ByteWriter<uint16_t>::WriteBigEndian, sizeof(uint16_t)>( |
| 133 | true); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 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) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 142 | TestRead<uint32_t, ByteReader<uint32_t>::ReadBigEndian, sizeof(uint32_t)>( |
| 143 | true); |
| 144 | TestWrite<uint32_t, ByteWriter<uint32_t>::WriteBigEndian, sizeof(uint32_t)>( |
| 145 | true); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST_F(ByteIoTest, Test64UBitBigEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 149 | TestRead<uint64_t, ByteReader<uint64_t>::ReadBigEndian, sizeof(uint64_t)>( |
| 150 | true); |
| 151 | TestWrite<uint64_t, ByteWriter<uint64_t>::WriteBigEndian, sizeof(uint64_t)>( |
| 152 | true); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | TEST_F(ByteIoTest, Test16SBitBigEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 156 | TestRead<int16_t, ByteReader<int16_t>::ReadBigEndian, sizeof(int16_t)>(true); |
| 157 | TestWrite<int16_t, ByteWriter<int16_t>::WriteBigEndian, sizeof(int16_t)>( |
| 158 | true); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | TEST_F(ByteIoTest, Test24SBitBigEndian) { |
| 162 | TestRead<int32_t, ByteReader<int32_t, 3>::ReadBigEndian, 3>(true); |
| 163 | TestWrite<int32_t, ByteWriter<int32_t, 3>::WriteBigEndian, 3>(true); |
| 164 | } |
| 165 | |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 166 | TEST_F(ByteIoTest, Test32SBitBigEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 167 | TestRead<int32_t, ByteReader<int32_t>::ReadBigEndian, sizeof(int32_t)>(true); |
| 168 | TestWrite<int32_t, ByteWriter<int32_t>::WriteBigEndian, sizeof(int32_t)>( |
| 169 | true); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 170 | } |
| 171 | |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 172 | TEST_F(ByteIoTest, Test64SBitBigEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 173 | TestRead<int64_t, ByteReader<int64_t>::ReadBigEndian, sizeof(int64_t)>(true); |
| 174 | TestWrite<int64_t, ByteWriter<int64_t>::WriteBigEndian, sizeof(int64_t)>( |
| 175 | true); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST_F(ByteIoTest, Test16UBitLittleEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 179 | TestRead<uint16_t, ByteReader<uint16_t>::ReadLittleEndian, sizeof(uint16_t)>( |
| 180 | false); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 181 | TestWrite<uint16_t, ByteWriter<uint16_t>::WriteLittleEndian, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 182 | sizeof(uint16_t)>(false); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | TEST_F(ByteIoTest, Test24UBitLittleEndian) { |
| 186 | TestRead<uint32_t, ByteReader<uint32_t, 3>::ReadLittleEndian, 3>(false); |
| 187 | TestWrite<uint32_t, ByteWriter<uint32_t, 3>::WriteLittleEndian, 3>(false); |
| 188 | } |
| 189 | |
| 190 | TEST_F(ByteIoTest, Test32UBitLittleEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 191 | TestRead<uint32_t, ByteReader<uint32_t>::ReadLittleEndian, sizeof(uint32_t)>( |
| 192 | false); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 193 | TestWrite<uint32_t, ByteWriter<uint32_t>::WriteLittleEndian, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 194 | sizeof(uint32_t)>(false); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | TEST_F(ByteIoTest, Test64UBitLittleEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 198 | TestRead<uint64_t, ByteReader<uint64_t>::ReadLittleEndian, sizeof(uint64_t)>( |
| 199 | false); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 200 | TestWrite<uint64_t, ByteWriter<uint64_t>::WriteLittleEndian, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 201 | sizeof(uint64_t)>(false); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(ByteIoTest, Test16SBitLittleEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 205 | TestRead<int16_t, ByteReader<int16_t>::ReadLittleEndian, sizeof(int16_t)>( |
| 206 | false); |
| 207 | TestWrite<int16_t, ByteWriter<int16_t>::WriteLittleEndian, sizeof(int16_t)>( |
| 208 | false); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | TEST_F(ByteIoTest, Test24SBitLittleEndian) { |
| 212 | TestRead<int32_t, ByteReader<int32_t, 3>::ReadLittleEndian, 3>(false); |
| 213 | TestWrite<int32_t, ByteWriter<int32_t, 3>::WriteLittleEndian, 3>(false); |
| 214 | } |
| 215 | |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 216 | TEST_F(ByteIoTest, Test32SBitLittleEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 217 | TestRead<int32_t, ByteReader<int32_t>::ReadLittleEndian, sizeof(int32_t)>( |
| 218 | false); |
| 219 | TestWrite<int32_t, ByteWriter<int32_t>::WriteLittleEndian, sizeof(int32_t)>( |
| 220 | false); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 221 | } |
| 222 | |
sprang | c63f79a | 2016-02-26 05:13:47 -0800 | [diff] [blame] | 223 | TEST_F(ByteIoTest, Test64SBitLittleEndian) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 224 | TestRead<int64_t, ByteReader<int64_t>::ReadLittleEndian, sizeof(int64_t)>( |
| 225 | false); |
| 226 | TestWrite<int64_t, ByteWriter<int64_t>::WriteLittleEndian, sizeof(int64_t)>( |
| 227 | false); |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 228 | } |
| 229 | |
henrik.lundin | 7a83951 | 2016-01-24 23:47:51 -0800 | [diff] [blame] | 230 | // Sets up a fixed byte array and converts N bytes from the array into a |
| 231 | // uint64_t. Verifies the value with hard-coded reference. |
| 232 | TEST(ByteIo, SanityCheckFixedByteArrayUnsignedReadBigEndian) { |
| 233 | uint8_t data[8] = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88}; |
| 234 | uint64_t value = ByteReader<uint64_t, 2>::ReadBigEndian(data); |
| 235 | EXPECT_EQ(static_cast<uint64_t>(0xFFEE), value); |
| 236 | value = ByteReader<uint64_t, 3>::ReadBigEndian(data); |
| 237 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDD), value); |
| 238 | value = ByteReader<uint64_t, 4>::ReadBigEndian(data); |
| 239 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCC), value); |
| 240 | value = ByteReader<uint64_t, 5>::ReadBigEndian(data); |
| 241 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCCBB), value); |
| 242 | value = ByteReader<uint64_t, 6>::ReadBigEndian(data); |
| 243 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCCBBAA), value); |
| 244 | value = ByteReader<uint64_t, 7>::ReadBigEndian(data); |
| 245 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCCBBAA99), value); |
| 246 | value = ByteReader<uint64_t, 8>::ReadBigEndian(data); |
| 247 | EXPECT_EQ(static_cast<uint64_t>(0xFFEEDDCCBBAA9988), value); |
| 248 | } |
| 249 | |
| 250 | // Same as above, but for little-endian reading. |
| 251 | TEST(ByteIo, SanityCheckFixedByteArrayUnsignedReadLittleEndian) { |
| 252 | uint8_t data[8] = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88}; |
| 253 | uint64_t value = ByteReader<uint64_t, 2>::ReadLittleEndian(data); |
| 254 | EXPECT_EQ(static_cast<uint64_t>(0xEEFF), value); |
| 255 | value = ByteReader<uint64_t, 3>::ReadLittleEndian(data); |
| 256 | EXPECT_EQ(static_cast<uint64_t>(0xDDEEFF), value); |
| 257 | value = ByteReader<uint64_t, 4>::ReadLittleEndian(data); |
| 258 | EXPECT_EQ(static_cast<uint64_t>(0xCCDDEEFF), value); |
| 259 | value = ByteReader<uint64_t, 5>::ReadLittleEndian(data); |
| 260 | EXPECT_EQ(static_cast<uint64_t>(0xBBCCDDEEFF), value); |
| 261 | value = ByteReader<uint64_t, 6>::ReadLittleEndian(data); |
| 262 | EXPECT_EQ(static_cast<uint64_t>(0xAABBCCDDEEFF), value); |
| 263 | value = ByteReader<uint64_t, 7>::ReadLittleEndian(data); |
| 264 | EXPECT_EQ(static_cast<uint64_t>(0x99AABBCCDDEEFF), value); |
| 265 | value = ByteReader<uint64_t, 8>::ReadLittleEndian(data); |
| 266 | EXPECT_EQ(static_cast<uint64_t>(0x8899AABBCCDDEEFF), value); |
| 267 | } |
sprang@webrtc.org | 499631c | 2013-12-03 13:22:48 +0000 | [diff] [blame] | 268 | } // namespace |
| 269 | } // namespace webrtc |