Blame googletest/include/gtest/gtest-death-test.h

Packit bd1cd8
// Copyright 2005, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
//
Packit bd1cd8
// The Google C++ Testing Framework (Google Test)
Packit bd1cd8
//
Packit bd1cd8
// This header file defines the public API for death tests.  It is
Packit bd1cd8
// #included by gtest.h so a user doesn't need to include this
Packit bd1cd8
// directly.
Packit bd1cd8
Packit bd1cd8
#ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
Packit bd1cd8
#define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
Packit bd1cd8
Packit bd1cd8
#include "gtest/internal/gtest-death-test-internal.h"
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
Packit bd1cd8
// This flag controls the style of death tests.  Valid values are "threadsafe",
Packit bd1cd8
// meaning that the death test child process will re-execute the test binary
Packit bd1cd8
// from the start, running only a single death test, or "fast",
Packit bd1cd8
// meaning that the child process will execute the test logic immediately
Packit bd1cd8
// after forking.
Packit bd1cd8
GTEST_DECLARE_string_(death_test_style);
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_DEATH_TEST
Packit bd1cd8
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// Returns a Boolean value indicating whether the caller is currently
Packit bd1cd8
// executing in the context of the death test child process.  Tools such as
Packit bd1cd8
// Valgrind heap checkers may need this to modify their behavior in death
Packit bd1cd8
// tests.  IMPORTANT: This is an internal utility.  Using it may break the
Packit bd1cd8
// implementation of death tests.  User code MUST NOT use it.
Packit bd1cd8
GTEST_API_ bool InDeathTestChild();
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
Packit bd1cd8
// The following macros are useful for writing death tests.
Packit bd1cd8
Packit bd1cd8
// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is
Packit bd1cd8
// executed:
Packit bd1cd8
//
Packit bd1cd8
//   1. It generates a warning if there is more than one active
Packit bd1cd8
//   thread.  This is because it's safe to fork() or clone() only
Packit bd1cd8
//   when there is a single thread.
Packit bd1cd8
//
Packit bd1cd8
//   2. The parent process clone()s a sub-process and runs the death
Packit bd1cd8
//   test in it; the sub-process exits with code 0 at the end of the
Packit bd1cd8
//   death test, if it hasn't exited already.
Packit bd1cd8
//
Packit bd1cd8
//   3. The parent process waits for the sub-process to terminate.
Packit bd1cd8
//
Packit bd1cd8
//   4. The parent process checks the exit code and error message of
Packit bd1cd8
//   the sub-process.
Packit bd1cd8
//
Packit bd1cd8
// Examples:
Packit bd1cd8
//
Packit bd1cd8
//   ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number");
Packit bd1cd8
//   for (int i = 0; i < 5; i++) {
Packit bd1cd8
//     EXPECT_DEATH(server.ProcessRequest(i),
Packit bd1cd8
//                  "Invalid request .* in ProcessRequest()")
Packit bd1cd8
//                  << "Failed to die on request " << i;
Packit bd1cd8
//   }
Packit bd1cd8
//
Packit bd1cd8
//   ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
Packit bd1cd8
//
Packit bd1cd8
//   bool KilledBySIGHUP(int exit_code) {
Packit bd1cd8
//     return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;
Packit bd1cd8
//   }
Packit bd1cd8
//
Packit bd1cd8
//   ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");
Packit bd1cd8
//
Packit bd1cd8
// On the regular expressions used in death tests:
Packit bd1cd8
//
Packit bd1cd8
//   On POSIX-compliant systems (*nix), we use the <regex.h> library,
Packit bd1cd8
//   which uses the POSIX extended regex syntax.
Packit bd1cd8
//
Packit bd1cd8
//   On other platforms (e.g. Windows), we only support a simple regex
Packit bd1cd8
//   syntax implemented as part of Google Test.  This limited
Packit bd1cd8
//   implementation should be enough most of the time when writing
Packit bd1cd8
//   death tests; though it lacks many features you can find in PCRE
Packit bd1cd8
//   or POSIX extended regex syntax.  For example, we don't support
Packit bd1cd8
//   union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and
Packit bd1cd8
//   repetition count ("x{5,7}"), among others.
Packit bd1cd8
//
Packit bd1cd8
//   Below is the syntax that we do support.  We chose it to be a
Packit bd1cd8
//   subset of both PCRE and POSIX extended regex, so it's easy to
Packit bd1cd8
//   learn wherever you come from.  In the following: 'A' denotes a
Packit bd1cd8
//   literal character, period (.), or a single \\ escape sequence;
Packit bd1cd8
//   'x' and 'y' denote regular expressions; 'm' and 'n' are for
Packit bd1cd8
//   natural numbers.
Packit bd1cd8
//
Packit bd1cd8
//     c     matches any literal character c
Packit bd1cd8
//     \\d   matches any decimal digit
Packit bd1cd8
//     \\D   matches any character that's not a decimal digit
Packit bd1cd8
//     \\f   matches \f
Packit bd1cd8
//     \\n   matches \n
Packit bd1cd8
//     \\r   matches \r
Packit bd1cd8
//     \\s   matches any ASCII whitespace, including \n
Packit bd1cd8
//     \\S   matches any character that's not a whitespace
Packit bd1cd8
//     \\t   matches \t
Packit bd1cd8
//     \\v   matches \v
Packit bd1cd8
//     \\w   matches any letter, _, or decimal digit
Packit bd1cd8
//     \\W   matches any character that \\w doesn't match
Packit bd1cd8
//     \\c   matches any literal character c, which must be a punctuation
Packit bd1cd8
//     .     matches any single character except \n
Packit bd1cd8
//     A?    matches 0 or 1 occurrences of A
Packit bd1cd8
//     A*    matches 0 or many occurrences of A
Packit bd1cd8
//     A+    matches 1 or many occurrences of A
Packit bd1cd8
//     ^     matches the beginning of a string (not that of each line)
Packit bd1cd8
//     $     matches the end of a string (not that of each line)
Packit bd1cd8
//     xy    matches x followed by y
Packit bd1cd8
//
Packit bd1cd8
//   If you accidentally use PCRE or POSIX extended regex features
Packit bd1cd8
//   not implemented by us, you will get a run-time failure.  In that
Packit bd1cd8
//   case, please try to rewrite your regular expression within the
Packit bd1cd8
//   above syntax.
Packit bd1cd8
//
Packit bd1cd8
//   This implementation is *not* meant to be as highly tuned or robust
Packit bd1cd8
//   as a compiled regex library, but should perform well enough for a
Packit bd1cd8
//   death test, which already incurs significant overhead by launching
Packit bd1cd8
//   a child process.
Packit bd1cd8
//
Packit bd1cd8
// Known caveats:
Packit bd1cd8
//
Packit bd1cd8
//   A "threadsafe" style death test obtains the path to the test
Packit bd1cd8
//   program from argv[0] and re-executes it in the sub-process.  For
Packit bd1cd8
//   simplicity, the current implementation doesn't search the PATH
Packit bd1cd8
//   when launching the sub-process.  This means that the user must
Packit bd1cd8
//   invoke the test program via a path that contains at least one
Packit bd1cd8
//   path separator (e.g. path/to/foo_test and
Packit bd1cd8
//   /absolute/path/to/bar_test are fine, but foo_test is not).  This
Packit bd1cd8
//   is rarely a problem as people usually don't put the test binary
Packit bd1cd8
//   directory in PATH.
Packit bd1cd8
//
Packit bd1cd8
// TODO(wan@google.com): make thread-safe death tests search the PATH.
Packit bd1cd8
Packit bd1cd8
// Asserts that a given statement causes the program to exit, with an
Packit bd1cd8
// integer exit status that satisfies predicate, and emitting error output
Packit bd1cd8
// that matches regex.
Packit bd1cd8
# define ASSERT_EXIT(statement, predicate, regex) \
Packit bd1cd8
    GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_)
