FileOutputBufferTest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===- llvm/unittest/Support/FileOutputBuffer.cpp - unit 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/Support/FileOutputBuffer.h"
  10. #include "llvm/ADT/OwningPtr.h"
  11. #include "llvm/Support/ErrorHandling.h"
  12. #include "llvm/Support/FileSystem.h"
  13. #include "llvm/Support/PathV2.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. using namespace llvm::sys;
  18. #define ASSERT_NO_ERROR(x) \
  19. if (error_code ASSERT_NO_ERROR_ec = x) { \
  20. errs() << #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. } else {}
  24. namespace {
  25. TEST(FileOutputBuffer, Test) {
  26. // Create unique temporary directory for these tests
  27. SmallString<128> TestDirectory;
  28. {
  29. int fd;
  30. ASSERT_NO_ERROR(
  31. fs::unique_file("FileOutputBuffer-test-%%-%%-%%-%%/dir", fd,
  32. TestDirectory));
  33. ::close(fd);
  34. TestDirectory = path::parent_path(TestDirectory);
  35. }
  36. // TEST 1: Verify commit case.
  37. SmallString<128> File1(TestDirectory);
  38. File1.append("/file1");
  39. {
  40. OwningPtr<FileOutputBuffer> Buffer;
  41. ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
  42. // Start buffer with special header.
  43. memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
  44. // Write to end of buffer to verify it is writable.
  45. memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
  46. // Commit buffer.
  47. ASSERT_NO_ERROR(Buffer->commit());
  48. }
  49. // Verify file exists and starts with special header.
  50. bool MagicMatches = false;
  51. ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
  52. MagicMatches));
  53. EXPECT_TRUE(MagicMatches);
  54. // Verify file is correct size.
  55. uint64_t File1Size;
  56. ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
  57. ASSERT_EQ(File1Size, 8192ULL);
  58. // TEST 2: Verify abort case.
  59. SmallString<128> File2(TestDirectory);
  60. File2.append("/file2");
  61. {
  62. OwningPtr<FileOutputBuffer> Buffer2;
  63. ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
  64. // Fill buffer with special header.
  65. memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
  66. // Do *not* commit buffer.
  67. }
  68. // Verify file does not exist (because buffer not commited).
  69. bool Exists = false;
  70. ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
  71. EXPECT_FALSE(Exists);
  72. // TEST 3: Verify sizing down case.
  73. SmallString<128> File3(TestDirectory);
  74. File3.append("/file3");
  75. {
  76. OwningPtr<FileOutputBuffer> Buffer;
  77. ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
  78. // Start buffer with special header.
  79. memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
  80. // Write to end of buffer to verify it is writable.
  81. memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
  82. // Commit buffer, but size down to smaller size
  83. ASSERT_NO_ERROR(Buffer->commit(5000));
  84. }
  85. // Verify file exists and starts with special header.
  86. bool MagicMatches3 = false;
  87. ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
  88. MagicMatches3));
  89. EXPECT_TRUE(MagicMatches3);
  90. // Verify file is correct size.
  91. uint64_t File3Size;
  92. ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
  93. ASSERT_EQ(File3Size, 5000ULL);
  94. // TEST 4: Verify file can be made executable.
  95. SmallString<128> File4(TestDirectory);
  96. File4.append("/file4");
  97. {
  98. OwningPtr<FileOutputBuffer> Buffer;
  99. ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
  100. FileOutputBuffer::F_executable));
  101. // Start buffer with special header.
  102. memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
  103. // Commit buffer.
  104. ASSERT_NO_ERROR(Buffer->commit());
  105. }
  106. // Verify file exists and is executable.
  107. fs::file_status Status;
  108. ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
  109. bool IsExecutable = (Status.permissions() & fs::owner_exe);
  110. EXPECT_TRUE(IsExecutable);
  111. // Clean up.
  112. uint32_t RemovedCount;
  113. ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount));
  114. }
  115. } // anonymous namespace