Nick Kledzik | 5fce8c4 | 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 | |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Errc.h" |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 11 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 12 | #include "llvm/Support/FileOutputBuffer.h" |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 13 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | 3bc8e71 | 2013-06-11 22:21:28 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Path.h" |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace llvm::sys; |
| 20 | |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 21 | #define ASSERT_NO_ERROR(x) \ |
| 22 | if (std::error_code ASSERT_NO_ERROR_ec = x) { \ |
Reid Kleckner | 1a4398a | 2016-09-02 01:10:53 +0000 | [diff] [blame] | 23 | SmallString<128> MessageStorage; \ |
| 24 | raw_svector_ostream Message(MessageStorage); \ |
| 25 | Message << #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 | GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 29 | } else { \ |
| 30 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 33 | TEST(FileOutputBuffer, Test) { |
| 34 | // Create unique temporary directory for these tests |
| 35 | SmallString<128> TestDirectory; |
| 36 | { |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 37 | ASSERT_NO_ERROR( |
Rafael Espindola | 7ffacc4 | 2013-06-27 03:45:31 +0000 | [diff] [blame] | 38 | fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory)); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 39 | } |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 40 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 41 | // TEST 1: Verify commit case. |
| 42 | SmallString<128> File1(TestDirectory); |
| 43 | File1.append("/file1"); |
| 44 | { |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 45 | ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 46 | FileOutputBuffer::create(File1, 8192); |
| 47 | ASSERT_NO_ERROR(BufferOrErr.getError()); |
| 48 | std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 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 | } |
Rafael Espindola | 70fbe6f | 2014-06-11 21:53:22 +0000 | [diff] [blame] | 56 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 57 | // Verify file is correct size. |
| 58 | uint64_t File1Size; |
| 59 | ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size)); |
| 60 | ASSERT_EQ(File1Size, 8192ULL); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 61 | ASSERT_NO_ERROR(fs::remove(File1.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 62 | |
Reid Kleckner | 1a4398a | 2016-09-02 01:10:53 +0000 | [diff] [blame] | 63 | // TEST 2: Verify abort case. |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 64 | SmallString<128> File2(TestDirectory); |
Reid Kleckner | 1a4398a | 2016-09-02 01:10:53 +0000 | [diff] [blame] | 65 | File2.append("/file2"); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 66 | { |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 67 | ErrorOr<std::unique_ptr<FileOutputBuffer>> Buffer2OrErr = |
| 68 | FileOutputBuffer::create(File2, 8192); |
| 69 | ASSERT_NO_ERROR(Buffer2OrErr.getError()); |
| 70 | std::unique_ptr<FileOutputBuffer> &Buffer2 = *Buffer2OrErr; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 71 | // Fill buffer with special header. |
| 72 | memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); |
| 73 | // Do *not* commit buffer. |
| 74 | } |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 75 | // Verify file does not exist (because buffer not committed). |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 76 | ASSERT_EQ(fs::access(Twine(File2), fs::AccessMode::Exist), |
| 77 | errc::no_such_file_or_directory); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 78 | ASSERT_NO_ERROR(fs::remove(File2.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 79 | |
| 80 | // TEST 3: Verify sizing down case. |
| 81 | SmallString<128> File3(TestDirectory); |
| 82 | File3.append("/file3"); |
| 83 | { |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 84 | ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 85 | FileOutputBuffer::create(File3, 8192000); |
| 86 | ASSERT_NO_ERROR(BufferOrErr.getError()); |
| 87 | std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 88 | // Start buffer with special header. |
| 89 | memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); |
| 90 | // Write to end of buffer to verify it is writable. |
| 91 | memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20); |
Rafael Espindola | 5753cf3 | 2014-12-12 17:35:34 +0000 | [diff] [blame] | 92 | ASSERT_NO_ERROR(Buffer->commit()); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 93 | } |
Rafael Espindola | 70fbe6f | 2014-06-11 21:53:22 +0000 | [diff] [blame] | 94 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 95 | // Verify file is correct size. |
| 96 | uint64_t File3Size; |
| 97 | ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size)); |
Rafael Espindola | 5753cf3 | 2014-12-12 17:35:34 +0000 | [diff] [blame] | 98 | ASSERT_EQ(File3Size, 8192000ULL); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 99 | ASSERT_NO_ERROR(fs::remove(File3.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 100 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 101 | // TEST 4: Verify file can be made executable. |
| 102 | SmallString<128> File4(TestDirectory); |
| 103 | File4.append("/file4"); |
| 104 | { |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 105 | ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 106 | FileOutputBuffer::create(File4, 8192, FileOutputBuffer::F_executable); |
| 107 | ASSERT_NO_ERROR(BufferOrErr.getError()); |
| 108 | std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 109 | // Start buffer with special header. |
| 110 | memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); |
| 111 | // Commit buffer. |
| 112 | ASSERT_NO_ERROR(Buffer->commit()); |
| 113 | } |
| 114 | // Verify file exists and is executable. |
| 115 | fs::file_status Status; |
| 116 | ASSERT_NO_ERROR(fs::status(Twine(File4), Status)); |
| 117 | bool IsExecutable = (Status.permissions() & fs::owner_exe); |
| 118 | EXPECT_TRUE(IsExecutable); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 119 | ASSERT_NO_ERROR(fs::remove(File4.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 120 | |
| 121 | // Clean up. |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 122 | ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 123 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 124 | } // anonymous namespace |