1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <stdlib.h>
30#include <string.h>
31#include <sys/mman.h>
32
33#include <gtest/gtest.h>
34
35#include "../linker_sleb128.h"
36
37TEST(linker_sleb128, smoke) {
38  std::vector<uint8_t> encoding;
39  // 624485
40  encoding.push_back(0xe5);
41  encoding.push_back(0x8e);
42  encoding.push_back(0x26);
43  // 0
44  encoding.push_back(0x00);
45  // 1
46  encoding.push_back(0x01);
47  // 63
48  encoding.push_back(0x3f);
49  // 64
50  encoding.push_back(0xc0);
51  encoding.push_back(0x00);
52  // -1
53  encoding.push_back(0x7f);
54  // -624485
55  encoding.push_back(0x9b);
56  encoding.push_back(0xf1);
57  encoding.push_back(0x59);
58  // 2147483647
59  encoding.push_back(0xff);
60  encoding.push_back(0xff);
61  encoding.push_back(0xff);
62  encoding.push_back(0xff);
63  encoding.push_back(0x07);
64  // -2147483648
65  encoding.push_back(0x80);
66  encoding.push_back(0x80);
67  encoding.push_back(0x80);
68  encoding.push_back(0x80);
69  encoding.push_back(0x78);
70#if defined(__LP64__)
71  // 9223372036854775807
72  encoding.push_back(0xff);
73  encoding.push_back(0xff);
74  encoding.push_back(0xff);
75  encoding.push_back(0xff);
76  encoding.push_back(0xff);
77  encoding.push_back(0xff);
78  encoding.push_back(0xff);
79  encoding.push_back(0xff);
80  encoding.push_back(0xff);
81  encoding.push_back(0x00);
82  // -9223372036854775808
83  encoding.push_back(0x80);
84  encoding.push_back(0x80);
85  encoding.push_back(0x80);
86  encoding.push_back(0x80);
87  encoding.push_back(0x80);
88  encoding.push_back(0x80);
89  encoding.push_back(0x80);
90  encoding.push_back(0x80);
91  encoding.push_back(0x80);
92  encoding.push_back(0x7f);
93#endif
94  sleb128_decoder decoder(&encoding[0], encoding.size());
95
96  EXPECT_EQ(624485U, decoder.pop_front());
97
98  EXPECT_EQ(0U, decoder.pop_front());
99  EXPECT_EQ(1U, decoder.pop_front());
100  EXPECT_EQ(63U, decoder.pop_front());
101  EXPECT_EQ(64U, decoder.pop_front());
102  EXPECT_EQ(static_cast<size_t>(-1), decoder.pop_front());
103  EXPECT_EQ(static_cast<size_t>(-624485), decoder.pop_front());
104  EXPECT_EQ(2147483647U, decoder.pop_front());
105  EXPECT_EQ(static_cast<size_t>(-2147483648), decoder.pop_front());
106#if defined(__LP64__)
107  EXPECT_EQ(9223372036854775807ULL, decoder.pop_front());
108  EXPECT_EQ(static_cast<uint64_t>(-9223372036854775807LL - 1), decoder.pop_front());
109#endif
110}
111