1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "gtest/gtest.h"
18
19 #include <inttypes.h>
20 #include <limits.h>
21
22 #include <vector>
23
24 #include "leb128.h"
25
26 namespace nogrod {
27
TEST(sleb128,smoke)28 TEST(sleb128, smoke) {
29 uint8_t array_0[] = {0x00};
30 uint8_t array_neg1[] = {0x7f};
31 uint8_t array_63[] = {0x3f};
32 uint8_t array_64[] = {0xc0, 0x00};
33 uint8_t array_neg64[] = {0xc0, 0x7f};
34 uint8_t array_9223372036854775807[] = {
35 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
36 uint8_t array_neg9223372036854775808[] = {
37 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f};
38
39 int64_t result = 42;
40
41 ASSERT_EQ(1U, DecodeSleb128(array_0, sizeof(array_0), &result));
42 ASSERT_EQ(0, result);
43 ASSERT_EQ(1U, DecodeSleb128(array_neg1, sizeof(array_neg1), &result));
44 ASSERT_EQ(-1, result);
45 ASSERT_EQ(1U, DecodeSleb128(array_63, sizeof(array_63), &result));
46 ASSERT_EQ(63, result);
47 ASSERT_EQ(2U, DecodeSleb128(array_64, sizeof(array_64), &result));
48 ASSERT_EQ(64, result);
49 ASSERT_EQ(2U, DecodeSleb128(array_neg64, sizeof(array_neg64), &result));
50 ASSERT_EQ(-64, result);
51 ASSERT_EQ(10U,
52 DecodeSleb128(array_9223372036854775807, sizeof(array_9223372036854775807), &result));
53 ASSERT_EQ(9223372036854775807LL, result);
54 ASSERT_EQ(
55 10U,
56 DecodeSleb128(array_neg9223372036854775808, sizeof(array_neg9223372036854775808), &result));
57 ASSERT_EQ(-9223372036854775807LL - 1, result);
58 }
59
TEST(sleb128,overflow)60 TEST(sleb128, overflow) {
61 uint8_t array_overflow[] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x0a};
62 int64_t result = 42;
63 EXPECT_DEATH(DecodeSleb128(array_overflow, sizeof(array_overflow), &result), "");
64 }
65
TEST(sleb128,out_of_bounds)66 TEST(sleb128, out_of_bounds) {
67 uint8_t array_out_of_bounds[] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88};
68 int64_t result = 42;
69 EXPECT_DEATH(DecodeSleb128(array_out_of_bounds, sizeof(array_out_of_bounds), &result), "");
70 }
71
TEST(leb128,smoke)72 TEST(leb128, smoke) {
73 uint8_t array_0[] = {0x00};
74 uint8_t array_63[] = {0x3f};
75 uint8_t array_64[] = {0xc0, 0x00};
76 uint8_t array_UINT64_MAX1[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01};
77 uint8_t array_UINT64_MAX2[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x05};
78
79 uint64_t result = 42;
80
81 ASSERT_EQ(1U, DecodeLeb128(array_0, sizeof(array_0), &result));
82 ASSERT_EQ(0U, result);
83 ASSERT_EQ(1U, DecodeLeb128(array_63, sizeof(array_63), &result));
84 ASSERT_EQ(63U, result);
85 ASSERT_EQ(2U, DecodeLeb128(array_64, sizeof(array_64), &result));
86 ASSERT_EQ(64U, result);
87 ASSERT_EQ(10U, DecodeLeb128(array_UINT64_MAX1, sizeof(array_UINT64_MAX1), &result));
88 ASSERT_EQ(UINT64_MAX, result);
89 ASSERT_EQ(10U, DecodeLeb128(array_UINT64_MAX2, sizeof(array_UINT64_MAX2), &result));
90 ASSERT_EQ(UINT64_MAX, result);
91 }
92
TEST(leb128,overflow)93 TEST(leb128, overflow) {
94 uint8_t array_overflow[] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x0a};
95 uint64_t result = 42;
96 EXPECT_DEATH(DecodeLeb128(array_overflow, sizeof(array_overflow), &result), "");
97 }
98
TEST(leb128,out_of_bounds)99 TEST(leb128, out_of_bounds) {
100 uint8_t array_out_of_bounds[] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88};
101 uint64_t result = 42;
102 EXPECT_DEATH(DecodeLeb128(array_out_of_bounds, sizeof(array_out_of_bounds), &result), "");
103 }
104
105 } // namespace nogrod
106