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" |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 11 | #include "llvm/Support/FileOutputBuffer.h" |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ErrorHandling.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) { \ |
| 23 | errs() << #x ": did not return errc::success.\n" \ |
| 24 | << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 25 | << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 26 | } else { \ |
| 27 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 30 | TEST(FileOutputBuffer, Test) { |
| 31 | // Create unique temporary directory for these tests |
| 32 | SmallString<128> TestDirectory; |
| 33 | { |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 34 | ASSERT_NO_ERROR( |
Rafael Espindola | 7ffacc4 | 2013-06-27 03:45:31 +0000 | [diff] [blame] | 35 | fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory)); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 36 | } |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 37 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 38 | // TEST 1: Verify commit case. |
| 39 | SmallString<128> File1(TestDirectory); |
| 40 | File1.append("/file1"); |
| 41 | { |
Craig Topper | b85bc4e | 2014-05-18 21:01:46 +0000 | [diff] [blame] | 42 | std::unique_ptr<FileOutputBuffer> Buffer; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 43 | ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer)); |
| 44 | // Start buffer with special header. |
| 45 | memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); |
| 46 | // Write to end of buffer to verify it is writable. |
| 47 | memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20); |
| 48 | // Commit buffer. |
| 49 | ASSERT_NO_ERROR(Buffer->commit()); |
| 50 | } |
Rafael Espindola | 70fbe6f | 2014-06-11 21:53:22 +0000 | [diff] [blame] | 51 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 52 | // Verify file is correct size. |
| 53 | uint64_t File1Size; |
| 54 | ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size)); |
| 55 | ASSERT_EQ(File1Size, 8192ULL); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 56 | ASSERT_NO_ERROR(fs::remove(File1.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 57 | |
| 58 | // TEST 2: Verify abort case. |
| 59 | SmallString<128> File2(TestDirectory); |
| 60 | File2.append("/file2"); |
| 61 | { |
Craig Topper | b85bc4e | 2014-05-18 21:01:46 +0000 | [diff] [blame] | 62 | std::unique_ptr<FileOutputBuffer> Buffer2; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 63 | ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2)); |
| 64 | // Fill buffer with special header. |
| 65 | memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); |
| 66 | // Do *not* commit buffer. |
| 67 | } |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 68 | // Verify file does not exist (because buffer not committed). |
Rafael Espindola | 281f23a | 2014-09-11 20:30:02 +0000 | [diff] [blame] | 69 | ASSERT_EQ(fs::access(Twine(File2), fs::AccessMode::Exist), |
| 70 | errc::no_such_file_or_directory); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 71 | ASSERT_NO_ERROR(fs::remove(File2.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 72 | |
| 73 | // TEST 3: Verify sizing down case. |
| 74 | SmallString<128> File3(TestDirectory); |
| 75 | File3.append("/file3"); |
| 76 | { |
Craig Topper | b85bc4e | 2014-05-18 21:01:46 +0000 | [diff] [blame] | 77 | std::unique_ptr<FileOutputBuffer> Buffer; |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 78 | ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer)); |
| 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); |
Rafael Espindola | 5753cf3 | 2014-12-12 17:35:34 +0000 | [diff] [blame] | 83 | ASSERT_NO_ERROR(Buffer->commit()); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 84 | } |
Rafael Espindola | 70fbe6f | 2014-06-11 21:53:22 +0000 | [diff] [blame] | 85 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 86 | // Verify file is correct size. |
| 87 | uint64_t File3Size; |
| 88 | ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size)); |
Rafael Espindola | 5753cf3 | 2014-12-12 17:35:34 +0000 | [diff] [blame] | 89 | ASSERT_EQ(File3Size, 8192000ULL); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 90 | ASSERT_NO_ERROR(fs::remove(File3.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 91 | |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 92 | // TEST 4: Verify file can be made executable. |
| 93 | SmallString<128> File4(TestDirectory); |
| 94 | File4.append("/file4"); |
| 95 | { |
Craig Topper | b85bc4e | 2014-05-18 21:01:46 +0000 | [diff] [blame] | 96 | std::unique_ptr<FileOutputBuffer> Buffer; |
Michael J. Spencer | 7fe24f5 | 2012-12-03 22:09:52 +0000 | [diff] [blame] | 97 | ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer, |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 98 | FileOutputBuffer::F_executable)); |
| 99 | // Start buffer with special header. |
| 100 | memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); |
| 101 | // Commit buffer. |
| 102 | ASSERT_NO_ERROR(Buffer->commit()); |
| 103 | } |
| 104 | // Verify file exists and is executable. |
| 105 | fs::file_status Status; |
| 106 | ASSERT_NO_ERROR(fs::status(Twine(File4), Status)); |
| 107 | bool IsExecutable = (Status.permissions() & fs::owner_exe); |
| 108 | EXPECT_TRUE(IsExecutable); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 109 | ASSERT_NO_ERROR(fs::remove(File4.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 110 | |
| 111 | // Clean up. |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 112 | ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 113 | } |
Nick Kledzik | 5fce8c4 | 2012-08-01 02:29:50 +0000 | [diff] [blame] | 114 | } // anonymous namespace |