]>
git.proxmox.com Git - rustc.git/blob - src/llvm/unittests/Support/FileOutputBufferTest.cpp
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/ADT/OwningPtr.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/FileOutputBuffer.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/PathV2.h"
15 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h"
20 using namespace llvm::sys
;
22 #define ASSERT_NO_ERROR(x) \
23 if (error_code ASSERT_NO_ERROR_ec = x) { \
24 errs() << #x ": did not return errc::success.\n" \
25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
32 // NOTE: Temporarily run this test on unix only. Once the file mapping
33 // routines are ported to Windows, this conditional can be removed.
37 TEST(FileOutputBuffer
, Test
) {
38 // Create unique temporary directory for these tests
39 SmallString
<128> TestDirectory
;
43 fs::unique_file("FileOutputBuffer-test-%%-%%-%%-%%/dir", fd
,
46 TestDirectory
= path::parent_path(TestDirectory
);
49 // TEST 1: Verify commit case.
50 SmallString
<128> File1(TestDirectory
);
51 File1
.append("/file1");
53 OwningPtr
<FileOutputBuffer
> Buffer
;
54 ASSERT_NO_ERROR(FileOutputBuffer::create(File1
, 8192, Buffer
));
55 // Start buffer with special header.
56 memcpy(Buffer
->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
57 // Write to end of buffer to verify it is writable.
58 memcpy(Buffer
->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
60 ASSERT_NO_ERROR(Buffer
->commit());
62 // Verify file exists and starts with special header.
63 bool MagicMatches
= false;
64 ASSERT_NO_ERROR(fs::has_magic(Twine(File1
), Twine("AABBCCDDEEFFGGHHIIJJ"),
66 EXPECT_TRUE(MagicMatches
);
67 // Verify file is correct size.
69 ASSERT_NO_ERROR(fs::file_size(Twine(File1
), File1Size
));
70 ASSERT_EQ(File1Size
, 8192ULL);
72 // TEST 2: Verify abort case.
73 SmallString
<128> File2(TestDirectory
);
74 File2
.append("/file2");
76 OwningPtr
<FileOutputBuffer
> Buffer2
;
77 ASSERT_NO_ERROR(FileOutputBuffer::create(File2
, 8192, Buffer2
));
78 // Fill buffer with special header.
79 memcpy(Buffer2
->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
80 // Do *not* commit buffer.
82 // Verify file does not exist (because buffer not commited).
84 ASSERT_NO_ERROR(fs::exists(Twine(File2
), Exists
));
88 // TEST 3: Verify sizing down case.
89 SmallString
<128> File3(TestDirectory
);
90 File3
.append("/file3");
92 OwningPtr
<FileOutputBuffer
> Buffer
;
93 ASSERT_NO_ERROR(FileOutputBuffer::create(File3
, 8192000, Buffer
));
94 // Start buffer with special header.
95 memcpy(Buffer
->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
96 // Write to end of buffer to verify it is writable.
97 memcpy(Buffer
->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
98 // Commit buffer, but size down to smaller size
99 ASSERT_NO_ERROR(Buffer
->commit(5000));
101 // Verify file exists and starts with special header.
102 bool MagicMatches3
= false;
103 ASSERT_NO_ERROR(fs::has_magic(Twine(File3
), Twine("AABBCCDDEEFFGGHHIIJJ"),
105 EXPECT_TRUE(MagicMatches3
);
106 // Verify file is correct size.
108 ASSERT_NO_ERROR(fs::file_size(Twine(File3
), File3Size
));
109 ASSERT_EQ(File3Size
, 5000ULL);
112 // TEST 4: Verify file can be made executable.
113 SmallString
<128> File4(TestDirectory
);
114 File4
.append("/file4");
116 OwningPtr
<FileOutputBuffer
> Buffer
;
117 ASSERT_NO_ERROR(FileOutputBuffer::create(File4
, 8192, Buffer
,
118 FileOutputBuffer::F_executable
));
119 // Start buffer with special header.
120 memcpy(Buffer
->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
122 ASSERT_NO_ERROR(Buffer
->commit());
124 // Verify file exists and is executable.
125 fs::file_status Status
;
126 ASSERT_NO_ERROR(fs::status(Twine(File4
), Status
));
127 bool IsExecutable
= (Status
.permissions() & fs::owner_exe
);
128 EXPECT_TRUE(IsExecutable
);
131 uint32_t RemovedCount
;
132 ASSERT_NO_ERROR(fs::remove_all(TestDirectory
.str(), RemovedCount
));
135 #endif // LLVM_ON_UNIX
137 } // anonymous namespace