forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathTestFileMagic.cpp
142 lines (131 loc) · 5.6 KB
/
TestFileMagic.cpp
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
139
140
141
142
//===- llvm/unittest/BinaryFormat/TestFileMagic.cpp - File magic tests ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include"llvm/ADT/SmallString.h"
#include"llvm/ADT/StringRef.h"
#include"llvm/BinaryFormat/Magic.h"
#include"llvm/Support/FileSystem.h"
#include"llvm/Support/Path.h"
#include"gtest/gtest.h"
usingnamespacellvm;
namespacefs= llvm::sys::fs;
#defineASSERT_NO_ERROR(x) \
if (std::error_code ASSERT_NO_ERROR_ec = x) { \
SmallString<128> MessageStorage; \
raw_svector_ostream Message(MessageStorage); \
Message << #x ": did not return errc::success.\n" \
<< "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
<< "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
} else { \
}
classMagicTest : publictesting::Test {
protected:
/// Unique temporary directory in which all created filesystem entities must
/// be placed. It is removed at the end of each test (must be empty).
SmallString<128> TestDirectory;
voidSetUp() override {
ASSERT_NO_ERROR(
fs::createUniqueDirectory("file-system-test", TestDirectory));
// We don't care about this specific file.
errs() << "Test Directory: " << TestDirectory << '\n';
errs().flush();
}
voidTearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
};
constchar archive[] = "!<arch>\x0A";
constchar bitcode[] = "\xde\xc0\x17\x0b";
constchar coff_object[] = "\x00\x00......";
constchar coff_bigobj[] =
"\x00\x00\xff\xff\x00\x02......"
"\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
constchar coff_import_library[] = "\x00\x00\xff\xff....";
constchar elf_relocatable[] = {0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1};
constchar goff_object[] = "\x03\xF0\x00";
constchar macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
constchar macho_object[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
constchar macho_executable[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
constchar macho_fixed_virtual_memory_shared_lib[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
constchar macho_core[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
constchar macho_preload_executable[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
constchar macho_dynamically_linked_shared_lib[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
constchar macho_dynamic_linker[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
constchar macho_bundle[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
constchar macho_dsym_companion[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
constchar macho_kext_bundle[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
constchar windows_resource[] =
"\x00\x00\x00\x00\x020\x00\x00\x00\xff\xff\x00\x00\xff\xff\x00\x00";
constchar macho_dynamically_linked_shared_lib_stub[] =
"\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
constchar ms_dos_stub_broken[] = "\x4d\x5a\x20\x20";
constchar pdb[] = "Microsoft C/C++ MSF 7.00\r\n\x1a"
"DS\x00\x00\x00";
constchar tapi_file[] = "--- !tapi-tbd-v1\n";
constchar tapi_file_tbd_v1[] = "---\narchs: [";
TEST_F(MagicTest, Magic) {
structtype {
constchar *filename;
constchar *magic_str;
size_t magic_str_len;
file_magic magic;
} types[] = {
#defineDEFINE(magic) {#magic, magic, sizeof(magic), file_magic::magic}
DEFINE(archive),
DEFINE(bitcode),
DEFINE(coff_object),
{"coff_bigobj", coff_bigobj, sizeof(coff_bigobj),
file_magic::coff_object},
DEFINE(coff_import_library),
DEFINE(elf_relocatable),
DEFINE(goff_object),
DEFINE(macho_universal_binary),
DEFINE(macho_object),
DEFINE(macho_executable),
DEFINE(macho_fixed_virtual_memory_shared_lib),
DEFINE(macho_core),
DEFINE(macho_preload_executable),
DEFINE(macho_dynamically_linked_shared_lib),
DEFINE(macho_dynamic_linker),
DEFINE(macho_bundle),
DEFINE(macho_dynamically_linked_shared_lib_stub),
DEFINE(macho_dsym_companion),
DEFINE(macho_kext_bundle),
DEFINE(windows_resource),
DEFINE(pdb),
{"ms_dos_stub_broken", ms_dos_stub_broken, sizeof(ms_dos_stub_broken),
file_magic::unknown},
DEFINE(tapi_file),
{"tapi_file_tbd_v1", tapi_file_tbd_v1, sizeof(tapi_file_tbd_v1),
file_magic::tapi_file},
#undef DEFINE
};
// Create some files filled with magic.
for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
++i) {
SmallString<128> file_pathname(TestDirectory);
llvm::sys::path::append(file_pathname, i->filename);
std::error_code EC;
raw_fd_ostream file(file_pathname, EC, sys::fs::OF_None);
ASSERT_FALSE(file.has_error());
StringRef magic(i->magic_str, i->magic_str_len);
file << magic;
file.close();
EXPECT_EQ(i->magic, identify_magic(magic));
ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
}
}