Fix known typos
[oota-llvm.git] / unittests / Support / FileOutputBufferTest.cpp
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/FileOutputBuffer.h"
11 #include "llvm/ADT/OwningPtr.h"
12 #include "llvm/Support/ErrorHandling.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 (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 namespace {
29 TEST(FileOutputBuffer, Test) {
30   // Create unique temporary directory for these tests
31   SmallString<128> TestDirectory;
32   {
33     ASSERT_NO_ERROR(
34         fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory));
35   }
36
37   // TEST 1: Verify commit case.
38   SmallString<128> File1(TestDirectory);
39         File1.append("/file1");
40   {
41     OwningPtr<FileOutputBuffer> Buffer;
42     ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
43     // Start buffer with special header.
44     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
45     // Write to end of buffer to verify it is writable.
46     memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
47     // Commit buffer.
48     ASSERT_NO_ERROR(Buffer->commit());
49   }
50   // Verify file exists and starts with special header.
51   bool MagicMatches = false;
52   ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
53                                                                 MagicMatches));
54   EXPECT_TRUE(MagicMatches);
55   // Verify file is correct size.
56   uint64_t File1Size;
57   ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
58   ASSERT_EQ(File1Size, 8192ULL);
59   ASSERT_NO_ERROR(fs::remove(File1.str()));
60
61         // TEST 2: Verify abort case.
62   SmallString<128> File2(TestDirectory);
63         File2.append("/file2");
64   {
65     OwningPtr<FileOutputBuffer> Buffer2;
66     ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
67     // Fill buffer with special header.
68     memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
69     // Do *not* commit buffer.
70   }
71   // Verify file does not exist (because buffer not committed).
72   bool Exists = false;
73   ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
74   EXPECT_FALSE(Exists);
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     OwningPtr<FileOutputBuffer> Buffer;
82     ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
83     // Start buffer with special header.
84     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
85     // Write to end of buffer to verify it is writable.
86     memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
87     // Commit buffer, but size down to smaller size
88     ASSERT_NO_ERROR(Buffer->commit(5000));
89   }
90   // Verify file exists and starts with special header.
91   bool MagicMatches3 = false;
92   ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
93                                                               MagicMatches3));
94   EXPECT_TRUE(MagicMatches3);
95   // Verify file is correct size.
96   uint64_t File3Size;
97   ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
98   ASSERT_EQ(File3Size, 5000ULL);
99   ASSERT_NO_ERROR(fs::remove(File3.str()));
100
101   // TEST 4: Verify file can be made executable.
102   SmallString<128> File4(TestDirectory);
103         File4.append("/file4");
104   {
105     OwningPtr<FileOutputBuffer> Buffer;
106     ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
107                                               FileOutputBuffer::F_executable));
108     // Start buffer with special header.
109     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
110     // Commit buffer.
111     ASSERT_NO_ERROR(Buffer->commit());
112   }
113   // Verify file exists and is executable.
114   fs::file_status Status;
115   ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
116   bool IsExecutable = (Status.permissions() & fs::owner_exe);
117   EXPECT_TRUE(IsExecutable);
118   ASSERT_NO_ERROR(fs::remove(File4.str()));
119
120   // Clean up.
121   ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
122 }
123 } // anonymous namespace