blob: 20b66362fc24bef977c62ffde96f0a97a04aa0e4 [file] [log] [blame]
sprang@webrtc.org499631c2013-12-03 13:22:48 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/rtp_rtcp/source/byte_io.h"
14#include "test/gtest.h"
sprang@webrtc.org499631c2013-12-03 13:22:48 +000015
16namespace webrtc {
17namespace {
18
19class ByteIoTest : public ::testing::Test {
20 protected:
Danil Chapovalovdd7e2842018-03-09 15:37:03 +000021 ByteIoTest() = default;
22 ~ByteIoTest() override = default;
sprang@webrtc.org499631c2013-12-03 13:22:48 +000023
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) {
sprangc63f79a2016-02-26 05:13:47 -080029 // 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.org499631c2013-12-03 13:22:48 +000035 T val = 0;
36 for (uint8_t i = 0; i != num_bytes; ++i) {
37 val = (val << 8) + (negative ? (0xFF - i) : (i + 1));
38 }
sprangc63f79a2016-02-26 05:13:47 -080039
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.org499631c2013-12-03 13:22:48 +000059 }
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.org2a6558c2015-01-28 12:37:36 +000079 template <typename T, T (*RM)(const uint8_t*), int B>
sprang@webrtc.org499631c2013-12-03 13:22:48 +000080 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
129TEST_F(ByteIoTest, Test16UBitBigEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200130 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.org499631c2013-12-03 13:22:48 +0000134}
135
136TEST_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
141TEST_F(ByteIoTest, Test32UBitBigEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200142 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.org499631c2013-12-03 13:22:48 +0000146}
147
148TEST_F(ByteIoTest, Test64UBitBigEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200149 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.org499631c2013-12-03 13:22:48 +0000153}
154
155TEST_F(ByteIoTest, Test16SBitBigEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200156 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.org499631c2013-12-03 13:22:48 +0000159}
160
161TEST_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
sprangc63f79a2016-02-26 05:13:47 -0800166TEST_F(ByteIoTest, Test32SBitBigEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200167 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.org499631c2013-12-03 13:22:48 +0000170}
171
sprangc63f79a2016-02-26 05:13:47 -0800172TEST_F(ByteIoTest, Test64SBitBigEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200173 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.org499631c2013-12-03 13:22:48 +0000176}
177
178TEST_F(ByteIoTest, Test16UBitLittleEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200179 TestRead<uint16_t, ByteReader<uint16_t>::ReadLittleEndian, sizeof(uint16_t)>(
180 false);
sprang@webrtc.org499631c2013-12-03 13:22:48 +0000181 TestWrite<uint16_t, ByteWriter<uint16_t>::WriteLittleEndian,
Yves Gerey665174f2018-06-19 15:03:05 +0200182 sizeof(uint16_t)>(false);
sprang@webrtc.org499631c2013-12-03 13:22:48 +0000183}
184
185TEST_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
190TEST_F(ByteIoTest, Test32UBitLittleEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200191 TestRead<uint32_t, ByteReader<uint32_t>::ReadLittleEndian, sizeof(uint32_t)>(
192 false);
sprang@webrtc.org499631c2013-12-03 13:22:48 +0000193 TestWrite<uint32_t, ByteWriter<uint32_t>::WriteLittleEndian,
Yves Gerey665174f2018-06-19 15:03:05 +0200194 sizeof(uint32_t)>(false);
sprang@webrtc.org499631c2013-12-03 13:22:48 +0000195}
196
197TEST_F(ByteIoTest, Test64UBitLittleEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200198 TestRead<uint64_t, ByteReader<uint64_t>::ReadLittleEndian, sizeof(uint64_t)>(
199 false);
sprang@webrtc.org499631c2013-12-03 13:22:48 +0000200 TestWrite<uint64_t, ByteWriter<uint64_t>::WriteLittleEndian,
Yves Gerey665174f2018-06-19 15:03:05 +0200201 sizeof(uint64_t)>(false);
sprang@webrtc.org499631c2013-12-03 13:22:48 +0000202}
203
204TEST_F(ByteIoTest, Test16SBitLittleEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200205 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.org499631c2013-12-03 13:22:48 +0000209}
210
211TEST_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
sprangc63f79a2016-02-26 05:13:47 -0800216TEST_F(ByteIoTest, Test32SBitLittleEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200217 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.org499631c2013-12-03 13:22:48 +0000221}
222
sprangc63f79a2016-02-26 05:13:47 -0800223TEST_F(ByteIoTest, Test64SBitLittleEndian) {
Yves Gerey665174f2018-06-19 15:03:05 +0200224 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.org499631c2013-12-03 13:22:48 +0000228}
229
henrik.lundin7a839512016-01-24 23:47:51 -0800230// 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.
232TEST(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.
251TEST(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.org499631c2013-12-03 13:22:48 +0000268} // namespace
269} // namespace webrtc