gtest_premature_exit_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // Author: wan@google.com (Zhanyong Wan)
  31. //
  32. // Tests that Google Test manipulates the premature-exit-detection
  33. // file correctly.
  34. #include <stdio.h>
  35. #include "gtest/gtest.h"
  36. using ::testing::InitGoogleTest;
  37. using ::testing::Test;
  38. using ::testing::internal::posix::GetEnv;
  39. using ::testing::internal::posix::Stat;
  40. using ::testing::internal::posix::StatStruct;
  41. namespace {
  42. class PrematureExitTest : public Test {
  43. public:
  44. // Returns true iff the given file exists.
  45. static bool FileExists(const char* filepath) {
  46. StatStruct stat;
  47. return Stat(filepath, &stat) == 0;
  48. }
  49. protected:
  50. PrematureExitTest() {
  51. premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
  52. // Normalize NULL to "" for ease of handling.
  53. if (premature_exit_file_path_ == NULL) {
  54. premature_exit_file_path_ = "";
  55. }
  56. }
  57. // Returns true iff the premature-exit file exists.
  58. bool PrematureExitFileExists() const {
  59. return FileExists(premature_exit_file_path_);
  60. }
  61. const char* premature_exit_file_path_;
  62. };
  63. typedef PrematureExitTest PrematureExitDeathTest;
  64. // Tests that:
  65. // - the premature-exit file exists during the execution of a
  66. // death test (EXPECT_DEATH*), and
  67. // - a death test doesn't interfere with the main test process's
  68. // handling of the premature-exit file.
  69. TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
  70. if (*premature_exit_file_path_ == '\0') {
  71. return;
  72. }
  73. EXPECT_DEATH_IF_SUPPORTED({
  74. // If the file exists, crash the process such that the main test
  75. // process will catch the (expected) crash and report a success;
  76. // otherwise don't crash, which will cause the main test process
  77. // to report that the death test has failed.
  78. if (PrematureExitFileExists()) {
  79. exit(1);
  80. }
  81. }, "");
  82. }
  83. // Tests that the premature-exit file exists during the execution of a
  84. // normal (non-death) test.
  85. TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
  86. if (*premature_exit_file_path_ == '\0') {
  87. return;
  88. }
  89. EXPECT_TRUE(PrematureExitFileExists())
  90. << " file " << premature_exit_file_path_
  91. << " should exist during test execution, but doesn't.";
  92. }
  93. } // namespace
  94. int main(int argc, char **argv) {
  95. InitGoogleTest(&argc, argv);
  96. const int exit_code = RUN_ALL_TESTS();
  97. // Test that the premature-exit file is deleted upon return from
  98. // RUN_ALL_TESTS().
  99. const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
  100. if (filepath != NULL && *filepath != '\0') {
  101. if (PrematureExitTest::FileExists(filepath)) {
  102. printf(
  103. "File %s shouldn't exist after the test program finishes, but does.",
  104. filepath);
  105. return 1;
  106. }
  107. }
  108. return exit_code;
  109. }