Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 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 | */ |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 11 | |
| 12 | #include "aom/aom_integer.h" |
Sarah Parker | 86bec88 | 2018-02-21 16:20:57 -0800 | [diff] [blame] | 13 | #include "third_party/googletest/src/googletest/include/gtest/gtest.h" |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 14 | |
| 15 | namespace { |
Tom Finegan | f412906 | 2018-02-08 08:32:42 -0800 | [diff] [blame] | 16 | const uint64_t kMaximumLeb128CodedSize = 8; |
Tom Finegan | 46fc1f0 | 2018-05-16 10:34:25 -0700 | [diff] [blame] | 17 | const uint8_t kLeb128PadByte = 0x80; // Binary: 10000000 |
| 18 | const uint64_t kMaximumLeb128Value = UINT32_MAX; |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 19 | const uint32_t kSizeTestNumValues = 6; |
| 20 | const uint32_t kSizeTestExpectedSizes[kSizeTestNumValues] = { |
| 21 | 1, 1, 2, 3, 4, 5 |
| 22 | }; |
Tom Finegan | 9540d59 | 2018-02-06 10:04:31 -0800 | [diff] [blame] | 23 | const uint64_t kSizeTestInputs[kSizeTestNumValues] = { |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 24 | 0, 0x7f, 0x3fff, 0x1fffff, 0xffffff, 0x10000000 |
| 25 | }; |
Tom Finegan | 46fc1f0 | 2018-05-16 10:34:25 -0700 | [diff] [blame] | 26 | |
| 27 | const uint8_t kOutOfRangeLeb128Value[5] = { 0x80, 0x80, 0x80, 0x80, |
| 28 | 0x10 }; // UINT32_MAX + 1 |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 29 | } // namespace |
| 30 | |
| 31 | TEST(AomLeb128, DecodeTest) { |
| 32 | const size_t num_leb128_bytes = 3; |
| 33 | const uint8_t leb128_bytes[num_leb128_bytes] = { 0xE5, 0x8E, 0x26 }; |
Tom Finegan | 9540d59 | 2018-02-06 10:04:31 -0800 | [diff] [blame] | 34 | const uint64_t expected_value = 0x98765; // 624485 |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 35 | const size_t expected_length = 3; |
James Zern | bd5aba0 | 2018-03-10 14:56:21 -0800 | [diff] [blame] | 36 | uint64_t value = ~0ULL; // make sure value is cleared by the function |
| 37 | size_t length; |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 38 | ASSERT_EQ( |
| 39 | aom_uleb_decode(&leb128_bytes[0], num_leb128_bytes, &value, &length), 0); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 40 | ASSERT_EQ(expected_value, value); |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 41 | ASSERT_EQ(expected_length, length); |
Tom Finegan | f2d40f6 | 2018-02-01 11:52:49 -0800 | [diff] [blame] | 42 | |
| 43 | // Make sure the decoder stops on the last marked LEB128 byte. |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 44 | aom_uleb_decode(&leb128_bytes[0], num_leb128_bytes + 1, &value, &length); |
Tom Finegan | f2d40f6 | 2018-02-01 11:52:49 -0800 | [diff] [blame] | 45 | ASSERT_EQ(expected_value, value); |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 46 | ASSERT_EQ(expected_length, length); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(AomLeb128, EncodeTest) { |
| 50 | const uint32_t test_value = 0x98765; // 624485 |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 51 | const uint8_t expected_bytes[3] = { 0xE5, 0x8E, 0x26 }; |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 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); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 59 | for (size_t i = 0; i < bytes_written; ++i) { |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 60 | ASSERT_EQ(write_buffer[i], expected_bytes[i]); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 61 | } |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Tom Finegan | f412906 | 2018-02-08 08:32:42 -0800 | [diff] [blame] | 64 | TEST(AomLeb128, EncodeDecodeTest) { |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 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); |
James Zern | bd5aba0 | 2018-03-10 14:56:21 -0800 | [diff] [blame] | 73 | uint64_t decoded_value; |
| 74 | size_t decoded_length; |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 75 | aom_uleb_decode(&write_buffer[0], bytes_written, &decoded_value, |
| 76 | &decoded_length); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 77 | ASSERT_EQ(value, decoded_value); |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 78 | ASSERT_EQ(bytes_written, decoded_length); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST(AomLeb128, FixedSizeEncodeTest) { |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 82 | const uint32_t test_value = 0x123; |
| 83 | const uint8_t expected_bytes[4] = { 0xa3, 0x82, 0x80, 0x00 }; |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 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); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 91 | for (size_t i = 0; i < bytes_written; ++i) { |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 92 | ASSERT_EQ(write_buffer[i], expected_bytes[i]); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 93 | } |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Tom Finegan | f412906 | 2018-02-08 08:32:42 -0800 | [diff] [blame] | 96 | TEST(AomLeb128, FixedSizeEncodeDecodeTest) { |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 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); |
James Zern | bd5aba0 | 2018-03-10 14:56:21 -0800 | [diff] [blame] | 106 | uint64_t decoded_value; |
| 107 | size_t decoded_length; |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 108 | aom_uleb_decode(&write_buffer[0], bytes_written, &decoded_value, |
| 109 | &decoded_length); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 110 | ASSERT_EQ(value, decoded_value); |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 111 | ASSERT_EQ(bytes_written, decoded_length); |
Tom Finegan | 10fea49 | 2018-01-08 18:07:46 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 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 | } |
Tom Finegan | 209ba85 | 2018-01-10 08:36:40 -0800 | [diff] [blame] | 120 | |
Tom Finegan | f412906 | 2018-02-08 08:32:42 -0800 | [diff] [blame] | 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 | }; |
James Zern | bd5aba0 | 2018-03-10 14:56:21 -0800 | [diff] [blame] | 129 | uint64_t decoded_value; |
Tom Finegan | f412906 | 2018-02-08 08:32:42 -0800 | [diff] [blame] | 130 | |
| 131 | // Test that decode fails when result would be valid 9 byte integer. |
| 132 | ASSERT_EQ(aom_uleb_decode(&kAllPadBytesBuffer[0], kMaximumLeb128CodedSize + 1, |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 133 | &decoded_value, NULL), |
Tom Finegan | f412906 | 2018-02-08 08:32:42 -0800 | [diff] [blame] | 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, |
David Barker | 0debbaa | 2018-02-23 14:39:01 +0000 | [diff] [blame] | 139 | &decoded_value, NULL), |
Tom Finegan | f412906 | 2018-02-08 08:32:42 -0800 | [diff] [blame] | 140 | -1); |
Tom Finegan | ba7fa41 | 2018-05-15 11:23:54 -0700 | [diff] [blame] | 141 | |
| 142 | // Test that LEB128 input that decodes to a value larger than 32-bits fails. |
Tom Finegan | ba7fa41 | 2018-05-15 11:23:54 -0700 | [diff] [blame] | 143 | size_t value_size = 0; |
Tom Finegan | 46fc1f0 | 2018-05-16 10:34:25 -0700 | [diff] [blame] | 144 | ASSERT_EQ(aom_uleb_decode(&kOutOfRangeLeb128Value[0], |
| 145 | sizeof(kOutOfRangeLeb128Value), &decoded_value, |
Tom Finegan | 2c468f5 | 2018-05-16 10:00:12 -0700 | [diff] [blame] | 146 | &value_size), |
Tom Finegan | ba7fa41 | 2018-05-15 11:23:54 -0700 | [diff] [blame] | 147 | -1); |
Tom Finegan | f412906 | 2018-02-08 08:32:42 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | TEST(AomLeb128, EncodeFailTest) { |
Tom Finegan | 209ba85 | 2018-01-10 08:36:40 -0800 | [diff] [blame] | 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 | |
Tom Finegan | f412906 | 2018-02-08 08:32:42 -0800 | [diff] [blame] | 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; |
Tom Finegan | 209ba85 | 2018-01-10 08:36:40 -0800 | [diff] [blame] | 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 | } |