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