Packit bd1cd8
Packit bd1cd8
// Like ASSERT_EXIT, but continues on to successive tests in the
Packit bd1cd8
// test case, if any:
Packit bd1cd8
# define EXPECT_EXIT(statement, predicate, regex) \
Packit bd1cd8
    GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_)
Packit bd1cd8
Packit bd1cd8
// Asserts that a given statement causes the program to exit, either by
Packit bd1cd8
// explicitly exiting with a nonzero exit code or being killed by a
Packit bd1cd8
// signal, and emitting error output that matches regex.
Packit bd1cd8
# define ASSERT_DEATH(statement, regex) \
Packit bd1cd8
    ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
Packit bd1cd8
Packit bd1cd8
// Like ASSERT_DEATH, but continues on to successive tests in the
Packit bd1cd8
// test case, if any:
Packit bd1cd8
# define EXPECT_DEATH(statement, regex) \
Packit bd1cd8
    EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
Packit bd1cd8
Packit bd1cd8
// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
Packit bd1cd8
Packit bd1cd8
// Tests that an exit code describes a normal exit with a given exit code.
Packit bd1cd8
class GTEST_API_ ExitedWithCode {
Packit bd1cd8
 public:
Packit bd1cd8
  explicit ExitedWithCode(int exit_code);
Packit bd1cd8
  bool operator()(int exit_status) const;
Packit bd1cd8
 private:
Packit bd1cd8
  // No implementation - assignment is unsupported.
Packit bd1cd8
  void operator=(const ExitedWithCode& other);
Packit bd1cd8
Packit bd1cd8
  const int exit_code_;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
# if !GTEST_OS_WINDOWS
Packit bd1cd8
// Tests that an exit code describes an exit due to termination by a
Packit bd1cd8
// given signal.
Packit bd1cd8
class GTEST_API_ KilledBySignal {
Packit bd1cd8
 public:
Packit bd1cd8
  explicit KilledBySignal(int signum);
Packit bd1cd8
  bool operator()(int exit_status) const;
Packit bd1cd8
 private:
Packit bd1cd8
  const int signum_;
Packit bd1cd8
};
Packit bd1cd8
# endif  // !GTEST_OS_WINDOWS
Packit bd1cd8
Packit bd1cd8
// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.
Packit bd1cd8
// The death testing framework causes this to have interesting semantics,
Packit bd1cd8
// since the sideeffects of the call are only visible in opt mode, and not
Packit bd1cd8
// in debug mode.
Packit bd1cd8
//
Packit bd1cd8
// In practice, this can be used to test functions that utilize the
Packit bd1cd8
// LOG(DFATAL) macro using the following style:
Packit bd1cd8
//
Packit bd1cd8
// int DieInDebugOr12(int* sideeffect) {
Packit bd1cd8
//   if (sideeffect) {
Packit bd1cd8
//     *sideeffect = 12;
Packit bd1cd8
//   }
Packit bd1cd8
//   LOG(DFATAL) << "death";
Packit bd1cd8
//   return 12;
Packit bd1cd8
// }
Packit bd1cd8
//
Packit bd1cd8
// TEST(TestCase, TestDieOr12WorksInDgbAndOpt) {
Packit bd1cd8
//   int sideeffect = 0;
Packit bd1cd8
//   // Only asserts in dbg.
Packit bd1cd8
//   EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");
Packit bd1cd8
//
Packit bd1cd8
// #ifdef NDEBUG
Packit bd1cd8
//   // opt-mode has sideeffect visible.
Packit bd1cd8
//   EXPECT_EQ(12, sideeffect);
Packit bd1cd8
// #else
Packit bd1cd8
//   // dbg-mode no visible sideeffect.
Packit bd1cd8
//   EXPECT_EQ(0, sideeffect);
Packit bd1cd8
// #endif
Packit bd1cd8
// }
Packit bd1cd8
//
Packit bd1cd8
// This will assert that DieInDebugReturn12InOpt() crashes in debug
Packit bd1cd8
// mode, usually due to a DCHECK or LOG(DFATAL), but returns the
Packit bd1cd8
// appropriate fallback value (12 in this case) in opt mode. If you
Packit bd1cd8
// need to test that a function has appropriate side-effects in opt
Packit bd1cd8
// mode, include assertions against the side-effects.  A general
Packit bd1cd8
// pattern for this is:
Packit bd1cd8
//
Packit bd1cd8
// EXPECT_DEBUG_DEATH({
Packit bd1cd8
//   // Side-effects here will have an effect after this statement in
Packit bd1cd8
//   // opt mode, but none in debug mode.
Packit bd1cd8
//   EXPECT_EQ(12, DieInDebugOr12(&sideeffect));
Packit bd1cd8
// }, "death");
Packit bd1cd8
//
Packit bd1cd8
# ifdef NDEBUG
Packit bd1cd8
Packit bd1cd8
#  define EXPECT_DEBUG_DEATH(statement, regex) \
Packit bd1cd8
  GTEST_EXECUTE_STATEMENT_(statement, regex)
Packit bd1cd8
Packit bd1cd8
#  define ASSERT_DEBUG_DEATH(statement, regex) \
Packit bd1cd8
  GTEST_EXECUTE_STATEMENT_(statement, regex)
Packit bd1cd8
Packit bd1cd8
# else
Packit bd1cd8
Packit bd1cd8
#  define EXPECT_DEBUG_DEATH(statement, regex) \
Packit bd1cd8
  EXPECT_DEATH(statement, regex)
Packit bd1cd8
Packit bd1cd8
#  define ASSERT_DEBUG_DEATH(statement, regex) \
Packit bd1cd8
  ASSERT_DEATH(statement, regex)
Packit bd1cd8
Packit bd1cd8
# endif  // NDEBUG for EXPECT_DEBUG_DEATH
Packit bd1cd8
#endif  // GTEST_HAS_DEATH_TEST
Packit bd1cd8
Packit bd1cd8
// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and
Packit bd1cd8
// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if
Packit bd1cd8
// death tests are supported; otherwise they just issue a warning.  This is
Packit bd1cd8
// useful when you are combining death test assertions with normal test
Packit bd1cd8
// assertions in one test.
Packit bd1cd8
#if GTEST_HAS_DEATH_TEST
Packit bd1cd8
# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
Packit bd1cd8
    EXPECT_DEATH(statement, regex)
Packit bd1cd8
# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
Packit bd1cd8
    ASSERT_DEATH(statement, regex)
Packit bd1cd8
#else
Packit bd1cd8
# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
Packit bd1cd8
    GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, )
Packit bd1cd8
# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
Packit bd1cd8
    GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return)
Packit bd1cd8
#endif
Packit bd1cd8
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_