1 //===- llvm/unittest/Support/FileOutputBuffer.cpp - unit 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 9 #include "llvm/Support/FileOutputBuffer.h" 10 #include "llvm/Support/Errc.h" 11 #include "llvm/Support/ErrorHandling.h" 12 #include "llvm/Support/FileSystem.h" 13 #include "llvm/Support/MemoryBuffer.h" 14 #include "llvm/Support/Path.h" 15 #include "llvm/Support/raw_ostream.h" 16 #include "gtest/gtest.h" 17 18 using namespace llvm; 19 using namespace llvm::sys; 20 21 #define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code()) 22 23 namespace { 24 TEST(FileOutputBuffer, Test) { 25 // Create unique temporary directory for these tests 26 SmallString<128> TestDirectory; 27 { 28 ASSERT_NO_ERROR( 29 fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory)); 30 } 31 32 // TEST 1: Verify commit case. 33 SmallString<128> File1(TestDirectory); 34 File1.append("/file1"); 35 { 36 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = 37 FileOutputBuffer::create(File1, 8192); 38 ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError())); 39 std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr; 40 // Start buffer with special header. 41 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); 42 // Write to end of buffer to verify it is writable. 43 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20); 44 // Commit buffer. 45 ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit())); 46 } 47 48 // Verify file is correct size. 49 uint64_t File1Size; 50 ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size)); 51 ASSERT_EQ(File1Size, 8192ULL); 52 ASSERT_NO_ERROR(fs::remove(File1.str())); 53 54 // TEST 2: Verify abort case. 55 SmallString<128> File2(TestDirectory); 56 File2.append("/file2"); 57 { 58 Expected<std::unique_ptr<FileOutputBuffer>> Buffer2OrErr = 59 FileOutputBuffer::create(File2, 8192); 60 ASSERT_NO_ERROR(errorToErrorCode(Buffer2OrErr.takeError())); 61 std::unique_ptr<FileOutputBuffer> &Buffer2 = *Buffer2OrErr; 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 ASSERT_EQ(fs::access(Twine(File2), fs::AccessMode::Exist), 68 errc::no_such_file_or_directory); 69 ASSERT_NO_ERROR(fs::remove(File2.str())); 70 71 // TEST 3: Verify sizing down case. 72 SmallString<128> File3(TestDirectory); 73 File3.append("/file3"); 74 { 75 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = 76 FileOutputBuffer::create(File3, 8192000); 77 ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError())); 78 std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr; 79 // Start buffer with special header. 80 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); 81 // Write to end of buffer to verify it is writable. 82 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20); 83 ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit())); 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, 8192000ULL); 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 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = 97 FileOutputBuffer::create(File4, 8192, FileOutputBuffer::F_executable); 98 ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError())); 99 std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr; 100 // Start buffer with special header. 101 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); 102 // Commit buffer. 103 ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit())); 104 } 105 // Verify file exists and is executable. 106 fs::file_status Status; 107 ASSERT_NO_ERROR(fs::status(Twine(File4), Status)); 108 bool IsExecutable = (Status.permissions() & fs::owner_exe); 109 EXPECT_TRUE(IsExecutable); 110 ASSERT_NO_ERROR(fs::remove(File4.str())); 111 112 // Clean up. 113 ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); 114 } 115 } // anonymous namespace 116