blob: 4c0d57b84526fc05bed21c3b9b5ad81c2a118547 [file] [log] [blame]
Nick Kledzikadfe2632012-08-01 02:29:50 +00001//===- 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
Michael J. Spencer6bc86012012-12-03 22:09:52 +000010#include "llvm/Support/FileOutputBuffer.h"
11
Nick Kledzikadfe2632012-08-01 02:29:50 +000012#include "llvm/ADT/OwningPtr.h"
13#include "llvm/Support/ErrorHandling.h"
Nick Kledzikadfe2632012-08-01 02:29:50 +000014#include "llvm/Support/FileSystem.h"
15#include "llvm/Support/PathV2.h"
16#include "llvm/Support/raw_ostream.h"
17
18#include "gtest/gtest.h"
19
20using namespace llvm;
21using namespace llvm::sys;
22
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"; \
28 } else {}
29
30namespace {
Nick Kledzikadfe2632012-08-01 02:29:50 +000031TEST(FileOutputBuffer, Test) {
32 // Create unique temporary directory for these tests
33 SmallString<128> TestDirectory;
34 {
35 int fd;
36 ASSERT_NO_ERROR(
37 fs::unique_file("FileOutputBuffer-test-%%-%%-%%-%%/dir", fd,
38 TestDirectory));
39 ::close(fd);
40 TestDirectory = path::parent_path(TestDirectory);
41 }
Michael J. Spencer6bc86012012-12-03 22:09:52 +000042
Nick Kledzikadfe2632012-08-01 02:29:50 +000043 // TEST 1: Verify commit case.
44 SmallString<128> File1(TestDirectory);
45 File1.append("/file1");
46 {
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);
53 // Commit buffer.
54 ASSERT_NO_ERROR(Buffer->commit());
55 }
56 // Verify file exists and starts with special header.
57 bool MagicMatches = false;
Michael J. Spencer6bc86012012-12-03 22:09:52 +000058 ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
Nick Kledzikadfe2632012-08-01 02:29:50 +000059 MagicMatches));
60 EXPECT_TRUE(MagicMatches);
61 // Verify file is correct size.
62 uint64_t File1Size;
63 ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
64 ASSERT_EQ(File1Size, 8192ULL);
65
66 // TEST 2: Verify abort case.
67 SmallString<128> File2(TestDirectory);
68 File2.append("/file2");
69 {
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.
75 }
76 // Verify file does not exist (because buffer not commited).
77 bool Exists = false;
78 ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
Michael J. Spencer6bc86012012-12-03 22:09:52 +000079 EXPECT_FALSE(Exists);
Nick Kledzikadfe2632012-08-01 02:29:50 +000080
81 // TEST 3: Verify sizing down case.
82 SmallString<128> File3(TestDirectory);
83 File3.append("/file3");
84 {
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));
93 }
94 // Verify file exists and starts with special header.
95 bool MagicMatches3 = false;
Michael J. Spencer6bc86012012-12-03 22:09:52 +000096 ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
Nick Kledzikadfe2632012-08-01 02:29:50 +000097 MagicMatches3));
98 EXPECT_TRUE(MagicMatches3);
99 // Verify file is correct size.
100 uint64_t File3Size;
101 ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
102 ASSERT_EQ(File3Size, 5000ULL);
103
Nick Kledzikadfe2632012-08-01 02:29:50 +0000104 // TEST 4: Verify file can be made executable.
105 SmallString<128> File4(TestDirectory);
106 File4.append("/file4");
107 {
108 OwningPtr<FileOutputBuffer> Buffer;
Michael J. Spencer6bc86012012-12-03 22:09:52 +0000109 ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
Nick Kledzikadfe2632012-08-01 02:29:50 +0000110 FileOutputBuffer::F_executable));
111 // Start buffer with special header.
112 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
113 // Commit buffer.
114 ASSERT_NO_ERROR(Buffer->commit());
115 }
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);
121
122 // Clean up.
123 uint32_t RemovedCount;
124 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount));
125}
Nick Kledzikadfe2632012-08-01 02:29:50 +0000126} // anonymous namespace