1 // Copyright 2013, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Tests that Google Test manipulates the premature-exit-detection
36 #include "gtest/gtest.h"
38 using ::testing::InitGoogleTest;
39 using ::testing::Test;
40 using ::testing::internal::posix::GetEnv;
41 using ::testing::internal::posix::Stat;
42 using ::testing::internal::posix::StatStruct;
46 class PrematureExitTest : public Test {
48 // Returns true iff the given file exists.
49 static bool FileExists(const char* filepath) {
51 return Stat(filepath, &stat) == 0;
56 premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
58 // Normalize NULL to "" for ease of handling.
59 if (premature_exit_file_path_ == NULL) {
60 premature_exit_file_path_ = "";
64 // Returns true iff the premature-exit file exists.
65 bool PrematureExitFileExists() const {
66 return FileExists(premature_exit_file_path_);
69 const char* premature_exit_file_path_;
72 typedef PrematureExitTest PrematureExitDeathTest;
75 // - the premature-exit file exists during the execution of a
76 // death test (EXPECT_DEATH*), and
77 // - a death test doesn't interfere with the main test process's
78 // handling of the premature-exit file.
79 TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
80 if (*premature_exit_file_path_ == '\0') {
84 EXPECT_DEATH_IF_SUPPORTED({
85 // If the file exists, crash the process such that the main test
86 // process will catch the (expected) crash and report a success;
87 // otherwise don't crash, which will cause the main test process
88 // to report that the death test has failed.
89 if (PrematureExitFileExists()) {
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') {
102 EXPECT_TRUE(PrematureExitFileExists())
103 << " file " << premature_exit_file_path_
104 << " should exist during test execution, but doesn't.";
109 int main(int argc, char **argv) {
110 InitGoogleTest(&argc, argv);
111 const int exit_code = RUN_ALL_TESTS();
113 // Test that the premature-exit file is deleted upon return from
115 const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
116 if (filepath != NULL && *filepath != '\0') {
117 if (PrematureExitTest::FileExists(filepath)) {
119 "File %s shouldn't exist after the test program finishes, but does.",