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