TestFileMagic.cpp 5.3 KB

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