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