raw_pwrite_stream_test.cpp 3.0 KB

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