leb128_unittest.cc (4502B)
1 /* 2 * Copyright (c) 2023 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 "modules/rtp_rtcp/source/leb128.h" 12 13 #include <cstdint> 14 #include <iterator> 15 #include <limits> 16 17 #include "api/array_view.h" 18 #include "test/gmock.h" 19 #include "test/gtest.h" 20 21 namespace webrtc { 22 namespace { 23 24 using ::testing::ElementsAre; 25 26 TEST(Leb128Test, Size) { 27 EXPECT_EQ(Leb128Size(0), 1); 28 EXPECT_EQ(Leb128Size(0b0111'1111), 1); 29 EXPECT_EQ(Leb128Size(0b1000'0000), 2); 30 EXPECT_EQ(Leb128Size(std::numeric_limits<uint64_t>::max()), 10); 31 } 32 33 TEST(Leb128Test, ReadZero) { 34 const uint8_t one_byte[] = {0}; 35 const uint8_t* read_at = one_byte; 36 EXPECT_EQ(ReadLeb128(read_at, std::end(one_byte)), uint64_t{0}); 37 EXPECT_EQ(std::distance(read_at, std::end(one_byte)), 0); 38 } 39 40 TEST(Leb128Test, ReadOneByte) { 41 const uint8_t buffer[] = {0b0010'1100}; 42 const uint8_t* read_at = buffer; 43 EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)), uint64_t{0b0010'1100}); 44 EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0); 45 } 46 47 TEST(Leb128Test, ReadTwoByte) { 48 const uint8_t buffer[] = {0b1010'1100, 0b0111'0000}; 49 const uint8_t* read_at = buffer; 50 EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)), 51 uint64_t{0b111'0000'010'1100}); 52 EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0); 53 } 54 55 TEST(Leb128Test, ReadNearlyMaxValue1) { 56 const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 57 0xff, 0xff, 0xff, 0x7f}; 58 const uint8_t* read_at = buffer; 59 EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)), 60 uint64_t{0x7fff'ffff'ffff'ffff}); 61 EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0); 62 } 63 64 TEST(Leb128Test, ReadNearlyMaxValue2) { 65 // This is valid, though not optimal way to store 63 bits of the value. 66 const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 67 0xff, 0xff, 0xff, 0xff, 0x0}; 68 const uint8_t* read_at = buffer; 69 EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)), 70 uint64_t{0x7fff'ffff'ffff'ffff}); 71 EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0); 72 } 73 74 TEST(Leb128Test, ReadMaxValue) { 75 // This is valid, though not optimal way to store 63 bits of the value. 76 const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 77 0xff, 0xff, 0xff, 0xff, 0x1}; 78 const uint8_t* read_at = buffer; 79 EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)), 0xffff'ffff'ffff'ffff); 80 EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0); 81 } 82 83 TEST(Leb128Test, FailsToReadMoreThanMaxValue) { 84 const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 85 0xff, 0xff, 0xff, 0xff, 0x2}; 86 const uint8_t* read_at = buffer; 87 ReadLeb128(read_at, std::end(buffer)); 88 EXPECT_EQ(read_at, nullptr); 89 } 90 91 TEST(Leb128Test, DoesntReadMoreThan10Bytes) { 92 // Though this array represent leb128 encoded value that can fit in uint64_t, 93 // ReadLeb128 function discards it to avoid reading too many bytes from the 94 // buffer. 95 const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 96 0xff, 0xff, 0xff, 0x80, 0x00}; 97 const uint8_t* read_at = buffer; 98 ReadLeb128(read_at, std::end(buffer)); 99 EXPECT_EQ(read_at, nullptr); 100 } 101 102 TEST(Leb128Test, WriteZero) { 103 uint8_t buffer[16]; 104 EXPECT_EQ(WriteLeb128(0, buffer), 1); 105 EXPECT_EQ(buffer[0], 0); 106 } 107 108 TEST(Leb128Test, WriteOneByteValue) { 109 uint8_t buffer[16]; 110 EXPECT_EQ(WriteLeb128(0b0010'1100, buffer), 1); 111 EXPECT_EQ(buffer[0], 0b0010'1100); 112 } 113 114 TEST(Leb128Test, WriteTwoByteValue) { 115 uint8_t buffer[16]; 116 EXPECT_EQ(WriteLeb128(0b11'1111'010'1100, buffer), 2); 117 EXPECT_EQ(buffer[0], 0b1010'1100); 118 EXPECT_EQ(buffer[1], 0b0011'1111); 119 } 120 121 TEST(Leb128Test, WriteNearlyMaxValue) { 122 uint8_t buffer[16]; 123 EXPECT_EQ(WriteLeb128(0x7fff'ffff'ffff'ffff, buffer), 9); 124 EXPECT_THAT( 125 MakeArrayView(buffer, 9), 126 ElementsAre(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f)); 127 } 128 129 TEST(Leb128Test, WriteMaxValue) { 130 uint8_t buffer[16]; 131 EXPECT_EQ(WriteLeb128(0xffff'ffff'ffff'ffff, buffer), 10); 132 EXPECT_THAT( 133 MakeArrayView(buffer, 10), 134 ElementsAre(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01)); 135 } 136 137 } // namespace 138 } // namespace webrtc