1
// Copyright 2005, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
32
// The Google C++ Testing Framework (Google Test)
34
// This header file defines internal utilities needed for implementing
35
// death tests. They are subject to change without notice.
37
#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
38
#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
40
#include <gtest/internal/gtest-internal.h>
45
GTEST_DECLARE_string_(internal_run_death_test);
47
// Names of the flags (needed for parsing Google Test flags).
48
const char kDeathTestStyleFlag[] = "death_test_style";
49
const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
51
#ifdef GTEST_HAS_DEATH_TEST
53
// DeathTest is a class that hides much of the complexity of the
54
// GTEST_DEATH_TEST_ macro. It is abstract; its static Create method
55
// returns a concrete class that depends on the prevailing death test
56
// style, as defined by the --gtest_death_test_style and/or
57
// --gtest_internal_run_death_test flags.
59
// In describing the results of death tests, these terms are used with
60
// the corresponding definitions:
62
// exit status: The integer exit information in the format specified
64
// exit code: The integer code passed to exit(3), _exit(2), or
65
// returned from main()
68
// Create returns false if there was an error determining the
69
// appropriate action to take for the current death test; for example,
70
// if the gtest_death_test_style flag is set to an invalid value.
71
// The LastMessage method will return a more detailed message in that
72
// case. Otherwise, the DeathTest pointer pointed to by the "test"
73
// argument is set. If the death test should be skipped, the pointer
74
// is set to NULL; otherwise, it is set to the address of a new concrete
75
// DeathTest object that controls the execution of the current test.
76
static bool Create(const char* statement, const RE* regex,
77
const char* file, int line, DeathTest** test);
79
virtual ~DeathTest() { }
81
// A helper class that aborts a death test when it's deleted.
82
class ReturnSentinel {
84
explicit ReturnSentinel(DeathTest* test) : test_(test) { }
85
~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
87
DeathTest* const test_;
88
GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);
89
} GTEST_ATTRIBUTE_UNUSED_;
91
// An enumeration of possible roles that may be taken when a death
92
// test is encountered. EXECUTE means that the death test logic should
93
// be executed immediately. OVERSEE means that the program should prepare
94
// the appropriate environment for a child process to execute the death
95
// test, then wait for it to complete.
96
enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
98
// An enumeration of the two reasons that a test might be aborted.
99
enum AbortReason { TEST_ENCOUNTERED_RETURN_STATEMENT, TEST_DID_NOT_DIE };
101
// Assumes one of the above roles.
102
virtual TestRole AssumeRole() = 0;
104
// Waits for the death test to finish and returns its status.
105
virtual int Wait() = 0;
107
// Returns true if the death test passed; that is, the test process
108
// exited during the test, its exit status matches a user-supplied
109
// predicate, and its stderr output matches a user-supplied regular
111
// The user-supplied predicate may be a macro expression rather
112
// than a function pointer or functor, or else Wait and Passed could
114
virtual bool Passed(bool exit_status_ok) = 0;
116
// Signals that the death test did not die as expected.
117
virtual void Abort(AbortReason reason) = 0;
119
// Returns a human-readable outcome message regarding the outcome of
120
// the last death test.
121
static const char* LastMessage();
124
GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest);
127
// Factory interface for death tests. May be mocked out for testing.
128
class DeathTestFactory {
130
virtual ~DeathTestFactory() { }
131
virtual bool Create(const char* statement, const RE* regex,
132
const char* file, int line, DeathTest** test) = 0;
135
// A concrete DeathTestFactory implementation for normal use.
136
class DefaultDeathTestFactory : public DeathTestFactory {
138
virtual bool Create(const char* statement, const RE* regex,
139
const char* file, int line, DeathTest** test);
142
// Returns true if exit_status describes a process that was terminated
143
// by a signal, or exited normally with a nonzero exit code.
144
bool ExitedUnsuccessfully(int exit_status);
146
// This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
147
// ASSERT_EXIT*, and EXPECT_EXIT*.
148
#define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \
149
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
151
const ::testing::internal::RE& gtest_regex = (regex); \
152
::testing::internal::DeathTest* gtest_dt; \
153
if (!::testing::internal::DeathTest::Create(#statement, >est_regex, \
154
__FILE__, __LINE__, >est_dt)) { \
155
goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
157
if (gtest_dt != NULL) { \
158
::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \
159
gtest_dt_ptr(gtest_dt); \
160
switch (gtest_dt->AssumeRole()) { \
161
case ::testing::internal::DeathTest::OVERSEE_TEST: \
162
if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
163
goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
166
case ::testing::internal::DeathTest::EXECUTE_TEST: { \
167
::testing::internal::DeathTest::ReturnSentinel \
168
gtest_sentinel(gtest_dt); \
170
gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
176
GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \
177
fail(::testing::internal::DeathTest::LastMessage())
178
// The symbol "fail" here expands to something into which a message
181
// A struct representing the parsed contents of the
182
// --gtest_internal_run_death_test flag, as it existed when
183
// RUN_ALL_TESTS was called.
184
struct InternalRunDeathTestFlag {
191
// Returns a newly created InternalRunDeathTestFlag object with fields
192
// initialized from the GTEST_FLAG(internal_run_death_test) flag if
193
// the flag is specified; otherwise returns NULL.
194
InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
196
#endif // GTEST_HAS_DEATH_TEST
198
} // namespace internal
199
} // namespace testing
201
#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_