brintos

brintos / llvm-project-archived public Read only

0
0
Text · 14.6 KiB · 08fef8c Raw
346 lines · c
1// Copyright 2005, Google Inc.2// All rights reserved.3//4// Redistribution and use in source and binary forms, with or without5// modification, are permitted provided that the following conditions are6// met:7//8//     * Redistributions of source code must retain the above copyright9// notice, this list of conditions and the following disclaimer.10//     * Redistributions in binary form must reproduce the above11// copyright notice, this list of conditions and the following disclaimer12// in the documentation and/or other materials provided with the13// distribution.14//     * Neither the name of Google Inc. nor the names of its15// contributors may be used to endorse or promote products derived from16// this software without specific prior written permission.17//18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29 30// The Google C++ Testing and Mocking Framework (Google Test)31//32// This header file defines the public API for death tests.  It is33// #included by gtest.h so a user doesn't need to include this34// directly.35 36// IWYU pragma: private, include "gtest/gtest.h"37// IWYU pragma: friend gtest/.*38// IWYU pragma: friend gmock/.*39 40#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_41#define GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_42 43#include "gtest/internal/gtest-death-test-internal.h"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 binary47// from the start, running only a single death test, or "fast",48// meaning that the child process will execute the test logic immediately49// after forking.50GTEST_DECLARE_string_(death_test_style);51 52namespace testing {53 54#ifdef GTEST_HAS_DEATH_TEST55 56namespace internal {57 58// Returns a Boolean value indicating whether the caller is currently59// executing in the context of the death test child process.  Tools such as60// Valgrind heap checkers may need this to modify their behavior in death61// tests.  IMPORTANT: This is an internal utility.  Using it may break the62// implementation of death tests.  User code MUST NOT use it.63GTEST_API_ bool InDeathTestChild();64 65}  // namespace internal66 67// The following macros are useful for writing death tests.68 69// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is70// executed:71//72//   1. It generates a warning if there is more than one active73//   thread.  This is because it's safe to fork() or clone() only74//   when there is a single thread.75//76//   2. The parent process clone()s a sub-process and runs the death77//   test in it; the sub-process exits with code 0 at the end of the78//   death test, if it hasn't exited already.79//80//   3. The parent process waits for the sub-process to terminate.81//82//   4. The parent process checks the exit code and error message of83//   the sub-process.84//85// Examples:86//87//   ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number");88//   for (int i = 0; i < 5; i++) {89//     EXPECT_DEATH(server.ProcessRequest(i),90//                  "Invalid request .* in ProcessRequest()")91//                  << "Failed to die on request " << i;92//   }93//94//   ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");95//96//   bool KilledBySIGHUP(int exit_code) {97//     return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;98//   }99//100//   ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");101//102// The final parameter to each of these macros is a matcher applied to any data103// the sub-process wrote to stderr.  For compatibility with existing tests, a104// bare string is interpreted as a regular expression matcher.105//106// On the regular expressions used in death tests:107//108//   On POSIX-compliant systems (*nix), we use the <regex.h> library,109//   which uses the POSIX extended regex syntax.110//111//   On other platforms (e.g. Windows or Mac), we only support a simple regex112//   syntax implemented as part of Google Test.  This limited113//   implementation should be enough most of the time when writing114//   death tests; though it lacks many features you can find in PCRE115//   or POSIX extended regex syntax.  For example, we don't support116//   union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and117//   repetition count ("x{5,7}"), among others.118//119//   Below is the syntax that we do support.  We chose it to be a120//   subset of both PCRE and POSIX extended regex, so it's easy to121//   learn wherever you come from.  In the following: 'A' denotes a122//   literal character, period (.), or a single \\ escape sequence;123//   'x' and 'y' denote regular expressions; 'm' and 'n' are for124//   natural numbers.125//126//     c     matches any literal character c127//     \\d   matches any decimal digit128//     \\D   matches any character that's not a decimal digit129//     \\f   matches \f130//     \\n   matches \n131//     \\r   matches \r132//     \\s   matches any ASCII whitespace, including \n133//     \\S   matches any character that's not a whitespace134//     \\t   matches \t135//     \\v   matches \v136//     \\w   matches any letter, _, or decimal digit137//     \\W   matches any character that \\w doesn't match138//     \\c   matches any literal character c, which must be a punctuation139//     .     matches any single character except \n140//     A?    matches 0 or 1 occurrences of A141//     A*    matches 0 or many occurrences of A142//     A+    matches 1 or many occurrences of A143//     ^     matches the beginning of a string (not that of each line)144//     $     matches the end of a string (not that of each line)145//     xy    matches x followed by y146//147//   If you accidentally use PCRE or POSIX extended regex features148//   not implemented by us, you will get a run-time failure.  In that149//   case, please try to rewrite your regular expression within the150//   above syntax.151//152//   This implementation is *not* meant to be as highly tuned or robust153//   as a compiled regex library, but should perform well enough for a154//   death test, which already incurs significant overhead by launching155//   a child process.156//157// Known caveats:158//159//   A "threadsafe" style death test obtains the path to the test160//   program from argv[0] and re-executes it in the sub-process.  For161//   simplicity, the current implementation doesn't search the PATH162//   when launching the sub-process.  This means that the user must163//   invoke the test program via a path that contains at least one164//   path separator (e.g. path/to/foo_test and165//   /absolute/path/to/bar_test are fine, but foo_test is not).  This166//   is rarely a problem as people usually don't put the test binary167//   directory in PATH.168//169 170// Asserts that a given `statement` causes the program to exit, with an171// integer exit status that satisfies `predicate`, and emitting error output172// that matches `matcher`.173#define ASSERT_EXIT(statement, predicate, matcher) \174  GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_FATAL_FAILURE_)175 176// Like `ASSERT_EXIT`, but continues on to successive tests in the177// test suite, if any:178#define EXPECT_EXIT(statement, predicate, matcher) \179  GTEST_DEATH_TEST_(statement, predicate, matcher, GTEST_NONFATAL_FAILURE_)180 181// Asserts that a given `statement` causes the program to exit, either by182// explicitly exiting with a nonzero exit code or being killed by a183// signal, and emitting error output that matches `matcher`.184#define ASSERT_DEATH(statement, matcher) \185  ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher)186 187// Like `ASSERT_DEATH`, but continues on to successive tests in the188// test suite, if any:189#define EXPECT_DEATH(statement, matcher) \190  EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, matcher)191 192// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:193 194// Tests that an exit code describes a normal exit with a given exit code.195class GTEST_API_ ExitedWithCode {196 public:197  explicit ExitedWithCode(int exit_code);198  ExitedWithCode(const ExitedWithCode&) = default;199  void operator=(const ExitedWithCode& other) = delete;200  bool operator()(int exit_status) const;201 202 private:203  const int exit_code_;204};205 206#if !defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_FUCHSIA)207// Tests that an exit code describes an exit due to termination by a208// given signal.209class GTEST_API_ KilledBySignal {210 public:211  explicit KilledBySignal(int signum);212  bool operator()(int exit_status) const;213 214 private:215  const int signum_;216};217#endif  // !GTEST_OS_WINDOWS218 219// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.220// The death testing framework causes this to have interesting semantics,221// since the sideeffects of the call are only visible in opt mode, and not222// in debug mode.223//224// In practice, this can be used to test functions that utilize the225// LOG(DFATAL) macro using the following style:226//227// int DieInDebugOr12(int* sideeffect) {228//   if (sideeffect) {229//     *sideeffect = 12;230//   }231//   LOG(DFATAL) << "death";232//   return 12;233// }234//235// TEST(TestSuite, TestDieOr12WorksInDgbAndOpt) {236//   int sideeffect = 0;237//   // Only asserts in dbg.238//   EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");239//240// #ifdef NDEBUG241//   // opt-mode has sideeffect visible.242//   EXPECT_EQ(12, sideeffect);243// #else244//   // dbg-mode no visible sideeffect.245//   EXPECT_EQ(0, sideeffect);246// #endif247// }248//249// This will assert that DieInDebugReturn12InOpt() crashes in debug250// mode, usually due to a DCHECK or LOG(DFATAL), but returns the251// appropriate fallback value (12 in this case) in opt mode. If you252// need to test that a function has appropriate side-effects in opt253// mode, include assertions against the side-effects.  A general254// pattern for this is:255//256// EXPECT_DEBUG_DEATH({257//   // Side-effects here will have an effect after this statement in258//   // opt mode, but none in debug mode.259//   EXPECT_EQ(12, DieInDebugOr12(&sideeffect));260// }, "death");261//262#ifdef NDEBUG263 264#define EXPECT_DEBUG_DEATH(statement, regex) \265  GTEST_EXECUTE_STATEMENT_(statement, regex)266 267#define ASSERT_DEBUG_DEATH(statement, regex) \268  GTEST_EXECUTE_STATEMENT_(statement, regex)269 270#else271 272#define EXPECT_DEBUG_DEATH(statement, regex) EXPECT_DEATH(statement, regex)273 274#define ASSERT_DEBUG_DEATH(statement, regex) ASSERT_DEATH(statement, regex)275 276#endif  // NDEBUG for EXPECT_DEBUG_DEATH277#endif  // GTEST_HAS_DEATH_TEST278 279// This macro is used for implementing macros such as280// EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where281// death tests are not supported. Those macros must compile on such systems282// if and only if EXPECT_DEATH and ASSERT_DEATH compile with the same parameters283// on systems that support death tests. This allows one to write such a macro on284// a system that does not support death tests and be sure that it will compile285// on a death-test supporting system. It is exposed publicly so that systems286// that have death-tests with stricter requirements than GTEST_HAS_DEATH_TEST287// can write their own equivalent of EXPECT_DEATH_IF_SUPPORTED and288// ASSERT_DEATH_IF_SUPPORTED.289//290// Parameters:291//   statement -  A statement that a macro such as EXPECT_DEATH would test292//                for program termination. This macro has to make sure this293//                statement is compiled but not executed, to ensure that294//                EXPECT_DEATH_IF_SUPPORTED compiles with a certain295//                parameter if and only if EXPECT_DEATH compiles with it.296//   regex     -  A regex that a macro such as EXPECT_DEATH would use to test297//                the output of statement.  This parameter has to be298//                compiled but not evaluated by this macro, to ensure that299//                this macro only accepts expressions that a macro such as300//                EXPECT_DEATH would accept.301//   terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED302//                and a return statement for ASSERT_DEATH_IF_SUPPORTED.303//                This ensures that ASSERT_DEATH_IF_SUPPORTED will not304//                compile inside functions where ASSERT_DEATH doesn't305//                compile.306//307//  The branch that has an always false condition is used to ensure that308//  statement and regex are compiled (and thus syntactically correct) but309//  never executed. The unreachable code macro protects the terminator310//  statement from generating an 'unreachable code' warning in case311//  statement unconditionally returns or throws. The Message constructor at312//  the end allows the syntax of streaming additional messages into the313//  macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.314#define GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, terminator)             \315  GTEST_AMBIGUOUS_ELSE_BLOCKER_                                                \316  if (::testing::internal::AlwaysTrue()) {                                     \317    GTEST_LOG_(WARNING) << "Death tests are not supported on this platform.\n" \318                        << "Statement '" #statement "' cannot be verified.";   \319  } else if (::testing::internal::AlwaysFalse()) {                             \320    ::testing::internal::RE::PartialMatch(".*", (regex));                      \321    GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement);                 \322    terminator;                                                                \323  } else                                                                       \324    ::testing::Message()325 326// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and327// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if328// death tests are supported; otherwise they just issue a warning.  This is329// useful when you are combining death test assertions with normal test330// assertions in one test.331#ifdef GTEST_HAS_DEATH_TEST332#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \333  EXPECT_DEATH(statement, regex)334#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \335  ASSERT_DEATH(statement, regex)336#else337#define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \338  GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, )339#define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \340  GTEST_UNSUPPORTED_DEATH_TEST(statement, regex, return)341#endif342 343}  // namespace testing344 345#endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_346