2015-04-14 17:00:34 +02:00
|
|
|
//===- raw_pwrite_stream_test.cpp - raw_pwrite_stream tests ---------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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
|
2015-04-14 17:00:34 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2018-04-30 16:59:11 +02:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2015-04-29 16:53:25 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-09-02 02:51:34 +02:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2015-04-14 17:00:34 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-06 13:06:56 +02:00
|
|
|
#include "gtest/gtest.h"
|
2015-04-14 17:00:34 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2019-08-14 15:59:04 +02:00
|
|
|
#define ASSERT_NO_ERROR(x) \
|
|
|
|
if (std::error_code ASSERT_NO_ERROR_ec = x) { \
|
|
|
|
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()); \
|
|
|
|
} else { \
|
|
|
|
}
|
2016-09-02 02:51:34 +02:00
|
|
|
|
2015-04-14 17:00:34 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(raw_pwrite_ostreamTest, TestSVector) {
|
2015-04-20 15:04:30 +02:00
|
|
|
SmallVector<char, 0> Buffer;
|
2015-04-14 17:00:34 +02:00
|
|
|
raw_svector_ostream OS(Buffer);
|
2015-04-20 15:04:30 +02:00
|
|
|
OS << "abcd";
|
2015-04-14 17:00:34 +02:00
|
|
|
StringRef Test = "test";
|
|
|
|
OS.pwrite(Test.data(), Test.size(), 0);
|
|
|
|
EXPECT_EQ(Test, OS.str());
|
2015-04-20 15:04:30 +02:00
|
|
|
|
|
|
|
#ifdef GTEST_HAS_DEATH_TEST
|
|
|
|
#ifndef NDEBUG
|
|
|
|
EXPECT_DEATH(OS.pwrite("12345", 5, 0),
|
|
|
|
"We don't support extending the stream");
|
|
|
|
#endif
|
|
|
|
#endif
|
2015-04-14 17:00:34 +02:00
|
|
|
}
|
2015-04-29 16:53:25 +02:00
|
|
|
|
2016-09-02 03:20:18 +02:00
|
|
|
#ifdef _WIN32
|
2016-09-02 02:51:34 +02:00
|
|
|
#define setenv(name, var, ignore) _putenv_s(name, var)
|
|
|
|
#endif
|
|
|
|
|
2015-04-29 16:53:25 +02:00
|
|
|
TEST(raw_pwrite_ostreamTest, TestFD) {
|
|
|
|
SmallString<64> Path;
|
|
|
|
int FD;
|
2016-09-02 02:51:34 +02:00
|
|
|
|
|
|
|
// If we want to clean up from a death test, we have to remove the file from
|
|
|
|
// the parent process. Have the parent create the file, pass it via
|
|
|
|
// environment variable to the child, let the child crash, and then remove it
|
|
|
|
// in the parent.
|
|
|
|
const char *ParentPath = getenv("RAW_PWRITE_TEST_FILE");
|
|
|
|
if (ParentPath) {
|
|
|
|
Path = ParentPath;
|
|
|
|
ASSERT_NO_ERROR(sys::fs::openFileForRead(Path, FD));
|
|
|
|
} else {
|
|
|
|
ASSERT_NO_ERROR(sys::fs::createTemporaryFile("foo", "bar", FD, Path));
|
|
|
|
setenv("RAW_PWRITE_TEST_FILE", Path.c_str(), true);
|
|
|
|
}
|
|
|
|
FileRemover Cleanup(Path);
|
|
|
|
|
2015-04-29 16:53:25 +02:00
|
|
|
raw_fd_ostream OS(FD, true);
|
|
|
|
OS << "abcd";
|
|
|
|
StringRef Test = "test";
|
|
|
|
OS.pwrite(Test.data(), Test.size(), 0);
|
|
|
|
OS.pwrite(Test.data(), Test.size(), 0);
|
|
|
|
|
|
|
|
#ifdef GTEST_HAS_DEATH_TEST
|
|
|
|
#ifndef NDEBUG
|
|
|
|
EXPECT_DEATH(OS.pwrite("12345", 5, 0),
|
|
|
|
"We don't support extending the stream");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:54:11 +02:00
|
|
|
#ifdef LLVM_ON_UNIX
|
2015-04-29 16:53:25 +02:00
|
|
|
TEST(raw_pwrite_ostreamTest, TestDevNull) {
|
|
|
|
int FD;
|
2018-06-07 21:58:58 +02:00
|
|
|
sys::fs::openFileForWrite("/dev/null", FD, sys::fs::CD_OpenExisting);
|
2015-04-29 16:53:25 +02:00
|
|
|
raw_fd_ostream OS(FD, true);
|
|
|
|
OS << "abcd";
|
|
|
|
StringRef Test = "test";
|
|
|
|
OS.pwrite(Test.data(), Test.size(), 0);
|
|
|
|
OS.pwrite(Test.data(), Test.size(), 0);
|
|
|
|
}
|
2015-04-29 18:54:11 +02:00
|
|
|
#endif
|
2015-04-14 17:00:34 +02:00
|
|
|
}
|