1 //===- llvm/unittest/Support/FileOutputBuffer.cpp - unit tests ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/FileOutputBuffer.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
18 using namespace llvm::sys;
20 #define ASSERT_NO_ERROR(x) \
21 if (error_code ASSERT_NO_ERROR_ec = x) { \
22 errs() << #x ": did not return errc::success.\n" \
23 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
24 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
28 TEST(FileOutputBuffer, Test) {
29 // Create unique temporary directory for these tests
30 SmallString<128> TestDirectory;
33 fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory));
36 // TEST 1: Verify commit case.
37 SmallString<128> File1(TestDirectory);
38 File1.append("/file1");
40 std::unique_ptr<FileOutputBuffer> Buffer;
41 ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
42 // Start buffer with special header.
43 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
44 // Write to end of buffer to verify it is writable.
45 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
47 ASSERT_NO_ERROR(Buffer->commit());
49 // Verify file exists and starts with special header.
50 bool MagicMatches = false;
51 ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
53 EXPECT_TRUE(MagicMatches);
54 // Verify file is correct size.
56 ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
57 ASSERT_EQ(File1Size, 8192ULL);
58 ASSERT_NO_ERROR(fs::remove(File1.str()));
60 // TEST 2: Verify abort case.
61 SmallString<128> File2(TestDirectory);
62 File2.append("/file2");
64 std::unique_ptr<FileOutputBuffer> Buffer2;
65 ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
66 // Fill buffer with special header.
67 memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
68 // Do *not* commit buffer.
70 // Verify file does not exist (because buffer not committed).
72 ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
74 ASSERT_NO_ERROR(fs::remove(File2.str()));
76 // TEST 3: Verify sizing down case.
77 SmallString<128> File3(TestDirectory);
78 File3.append("/file3");
80 std::unique_ptr<FileOutputBuffer> Buffer;
81 ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
82 // Start buffer with special header.
83 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
84 // Write to end of buffer to verify it is writable.
85 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
86 // Commit buffer, but size down to smaller size
87 ASSERT_NO_ERROR(Buffer->commit(5000));
89 // Verify file exists and starts with special header.
90 bool MagicMatches3 = false;
91 ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
93 EXPECT_TRUE(MagicMatches3);
94 // Verify file is correct size.
96 ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
97 ASSERT_EQ(File3Size, 5000ULL);
98 ASSERT_NO_ERROR(fs::remove(File3.str()));
100 // TEST 4: Verify file can be made executable.
101 SmallString<128> File4(TestDirectory);
102 File4.append("/file4");
104 std::unique_ptr<FileOutputBuffer> Buffer;
105 ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
106 FileOutputBuffer::F_executable));
107 // Start buffer with special header.
108 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
110 ASSERT_NO_ERROR(Buffer->commit());
112 // Verify file exists and is executable.
113 fs::file_status Status;
114 ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
115 bool IsExecutable = (Status.permissions() & fs::owner_exe);
116 EXPECT_TRUE(IsExecutable);
117 ASSERT_NO_ERROR(fs::remove(File4.str()));
120 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
122 } // anonymous namespace