[Alignment][NFC] Migrate Instructions to Align
[llvm-core.git] / unittests / BinaryFormat / TestFileMagic.cpp
blob0919abe2a523fcd0a44ab41de87980da939348fb
1 //===- llvm/unittest/BinaryFormat/TestFileMagic.cpp - File magic 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/ADT/SmallString.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/BinaryFormat/Magic.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/Path.h"
15 #include "gtest/gtest.h"
17 using namespace llvm;
18 namespace fs = llvm::sys::fs;
20 #define ASSERT_NO_ERROR(x) \
21 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
22 SmallString<128> MessageStorage; \
23 raw_svector_ostream Message(MessageStorage); \
24 Message << #x ": did not return errc::success.\n" \
25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
27 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
28 } else { \
31 class MagicTest : public testing::Test {
32 protected:
33 /// Unique temporary directory in which all created filesystem entities must
34 /// be placed. It is removed at the end of each test (must be empty).
35 SmallString<128> TestDirectory;
37 void SetUp() override {
38 ASSERT_NO_ERROR(
39 fs::createUniqueDirectory("file-system-test", TestDirectory));
40 // We don't care about this specific file.
41 errs() << "Test Directory: " << TestDirectory << '\n';
42 errs().flush();
45 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
48 const char archive[] = "!<arch>\x0A";
49 const char bitcode[] = "\xde\xc0\x17\x0b";
50 const char coff_object[] = "\x00\x00......";
51 const char coff_bigobj[] =
52 "\x00\x00\xff\xff\x00\x02......"
53 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
54 const char coff_import_library[] = "\x00\x00\xff\xff....";
55 const char elf_relocatable[] = {0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 1};
57 const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
58 const char macho_object[] =
59 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
60 const char macho_executable[] =
61 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
62 const char macho_fixed_virtual_memory_shared_lib[] =
63 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
64 const char macho_core[] =
65 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
66 const char macho_preload_executable[] =
67 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
68 const char macho_dynamically_linked_shared_lib[] =
69 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
70 const char macho_dynamic_linker[] =
71 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
72 const char macho_bundle[] =
73 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
74 const char macho_dsym_companion[] =
75 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
76 const char macho_kext_bundle[] =
77 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
78 const char windows_resource[] =
79 "\x00\x00\x00\x00\x020\x00\x00\x00\xff\xff\x00\x00\xff\xff\x00\x00";
80 const char macho_dynamically_linked_shared_lib_stub[] =
81 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
82 const char ms_dos_stub_broken[] = "\x4d\x5a\x20\x20";
83 const char pdb[] = "Microsoft C/C++ MSF 7.00\r\n\x1a"
84 "DS\x00\x00\x00";
85 const char tapi_file[] = "--- !tapi-tbd-v1\n";
86 const char tapi_file_tbd_v1[] = "---\narchs: [";
88 TEST_F(MagicTest, Magic) {
89 struct type {
90 const char *filename;
91 const char *magic_str;
92 size_t magic_str_len;
93 file_magic magic;
94 } types[] = {
95 #define DEFINE(magic) {#magic, magic, sizeof(magic), file_magic::magic}
96 DEFINE(archive),
97 DEFINE(bitcode),
98 DEFINE(coff_object),
99 {"coff_bigobj", coff_bigobj, sizeof(coff_bigobj),
100 file_magic::coff_object},
101 DEFINE(coff_import_library),
102 DEFINE(elf_relocatable),
103 DEFINE(macho_universal_binary),
104 DEFINE(macho_object),
105 DEFINE(macho_executable),
106 DEFINE(macho_fixed_virtual_memory_shared_lib),
107 DEFINE(macho_core),
108 DEFINE(macho_preload_executable),
109 DEFINE(macho_dynamically_linked_shared_lib),
110 DEFINE(macho_dynamic_linker),
111 DEFINE(macho_bundle),
112 DEFINE(macho_dynamically_linked_shared_lib_stub),
113 DEFINE(macho_dsym_companion),
114 DEFINE(macho_kext_bundle),
115 DEFINE(windows_resource),
116 DEFINE(pdb),
117 {"ms_dos_stub_broken", ms_dos_stub_broken, sizeof(ms_dos_stub_broken),
118 file_magic::unknown},
119 DEFINE(tapi_file),
120 {"tapi_file_tbd_v1", tapi_file_tbd_v1, sizeof(tapi_file_tbd_v1),
121 file_magic::tapi_file},
122 #undef DEFINE
125 // Create some files filled with magic.
126 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
127 ++i) {
128 SmallString<128> file_pathname(TestDirectory);
129 llvm::sys::path::append(file_pathname, i->filename);
130 std::error_code EC;
131 raw_fd_ostream file(file_pathname, EC, sys::fs::OF_None);
132 ASSERT_FALSE(file.has_error());
133 StringRef magic(i->magic_str, i->magic_str_len);
134 file << magic;
135 file.close();
136 EXPECT_EQ(i->magic, identify_magic(magic));
137 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));