[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / Support / DataExtractorTest.cpp
blob358751cbb129294a17a31a9dc6cc4f5f71835bf4
1 //===- llvm/unittest/Support/DataExtractorTest.cpp - DataExtractor tests --===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/DataExtractor.h"
10 #include "llvm/Testing/Support/Error.h"
11 #include "gtest/gtest.h"
12 using namespace llvm;
14 namespace {
16 const char numberData[] = "\x80\x90\xFF\xFF\x80\x00\x00\x00";
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);
27 uint64_t offset = 0;
29 EXPECT_EQ(0x80U, DE.getU8(&offset));
30 EXPECT_EQ(1U, offset);
31 offset = 0;
32 EXPECT_EQ(0x8090U, DE.getU16(&offset));
33 EXPECT_EQ(2U, offset);
34 offset = 0;
35 EXPECT_EQ(0x8090FFFFU, DE.getU32(&offset));
36 EXPECT_EQ(4U, offset);
37 offset = 0;
38 EXPECT_EQ(0x8090FFFF80000000ULL, DE.getU64(&offset));
39 EXPECT_EQ(8U, offset);
40 offset = 0;
41 EXPECT_EQ(0x8090FFFF80000000ULL, DE.getAddress(&offset));
42 EXPECT_EQ(8U, offset);
43 offset = 0;
45 uint32_t data[2];
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);
50 offset = 0;
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);
56 offset = 0;
57 EXPECT_EQ(0xFFFF9080U, DE.getU32(&offset));
58 EXPECT_EQ(4U, offset);
59 offset = 0;
60 EXPECT_EQ(0x80FFFF9080ULL, DE.getU64(&offset));
61 EXPECT_EQ(8U, offset);
62 offset = 0;
63 EXPECT_EQ(0xFFFF9080U, DE.getAddress(&offset));
64 EXPECT_EQ(4U, offset);
65 offset = 0;
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);
75 uint64_t offset = 0;
77 EXPECT_EQ(-128, DE.getSigned(&offset, 1));
78 EXPECT_EQ(1U, offset);
79 offset = 0;
80 EXPECT_EQ(-32624, DE.getSigned(&offset, 2));
81 EXPECT_EQ(2U, offset);
82 offset = 0;
83 EXPECT_EQ(-2137980929, DE.getSigned(&offset, 4));
84 EXPECT_EQ(4U, offset);
85 offset = 0;
86 EXPECT_EQ(-9182558167379214336LL, DE.getSigned(&offset, 8));
87 EXPECT_EQ(8U, offset);
90 TEST(DataExtractorTest, Strings) {
91 const char stringData[] = "hellohello\0hello";
92 DataExtractor DE(StringRef(stringData, sizeof(stringData)-1), false, 8);
93 uint64_t offset = 0;
95 EXPECT_EQ(stringData, DE.getCStr(&offset));
96 EXPECT_EQ(11U, offset);
97 EXPECT_EQ(nullptr, DE.getCStr(&offset));
98 EXPECT_EQ(11U, offset);
100 DataExtractor::Cursor C(0);
101 EXPECT_EQ(stringData, DE.getCStr(C));
102 EXPECT_EQ(11U, C.tell());
103 EXPECT_EQ(nullptr, DE.getCStr(C));
104 EXPECT_EQ(11U, C.tell());
105 EXPECT_THAT_ERROR(
106 C.takeError(),
107 FailedWithMessage("no null terminated string at offset 0xb"));
110 TEST(DataExtractorTest, LEB128) {
111 DataExtractor DE(StringRef(leb128data, sizeof(leb128data)-1), false, 8);
112 uint64_t offset = 0;
114 EXPECT_EQ(9382ULL, DE.getULEB128(&offset));
115 EXPECT_EQ(2U, offset);
116 offset = 0;
117 EXPECT_EQ(-7002LL, DE.getSLEB128(&offset));
118 EXPECT_EQ(2U, offset);
120 DataExtractor BDE(StringRef(bigleb128data, sizeof(bigleb128data)-1), false,8);
121 offset = 0;
122 EXPECT_EQ(42218325750568106ULL, BDE.getULEB128(&offset));
123 EXPECT_EQ(8U, offset);
124 offset = 0;
125 EXPECT_EQ(-29839268287359830LL, BDE.getSLEB128(&offset));
126 EXPECT_EQ(8U, offset);
129 TEST(DataExtractorTest, LEB128_error) {
130 DataExtractor DE(StringRef("\x81"), false, 8);
131 uint64_t Offset = 0;
132 EXPECT_EQ(0U, DE.getULEB128(&Offset));
133 EXPECT_EQ(0U, Offset);
135 Offset = 0;
136 EXPECT_EQ(0U, DE.getSLEB128(&Offset));
137 EXPECT_EQ(0U, Offset);
139 DataExtractor::Cursor C(0);
140 EXPECT_EQ(0U, DE.getULEB128(C));
141 EXPECT_THAT_ERROR(
142 C.takeError(),
143 FailedWithMessage("unable to decode LEB128 at offset 0x00000000: "
144 "malformed uleb128, extends past end"));
146 C = DataExtractor::Cursor(0);
147 EXPECT_EQ(0U, DE.getSLEB128(C));
148 EXPECT_THAT_ERROR(
149 C.takeError(),
150 FailedWithMessage("unable to decode LEB128 at offset 0x00000000: "
151 "malformed sleb128, extends past end"));
153 // Show non-zero offsets are reported appropriately.
154 C = DataExtractor::Cursor(1);
155 EXPECT_EQ(0U, DE.getULEB128(C));
156 EXPECT_THAT_ERROR(
157 C.takeError(),
158 FailedWithMessage("unable to decode LEB128 at offset 0x00000001: "
159 "malformed uleb128, extends past end"));
162 TEST(DataExtractorTest, Cursor_tell) {
163 DataExtractor DE(StringRef("AB"), false, 8);
164 DataExtractor::Cursor C(0);
165 // A successful read operation advances the cursor
166 EXPECT_EQ('A', DE.getU8(C));
167 EXPECT_EQ(1u, C.tell());
169 // An unsuccessful one doesn't.
170 EXPECT_EQ(0u, DE.getU16(C));
171 EXPECT_EQ(1u, C.tell());
173 // And neither do any subsequent operations.
174 EXPECT_EQ(0, DE.getU8(C));
175 EXPECT_EQ(1u, C.tell());
177 consumeError(C.takeError());
180 TEST(DataExtractorTest, Cursor_seek) {
181 DataExtractor::Cursor C(5);
183 C.seek(3);
184 EXPECT_EQ(3u, C.tell());
186 C.seek(8);
187 EXPECT_EQ(8u, C.tell());
189 EXPECT_THAT_ERROR(C.takeError(), Succeeded());
192 TEST(DataExtractorTest, Cursor_takeError) {
193 DataExtractor DE(StringRef("AB"), false, 8);
194 DataExtractor::Cursor C(0);
195 // Initially, the cursor is in the "success" state.
196 EXPECT_THAT_ERROR(C.takeError(), Succeeded());
198 // It remains "success" after a successful read.
199 EXPECT_EQ('A', DE.getU8(C));
200 EXPECT_THAT_ERROR(C.takeError(), Succeeded());
202 // An unsuccessful read sets the error state.
203 EXPECT_EQ(0u, DE.getU32(C));
204 EXPECT_THAT_ERROR(C.takeError(), Failed());
206 // Once set the error sticks until explicitly cleared.
207 EXPECT_EQ(0u, DE.getU32(C));
208 EXPECT_EQ(0, DE.getU8(C));
209 EXPECT_THAT_ERROR(C.takeError(), Failed());
211 // At which point reads can be succeed again.
212 EXPECT_EQ('B', DE.getU8(C));
213 EXPECT_THAT_ERROR(C.takeError(), Succeeded());
216 TEST(DataExtractorTest, Cursor_chaining) {
217 DataExtractor DE(StringRef("ABCD"), false, 8);
218 DataExtractor::Cursor C(0);
220 // Multiple reads can be chained without trigerring any assertions.
221 EXPECT_EQ('A', DE.getU8(C));
222 EXPECT_EQ('B', DE.getU8(C));
223 EXPECT_EQ('C', DE.getU8(C));
224 EXPECT_EQ('D', DE.getU8(C));
225 // And the error checked at the end.
226 EXPECT_THAT_ERROR(C.takeError(), Succeeded());
229 #if defined(GTEST_HAS_DEATH_TEST) && defined(_DEBUG) && \
230 LLVM_ENABLE_ABI_BREAKING_CHECKS
231 TEST(DataExtractorDeathTest, Cursor) {
232 DataExtractor DE(StringRef("AB"), false, 8);
234 // Even an unused cursor must be checked for errors:
235 EXPECT_DEATH(DataExtractor::Cursor(0),
236 "Success values must still be checked prior to being destroyed");
239 auto C = std::make_unique<DataExtractor::Cursor>(0);
240 EXPECT_EQ(0u, DE.getU32(*C));
241 // It must also be checked after an unsuccessful operation.
242 // destruction.
243 EXPECT_DEATH(C.reset(), "unexpected end of data");
244 EXPECT_THAT_ERROR(C->takeError(), Failed());
247 auto C = std::make_unique<DataExtractor::Cursor>(0);
248 EXPECT_EQ('A', DE.getU8(*C));
249 // Same goes for a successful one.
250 EXPECT_DEATH(
251 C.reset(),
252 "Success values must still be checked prior to being destroyed");
253 EXPECT_THAT_ERROR(C->takeError(), Succeeded());
256 auto C = std::make_unique<DataExtractor::Cursor>(0);
257 EXPECT_EQ('A', DE.getU8(*C));
258 EXPECT_EQ(0u, DE.getU32(*C));
259 // Even if a successful operation is followed by an unsuccessful one.
260 EXPECT_DEATH(C.reset(), "unexpected end of data");
261 EXPECT_THAT_ERROR(C->takeError(), Failed());
264 auto C = std::make_unique<DataExtractor::Cursor>(0);
265 EXPECT_EQ(0u, DE.getU32(*C));
266 EXPECT_EQ(0, DE.getU8(*C));
267 // Even if an unsuccessful operation is followed by one that would normally
268 // succeed.
269 EXPECT_DEATH(C.reset(), "unexpected end of data");
270 EXPECT_THAT_ERROR(C->takeError(), Failed());
273 #endif
275 TEST(DataExtractorTest, getU8_vector) {
276 DataExtractor DE(StringRef("AB"), false, 8);
277 DataExtractor::Cursor C(0);
278 SmallVector<uint8_t, 2> S;
280 DE.getU8(C, S, 4);
281 EXPECT_THAT_ERROR(C.takeError(), Failed());
282 EXPECT_EQ("", toStringRef(S));
284 DE.getU8(C, S, 2);
285 EXPECT_THAT_ERROR(C.takeError(), Succeeded());
286 EXPECT_EQ("AB", toStringRef(S));
288 C = DataExtractor::Cursor(0x47);
289 DE.getU8(C, S, 2);
290 EXPECT_THAT_ERROR(
291 C.takeError(),
292 FailedWithMessage("offset 0x47 is beyond the end of data at 0x2"));
295 TEST(DataExtractorTest, getU24) {
296 DataExtractor DE(StringRef("ABCD"), false, 8);
297 DataExtractor::Cursor C(0);
299 EXPECT_EQ(0x414243u, DE.getU24(C));
300 EXPECT_EQ(0u, DE.getU24(C));
301 EXPECT_EQ(3u, C.tell());
302 EXPECT_THAT_ERROR(C.takeError(), Failed());
305 TEST(DataExtractorTest, skip) {
306 DataExtractor DE(StringRef("AB"), false, 8);
307 DataExtractor::Cursor C(0);
309 DE.skip(C, 4);
310 EXPECT_THAT_ERROR(C.takeError(), Failed());
311 EXPECT_EQ(0u, C.tell());
313 DE.skip(C, 2);
314 EXPECT_THAT_ERROR(C.takeError(), Succeeded());
315 EXPECT_EQ(2u, C.tell());
318 TEST(DataExtractorTest, eof) {
319 DataExtractor DE(StringRef("A"), false, 8);
320 DataExtractor::Cursor C(0);
322 EXPECT_FALSE(DE.eof(C));
324 EXPECT_EQ(0, DE.getU16(C));
325 EXPECT_FALSE(DE.eof(C));
326 EXPECT_THAT_ERROR(C.takeError(), Failed());
328 EXPECT_EQ('A', DE.getU8(C));
329 EXPECT_TRUE(DE.eof(C));
330 EXPECT_THAT_ERROR(C.takeError(), Succeeded());
333 TEST(DataExtractorTest, size) {
334 uint8_t Data[] = {'A', 'B', 'C', 'D'};
335 DataExtractor DE1(StringRef(reinterpret_cast<char *>(Data), sizeof(Data)),
336 false, 8);
337 EXPECT_EQ(DE1.size(), sizeof(Data));
338 DataExtractor DE2(ArrayRef<uint8_t>(Data), false, 8);
339 EXPECT_EQ(DE2.size(), sizeof(Data));
342 TEST(DataExtractorTest, FixedLengthString) {
343 const char Data[] = "hello\x00\x00\x00world \thola\x00";
344 DataExtractor DE(StringRef(Data, sizeof(Data)-1), false, 8);
345 uint64_t Offset = 0;
346 StringRef Str;
347 // Test extracting too many bytes doesn't modify Offset and returns None.
348 Str = DE.getFixedLengthString(&Offset, sizeof(Data));
349 EXPECT_TRUE(Str.empty());
350 EXPECT_EQ(Offset, 0u);
352 // Test extracting a fixed width C string with trailing NULL characters.
353 Str = DE.getFixedLengthString(&Offset, 8);
354 EXPECT_EQ(Offset, 8u);
355 EXPECT_EQ(Str.size(), 5u);
356 EXPECT_EQ(Str, "hello");
357 // Test extracting a fixed width C string with trailing space and tab
358 // characters.
359 Str = DE.getFixedLengthString(&Offset, 8, " \t");
360 EXPECT_EQ(Offset, 16u);
361 EXPECT_EQ(Str.size(), 5u);
362 EXPECT_EQ(Str, "world");
363 // Now extract a normal C string.
364 Str = DE.getCStrRef(&Offset);
365 EXPECT_EQ(Str.size(), 4u);
366 EXPECT_EQ(Str, "hola");
370 TEST(DataExtractorTest, GetBytes) {
371 // Use data with an embedded NULL character for good measure.
372 const char Data[] = "\x01\x02\x00\x04";
373 StringRef Bytes(Data, sizeof(Data)-1);
374 DataExtractor DE(Bytes, false, 8);
375 uint64_t Offset = 0;
376 StringRef Str;
377 // Test extracting too many bytes doesn't modify Offset and returns None.
378 Str = DE.getBytes(&Offset, sizeof(Data));
379 EXPECT_TRUE(Str.empty());
380 EXPECT_EQ(Offset, 0u);
381 // Test extracting 4 bytes from the stream.
382 Str = DE.getBytes(&Offset, 4);
383 EXPECT_EQ(Offset, 4u);
384 EXPECT_EQ(Str.size(), 4u);
385 EXPECT_EQ(Str, Bytes);
387 DataExtractor::Cursor C(0);
388 EXPECT_EQ(StringRef("\x01\x02"), DE.getBytes(C, 2));
389 EXPECT_EQ(StringRef("\x00\x04", 2), DE.getBytes(C, 2));
390 EXPECT_EQ(StringRef(), DE.getBytes(C, 2));
391 EXPECT_EQ(StringRef(), DE.getBytes(C, 2));
392 EXPECT_EQ(4u, C.tell());
393 EXPECT_THAT_ERROR(C.takeError(), Failed());