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-exception 6 // 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 16 using 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 29 namespace { 30 31 TEST(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_TEST 40 #ifndef NDEBUG 41 EXPECT_DEATH(OS.pwrite("12345", 5, 0), 42 "We don't support extending the stream"); 43 #endif 44 #endif 45 } 46 47 #ifdef _WIN32 48 #define setenv(name, var, ignore) _putenv_s(name, var) 49 #endif 50 51 TEST(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 from 56 // the parent process. Have the parent create the file, pass it via 57 // environment variable to the child, let the child crash, and then remove it 58 // 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_TEST 76 #ifndef NDEBUG 77 EXPECT_DEATH(OS.pwrite("12345", 5, 0), 78 "We don't support extending the stream"); 79 #endif 80 #endif 81 } 82 83 #ifdef LLVM_ON_UNIX 84 TEST(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 #endif 94 } 95