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
// Author: wan@google.com (Zhanyong Wan)
32
// The Google C++ Testing Framework (Google Test)
34
// This header file defines the public API for death tests. It is
35
// #included by gtest.h so a user doesn't need to include this
38
#ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
39
#define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
41
#include <gtest/internal/gtest-death-test-internal.h>
45
// This flag controls the style of death tests. Valid values are "threadsafe",
46
// meaning that the death test child process will re-execute the test binary
47
// from the start, running only a single death test, or "fast",
48
// meaning that the child process will execute the test logic immediately
50
GTEST_DECLARE_string_(death_test_style);
52
#if GTEST_HAS_DEATH_TEST
54
// The following macros are useful for writing death tests.
56
// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is
59
// 1. It generates a warning if there is more than one active
60
// thread. This is because it's safe to fork() or clone() only
61
// when there is a single thread.
63
// 2. The parent process clone()s a sub-process and runs the death
64
// test in it; the sub-process exits with code 0 at the end of the
65
// death test, if it hasn't exited already.
67
// 3. The parent process waits for the sub-process to terminate.
69
// 4. The parent process checks the exit code and error message of
74
// ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number");
75
// for (int i = 0; i < 5; i++) {
76
// EXPECT_DEATH(server.ProcessRequest(i),
77
// "Invalid request .* in ProcessRequest()")
78
// << "Failed to die on request " << i);
81
// ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
83
// bool KilledBySIGHUP(int exit_code) {
84
// return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;
87
// ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");
89
// On the regular expressions used in death tests:
91
// On POSIX-compliant systems (*nix), we use the <regex.h> library,
92
// which uses the POSIX extended regex syntax.
94
// On other platforms (e.g. Windows), we only support a simple regex
95
// syntax implemented as part of Google Test. This limited
96
// implementation should be enough most of the time when writing
97
// death tests; though it lacks many features you can find in PCRE
98
// or POSIX extended regex syntax. For example, we don't support
99
// union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and
100
// repetition count ("x{5,7}"), among others.
102
// Below is the syntax that we do support. We chose it to be a
103
// subset of both PCRE and POSIX extended regex, so it's easy to
104
// learn wherever you come from. In the following: 'A' denotes a
105
// literal character, period (.), or a single \\ escape sequence;
106
// 'x' and 'y' denote regular expressions; 'm' and 'n' are for
109
// c matches any literal character c
110
// \\d matches any decimal digit
111
// \\D matches any character that's not a decimal digit
115
// \\s matches any ASCII whitespace, including \n
116
// \\S matches any character that's not a whitespace
119
// \\w matches any letter, _, or decimal digit
120
// \\W matches any character that \\w doesn't match
121
// \\c matches any literal character c, which must be a punctuation
122
// . matches any single character except \n
123
// A? matches 0 or 1 occurrences of A
124
// A* matches 0 or many occurrences of A
125
// A+ matches 1 or many occurrences of A
126
// ^ matches the beginning of a string (not that of each line)
127
// $ matches the end of a string (not that of each line)
128
// xy matches x followed by y
130
// If you accidentally use PCRE or POSIX extended regex features
131
// not implemented by us, you will get a run-time failure. In that
132
// case, please try to rewrite your regular expression within the
135
// This implementation is *not* meant to be as highly tuned or robust
136
// as a compiled regex library, but should perform well enough for a
137
// death test, which already incurs significant overhead by launching
142
// A "threadsafe" style death test obtains the path to the test
143
// program from argv[0] and re-executes it in the sub-process. For
144
// simplicity, the current implementation doesn't search the PATH
145
// when launching the sub-process. This means that the user must
146
// invoke the test program via a path that contains at least one
147
// path separator (e.g. path/to/foo_test and
148
// /absolute/path/to/bar_test are fine, but foo_test is not). This
149
// is rarely a problem as people usually don't put the test binary
150
// directory in PATH.
152
// TODO(wan@google.com): make thread-safe death tests search the PATH.
154
// Asserts that a given statement causes the program to exit, with an
155
// integer exit status that satisfies predicate, and emitting error output
156
// that matches regex.
157
#define ASSERT_EXIT(statement, predicate, regex) \
158
GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_)
160
// Like ASSERT_EXIT, but continues on to successive tests in the
161
// test case, if any:
162
#define EXPECT_EXIT(statement, predicate, regex) \
163
GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_)
165
// Asserts that a given statement causes the program to exit, either by
166
// explicitly exiting with a nonzero exit code or being killed by a
167
// signal, and emitting error output that matches regex.
168
#define ASSERT_DEATH(statement, regex) \
169
ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
171
// Like ASSERT_DEATH, but continues on to successive tests in the
172
// test case, if any:
173
#define EXPECT_DEATH(statement, regex) \
174
EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
176
// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
178
// Tests that an exit code describes a normal exit with a given exit code.
179
class GTEST_API_ ExitedWithCode {
181
explicit ExitedWithCode(int exit_code);
182
bool operator()(int exit_status) const;
184
// No implementation - assignment is unsupported.
185
void operator=(const ExitedWithCode& other);
187
const int exit_code_;
190
#if !GTEST_OS_WINDOWS
191
// Tests that an exit code describes an exit due to termination by a
193
class GTEST_API_ KilledBySignal {
195
explicit KilledBySignal(int signum);
196
bool operator()(int exit_status) const;
200
#endif // !GTEST_OS_WINDOWS
202
// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.
203
// The death testing framework causes this to have interesting semantics,
204
// since the sideeffects of the call are only visible in opt mode, and not
207
// In practice, this can be used to test functions that utilize the
208
// LOG(DFATAL) macro using the following style:
210
// int DieInDebugOr12(int* sideeffect) {
214
// LOG(DFATAL) << "death";
218
// TEST(TestCase, TestDieOr12WorksInDgbAndOpt) {
219
// int sideeffect = 0;
220
// // Only asserts in dbg.
221
// EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");
224
// // opt-mode has sideeffect visible.
225
// EXPECT_EQ(12, sideeffect);
227
// // dbg-mode no visible sideeffect.
228
// EXPECT_EQ(0, sideeffect);
232
// This will assert that DieInDebugReturn12InOpt() crashes in debug
233
// mode, usually due to a DCHECK or LOG(DFATAL), but returns the
234
// appropriate fallback value (12 in this case) in opt mode. If you
235
// need to test that a function has appropriate side-effects in opt
236
// mode, include assertions against the side-effects. A general
237
// pattern for this is:
239
// EXPECT_DEBUG_DEATH({
240
// // Side-effects here will have an effect after this statement in
241
// // opt mode, but none in debug mode.
242
// EXPECT_EQ(12, DieInDebugOr12(&sideeffect));
247
#define EXPECT_DEBUG_DEATH(statement, regex) \
248
do { statement; } while (::testing::internal::AlwaysFalse())
250
#define ASSERT_DEBUG_DEATH(statement, regex) \
251
do { statement; } while (::testing::internal::AlwaysFalse())
255
#define EXPECT_DEBUG_DEATH(statement, regex) \
256
EXPECT_DEATH(statement, regex)
258
#define ASSERT_DEBUG_DEATH(statement, regex) \
259
ASSERT_DEATH(statement, regex)
261
#endif // NDEBUG for EXPECT_DEBUG_DEATH
262
#endif // GTEST_HAS_DEATH_TEST
264
// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and
265
// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if
266
// death tests are supported; otherwise they just issue a warning. This is
267
// useful when you are combining death test assertions with normal test
268
// assertions in one test.
269
#if GTEST_HAS_DEATH_TEST
270
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
271
EXPECT_DEATH(statement, regex)
272
#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
273
ASSERT_DEATH(statement, regex)
275
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
276
GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, )
277
#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
278
GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return)
281
} // namespace testing
283
#endif // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_