• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #include "llvm/Support/Errc.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/FileOutputBuffer.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "gtest/gtest.h"
17 
18 using namespace llvm;
19 using namespace llvm::sys;
20 
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   }
28 
29 namespace {
TEST(FileOutputBuffer,Test)30 TEST(FileOutputBuffer, Test) {
31   // Create unique temporary directory for these tests
32   SmallString<128> TestDirectory;
33   {
34     ASSERT_NO_ERROR(
35         fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory));
36   }
37 
38   // TEST 1: Verify commit case.
39   SmallString<128> File1(TestDirectory);
40 	File1.append("/file1");
41   {
42     ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
43         FileOutputBuffer::create(File1, 8192);
44     ASSERT_NO_ERROR(BufferOrErr.getError());
45     std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
46     // Start buffer with special header.
47     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
48     // Write to end of buffer to verify it is writable.
49     memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
50     // Commit buffer.
51     ASSERT_NO_ERROR(Buffer->commit());
52   }
53 
54   // Verify file is correct size.
55   uint64_t File1Size;
56   ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
57   ASSERT_EQ(File1Size, 8192ULL);
58   ASSERT_NO_ERROR(fs::remove(File1.str()));
59 
60  	// TEST 2: Verify abort case.
61   SmallString<128> File2(TestDirectory);
62 	File2.append("/file2");
63   {
64     ErrorOr<std::unique_ptr<FileOutputBuffer>> Buffer2OrErr =
65         FileOutputBuffer::create(File2, 8192);
66     ASSERT_NO_ERROR(Buffer2OrErr.getError());
67     std::unique_ptr<FileOutputBuffer> &Buffer2 = *Buffer2OrErr;
68     // Fill buffer with special header.
69     memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
70     // Do *not* commit buffer.
71   }
72   // Verify file does not exist (because buffer not committed).
73   ASSERT_EQ(fs::access(Twine(File2), fs::AccessMode::Exist),
74             errc::no_such_file_or_directory);
75   ASSERT_NO_ERROR(fs::remove(File2.str()));
76 
77   // TEST 3: Verify sizing down case.
78   SmallString<128> File3(TestDirectory);
79 	File3.append("/file3");
80   {
81     ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
82         FileOutputBuffer::create(File3, 8192000);
83     ASSERT_NO_ERROR(BufferOrErr.getError());
84     std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
85     // Start buffer with special header.
86     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
87     // Write to end of buffer to verify it is writable.
88     memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
89     ASSERT_NO_ERROR(Buffer->commit());
90   }
91 
92   // Verify file is correct size.
93   uint64_t File3Size;
94   ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
95   ASSERT_EQ(File3Size, 8192000ULL);
96   ASSERT_NO_ERROR(fs::remove(File3.str()));
97 
98   // TEST 4: Verify file can be made executable.
99   SmallString<128> File4(TestDirectory);
100 	File4.append("/file4");
101   {
102     ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
103         FileOutputBuffer::create(File4, 8192, FileOutputBuffer::F_executable);
104     ASSERT_NO_ERROR(BufferOrErr.getError());
105     std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
106     // Start buffer with special header.
107     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
108     // Commit buffer.
109     ASSERT_NO_ERROR(Buffer->commit());
110   }
111   // Verify file exists and is executable.
112   fs::file_status Status;
113   ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
114   bool IsExecutable = (Status.permissions() & fs::owner_exe);
115   EXPECT_TRUE(IsExecutable);
116   ASSERT_NO_ERROR(fs::remove(File4.str()));
117 
118   // Clean up.
119   ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
120 }
121 } // anonymous namespace
122