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"
12 #include "llvm/ADT/OwningPtr.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/PathV2.h"
16 #include "llvm/Support/raw_ostream.h"
18 #include "gtest/gtest.h"
21 using namespace llvm::sys;
23 #define ASSERT_NO_ERROR(x) \
24 if (error_code ASSERT_NO_ERROR_ec = x) { \
25 errs() << #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"; \
31 TEST(FileOutputBuffer, Test) {
32 // Create unique temporary directory for these tests
33 SmallString<128> TestDirectory;
37 fs::unique_file("FileOutputBuffer-test-%%-%%-%%-%%/dir", fd,
40 TestDirectory = path::parent_path(TestDirectory);
43 // TEST 1: Verify commit case.
44 SmallString<128> File1(TestDirectory);
45 File1.append("/file1");
47 OwningPtr<FileOutputBuffer> Buffer;
48 ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
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);
54 ASSERT_NO_ERROR(Buffer->commit());
56 // Verify file exists and starts with special header.
57 bool MagicMatches = false;
58 ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
60 EXPECT_TRUE(MagicMatches);
61 // Verify file is correct size.
63 ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
64 ASSERT_EQ(File1Size, 8192ULL);
66 // TEST 2: Verify abort case.
67 SmallString<128> File2(TestDirectory);
68 File2.append("/file2");
70 OwningPtr<FileOutputBuffer> Buffer2;
71 ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
72 // Fill buffer with special header.
73 memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
74 // Do *not* commit buffer.
76 // Verify file does not exist (because buffer not commited).
78 ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
81 // TEST 3: Verify sizing down case.
82 SmallString<128> File3(TestDirectory);
83 File3.append("/file3");
85 OwningPtr<FileOutputBuffer> Buffer;
86 ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
87 // Start buffer with special header.
88 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
89 // Write to end of buffer to verify it is writable.
90 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
91 // Commit buffer, but size down to smaller size
92 ASSERT_NO_ERROR(Buffer->commit(5000));
94 // Verify file exists and starts with special header.
95 bool MagicMatches3 = false;
96 ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
98 EXPECT_TRUE(MagicMatches3);
99 // Verify file is correct size.
101 ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
102 ASSERT_EQ(File3Size, 5000ULL);
104 // TEST 4: Verify file can be made executable.
105 SmallString<128> File4(TestDirectory);
106 File4.append("/file4");
108 OwningPtr<FileOutputBuffer> Buffer;
109 ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
110 FileOutputBuffer::F_executable));
111 // Start buffer with special header.
112 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
114 ASSERT_NO_ERROR(Buffer->commit());
116 // Verify file exists and is executable.
117 fs::file_status Status;
118 ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
119 bool IsExecutable = (Status.permissions() & fs::owner_exe);
120 EXPECT_TRUE(IsExecutable);
123 uint32_t RemovedCount;
124 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount));
126 } // anonymous namespace