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