blob: 0801f8539909bfb27d9cbaea1238431b6f6571e1 [file] [log] [blame]
Nick Kledzik5fce8c42012-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. Spencer7fe24f52012-12-03 22:09:52 +000010#include "llvm/Support/FileOutputBuffer.h"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000011#include "llvm/Support/ErrorHandling.h"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000012#include "llvm/Support/FileSystem.h"
Rafael Espindola3bc8e712013-06-11 22:21:28 +000013#include "llvm/Support/Path.h"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000014#include "llvm/Support/raw_ostream.h"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000015#include "gtest/gtest.h"
16
17using namespace llvm;
18using namespace llvm::sys;
19
20#define ASSERT_NO_ERROR(x) \
21 if (error_code ASSERT_NO_ERROR_ec = x) { \
22 errs() << #x ": did not return errc::success.\n" \
23 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
24 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
25 } else {}
26
27namespace {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000028TEST(FileOutputBuffer, Test) {
29 // Create unique temporary directory for these tests
30 SmallString<128> TestDirectory;
31 {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000032 ASSERT_NO_ERROR(
Rafael Espindola7ffacc42013-06-27 03:45:31 +000033 fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000034 }
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000035
Nick Kledzik5fce8c42012-08-01 02:29:50 +000036 // TEST 1: Verify commit case.
37 SmallString<128> File1(TestDirectory);
38 File1.append("/file1");
39 {
Craig Topperb85bc4e2014-05-18 21:01:46 +000040 std::unique_ptr<FileOutputBuffer> Buffer;
Nick Kledzik5fce8c42012-08-01 02:29:50 +000041 ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
42 // Start buffer with special header.
43 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
44 // Write to end of buffer to verify it is writable.
45 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
46 // Commit buffer.
47 ASSERT_NO_ERROR(Buffer->commit());
48 }
49 // Verify file exists and starts with special header.
50 bool MagicMatches = false;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000051 ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
Nick Kledzik5fce8c42012-08-01 02:29:50 +000052 MagicMatches));
53 EXPECT_TRUE(MagicMatches);
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);
Rafael Espindola78dcc032014-01-10 20:36:42 +000058 ASSERT_NO_ERROR(fs::remove(File1.str()));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000059
60 // TEST 2: Verify abort case.
61 SmallString<128> File2(TestDirectory);
62 File2.append("/file2");
63 {
Craig Topperb85bc4e2014-05-18 21:01:46 +000064 std::unique_ptr<FileOutputBuffer> Buffer2;
Nick Kledzik5fce8c42012-08-01 02:29:50 +000065 ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
66 // Fill buffer with special header.
67 memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
68 // Do *not* commit buffer.
69 }
Alp Tokercb402912014-01-24 17:20:08 +000070 // Verify file does not exist (because buffer not committed).
Nick Kledzik5fce8c42012-08-01 02:29:50 +000071 bool Exists = false;
72 ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000073 EXPECT_FALSE(Exists);
Rafael Espindola78dcc032014-01-10 20:36:42 +000074 ASSERT_NO_ERROR(fs::remove(File2.str()));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000075
76 // TEST 3: Verify sizing down case.
77 SmallString<128> File3(TestDirectory);
78 File3.append("/file3");
79 {
Craig Topperb85bc4e2014-05-18 21:01:46 +000080 std::unique_ptr<FileOutputBuffer> Buffer;
Nick Kledzik5fce8c42012-08-01 02:29:50 +000081 ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
82 // Start buffer with special header.
83 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
84 // Write to end of buffer to verify it is writable.
85 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
86 // Commit buffer, but size down to smaller size
87 ASSERT_NO_ERROR(Buffer->commit(5000));
88 }
89 // Verify file exists and starts with special header.
90 bool MagicMatches3 = false;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000091 ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
Nick Kledzik5fce8c42012-08-01 02:29:50 +000092 MagicMatches3));
93 EXPECT_TRUE(MagicMatches3);
94 // Verify file is correct size.
95 uint64_t File3Size;
96 ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
97 ASSERT_EQ(File3Size, 5000ULL);
Rafael Espindola78dcc032014-01-10 20:36:42 +000098 ASSERT_NO_ERROR(fs::remove(File3.str()));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000099
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000100 // TEST 4: Verify file can be made executable.
101 SmallString<128> File4(TestDirectory);
102 File4.append("/file4");
103 {
Craig Topperb85bc4e2014-05-18 21:01:46 +0000104 std::unique_ptr<FileOutputBuffer> Buffer;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +0000105 ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000106 FileOutputBuffer::F_executable));
107 // Start buffer with special header.
108 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
109 // Commit buffer.
110 ASSERT_NO_ERROR(Buffer->commit());
111 }
112 // Verify file exists and is executable.
113 fs::file_status Status;
114 ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
115 bool IsExecutable = (Status.permissions() & fs::owner_exe);
116 EXPECT_TRUE(IsExecutable);
Rafael Espindola78dcc032014-01-10 20:36:42 +0000117 ASSERT_NO_ERROR(fs::remove(File4.str()));
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000118
119 // Clean up.
Rafael Espindola78dcc032014-01-10 20:36:42 +0000120 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000121}
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000122} // anonymous namespace