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