Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/FileOutputBuffer.cpp - unit tests ------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 9 | #include "llvm/Support/FileOutputBuffer.h" |
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" |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 12 | #include "llvm/Support/FileSystem.h" |
Zachary Turner | 1adca7c | 2018-06-28 18:49:09 +0000 | [diff] [blame] | 13 | #include "llvm/Support/MemoryBuffer.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 | |
Pavel Labath | 0d802a4 | 2019-08-14 13:59:04 +0000 | [diff] [blame] | 21 | #define ASSERT_NO_ERROR(x) \ |
| 22 | if (std::error_code ASSERT_NO_ERROR_ec = x) { \ |
| 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()); \ |
| 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); |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 43 | File1.append("/file1"); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 44 | { |
Rafael Espindola | 81ca0df | 2017-11-08 01:10:05 +0000 | [diff] [blame] | 45 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 46 | FileOutputBuffer::create(File1, 8192); |
Rafael Espindola | 81ca0df | 2017-11-08 01:10:05 +0000 | [diff] [blame] | 47 | ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError())); |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 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. |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame] | 54 | ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 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 | 81ca0df | 2017-11-08 01:10:05 +0000 | [diff] [blame] | 67 | Expected<std::unique_ptr<FileOutputBuffer>> Buffer2OrErr = |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 68 | FileOutputBuffer::create(File2, 8192); |
Rafael Espindola | 81ca0df | 2017-11-08 01:10:05 +0000 | [diff] [blame] | 69 | ASSERT_NO_ERROR(errorToErrorCode(Buffer2OrErr.takeError())); |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 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); |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 82 | File3.append("/file3"); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 83 | { |
Rafael Espindola | 81ca0df | 2017-11-08 01:10:05 +0000 | [diff] [blame] | 84 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 85 | FileOutputBuffer::create(File3, 8192000); |
Rafael Espindola | 81ca0df | 2017-11-08 01:10:05 +0000 | [diff] [blame] | 86 | ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError())); |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 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 | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame] | 92 | ASSERT_NO_ERROR(errorToErrorCode(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); |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 103 | File4.append("/file4"); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 104 | { |
Rafael Espindola | 81ca0df | 2017-11-08 01:10:05 +0000 | [diff] [blame] | 105 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 106 | FileOutputBuffer::create(File4, 8192, FileOutputBuffer::F_executable); |
Rafael Espindola | 81ca0df | 2017-11-08 01:10:05 +0000 | [diff] [blame] | 107 | ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError())); |
Rafael Espindola | 169284a | 2015-08-13 00:31:39 +0000 | [diff] [blame] | 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. |
Rafael Espindola | 0d7a38a | 2017-11-08 01:50:29 +0000 | [diff] [blame] | 112 | ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 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 |