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) \ 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 { \ 29 } 30 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; 36 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(); 43 } 44 45 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); } 46 }; 47 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 86 TEST_F(MagicTest, Magic) { 87 struct type { 88 const char *filename; 89 const char *magic_str; 90 size_t magic_str_len; 91 file_magic magic; 92 } types[] = { 93 #define DEFINE(magic) {#magic, magic, sizeof(magic), file_magic::magic} 94 DEFINE(archive), 95 DEFINE(bitcode), 96 DEFINE(coff_object), 97 {"coff_bigobj", coff_bigobj, sizeof(coff_bigobj), 98 file_magic::coff_object}, 99 DEFINE(coff_import_library), 100 DEFINE(elf_relocatable), 101 DEFINE(macho_universal_binary), 102 DEFINE(macho_object), 103 DEFINE(macho_executable), 104 DEFINE(macho_fixed_virtual_memory_shared_lib), 105 DEFINE(macho_core), 106 DEFINE(macho_preload_executable), 107 DEFINE(macho_dynamically_linked_shared_lib), 108 DEFINE(macho_dynamic_linker), 109 DEFINE(macho_bundle), 110 DEFINE(macho_dynamically_linked_shared_lib_stub), 111 DEFINE(macho_dsym_companion), 112 DEFINE(macho_kext_bundle), 113 DEFINE(windows_resource), 114 DEFINE(pdb), 115 {"ms_dos_stub_broken", ms_dos_stub_broken, sizeof(ms_dos_stub_broken), 116 file_magic::unknown}, 117 #undef DEFINE 118 }; 119 120 // Create some files filled with magic. 121 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; 122 ++i) { 123 SmallString<128> file_pathname(TestDirectory); 124 llvm::sys::path::append(file_pathname, i->filename); 125 std::error_code EC; 126 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None); 127 ASSERT_FALSE(file.has_error()); 128 StringRef magic(i->magic_str, i->magic_str_len); 129 file << magic; 130 file.close(); 131 EXPECT_EQ(i->magic, identify_magic(magic)); 132 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); 133 } 134 } 135