blob: fc2c1eef9fbf79170caee225025a7bd343a84d2f [file] [log] [blame]
Zachary Turner264b5d92017-06-07 03:48:56 +00001//===- 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
18using namespace llvm;
19namespace 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
32class MagicTest : public testing::Test {
33protected:
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
49const char archive[] = "!<arch>\x0A";
50const char bitcode[] = "\xde\xc0\x17\x0b";
51const char coff_object[] = "\x00\x00......";
52const 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";
55const char coff_import_library[] = "\x00\x00\xff\xff....";
56const char elf_relocatable[] = {0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 1};
58const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\x00";
59const char macho_object[] =
60 "\xfe\xed\xfa\xce........\x00\x00\x00\x01............";
61const char macho_executable[] =
62 "\xfe\xed\xfa\xce........\x00\x00\x00\x02............";
63const char macho_fixed_virtual_memory_shared_lib[] =
64 "\xfe\xed\xfa\xce........\x00\x00\x00\x03............";
65const char macho_core[] =
66 "\xfe\xed\xfa\xce........\x00\x00\x00\x04............";
67const char macho_preload_executable[] =
68 "\xfe\xed\xfa\xce........\x00\x00\x00\x05............";
69const char macho_dynamically_linked_shared_lib[] =
70 "\xfe\xed\xfa\xce........\x00\x00\x00\x06............";
71const char macho_dynamic_linker[] =
72 "\xfe\xed\xfa\xce........\x00\x00\x00\x07............";
73const char macho_bundle[] =
74 "\xfe\xed\xfa\xce........\x00\x00\x00\x08............";
75const char macho_dsym_companion[] =
76 "\xfe\xed\xfa\xce........\x00\x00\x00\x0a............";
77const char macho_kext_bundle[] =
78 "\xfe\xed\xfa\xce........\x00\x00\x00\x0b............";
Eric Beckmannd40dd642017-06-29 00:17:26 +000079const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
Zachary Turner264b5d92017-06-07 03:48:56 +000080const char macho_dynamically_linked_shared_lib_stub[] =
81 "\xfe\xed\xfa\xce........\x00\x00\x00\x09............";
82
83TEST_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}