1 //===- raw_pwrite_stream_test.cpp - raw_pwrite_stream 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 10 #include "llvm/ADT/SmallString.h" 11 #include "llvm/Config/llvm-config.h" 12 #include "llvm/Support/FileSystem.h" 13 #include "llvm/Support/FileUtilities.h" 14 #include "llvm/Support/raw_ostream.h" 15 #include "gtest/gtest.h" 16 17 using namespace llvm; 18 19 #define ASSERT_NO_ERROR(x) \ 20 if (std::error_code ASSERT_NO_ERROR_ec = x) { \ 21 SmallString<128> MessageStorage; \ 22 raw_svector_ostream Message(MessageStorage); \ 23 Message << #x ": did not return errc::success.\n" \ 24 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ 25 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ 26 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ 27 } else { \ 28 } 29 30 namespace { 31 32 TEST(raw_pwrite_ostreamTest, TestSVector) { 33 SmallVector<char, 0> Buffer; 34 raw_svector_ostream OS(Buffer); 35 OS << "abcd"; 36 StringRef Test = "test"; 37 OS.pwrite(Test.data(), Test.size(), 0); 38 EXPECT_EQ(Test, OS.str()); 39 40 #ifdef GTEST_HAS_DEATH_TEST 41 #ifndef NDEBUG 42 EXPECT_DEATH(OS.pwrite("12345", 5, 0), 43 "We don't support extending the stream"); 44 #endif 45 #endif 46 } 47 48 #ifdef _WIN32 49 #define setenv(name, var, ignore) _putenv_s(name, var) 50 #endif 51 52 TEST(raw_pwrite_ostreamTest, TestFD) { 53 SmallString<64> Path; 54 int FD; 55 56 // If we want to clean up from a death test, we have to remove the file from 57 // the parent process. Have the parent create the file, pass it via 58 // environment variable to the child, let the child crash, and then remove it 59 // in the parent. 60 const char *ParentPath = getenv("RAW_PWRITE_TEST_FILE"); 61 if (ParentPath) { 62 Path = ParentPath; 63 ASSERT_NO_ERROR(sys::fs::openFileForRead(Path, FD)); 64 } else { 65 ASSERT_NO_ERROR(sys::fs::createTemporaryFile("foo", "bar", FD, Path)); 66 setenv("RAW_PWRITE_TEST_FILE", Path.c_str(), true); 67 } 68 FileRemover Cleanup(Path); 69 70 raw_fd_ostream OS(FD, true); 71 OS << "abcd"; 72 StringRef Test = "test"; 73 OS.pwrite(Test.data(), Test.size(), 0); 74 OS.pwrite(Test.data(), Test.size(), 0); 75 76 #ifdef GTEST_HAS_DEATH_TEST 77 #ifndef NDEBUG 78 EXPECT_DEATH(OS.pwrite("12345", 5, 0), 79 "We don't support extending the stream"); 80 #endif 81 #endif 82 } 83 84 #ifdef LLVM_ON_UNIX 85 TEST(raw_pwrite_ostreamTest, TestDevNull) { 86 int FD; 87 sys::fs::openFileForWrite("/dev/null", FD, sys::fs::CD_OpenExisting); 88 raw_fd_ostream OS(FD, true); 89 OS << "abcd"; 90 StringRef Test = "test"; 91 OS.pwrite(Test.data(), Test.size(), 0); 92 OS.pwrite(Test.data(), Test.size(), 0); 93 } 94 #endif 95 } 96