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