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 // 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.
54  static bool FileExists(const char* filepath) {
56  return Stat(filepath, &stat) == 0;
57  }
58 
59  protected:
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.
70  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.
84 TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
85  if (*premature_exit_file_path_ == '\0') {
86  return;
87  }
88 
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.
102 TEST_F(PrematureExitTest, TestPrematureExitFileEnvVarIsSet) {
104  if (kTestPrematureExitFileEnvVarShouldBeSet) {
106  const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
107  ASSERT_TRUE(filepath != NULL);
108  ASSERT_NE(*filepath, '\0');
109  }
110 }
111 
112 // Tests that the premature-exit file exists during the execution of a
113 // normal (non-death) test.
114 TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
115  if (*premature_exit_file_path_ == '\0') {
116  return;
117  }
118 
119  EXPECT_TRUE(PrematureExitFileExists())
120  << " file " << premature_exit_file_path_
121  << " should exist during test execution, but doesn't.";
122 }
123 
124 } // namespace
125 
126 int main(int argc, char **argv) {
127  InitGoogleTest(&argc, argv);
128  const int exit_code = RUN_ALL_TESTS();
129 
130  // Test that the premature-exit file is deleted upon return from
131  // RUN_ALL_TESTS().
132  const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
133  if (filepath != NULL && *filepath != '\0') {
134  if (PrematureExitTest::FileExists(filepath)) {
135  printf(
136  "File %s shouldn't exist after the test program finishes, but does.",
137  filepath);
138  return 1;
139  }
140  }
141 
142  return exit_code;
143 }
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
int stat(const char *path, struct stat *buffer)
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
Definition: gtest-port.h:929
int main(int argc, char **argv)
#define ASSERT_NE(val1, val2)
Definition: gtest.h:2053
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2309
int Stat(const char *path, StatStruct *buf)
Definition: gtest-port.h:2284
if(strcmp(arg,"1305")!=0)
Definition: unit1305.c:127
static char * GetEnv(const char *variable)
Definition: getenv.c:31
#define ASSERT_TRUE(condition)
Definition: gtest.h:1958
#define printf
Definition: curl_printf.h:40
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2325
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5292
#define EXPECT_TRUE(condition)
Definition: gtest.h:1952
#define GTEST_INTENTIONAL_CONST_COND_POP_()
Definition: gtest-port.h:931


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:13