1 //===- llvm/unittest/Support/DataExtractorTest.cpp - DataExtractor tests --===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/DataExtractor.h"
10 #include "gtest/gtest.h"
15 const char numberData
[] = "\x80\x90\xFF\xFF\x80\x00\x00\x00";
16 const char stringData
[] = "hellohello\0hello";
17 const char leb128data
[] = "\xA6\x49";
18 const char bigleb128data
[] = "\xAA\xA9\xFF\xAA\xFF\xAA\xFF\x4A";
20 TEST(DataExtractorTest
, OffsetOverflow
) {
21 DataExtractor
DE(StringRef(numberData
, sizeof(numberData
)-1), false, 8);
22 EXPECT_FALSE(DE
.isValidOffsetForDataOfSize(-2U, 5));
25 TEST(DataExtractorTest
, UnsignedNumbers
) {
26 DataExtractor
DE(StringRef(numberData
, sizeof(numberData
)-1), false, 8);
29 EXPECT_EQ(0x80U
, DE
.getU8(&offset
));
30 EXPECT_EQ(1U, offset
);
32 EXPECT_EQ(0x8090U
, DE
.getU16(&offset
));
33 EXPECT_EQ(2U, offset
);
35 EXPECT_EQ(0x8090FFFFU
, DE
.getU32(&offset
));
36 EXPECT_EQ(4U, offset
);
38 EXPECT_EQ(0x8090FFFF80000000ULL
, DE
.getU64(&offset
));
39 EXPECT_EQ(8U, offset
);
41 EXPECT_EQ(0x8090FFFF80000000ULL
, DE
.getAddress(&offset
));
42 EXPECT_EQ(8U, offset
);
46 EXPECT_EQ(data
, DE
.getU32(&offset
, data
, 2));
47 EXPECT_EQ(0x8090FFFFU
, data
[0]);
48 EXPECT_EQ(0x80000000U
, data
[1]);
49 EXPECT_EQ(8U, offset
);
52 // Now for little endian.
53 DE
= DataExtractor(StringRef(numberData
, sizeof(numberData
)-1), true, 4);
54 EXPECT_EQ(0x9080U
, DE
.getU16(&offset
));
55 EXPECT_EQ(2U, offset
);
57 EXPECT_EQ(0xFFFF9080U
, DE
.getU32(&offset
));
58 EXPECT_EQ(4U, offset
);
60 EXPECT_EQ(0x80FFFF9080ULL
, DE
.getU64(&offset
));
61 EXPECT_EQ(8U, offset
);
63 EXPECT_EQ(0xFFFF9080U
, DE
.getAddress(&offset
));
64 EXPECT_EQ(4U, offset
);
67 EXPECT_EQ(data
, DE
.getU32(&offset
, data
, 2));
68 EXPECT_EQ(0xFFFF9080U
, data
[0]);
69 EXPECT_EQ(0x80U
, data
[1]);
70 EXPECT_EQ(8U, offset
);
73 TEST(DataExtractorTest
, SignedNumbers
) {
74 DataExtractor
DE(StringRef(numberData
, sizeof(numberData
)-1), false, 8);
77 EXPECT_EQ(-128, DE
.getSigned(&offset
, 1));
78 EXPECT_EQ(1U, offset
);
80 EXPECT_EQ(-32624, DE
.getSigned(&offset
, 2));
81 EXPECT_EQ(2U, offset
);
83 EXPECT_EQ(-2137980929, DE
.getSigned(&offset
, 4));
84 EXPECT_EQ(4U, offset
);
86 EXPECT_EQ(-9182558167379214336LL, DE
.getSigned(&offset
, 8));
87 EXPECT_EQ(8U, offset
);
90 TEST(DataExtractorTest
, Strings
) {
91 DataExtractor
DE(StringRef(stringData
, sizeof(stringData
)-1), false, 8);
94 EXPECT_EQ(stringData
, DE
.getCStr(&offset
));
95 EXPECT_EQ(11U, offset
);
96 EXPECT_EQ(nullptr, DE
.getCStr(&offset
));
97 EXPECT_EQ(11U, offset
);
100 TEST(DataExtractorTest
, LEB128
) {
101 DataExtractor
DE(StringRef(leb128data
, sizeof(leb128data
)-1), false, 8);
104 EXPECT_EQ(9382ULL, DE
.getULEB128(&offset
));
105 EXPECT_EQ(2U, offset
);
107 EXPECT_EQ(-7002LL, DE
.getSLEB128(&offset
));
108 EXPECT_EQ(2U, offset
);
110 DataExtractor
BDE(StringRef(bigleb128data
, sizeof(bigleb128data
)-1), false,8);
112 EXPECT_EQ(42218325750568106ULL, BDE
.getULEB128(&offset
));
113 EXPECT_EQ(8U, offset
);
115 EXPECT_EQ(-29839268287359830LL, BDE
.getSLEB128(&offset
));
116 EXPECT_EQ(8U, offset
);
119 TEST(DataExtractorTest
, LEB128_error
) {
120 DataExtractor
DE(StringRef("\x81"), false, 8);
122 EXPECT_EQ(0U, DE
.getULEB128(&Offset
));
123 EXPECT_EQ(0U, Offset
);
126 EXPECT_EQ(0U, DE
.getSLEB128(&Offset
));
127 EXPECT_EQ(0U, Offset
);