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 50 // Verify file is correct size. 51 uint64_t File1Size; 52 ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size)); 53 ASSERT_EQ(File1Size, 8192ULL); 54 ASSERT_NO_ERROR(fs::remove(File1.str())); 55 56 // TEST 2: Verify abort case. 57 SmallString<128> File2(TestDirectory); 58 File2.append("/file2"); 59 { 60 std::unique_ptr<FileOutputBuffer> Buffer2; 61 ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2)); 62 // Fill buffer with special header. 63 memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); 64 // Do *not* commit buffer. 65 } 66 // Verify file does not exist (because buffer not committed). 67 bool Exists = false; 68 ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists)); 69 EXPECT_FALSE(Exists); 70 ASSERT_NO_ERROR(fs::remove(File2.str())); 71 72 // TEST 3: Verify sizing down case. 73 SmallString<128> File3(TestDirectory); 74 File3.append("/file3"); 75 { 76 std::unique_ptr<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 86 // Verify file is correct size. 87 uint64_t File3Size; 88 ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size)); 89 ASSERT_EQ(File3Size, 5000ULL); 90 ASSERT_NO_ERROR(fs::remove(File3.str())); 91 92 // TEST 4: Verify file can be made executable. 93 SmallString<128> File4(TestDirectory); 94 File4.append("/file4"); 95 { 96 std::unique_ptr<FileOutputBuffer> Buffer; 97 ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer, 98 FileOutputBuffer::F_executable)); 99 // Start buffer with special header. 100 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); 101 // Commit buffer. 102 ASSERT_NO_ERROR(Buffer->commit()); 103 } 104 // Verify file exists and is executable. 105 fs::file_status Status; 106 ASSERT_NO_ERROR(fs::status(Twine(File4), Status)); 107 bool IsExecutable = (Status.permissions() & fs::owner_exe); 108 EXPECT_TRUE(IsExecutable); 109 ASSERT_NO_ERROR(fs::remove(File4.str())); 110 111 // Clean up. 112 ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); 113 } 114 } // anonymous namespace 115