File: leb128_unittest.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,772 kB
  • sloc: cpp: 35,093,800; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,798; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (138 lines) | stat: -rw-r--r-- 4,502 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *  Copyright (c) 2023 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/rtp_rtcp/source/leb128.h"

#include <cstdint>
#include <iterator>
#include <limits>

#include "api/array_view.h"
#include "test/gmock.h"
#include "test/gtest.h"

namespace webrtc {
namespace {

using ::testing::ElementsAre;

TEST(Leb128Test, Size) {
  EXPECT_EQ(Leb128Size(0), 1);
  EXPECT_EQ(Leb128Size(0b0111'1111), 1);
  EXPECT_EQ(Leb128Size(0b1000'0000), 2);
  EXPECT_EQ(Leb128Size(std::numeric_limits<uint64_t>::max()), 10);
}

TEST(Leb128Test, ReadZero) {
  const uint8_t one_byte[] = {0};
  const uint8_t* read_at = one_byte;
  EXPECT_EQ(ReadLeb128(read_at, std::end(one_byte)), uint64_t{0});
  EXPECT_EQ(std::distance(read_at, std::end(one_byte)), 0);
}

TEST(Leb128Test, ReadOneByte) {
  const uint8_t buffer[] = {0b0010'1100};
  const uint8_t* read_at = buffer;
  EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)), uint64_t{0b0010'1100});
  EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0);
}

TEST(Leb128Test, ReadTwoByte) {
  const uint8_t buffer[] = {0b1010'1100, 0b0111'0000};
  const uint8_t* read_at = buffer;
  EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)),
            uint64_t{0b111'0000'010'1100});
  EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0);
}

TEST(Leb128Test, ReadNearlyMaxValue1) {
  const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff,
                            0xff, 0xff, 0xff, 0x7f};
  const uint8_t* read_at = buffer;
  EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)),
            uint64_t{0x7fff'ffff'ffff'ffff});
  EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0);
}

TEST(Leb128Test, ReadNearlyMaxValue2) {
  // This is valid, though not optimal way to store 63 bits of the value.
  const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff,
                            0xff, 0xff, 0xff, 0xff, 0x0};
  const uint8_t* read_at = buffer;
  EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)),
            uint64_t{0x7fff'ffff'ffff'ffff});
  EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0);
}

TEST(Leb128Test, ReadMaxValue) {
  // This is valid, though not optimal way to store 63 bits of the value.
  const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff,
                            0xff, 0xff, 0xff, 0xff, 0x1};
  const uint8_t* read_at = buffer;
  EXPECT_EQ(ReadLeb128(read_at, std::end(buffer)), 0xffff'ffff'ffff'ffff);
  EXPECT_EQ(std::distance(read_at, std::end(buffer)), 0);
}

TEST(Leb128Test, FailsToReadMoreThanMaxValue) {
  const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff,
                            0xff, 0xff, 0xff, 0xff, 0x2};
  const uint8_t* read_at = buffer;
  ReadLeb128(read_at, std::end(buffer));
  EXPECT_EQ(read_at, nullptr);
}

TEST(Leb128Test, DoesntReadMoreThan10Bytes) {
  // Though this array represent leb128 encoded value that can fit in uint64_t,
  // ReadLeb128 function discards it to avoid reading too many bytes from the
  // buffer.
  const uint8_t buffer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                            0xff, 0xff, 0xff, 0x80, 0x00};
  const uint8_t* read_at = buffer;
  ReadLeb128(read_at, std::end(buffer));
  EXPECT_EQ(read_at, nullptr);
}

TEST(Leb128Test, WriteZero) {
  uint8_t buffer[16];
  EXPECT_EQ(WriteLeb128(0, buffer), 1);
  EXPECT_EQ(buffer[0], 0);
}

TEST(Leb128Test, WriteOneByteValue) {
  uint8_t buffer[16];
  EXPECT_EQ(WriteLeb128(0b0010'1100, buffer), 1);
  EXPECT_EQ(buffer[0], 0b0010'1100);
}

TEST(Leb128Test, WriteTwoByteValue) {
  uint8_t buffer[16];
  EXPECT_EQ(WriteLeb128(0b11'1111'010'1100, buffer), 2);
  EXPECT_EQ(buffer[0], 0b1010'1100);
  EXPECT_EQ(buffer[1], 0b0011'1111);
}

TEST(Leb128Test, WriteNearlyMaxValue) {
  uint8_t buffer[16];
  EXPECT_EQ(WriteLeb128(0x7fff'ffff'ffff'ffff, buffer), 9);
  EXPECT_THAT(
      MakeArrayView(buffer, 9),
      ElementsAre(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f));
}

TEST(Leb128Test, WriteMaxValue) {
  uint8_t buffer[16];
  EXPECT_EQ(WriteLeb128(0xffff'ffff'ffff'ffff, buffer), 10);
  EXPECT_THAT(
      MakeArrayView(buffer, 10),
      ElementsAre(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01));
}

}  // namespace
}  // namespace webrtc