tesseract v5.3.3.20231005
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//
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
39using ::testing::Test;
43
44namespace {
45
46class PrematureExitTest : public Test {
47 public:
48 // Returns true if and only if the given file exists.
49 static bool FileExists(const char* filepath) {
50 StatStruct stat;
51 return Stat(filepath, &stat) == 0;
52 }
53
54 protected:
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.
65 bool PrematureExitFileExists() const {
66 return FileExists(premature_exit_file_path_);
67 }
68
69 const char* premature_exit_file_path_;
70};
71
72typedef 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.
79TEST_F(PrematureExitDeathTest, FileExistsDuringExecutionOfDeathTest) {
80 if (*premature_exit_file_path_ == '\0') {
81 return;
82 }
83
85 // If the file exists, crash the process such that the main test
86 // process will catch the (expected) crash and report a success;
87 // otherwise don't crash, which will cause the main test process
88 // to report that the death test has failed.
89 if (PrematureExitFileExists()) {
90 exit(1);
91 }
92 }, "");
93}
94
95// Tests that the premature-exit file exists during the execution of a
96// normal (non-death) test.
97TEST_F(PrematureExitTest, PrematureExitFileExistsDuringTestExecution) {
98 if (*premature_exit_file_path_ == '\0') {
99 return;
100 }
101
102 EXPECT_TRUE(PrematureExitFileExists())
103 << " file " << premature_exit_file_path_
104 << " should exist during test execution, but doesn't.";
105}
106
107} // namespace
108
109int main(int argc, char **argv) {
110 InitGoogleTest(&argc, argv);
111 const int exit_code = RUN_ALL_TESTS();
112
113 // Test that the premature-exit file is deleted upon return from
114 // RUN_ALL_TESTS().
115 const char* const filepath = GetEnv("TEST_PREMATURE_EXIT_FILE");
116 if (filepath != nullptr && *filepath != '\0') {
117 if (PrematureExitTest::FileExists(filepath)) {
118 printf(
119 "File %s shouldn't exist after the test program finishes, but does.",
120 filepath);
121 return 1;
122 }
123 }
124
125 return exit_code;
126}
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2384
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2489
#define EXPECT_TRUE(condition)
Definition: gtest.h:1982
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
int main(int argc, char **argv)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6660
int Stat(const char *path, StatStruct *buf)
Definition: gtest-port.h:2042
const char * GetEnv(const char *name)
Definition: gtest-port.h:2103