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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Tests that Google Test manipulates the premature-exit-detection
37 #include "gtest/gtest.h"
39 using ::testing::InitGoogleTest
;
40 using ::testing::Test
;
41 using ::testing::internal::posix::GetEnv
;
42 using ::testing::internal::posix::Stat
;
43 using ::testing::internal::posix::StatStruct
;
47 class PrematureExitTest
: public Test
{
49 // Returns true iff the given file exists.
50 static bool FileExists(const char* filepath
) {
52 return Stat(filepath
, &stat
) == 0;
57 premature_exit_file_path_
= GetEnv("TEST_PREMATURE_EXIT_FILE");
59 // Normalize NULL to "" for ease of handling.
60 if (premature_exit_file_path_
== NULL
) {
61 premature_exit_file_path_
= "";
65 // Returns true iff the premature-exit file exists.
66 bool PrematureExitFileExists() const {
67 return FileExists(premature_exit_file_path_
);
70 const char* premature_exit_file_path_
;
73 typedef PrematureExitTest PrematureExitDeathTest
;
76 // - the premature-exit file exists during the execution of a
77 // death test (EXPECT_DEATH*), and
78 // - a death test doesn't interfere with the main test process's
79 // handling of the premature-exit file.
80 TEST_F(PrematureExitDeathTest
, FileExistsDuringExecutionOfDeathTest
) {
81 if (*premature_exit_file_path_
== '\0') {
85 EXPECT_DEATH_IF_SUPPORTED({
86 // If the file exists, crash the process such that the main test
87 // process will catch the (expected) crash and report a success;
88 // otherwise don't crash, which will cause the main test process
89 // to report that the death test has failed.
90 if (PrematureExitFileExists()) {
96 // Tests that the premature-exit file exists during the execution of a
97 // normal (non-death) test.
98 TEST_F(PrematureExitTest
, PrematureExitFileExistsDuringTestExecution
) {
99 if (*premature_exit_file_path_
== '\0') {
103 EXPECT_TRUE(PrematureExitFileExists())
104 << " file " << premature_exit_file_path_
105 << " should exist during test execution, but doesn't.";
110 int main(int argc
, char **argv
) {
111 InitGoogleTest(&argc
, argv
);
112 const int exit_code
= RUN_ALL_TESTS();
114 // Test that the premature-exit file is deleted upon return from
116 const char* const filepath
= GetEnv("TEST_PREMATURE_EXIT_FILE");
117 if (filepath
!= NULL
&& *filepath
!= '\0') {
118 if (PrematureExitTest::FileExists(filepath
)) {
120 "File %s shouldn't exist after the test program finishes, but does.",