2012-08-01 10:29:50 +08:00
|
|
|
//===- llvm/unittest/Support/FileOutputBuffer.cpp - unit tests ------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-08-01 10:29:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:06:56 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2014-09-12 04:30:02 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2012-08-01 10:29:50 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
2018-06-29 02:49:09 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2013-06-12 06:21:28 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2012-08-01 10:29:50 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::sys;
|
|
|
|
|
2014-06-13 11:20:08 +08:00
|
|
|
#define ASSERT_NO_ERROR(x) \
|
|
|
|
if (std::error_code ASSERT_NO_ERROR_ec = x) { \
|
2016-09-02 09:10:53 +08:00
|
|
|
SmallString<128> MessageStorage; \
|
|
|
|
raw_svector_ostream Message(MessageStorage); \
|
|
|
|
Message << #x ": did not return errc::success.\n" \
|
|
|
|
<< "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
|
|
|
|
<< "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
|
|
|
|
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
|
2014-06-13 11:20:08 +08:00
|
|
|
} else { \
|
|
|
|
}
|
2012-08-01 10:29:50 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
TEST(FileOutputBuffer, Test) {
|
|
|
|
// Create unique temporary directory for these tests
|
|
|
|
SmallString<128> TestDirectory;
|
|
|
|
{
|
|
|
|
ASSERT_NO_ERROR(
|
2013-06-27 11:45:31 +08:00
|
|
|
fs::createUniqueDirectory("FileOutputBuffer-test", TestDirectory));
|
2012-08-01 10:29:50 +08:00
|
|
|
}
|
2012-12-04 06:09:52 +08:00
|
|
|
|
2012-08-01 10:29:50 +08:00
|
|
|
// TEST 1: Verify commit case.
|
|
|
|
SmallString<128> File1(TestDirectory);
|
2017-08-28 14:47:47 +08:00
|
|
|
File1.append("/file1");
|
2012-08-01 10:29:50 +08:00
|
|
|
{
|
2017-11-08 09:10:05 +08:00
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
2015-08-13 08:31:39 +08:00
|
|
|
FileOutputBuffer::create(File1, 8192);
|
2017-11-08 09:10:05 +08:00
|
|
|
ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError()));
|
2015-08-13 08:31:39 +08:00
|
|
|
std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
|
2012-08-01 10:29:50 +08:00
|
|
|
// Start buffer with special header.
|
|
|
|
memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
|
|
|
|
// Write to end of buffer to verify it is writable.
|
|
|
|
memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
|
|
|
|
// Commit buffer.
|
2017-11-08 09:50:29 +08:00
|
|
|
ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit()));
|
2012-08-01 10:29:50 +08:00
|
|
|
}
|
2014-06-12 05:53:22 +08:00
|
|
|
|
2012-08-01 10:29:50 +08:00
|
|
|
// Verify file is correct size.
|
|
|
|
uint64_t File1Size;
|
|
|
|
ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
|
|
|
|
ASSERT_EQ(File1Size, 8192ULL);
|
2014-01-11 04:36:42 +08:00
|
|
|
ASSERT_NO_ERROR(fs::remove(File1.str()));
|
2012-08-01 10:29:50 +08:00
|
|
|
|
2016-09-02 09:10:53 +08:00
|
|
|
// TEST 2: Verify abort case.
|
2012-08-01 10:29:50 +08:00
|
|
|
SmallString<128> File2(TestDirectory);
|
2016-09-02 09:10:53 +08:00
|
|
|
File2.append("/file2");
|
2012-08-01 10:29:50 +08:00
|
|
|
{
|
2017-11-08 09:10:05 +08:00
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> Buffer2OrErr =
|
2015-08-13 08:31:39 +08:00
|
|
|
FileOutputBuffer::create(File2, 8192);
|
2017-11-08 09:10:05 +08:00
|
|
|
ASSERT_NO_ERROR(errorToErrorCode(Buffer2OrErr.takeError()));
|
2015-08-13 08:31:39 +08:00
|
|
|
std::unique_ptr<FileOutputBuffer> &Buffer2 = *Buffer2OrErr;
|
2012-08-01 10:29:50 +08:00
|
|
|
// Fill buffer with special header.
|
|
|
|
memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
|
|
|
|
// Do *not* commit buffer.
|
|
|
|
}
|
2014-01-25 01:20:08 +08:00
|
|
|
// Verify file does not exist (because buffer not committed).
|
2014-09-12 04:30:02 +08:00
|
|
|
ASSERT_EQ(fs::access(Twine(File2), fs::AccessMode::Exist),
|
|
|
|
errc::no_such_file_or_directory);
|
2014-01-11 04:36:42 +08:00
|
|
|
ASSERT_NO_ERROR(fs::remove(File2.str()));
|
2012-08-01 10:29:50 +08:00
|
|
|
|
|
|
|
// TEST 3: Verify sizing down case.
|
|
|
|
SmallString<128> File3(TestDirectory);
|
2017-08-28 14:47:47 +08:00
|
|
|
File3.append("/file3");
|
2012-08-01 10:29:50 +08:00
|
|
|
{
|
2017-11-08 09:10:05 +08:00
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
2015-08-13 08:31:39 +08:00
|
|
|
FileOutputBuffer::create(File3, 8192000);
|
2017-11-08 09:10:05 +08:00
|
|
|
ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError()));
|
2015-08-13 08:31:39 +08:00
|
|
|
std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
|
2012-08-01 10:29:50 +08:00
|
|
|
// Start buffer with special header.
|
|
|
|
memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
|
|
|
|
// Write to end of buffer to verify it is writable.
|
|
|
|
memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
|
2017-11-08 09:50:29 +08:00
|
|
|
ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit()));
|
2012-08-01 10:29:50 +08:00
|
|
|
}
|
2014-06-12 05:53:22 +08:00
|
|
|
|
2012-08-01 10:29:50 +08:00
|
|
|
// Verify file is correct size.
|
|
|
|
uint64_t File3Size;
|
|
|
|
ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
|
2014-12-13 01:35:34 +08:00
|
|
|
ASSERT_EQ(File3Size, 8192000ULL);
|
2014-01-11 04:36:42 +08:00
|
|
|
ASSERT_NO_ERROR(fs::remove(File3.str()));
|
2012-08-01 10:29:50 +08:00
|
|
|
|
|
|
|
// TEST 4: Verify file can be made executable.
|
|
|
|
SmallString<128> File4(TestDirectory);
|
2017-08-28 14:47:47 +08:00
|
|
|
File4.append("/file4");
|
2012-08-01 10:29:50 +08:00
|
|
|
{
|
2017-11-08 09:10:05 +08:00
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
2015-08-13 08:31:39 +08:00
|
|
|
FileOutputBuffer::create(File4, 8192, FileOutputBuffer::F_executable);
|
2017-11-08 09:10:05 +08:00
|
|
|
ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError()));
|
2015-08-13 08:31:39 +08:00
|
|
|
std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
|
2012-08-01 10:29:50 +08:00
|
|
|
// Start buffer with special header.
|
|
|
|
memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
|
|
|
|
// Commit buffer.
|
2017-11-08 09:50:29 +08:00
|
|
|
ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit()));
|
2012-08-01 10:29:50 +08:00
|
|
|
}
|
|
|
|
// Verify file exists and is executable.
|
|
|
|
fs::file_status Status;
|
|
|
|
ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
|
|
|
|
bool IsExecutable = (Status.permissions() & fs::owner_exe);
|
|
|
|
EXPECT_TRUE(IsExecutable);
|
2014-01-11 04:36:42 +08:00
|
|
|
ASSERT_NO_ERROR(fs::remove(File4.str()));
|
2012-08-01 10:29:50 +08:00
|
|
|
|
|
|
|
// Clean up.
|
2014-01-11 04:36:42 +08:00
|
|
|
ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
|
2012-08-01 10:29:50 +08:00
|
|
|
}
|
|
|
|
} // anonymous namespace
|