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