gtest_premature_exit_test.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // Is the TEST_PREMATURE_EXIT_FILE environment variable expected to be
  43. // set?
  44. const bool kTestPrematureExitFileEnvVarShouldBeSet = false;
  45. class PrematureExitTest : public Test {
  46. public:
  47. // Returns true iff the given file exists.
  48. static bool FileExists(const char* filepath) {
  49. StatStruct stat;
  50. return Stat(filepath, &stat) == 0;
  51. }
  52. protected:
  53. PrematureExitTest() {
  54. premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
  55. // Normalize NULL to "" for ease of handling.
  56. if (premature_exit_file_path_ == NULL) {
  57. premature_exit_file_path_ = "";
  58. }
  59. }
  60. // Returns true iff the premature-exit file exists.
  61. bool PrematureExitFileExists() const {
  62. return FileExists(premature_exit_file_path_);
  63. }
  64. const char* premature_exit_file_path_;
  65. };
  66. typedef PrematureExitTest PrematureExitDeathTest;
  67. // Tests that:
  68. // - the premature-exit file exists during the execution of a
  69. // death test (EXPECT_DEATH*), and
  70. // - a death test doesn't interfere with the main test process's
  71. // handling of the premature-exit file.
  72. TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
  73. if (*premature_exit_file_path_ == '\0') {
  74. return;
  75. }
  76. EXPECT_DEATH_IF_SUPPORTED({
  77. // If the file exists, crash the process such that the main test
  78. // process will catch the (expected) crash and report a success;
  79. // otherwise don't crash, which will cause the main test process
  80. // to report that the death test has failed.
  81. if (PrematureExitFileExists()) {
  82. exit(1);
  83. }
  84. }, "");
  85. }
  86. // Tests that TEST_PREMATURE_EXIT_FILE is set where it's expected to
  87. // be set.
  88. TEST_F(PrematureExitTest, TestPrematureExitFileEnvVarIsSet) {
  89. if (kTestPrematureExitFileEnvVarShouldBeSet) {
  90. const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
  91. ASSERT_TRUE(filepath != NULL);
  92. ASSERT_NE(*filepath, '\0');
  93. }
  94. }
  95. // Tests that the premature-exit file exists during the execution of a
  96. // normal (non-death) test.
  97. TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
  98. if (*premature_exit_file_path_ == '\0') {
  99. return;
  100. }
  101. EXPECT_TRUE(PrematureExitFileExists())
  102. << " file " << premature_exit_file_path_
  103. << " should exist during test execution, but doesn't.";
  104. }
  105. } // namespace
  106. int main(int argc, char **argv) {
  107. InitGoogleTest(&argc, argv);
  108. const int exit_code = RUN_ALL_TESTS();
  109. // Test that the premature-exit file is deleted upon return from
  110. // RUN_ALL_TESTS().
  111. const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
  112. if (filepath != NULL && *filepath != '\0') {
  113. if (PrematureExitTest::FileExists(filepath)) {
  114. printf(
  115. "File %s shouldn't exist after the test program finishes, but does.",
  116. filepath);
  117. return 1;
  118. }
  119. }
  120. return exit_code;
  121. }