proxygen
gtest_premature_exit_test.cc
Go to the documentation of this file.
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 
40 using ::testing::Test;
44 
45 namespace {
46 
47 class PrematureExitTest : public Test {
48  public:
49  // Returns true iff the given file exists.
50  static bool FileExists(const char* filepath) {
51  StatStruct stat;
52  return Stat(filepath, &stat) == 0;
53  }
54 
55  protected:
56  PrematureExitTest() {
57  premature_exit_file_path_ = GetEnv("TEST_PREMATURE_EXIT_FILE");
58 
59  // Normalize NULL to "" for ease of handling.
60  if (premature_exit_file_path_ == NULL) {
61  premature_exit_file_path_ = "";
62  }
63  }
64 
65  // Returns true iff the premature-exit file exists.
66  bool PrematureExitFileExists() const {
67  return FileExists(premature_exit_file_path_);
68  }
69 
70  const char* premature_exit_file_path_;
71 };
72 
73 typedef PrematureExitTest PrematureExitDeathTest;
74 
75 // Tests that:
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') {
82  return;
83  }
84 
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 // 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') {
100  return;
101  }
102 
103  EXPECT_TRUE(PrematureExitFileExists())
104  << " file " << premature_exit_file_path_
105  << " should exist during test execution, but doesn't.";
106 }
107 
108 } // namespace
109 
110 int main(int argc, char **argv) {
111  InitGoogleTest(&argc, argv);
112  const int exit_code = RUN_ALL_TESTS();
113 
114  // Test that the premature-exit file is deleted upon return from
115  // RUN_ALL_TESTS().
116  const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
117  if (filepath != NULL && *filepath != '\0') {
118  if (PrematureExitTest::FileExists(filepath)) {
119  printf(
120  "File %s shouldn't exist after the test program finishes, but does.",
121  filepath);
122  return 1;
123  }
124  }
125 
126  return exit_code;
127 }
int main(int argc, char **argv)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
int Stat(const char *path, StatStruct *buf)
Definition: gtest-port.h:2342
char ** argv
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
const char * GetEnv(const char *name)
Definition: gtest-port.h:2387
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2216