TestFileMagic.cpp 5.0 KB

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