ArrayRef-ize MD5 and clean up a few variable names.
[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/PathV2.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     int fd;
34     ASSERT_NO_ERROR(
35       fs::unique_file("FileOutputBuffer-test-%%-%%-%%-%%/dir", fd,
36                       TestDirectory));
37     ::close(fd);
38     TestDirectory = path::parent_path(TestDirectory);
39   }
40
41   // TEST 1: Verify commit case.
42   SmallString<128> File1(TestDirectory);
43         File1.append("/file1");
44   {
45     OwningPtr<FileOutputBuffer> Buffer;
46     ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
47     // Start buffer with special header.
48     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
49     // Write to end of buffer to verify it is writable.
50     memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
51     // Commit buffer.
52     ASSERT_NO_ERROR(Buffer->commit());
53   }
54   // Verify file exists and starts with special header.
55   bool MagicMatches = false;
56   ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
57                                                                 MagicMatches));
58   EXPECT_TRUE(MagicMatches);
59   // Verify file is correct size.
60   uint64_t File1Size;
61   ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
62   ASSERT_EQ(File1Size, 8192ULL);
63
64         // TEST 2: Verify abort case.
65   SmallString<128> File2(TestDirectory);
66         File2.append("/file2");
67   {
68     OwningPtr<FileOutputBuffer> Buffer2;
69     ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
70     // Fill buffer with special header.
71     memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
72     // Do *not* commit buffer.
73   }
74   // Verify file does not exist (because buffer not commited).
75   bool Exists = false;
76   ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
77   EXPECT_FALSE(Exists);
78
79   // TEST 3: Verify sizing down case.
80   SmallString<128> File3(TestDirectory);
81         File3.append("/file3");
82   {
83     OwningPtr<FileOutputBuffer> Buffer;
84     ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
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     // Commit buffer, but size down to smaller size
90     ASSERT_NO_ERROR(Buffer->commit(5000));
91   }
92   // Verify file exists and starts with special header.
93   bool MagicMatches3 = false;
94   ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
95                                                               MagicMatches3));
96   EXPECT_TRUE(MagicMatches3);
97   // Verify file is correct size.
98   uint64_t File3Size;
99   ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
100   ASSERT_EQ(File3Size, 5000ULL);
101
102   // TEST 4: Verify file can be made executable.
103   SmallString<128> File4(TestDirectory);
104         File4.append("/file4");
105   {
106     OwningPtr<FileOutputBuffer> Buffer;
107     ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
108                                               FileOutputBuffer::F_executable));
109     // Start buffer with special header.
110     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
111     // Commit buffer.
112     ASSERT_NO_ERROR(Buffer->commit());
113   }
114   // Verify file exists and is executable.
115   fs::file_status Status;
116   ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
117   bool IsExecutable = (Status.permissions() & fs::owner_exe);
118   EXPECT_TRUE(IsExecutable);
119
120   // Clean up.
121   uint32_t RemovedCount;
122   ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount));
123 }
124 } // anonymous namespace