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 //
31 // Tests that Google Test manipulates the premature-exit-detection
32 // file correctly.
33
34 #include <stdio.h>
35
36 #include "gtest/gtest.h"
37
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;
43
44 namespace {
45
46 class PrematureExitTest : public Test {
47 public:
48 // Returns true if and only if the given file exists.
FileExists(const char * filepath)49 static bool FileExists(const char* filepath) {
50 StatStruct stat;
51 return Stat(filepath, &stat) == 0;
52 }
53
54 protected:
PrematureExitTest()55 PrematureExitTest() {
56 premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
57
58 // Normalize NULL to "" for ease of handling.
59 if (premature_exit_file_path_ == nullptr) {
60 premature_exit_file_path_ = "";
61 }
62 }
63
64 // Returns true if and only if the premature-exit file exists.
PrematureExitFileExists() const65 bool PrematureExitFileExists() const {
66 return FileExists(premature_exit_file_path_);
67 }
68
69 const char* premature_exit_file_path_;
70 };
71
72 typedef PrematureExitTest PrematureExitDeathTest;
73
74 // Tests that:
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.
TEST_F(PrematureExitDeathTest,FileExistsDuringExecutionOfDeathTest)79 TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
80 if (*premature_exit_file_path_ == '\0') {
81 return;
82 }
83
84 EXPECT_DEATH_IF_SUPPORTED(
85 {
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()) {
91 exit(1);
92 }
93 },
94 "");
95 }
96
97 // Tests that the premature-exit file exists during the execution of a
98 // normal (non-death) test.
TEST_F(PrematureExitTest,PrematureExitFileExistsDuringTestExecution)99 TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
100 if (*premature_exit_file_path_ == '\0') {
101 return;
102 }
103
104 EXPECT_TRUE(PrematureExitFileExists())
105 << " file " << premature_exit_file_path_
106 << " should exist during test execution, but doesn't.";
107 }
108
109 } // namespace
110
main(int argc,char ** argv)111 int main(int argc, char** argv) {
112 InitGoogleTest(&argc, argv);
113 const int exit_code = RUN_ALL_TESTS();
114
115 // Test that the premature-exit file is deleted upon return from
116 // RUN_ALL_TESTS().
117 const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
118 if (filepath != nullptr && *filepath != '\0') {
119 if (PrematureExitTest::FileExists(filepath)) {
120 printf(
121 "File %s shouldn't exist after the test program finishes, but does.",
122 filepath);
123 return 1;
124 }
125 }
126
127 return exit_code;
128 }
129