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 10 #include "llvm/Support/FileOutputBuffer.h" 11 #include "llvm/Support/ErrorHandling.h" 12 #include "llvm/Support/FileSystem.h" 13 #include "llvm/Support/Path.h" 14 #include "llvm/Support/raw_ostream.h" 15 #include "gtest/gtest.h" 16 17 using namespace llvm; 18 using namespace llvm::sys; 19 20 #define ASSERT_NO_ERROR(x) \ 21 if (error_code ASSERT_NO_ERROR_ec = x) { \ 22 errs() << #x ": did not return errc::success.\n" \ 23 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ 24 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ 25 } else {} 26 27 namespace { 28 TEST(FileOutputBuffer, Test) { 29 // Create unique temporary directory for these tests 30 SmallString<128> TestDirectory; 31 { 32 ASSERT_NO_ERROR( 33 fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory)); 34 } 35 36 // TEST 1: Verify commit case. 37 SmallString<128> File1(TestDirectory); 38 File1.append("/file1"); 39 { 40 std::unique_ptr<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 ASSERT_NO_ERROR(fs::remove(File1.str())); 59 60 // TEST 2: Verify abort case. 61 SmallString<128> File2(TestDirectory); 62 File2.append("/file2"); 63 { 64 std::unique_ptr<FileOutputBuffer> Buffer2; 65 ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2)); 66 // Fill buffer with special header. 67 memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); 68 // Do *not* commit buffer. 69 } 70 // Verify file does not exist (because buffer not committed). 71 bool Exists = false; 72 ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists)); 73 EXPECT_FALSE(Exists); 74 ASSERT_NO_ERROR(fs::remove(File2.str())); 75 76 // TEST 3: Verify sizing down case. 77 SmallString<128> File3(TestDirectory); 78 File3.append("/file3"); 79 { 80 std::unique_ptr<FileOutputBuffer> Buffer; 81 ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer)); 82 // Start buffer with special header. 83 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); 84 // Write to end of buffer to verify it is writable. 85 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20); 86 // Commit buffer, but size down to smaller size 87 ASSERT_NO_ERROR(Buffer->commit(5000)); 88 } 89 // Verify file exists and starts with special header. 90 bool MagicMatches3 = false; 91 ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"), 92 MagicMatches3)); 93 EXPECT_TRUE(MagicMatches3); 94 // Verify file is correct size. 95 uint64_t File3Size; 96 ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size)); 97 ASSERT_EQ(File3Size, 5000ULL); 98 ASSERT_NO_ERROR(fs::remove(File3.str())); 99 100 // TEST 4: Verify file can be made executable. 101 SmallString<128> File4(TestDirectory); 102 File4.append("/file4"); 103 { 104 std::unique_ptr<FileOutputBuffer> Buffer; 105 ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer, 106 FileOutputBuffer::F_executable)); 107 // Start buffer with special header. 108 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); 109 // Commit buffer. 110 ASSERT_NO_ERROR(Buffer->commit()); 111 } 112 // Verify file exists and is executable. 113 fs::file_status Status; 114 ASSERT_NO_ERROR(fs::status(Twine(File4), Status)); 115 bool IsExecutable = (Status.permissions() & fs::owner_exe); 116 EXPECT_TRUE(IsExecutable); 117 ASSERT_NO_ERROR(fs::remove(File4.str())); 118 119 // Clean up. 120 ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); 121 } 122 } // anonymous namespace 123