1b89a7cc2SEnji Cooper // Copyright 2013, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper 
30b89a7cc2SEnji Cooper //
31b89a7cc2SEnji Cooper // Tests that Google Test manipulates the premature-exit-detection
32b89a7cc2SEnji Cooper // file correctly.
33b89a7cc2SEnji Cooper 
34b89a7cc2SEnji Cooper #include <stdio.h>
35b89a7cc2SEnji Cooper 
36b89a7cc2SEnji Cooper #include "gtest/gtest.h"
37b89a7cc2SEnji Cooper 
38b89a7cc2SEnji Cooper using ::testing::InitGoogleTest;
39b89a7cc2SEnji Cooper using ::testing::Test;
40b89a7cc2SEnji Cooper using ::testing::internal::posix::GetEnv;
41b89a7cc2SEnji Cooper using ::testing::internal::posix::Stat;
42b89a7cc2SEnji Cooper using ::testing::internal::posix::StatStruct;
43b89a7cc2SEnji Cooper 
44b89a7cc2SEnji Cooper namespace {
45b89a7cc2SEnji Cooper 
46b89a7cc2SEnji Cooper class PrematureExitTest : public Test {
47b89a7cc2SEnji Cooper  public:
4828f6c2f2SEnji Cooper   // Returns true if and only if the given file exists.
FileExists(const char * filepath)49b89a7cc2SEnji Cooper   static bool FileExists(const char* filepath) {
50b89a7cc2SEnji Cooper     StatStruct stat;
51b89a7cc2SEnji Cooper     return Stat(filepath, &stat) == 0;
52b89a7cc2SEnji Cooper   }
53b89a7cc2SEnji Cooper 
54b89a7cc2SEnji Cooper  protected:
PrematureExitTest()55b89a7cc2SEnji Cooper   PrematureExitTest() {
56b89a7cc2SEnji Cooper     premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
57b89a7cc2SEnji Cooper 
58b89a7cc2SEnji Cooper     // Normalize NULL to "" for ease of handling.
5928f6c2f2SEnji Cooper     if (premature_exit_file_path_ == nullptr) {
60b89a7cc2SEnji Cooper       premature_exit_file_path_ = "";
61b89a7cc2SEnji Cooper     }
62b89a7cc2SEnji Cooper   }
63b89a7cc2SEnji Cooper 
6428f6c2f2SEnji Cooper   // Returns true if and only if the premature-exit file exists.
PrematureExitFileExists() const65b89a7cc2SEnji Cooper   bool PrematureExitFileExists() const {
66b89a7cc2SEnji Cooper     return FileExists(premature_exit_file_path_);
67b89a7cc2SEnji Cooper   }
68b89a7cc2SEnji Cooper 
69b89a7cc2SEnji Cooper   const char* premature_exit_file_path_;
70b89a7cc2SEnji Cooper };
71b89a7cc2SEnji Cooper 
72b89a7cc2SEnji Cooper typedef PrematureExitTest PrematureExitDeathTest;
73b89a7cc2SEnji Cooper 
74b89a7cc2SEnji Cooper // Tests that:
75b89a7cc2SEnji Cooper //   - the premature-exit file exists during the execution of a
76b89a7cc2SEnji Cooper //     death test (EXPECT_DEATH*), and
77b89a7cc2SEnji Cooper //   - a death test doesn't interfere with the main test process's
78b89a7cc2SEnji Cooper //     handling of the premature-exit file.
TEST_F(PrematureExitDeathTest,FileExistsDuringExecutionOfDeathTest)79b89a7cc2SEnji Cooper TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
80b89a7cc2SEnji Cooper   if (*premature_exit_file_path_ == '\0') {
81b89a7cc2SEnji Cooper     return;
82b89a7cc2SEnji Cooper   }
83b89a7cc2SEnji Cooper 
8428f6c2f2SEnji Cooper   EXPECT_DEATH_IF_SUPPORTED(
8528f6c2f2SEnji Cooper       {
86b89a7cc2SEnji Cooper         // If the file exists, crash the process such that the main test
87b89a7cc2SEnji Cooper         // process will catch the (expected) crash and report a success;
88b89a7cc2SEnji Cooper         // otherwise don't crash, which will cause the main test process
89b89a7cc2SEnji Cooper         // to report that the death test has failed.
90b89a7cc2SEnji Cooper         if (PrematureExitFileExists()) {
91b89a7cc2SEnji Cooper           exit(1);
92b89a7cc2SEnji Cooper         }
9328f6c2f2SEnji Cooper       },
9428f6c2f2SEnji Cooper       "");
95b89a7cc2SEnji Cooper }
96b89a7cc2SEnji Cooper 
97b89a7cc2SEnji Cooper // Tests that the premature-exit file exists during the execution of a
98b89a7cc2SEnji Cooper // normal (non-death) test.
TEST_F(PrematureExitTest,PrematureExitFileExistsDuringTestExecution)99b89a7cc2SEnji Cooper TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
100b89a7cc2SEnji Cooper   if (*premature_exit_file_path_ == '\0') {
101b89a7cc2SEnji Cooper     return;
102b89a7cc2SEnji Cooper   }
103b89a7cc2SEnji Cooper 
104b89a7cc2SEnji Cooper   EXPECT_TRUE(PrematureExitFileExists())
105b89a7cc2SEnji Cooper       << " file " << premature_exit_file_path_
106b89a7cc2SEnji Cooper       << " should exist during test execution, but doesn't.";
107b89a7cc2SEnji Cooper }
108b89a7cc2SEnji Cooper 
109b89a7cc2SEnji Cooper }  // namespace
110b89a7cc2SEnji Cooper 
main(int argc,char ** argv)111b89a7cc2SEnji Cooper int main(int argc, char** argv) {
112b89a7cc2SEnji Cooper   InitGoogleTest(&argc, argv);
113b89a7cc2SEnji Cooper   const int exit_code = RUN_ALL_TESTS();
114b89a7cc2SEnji Cooper 
115b89a7cc2SEnji Cooper   // Test that the premature-exit file is deleted upon return from
116b89a7cc2SEnji Cooper   // RUN_ALL_TESTS().
117b89a7cc2SEnji Cooper   const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
11828f6c2f2SEnji Cooper   if (filepath != nullptr && *filepath != '\0') {
119b89a7cc2SEnji Cooper     if (PrematureExitTest::FileExists(filepath)) {
120b89a7cc2SEnji Cooper       printf(
121b89a7cc2SEnji Cooper           "File %s shouldn't exist after the test program finishes, but does.",
122b89a7cc2SEnji Cooper           filepath);
123b89a7cc2SEnji Cooper       return 1;
124b89a7cc2SEnji Cooper     }
125b89a7cc2SEnji Cooper   }
126b89a7cc2SEnji Cooper 
127b89a7cc2SEnji Cooper   return exit_code;
128b89a7cc2SEnji Cooper }
129