blob: f0e109a3a3a8348805dfdf25fb29c1cbe623ccb0 [file] [log] [blame]
Misha Brukmanc89aba32008-12-31 17:34:06 +00001// Copyright 2005, 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// The Google C++ Testing Framework (Google Test)
33//
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
36// directly.
37
38#ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
39#define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
40
41#include <gtest/internal/gtest-death-test-internal.h>
42
43namespace testing {
44
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
49// after forking.
50GTEST_DECLARE_string_(death_test_style);
51
52#ifdef GTEST_HAS_DEATH_TEST
53
54// The following macros are useful for writing death tests.
55
56// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is
57// executed:
58//
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.
62//
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.
66//
67// 3. The parent process waits for the sub-process to terminate.
68//
69// 4. The parent process checks the exit code and error message of
70// the sub-process.
71//
72// Examples:
73//
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);
79// }
80//
81// ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
82//
83// bool KilledBySIGHUP(int exit_code) {
84// return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;
85// }
86//
87// ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");
88//
89// Known caveats:
90//
91// A "threadsafe" style death test obtains the path to the test
92// program from argv[0] and re-executes it in the sub-process. For
93// simplicity, the current implementation doesn't search the PATH
94// when launching the sub-process. This means that the user must
95// invoke the test program via a path that contains at least one
96// path separator (e.g. path/to/foo_test and
97// /absolute/path/to/bar_test are fine, but foo_test is not). This
98// is rarely a problem as people usually don't put the test binary
99// directory in PATH.
100//
101// TODO(wan@google.com): make thread-safe death tests search the PATH.
102
103// Asserts that a given statement causes the program to exit, with an
104// integer exit status that satisfies predicate, and emitting error output
105// that matches regex.
106#define ASSERT_EXIT(statement, predicate, regex) \
107 GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_)
108
109// Like ASSERT_EXIT, but continues on to successive tests in the
110// test case, if any:
111#define EXPECT_EXIT(statement, predicate, regex) \
112 GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_)
113
114// Asserts that a given statement causes the program to exit, either by
115// explicitly exiting with a nonzero exit code or being killed by a
116// signal, and emitting error output that matches regex.
117#define ASSERT_DEATH(statement, regex) \
118 ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
119
120// Like ASSERT_DEATH, but continues on to successive tests in the
121// test case, if any:
122#define EXPECT_DEATH(statement, regex) \
123 EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
124
125// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
126
127// Tests that an exit code describes a normal exit with a given exit code.
128class ExitedWithCode {
129 public:
130 explicit ExitedWithCode(int exit_code);
131 bool operator()(int exit_status) const;
132 private:
133 const int exit_code_;
134};
135
136// Tests that an exit code describes an exit due to termination by a
137// given signal.
138class KilledBySignal {
139 public:
140 explicit KilledBySignal(int signum);
141 bool operator()(int exit_status) const;
142 private:
143 const int signum_;
144};
145
146// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.
147// The death testing framework causes this to have interesting semantics,
148// since the sideeffects of the call are only visible in opt mode, and not
149// in debug mode.
150//
151// In practice, this can be used to test functions that utilize the
152// LOG(DFATAL) macro using the following style:
153//
154// int DieInDebugOr12(int* sideeffect) {
155// if (sideeffect) {
156// *sideeffect = 12;
157// }
158// LOG(DFATAL) << "death";
159// return 12;
160// }
161//
162// TEST(TestCase, TestDieOr12WorksInDgbAndOpt) {
163// int sideeffect = 0;
164// // Only asserts in dbg.
165// EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");
166//
167// #ifdef NDEBUG
168// // opt-mode has sideeffect visible.
169// EXPECT_EQ(12, sideeffect);
170// #else
171// // dbg-mode no visible sideeffect.
172// EXPECT_EQ(0, sideeffect);
173// #endif
174// }
175//
176// This will assert that DieInDebugReturn12InOpt() crashes in debug
177// mode, usually due to a DCHECK or LOG(DFATAL), but returns the
178// appropriate fallback value (12 in this case) in opt mode. If you
179// need to test that a function has appropriate side-effects in opt
180// mode, include assertions against the side-effects. A general
181// pattern for this is:
182//
183// EXPECT_DEBUG_DEATH({
184// // Side-effects here will have an effect after this statement in
185// // opt mode, but none in debug mode.
186// EXPECT_EQ(12, DieInDebugOr12(&sideeffect));
187// }, "death");
188//
189#ifdef NDEBUG
190
191#define EXPECT_DEBUG_DEATH(statement, regex) \
192 do { statement; } while (false)
193
194#define ASSERT_DEBUG_DEATH(statement, regex) \
195 do { statement; } while (false)
196
197#else
198
199#define EXPECT_DEBUG_DEATH(statement, regex) \
200 EXPECT_DEATH(statement, regex)
201
202#define ASSERT_DEBUG_DEATH(statement, regex) \
203 ASSERT_DEATH(statement, regex)
204
205#endif // NDEBUG for EXPECT_DEBUG_DEATH
206#endif // GTEST_HAS_DEATH_TEST
207} // namespace testing
208
209#endif // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_