95 lines · cpp
1//===- raw_pwrite_stream_test.cpp - raw_pwrite_stream tests ---------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/ADT/SmallString.h"10#include "llvm/Config/llvm-config.h"11#include "llvm/Support/FileSystem.h"12#include "llvm/Support/FileUtilities.h"13#include "llvm/Support/raw_ostream.h"14#include "gtest/gtest.h"15 16using namespace llvm;17 18#define ASSERT_NO_ERROR(x) \19 if (std::error_code ASSERT_NO_ERROR_ec = x) { \20 SmallString<128> MessageStorage; \21 raw_svector_ostream Message(MessageStorage); \22 Message << #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 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \26 } else { \27 }28 29namespace {30 31TEST(raw_pwrite_ostreamTest, TestSVector) {32 SmallVector<char, 0> Buffer;33 raw_svector_ostream OS(Buffer);34 OS << "abcd";35 StringRef Test = "test";36 OS.pwrite(Test.data(), Test.size(), 0);37 EXPECT_EQ(Test, OS.str());38 39#ifdef GTEST_HAS_DEATH_TEST40#ifndef NDEBUG41 EXPECT_DEATH(OS.pwrite("12345", 5, 0),42 "We don't support extending the stream");43#endif44#endif45}46 47#ifdef _WIN3248#define setenv(name, var, ignore) _putenv_s(name, var)49#endif50 51TEST(raw_pwrite_ostreamTest, TestFD) {52 SmallString<64> Path;53 int FD;54 55 // If we want to clean up from a death test, we have to remove the file from56 // the parent process. Have the parent create the file, pass it via57 // environment variable to the child, let the child crash, and then remove it58 // in the parent.59 const char *ParentPath = getenv("RAW_PWRITE_TEST_FILE");60 if (ParentPath) {61 Path = ParentPath;62 ASSERT_NO_ERROR(sys::fs::openFileForRead(Path, FD));63 } else {64 ASSERT_NO_ERROR(sys::fs::createTemporaryFile("foo", "bar", FD, Path));65 setenv("RAW_PWRITE_TEST_FILE", Path.c_str(), true);66 }67 FileRemover Cleanup(Path);68 69 raw_fd_ostream OS(FD, true);70 OS << "abcd";71 StringRef Test = "test";72 OS.pwrite(Test.data(), Test.size(), 0);73 OS.pwrite(Test.data(), Test.size(), 0);74 75#ifdef GTEST_HAS_DEATH_TEST76#ifndef NDEBUG77 EXPECT_DEATH(OS.pwrite("12345", 5, 0),78 "We don't support extending the stream");79#endif80#endif81}82 83#ifdef LLVM_ON_UNIX84TEST(raw_pwrite_ostreamTest, TestDevNull) {85 int FD;86 sys::fs::openFileForWrite("/dev/null", FD, sys::fs::CD_OpenExisting);87 raw_fd_ostream OS(FD, true);88 OS << "abcd";89 StringRef Test = "test";90 OS.pwrite(Test.data(), Test.size(), 0);91 OS.pwrite(Test.data(), Test.size(), 0);92}93#endif94}95