You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.2 KiB

  1. // Copyright 2013, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Tests that Google Test manipulates the premature-exit-detection
  31. // file correctly.
  32. #include <stdio.h>
  33. #include "gtest/gtest.h"
  34. using ::testing::InitGoogleTest;
  35. using ::testing::Test;
  36. using ::testing::internal::posix::GetEnv;
  37. using ::testing::internal::posix::Stat;
  38. using ::testing::internal::posix::StatStruct;
  39. namespace {
  40. class PrematureExitTest : public Test {
  41. public:
  42. // Returns true if and only if the given file exists.
  43. static bool FileExists(const char* filepath) {
  44. StatStruct stat;
  45. return Stat(filepath, &stat) == 0;
  46. }
  47. protected:
  48. PrematureExitTest() {
  49. premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
  50. // Normalize NULL to "" for ease of handling.
  51. if (premature_exit_file_path_ == nullptr) {
  52. premature_exit_file_path_ = "";
  53. }
  54. }
  55. // Returns true if and only if the premature-exit file exists.
  56. bool PrematureExitFileExists() const {
  57. return FileExists(premature_exit_file_path_);
  58. }
  59. const char* premature_exit_file_path_;
  60. };
  61. typedef PrematureExitTest PrematureExitDeathTest;
  62. // Tests that:
  63. // - the premature-exit file exists during the execution of a
  64. // death test (EXPECT_DEATH*), and
  65. // - a death test doesn't interfere with the main test process's
  66. // handling of the premature-exit file.
  67. TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
  68. if (*premature_exit_file_path_ == '\0') {
  69. return;
  70. }
  71. EXPECT_DEATH_IF_SUPPORTED({
  72. // If the file exists, crash the process such that the main test
  73. // process will catch the (expected) crash and report a success;
  74. // otherwise don't crash, which will cause the main test process
  75. // to report that the death test has failed.
  76. if (PrematureExitFileExists()) {
  77. exit(1);
  78. }
  79. }, "");
  80. }
  81. // Tests that the premature-exit file exists during the execution of a
  82. // normal (non-death) test.
  83. TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
  84. if (*premature_exit_file_path_ == '\0') {
  85. return;
  86. }
  87. EXPECT_TRUE(PrematureExitFileExists())
  88. << " file " << premature_exit_file_path_
  89. << " should exist during test execution, but doesn't.";
  90. }
  91. } // namespace
  92. int main(int argc, char **argv) {
  93. InitGoogleTest(&argc, argv);
  94. const int exit_code = RUN_ALL_TESTS();
  95. // Test that the premature-exit file is deleted upon return from
  96. // RUN_ALL_TESTS().
  97. const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
  98. if (filepath != nullptr && *filepath != '\0') {
  99. if (PrematureExitTest::FileExists(filepath)) {
  100. printf(
  101. "File %s shouldn't exist after the test program finishes, but does.",
  102. filepath);
  103. return 1;
  104. }
  105. }
  106. return exit_code;
  107. }