1 /*
2  * Copyright (c) 2018, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "aom/aom_integer.h"
13 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
14 
15 namespace {
16 const uint64_t kMaximumLeb128CodedSize = 8;
17 const uint8_t kLeb128PadByte = 0x80;  // Binary: 10000000
18 const uint64_t kMaximumLeb128Value = UINT32_MAX;
19 const uint32_t kSizeTestNumValues = 6;
20 const uint32_t kSizeTestExpectedSizes[kSizeTestNumValues] = {
21   1, 1, 2, 3, 4, 5
22 };
23 const uint64_t kSizeTestInputs[kSizeTestNumValues] = {
24   0, 0x7f, 0x3fff, 0x1fffff, 0xffffff, 0x10000000
25 };
26 
27 const uint8_t kOutOfRangeLeb128Value[5] = { 0x80, 0x80, 0x80, 0x80,
28                                             0x10 };  // UINT32_MAX + 1
29 }  // namespace
30 
TEST(AomLeb128,DecodeTest)31 TEST(AomLeb128, DecodeTest) {
32   const size_t num_leb128_bytes = 3;
33   const uint8_t leb128_bytes[num_leb128_bytes] = { 0xE5, 0x8E, 0x26 };
34   const uint64_t expected_value = 0x98765;  // 624485
35   const size_t expected_length = 3;
36   uint64_t value = ~0ULL;  // make sure value is cleared by the function
37   size_t length;
38   ASSERT_EQ(
39       aom_uleb_decode(&leb128_bytes[0], num_leb128_bytes, &value, &length), 0);
40   ASSERT_EQ(expected_value, value);
41   ASSERT_EQ(expected_length, length);
42 
43   // Make sure the decoder stops on the last marked LEB128 byte.
44   aom_uleb_decode(&leb128_bytes[0], num_leb128_bytes + 1, &value, &length);
45   ASSERT_EQ(expected_value, value);
46   ASSERT_EQ(expected_length, length);
47 }
48 
TEST(AomLeb128,EncodeTest)49 TEST(AomLeb128, EncodeTest) {
50   const uint32_t test_value = 0x98765;  // 624485
51   const uint8_t expected_bytes[3] = { 0xE5, 0x8E, 0x26 };
52   const size_t kWriteBufferSize = 4;
53   uint8_t write_buffer[kWriteBufferSize] = { 0 };
54   size_t bytes_written = 0;
55   ASSERT_EQ(aom_uleb_encode(test_value, kWriteBufferSize, &write_buffer[0],
56                             &bytes_written),
57             0);
58   ASSERT_EQ(bytes_written, 3u);
59   for (size_t i = 0; i < bytes_written; ++i) {
60     ASSERT_EQ(write_buffer[i], expected_bytes[i]);
61   }
62 }
63 
TEST(AomLeb128,EncodeDecodeTest)64 TEST(AomLeb128, EncodeDecodeTest) {
65   const uint32_t value = 0x98765;  // 624485
66   const size_t kWriteBufferSize = 4;
67   uint8_t write_buffer[kWriteBufferSize] = { 0 };
68   size_t bytes_written = 0;
69   ASSERT_EQ(aom_uleb_encode(value, kWriteBufferSize, &write_buffer[0],
70                             &bytes_written),
71             0);
72   ASSERT_EQ(bytes_written, 3u);
73   uint64_t decoded_value;
74   size_t decoded_length;
75   aom_uleb_decode(&write_buffer[0], bytes_written, &decoded_value,
76                   &decoded_length);
77   ASSERT_EQ(value, decoded_value);
78   ASSERT_EQ(bytes_written, decoded_length);
79 }
80 
TEST(AomLeb128,FixedSizeEncodeTest)81 TEST(AomLeb128, FixedSizeEncodeTest) {
82   const uint32_t test_value = 0x123;
83   const uint8_t expected_bytes[4] = { 0xa3, 0x82, 0x80, 0x00 };
84   const size_t kWriteBufferSize = 4;
85   uint8_t write_buffer[kWriteBufferSize] = { 0 };
86   size_t bytes_written = 0;
87   ASSERT_EQ(0, aom_uleb_encode_fixed_size(test_value, kWriteBufferSize,
88                                           kWriteBufferSize, &write_buffer[0],
89                                           &bytes_written));
90   ASSERT_EQ(kWriteBufferSize, bytes_written);
91   for (size_t i = 0; i < bytes_written; ++i) {
92     ASSERT_EQ(write_buffer[i], expected_bytes[i]);
93   }
94 }
95 
TEST(AomLeb128,FixedSizeEncodeDecodeTest)96 TEST(AomLeb128, FixedSizeEncodeDecodeTest) {
97   const uint32_t value = 0x1;
98   const size_t kWriteBufferSize = 4;
99   uint8_t write_buffer[kWriteBufferSize] = { 0 };
100   size_t bytes_written = 0;
101   ASSERT_EQ(
102       aom_uleb_encode_fixed_size(value, kWriteBufferSize, kWriteBufferSize,
103                                  &write_buffer[0], &bytes_written),
104       0);
105   ASSERT_EQ(bytes_written, 4u);
106   uint64_t decoded_value;
107   size_t decoded_length;
108   aom_uleb_decode(&write_buffer[0], bytes_written, &decoded_value,
109                   &decoded_length);
110   ASSERT_EQ(value, decoded_value);
111   ASSERT_EQ(bytes_written, decoded_length);
112 }
113 
TEST(AomLeb128,SizeTest)114 TEST(AomLeb128, SizeTest) {
115   for (size_t i = 0; i < kSizeTestNumValues; ++i) {
116     ASSERT_EQ(kSizeTestExpectedSizes[i],
117               aom_uleb_size_in_bytes(kSizeTestInputs[i]));
118   }
119 }
120 
TEST(AomLeb128,DecodeFailTest)121 TEST(AomLeb128, DecodeFailTest) {
122   // Input buffer containing what would be a valid 9 byte LEB128 encoded
123   // unsigned integer.
124   const uint8_t kAllPadBytesBuffer[kMaximumLeb128CodedSize + 1] = {
125     kLeb128PadByte, kLeb128PadByte, kLeb128PadByte,
126     kLeb128PadByte, kLeb128PadByte, kLeb128PadByte,
127     kLeb128PadByte, kLeb128PadByte, 0
128   };
129   uint64_t decoded_value;
130 
131   // Test that decode fails when result would be valid 9 byte integer.
132   ASSERT_EQ(aom_uleb_decode(&kAllPadBytesBuffer[0], kMaximumLeb128CodedSize + 1,
133                             &decoded_value, NULL),
134             -1);
135 
136   // Test that encoded value missing terminator byte within available buffer
137   // range causes decode error.
138   ASSERT_EQ(aom_uleb_decode(&kAllPadBytesBuffer[0], kMaximumLeb128CodedSize,
139                             &decoded_value, NULL),
140             -1);
141 
142   // Test that LEB128 input that decodes to a value larger than 32-bits fails.
143   size_t value_size = 0;
144   ASSERT_EQ(aom_uleb_decode(&kOutOfRangeLeb128Value[0],
145                             sizeof(kOutOfRangeLeb128Value), &decoded_value,
146                             &value_size),
147             -1);
148 }
149 
TEST(AomLeb128,EncodeFailTest)150 TEST(AomLeb128, EncodeFailTest) {
151   const size_t kWriteBufferSize = 4;
152   const uint32_t kValidTestValue = 1;
153   uint8_t write_buffer[kWriteBufferSize] = { 0 };
154   size_t coded_size = 0;
155   ASSERT_EQ(
156       aom_uleb_encode(kValidTestValue, kWriteBufferSize, NULL, &coded_size),
157       -1);
158   ASSERT_EQ(aom_uleb_encode(kValidTestValue, kWriteBufferSize, &write_buffer[0],
159                             NULL),
160             -1);
161 
162   const uint32_t kValueOutOfRangeForBuffer = 0xFFFFFFFF;
163   ASSERT_EQ(aom_uleb_encode(kValueOutOfRangeForBuffer, kWriteBufferSize,
164                             &write_buffer[0], &coded_size),
165             -1);
166 
167   const uint64_t kValueOutOfRange = kMaximumLeb128Value + 1;
168   ASSERT_EQ(aom_uleb_encode(kValueOutOfRange, kWriteBufferSize,
169                             &write_buffer[0], &coded_size),
170             -1);
171 
172   const size_t kPadSizeOutOfRange = 5;
173   ASSERT_EQ(aom_uleb_encode_fixed_size(kValidTestValue, kWriteBufferSize,
174                                        kPadSizeOutOfRange, &write_buffer[0],
175                                        &coded_size),
176             -1);
177 }
178