Blame googletest/include/gtest/internal/gtest-death-test-internal.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
// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
Packit bd1cd8
//
Packit bd1cd8
// The Google C++ Testing Framework (Google Test)
Packit bd1cd8
//
Packit bd1cd8
// This header file defines internal utilities needed for implementing
Packit bd1cd8
// death tests.  They are subject to change without notice.
Packit bd1cd8
Packit bd1cd8
#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
Packit bd1cd8
#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
Packit bd1cd8
Packit bd1cd8
#include "gtest/internal/gtest-internal.h"
Packit bd1cd8
Packit bd1cd8
#include <stdio.h>
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
GTEST_DECLARE_string_(internal_run_death_test);
Packit bd1cd8
Packit bd1cd8
// Names of the flags (needed for parsing Google Test flags).
Packit bd1cd8
const char kDeathTestStyleFlag[] = "death_test_style";
Packit bd1cd8
const char kDeathTestUseFork[] = "death_test_use_fork";
Packit bd1cd8
const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
Packit bd1cd8
Packit bd1cd8
#if GTEST_HAS_DEATH_TEST
Packit bd1cd8
Packit bd1cd8
// DeathTest is a class that hides much of the complexity of the
Packit bd1cd8
// GTEST_DEATH_TEST_ macro.  It is abstract; its static Create method
Packit bd1cd8
// returns a concrete class that depends on the prevailing death test
Packit bd1cd8
// style, as defined by the --gtest_death_test_style and/or
Packit bd1cd8
// --gtest_internal_run_death_test flags.
Packit bd1cd8
Packit bd1cd8
// In describing the results of death tests, these terms are used with
Packit bd1cd8
// the corresponding definitions:
Packit bd1cd8
//
Packit bd1cd8
// exit status:  The integer exit information in the format specified
Packit bd1cd8
//               by wait(2)
Packit bd1cd8
// exit code:    The integer code passed to exit(3), _exit(2), or
Packit bd1cd8
//               returned from main()
Packit bd1cd8
class GTEST_API_ DeathTest {
Packit bd1cd8
 public:
Packit bd1cd8
  // Create returns false if there was an error determining the
Packit bd1cd8
  // appropriate action to take for the current death test; for example,
Packit bd1cd8
  // if the gtest_death_test_style flag is set to an invalid value.
Packit bd1cd8
  // The LastMessage method will return a more detailed message in that
Packit bd1cd8
  // case.  Otherwise, the DeathTest pointer pointed to by the "test"
Packit bd1cd8
  // argument is set.  If the death test should be skipped, the pointer
Packit bd1cd8
  // is set to NULL; otherwise, it is set to the address of a new concrete
Packit bd1cd8
  // DeathTest object that controls the execution of the current test.
Packit bd1cd8
  static bool Create(const char* statement, const RE* regex,
Packit bd1cd8
                     const char* file, int line, DeathTest** test);
Packit bd1cd8
  DeathTest();
Packit bd1cd8
  virtual ~DeathTest() { }
Packit bd1cd8
Packit bd1cd8
  // A helper class that aborts a death test when it's deleted.
Packit bd1cd8
  class ReturnSentinel {
Packit bd1cd8
   public:
Packit bd1cd8
    explicit ReturnSentinel(DeathTest* test) : test_(test) { }
Packit bd1cd8
    ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
Packit bd1cd8
   private:
Packit bd1cd8
    DeathTest* const test_;
Packit bd1cd8
    GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);
Packit bd1cd8
  } GTEST_ATTRIBUTE_UNUSED_;
Packit bd1cd8
Packit bd1cd8
  // An enumeration of possible roles that may be taken when a death
Packit bd1cd8
  // test is encountered.  EXECUTE means that the death test logic should
Packit bd1cd8
  // be executed immediately.  OVERSEE means that the program should prepare
Packit bd1cd8
  // the appropriate environment for a child process to execute the death
Packit bd1cd8
  // test, then wait for it to complete.
Packit bd1cd8
  enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
Packit bd1cd8
Packit bd1cd8
  // An enumeration of the three reasons that a test might be aborted.
Packit bd1cd8
  enum AbortReason {
Packit bd1cd8
    TEST_ENCOUNTERED_RETURN_STATEMENT,
Packit bd1cd8
    TEST_THREW_EXCEPTION,
Packit bd1cd8
    TEST_DID_NOT_DIE
Packit bd1cd8
  };
Packit bd1cd8
Packit bd1cd8
  // Assumes one of the above roles.
Packit bd1cd8
  virtual TestRole AssumeRole() = 0;
Packit bd1cd8
Packit bd1cd8
  // Waits for the death test to finish and returns its status.
Packit bd1cd8
  virtual int Wait() = 0;
Packit bd1cd8
Packit bd1cd8
  // Returns true if the death test passed; that is, the test process
Packit bd1cd8
  // exited during the test, its exit status matches a user-supplied
Packit bd1cd8
  // predicate, and its stderr output matches a user-supplied regular
Packit bd1cd8
  // expression.
Packit bd1cd8
  // The user-supplied predicate may be a macro expression rather
Packit bd1cd8
  // than a function pointer or functor, or else Wait and Passed could
Packit bd1cd8
  // be combined.
Packit bd1cd8
  virtual bool Passed(bool exit_status_ok) = 0;
Packit bd1cd8
Packit bd1cd8
  // Signals that the death test did not die as expected.
Packit bd1cd8
  virtual void Abort(AbortReason reason) = 0;
Packit bd1cd8
Packit bd1cd8
  // Returns a human-readable outcome message regarding the outcome of
Packit bd1cd8
  // the last death test.
Packit bd1cd8
  static const char* LastMessage();
Packit bd1cd8
Packit bd1cd8
  static void set_last_death_test_message(const std::string& message);
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  // A string containing a description of the outcome of the last death test.
Packit bd1cd8
  static std::string last_death_test_message_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Factory interface for death tests.  May be mocked out for testing.
Packit bd1cd8
class DeathTestFactory {
Packit bd1cd8
 public:
Packit bd1cd8
  virtual ~DeathTestFactory() { }
Packit bd1cd8
  virtual bool Create(const char* statement, const RE* regex,
Packit bd1cd8
                      const char* file, int line, DeathTest** test) = 0;
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// A concrete DeathTestFactory implementation for normal use.
Packit bd1cd8
class DefaultDeathTestFactory : public DeathTestFactory {
Packit bd1cd8
 public:
Packit bd1cd8
  virtual bool Create(const char* statement, const RE* regex,
Packit bd1cd8
                      const char* file, int line, DeathTest** test);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Returns true if exit_status describes a process that was terminated
Packit bd1cd8
// by a signal, or exited normally with a nonzero exit code.
Packit bd1cd8
GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
Packit bd1cd8
Packit bd1cd8
// Traps C++ exceptions escaping statement and reports them as test
Packit bd1cd8
// failures. Note that trapping SEH exceptions is not implemented here.
Packit bd1cd8
# if GTEST_HAS_EXCEPTIONS
Packit bd1cd8
#  define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
Packit bd1cd8
  try { \
Packit bd1cd8
    GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
Packit bd1cd8
  } catch (const ::std::exception& gtest_exception) { \
Packit bd1cd8
    fprintf(\
Packit bd1cd8
        stderr, \
Packit bd1cd8
        "\n%s: Caught std::exception-derived exception escaping the " \
Packit bd1cd8
        "death test statement. Exception message: %s\n", \
Packit bd1cd8
        ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \
Packit bd1cd8
        gtest_exception.what()); \
Packit bd1cd8
    fflush(stderr); \
Packit bd1cd8
    death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
Packit bd1cd8
  } catch (...) { \
Packit bd1cd8
    death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
# else
Packit bd1cd8
#  define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
Packit bd1cd8
  GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
Packit bd1cd8
Packit bd1cd8
# endif
Packit bd1cd8
Packit bd1cd8
// This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
Packit bd1cd8
// ASSERT_EXIT*, and EXPECT_EXIT*.
Packit bd1cd8
# define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \
Packit bd1cd8
  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
Packit bd1cd8
  if (::testing::internal::AlwaysTrue()) { \
Packit bd1cd8
    const ::testing::internal::RE& gtest_regex = (regex); \
Packit bd1cd8
    ::testing::internal::DeathTest* gtest_dt; \
Packit bd1cd8
    if (!::testing::internal::DeathTest::Create(#statement, &gtest_regex, \
Packit bd1cd8
        __FILE__, __LINE__, &gtest_dt)) { \
Packit bd1cd8
      goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
Packit bd1cd8
    } \
Packit bd1cd8
    if (gtest_dt != NULL) { \
Packit bd1cd8
      ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \
Packit bd1cd8
          gtest_dt_ptr(gtest_dt); \
Packit bd1cd8
      switch (gtest_dt->AssumeRole()) { \
Packit bd1cd8
        case ::testing::internal::DeathTest::OVERSEE_TEST: \
Packit bd1cd8
          if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
Packit bd1cd8
            goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
Packit bd1cd8
          } \
Packit bd1cd8
          break; \
Packit bd1cd8
        case ::testing::internal::DeathTest::EXECUTE_TEST: { \
Packit bd1cd8
          ::testing::internal::DeathTest::ReturnSentinel \
Packit bd1cd8
              gtest_sentinel(gtest_dt); \
Packit bd1cd8
          GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \
Packit bd1cd8
          gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
Packit bd1cd8
          break; \
Packit bd1cd8
        } \
Packit bd1cd8
        default: \
Packit bd1cd8
          break; \
Packit bd1cd8
      } \
Packit bd1cd8
    } \
Packit bd1cd8
  } else \
Packit bd1cd8
    GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \
Packit bd1cd8
      fail(::testing::internal::DeathTest::LastMessage())
Packit bd1cd8
// The symbol "fail" here expands to something into which a message
Packit bd1cd8
// can be streamed.
Packit bd1cd8
Packit bd1cd8
// This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in
Packit bd1cd8
// NDEBUG mode. In this case we need the statements to be executed, the regex is
Packit bd1cd8
// ignored, and the macro must accept a streamed message even though the message
Packit bd1cd8
// is never printed.
Packit bd1cd8
# define GTEST_EXECUTE_STATEMENT_(statement, regex) \
Packit bd1cd8
  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
Packit bd1cd8
  if (::testing::internal::AlwaysTrue()) { \
Packit bd1cd8
     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
Packit bd1cd8
  } else \
Packit bd1cd8
    ::testing::Message()
Packit bd1cd8
Packit bd1cd8
// A class representing the parsed contents of the
Packit bd1cd8
// --gtest_internal_run_death_test flag, as it existed when
Packit bd1cd8
// RUN_ALL_TESTS was called.
Packit bd1cd8
class InternalRunDeathTestFlag {
Packit bd1cd8
 public:
Packit bd1cd8
  InternalRunDeathTestFlag(const std::string& a_file,
Packit bd1cd8
                           int a_line,
Packit bd1cd8
                           int an_index,
Packit bd1cd8
                           int a_write_fd)
Packit bd1cd8
      : file_(a_file), line_(a_line), index_(an_index),
Packit bd1cd8
        write_fd_(a_write_fd) {}
Packit bd1cd8
Packit bd1cd8
  ~InternalRunDeathTestFlag() {
Packit bd1cd8
    if (write_fd_ >= 0)
Packit bd1cd8
      posix::Close(write_fd_);
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  const std::string& file() const { return file_; }
Packit bd1cd8
  int line() const { return line_; }
Packit bd1cd8
  int index() const { return index_; }
Packit bd1cd8
  int write_fd() const { return write_fd_; }
Packit bd1cd8
Packit bd1cd8
 private:
Packit bd1cd8
  std::string file_;
Packit bd1cd8
  int line_;
Packit bd1cd8
  int index_;
Packit bd1cd8
  int write_fd_;
Packit bd1cd8
Packit bd1cd8
  GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag);
Packit bd1cd8
};
Packit bd1cd8
Packit bd1cd8
// Returns a newly created InternalRunDeathTestFlag object with fields
Packit bd1cd8
// initialized from the GTEST_FLAG(internal_run_death_test) flag if
Packit bd1cd8
// the flag is specified; otherwise returns NULL.
Packit bd1cd8
InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
Packit bd1cd8
Packit bd1cd8
#else  // GTEST_HAS_DEATH_TEST
Packit bd1cd8
Packit bd1cd8
// This macro is used for implementing macros such as
Packit bd1cd8
// EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where
Packit bd1cd8
// death tests are not supported. Those macros must compile on such systems
Packit bd1cd8
// iff EXPECT_DEATH and ASSERT_DEATH compile with the same parameters on
Packit bd1cd8
// systems that support death tests. This allows one to write such a macro
Packit bd1cd8
// on a system that does not support death tests and be sure that it will
Packit bd1cd8
// compile on a death-test supporting system.
Packit bd1cd8
//
Packit bd1cd8
// Parameters:
Packit bd1cd8
//   statement -  A statement that a macro such as EXPECT_DEATH would test
Packit bd1cd8
//                for program termination. This macro has to make sure this
Packit bd1cd8
//                statement is compiled but not executed, to ensure that
Packit bd1cd8
//                EXPECT_DEATH_IF_SUPPORTED compiles with a certain
Packit bd1cd8
//                parameter iff EXPECT_DEATH compiles with it.
Packit bd1cd8
//   regex     -  A regex that a macro such as EXPECT_DEATH would use to test
Packit bd1cd8
//                the output of statement.  This parameter has to be
Packit bd1cd8
//                compiled but not evaluated by this macro, to ensure that
Packit bd1cd8
//                this macro only accepts expressions that a macro such as
Packit bd1cd8
//                EXPECT_DEATH would accept.
Packit bd1cd8
//   terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED
Packit bd1cd8
//                and a return statement for ASSERT_DEATH_IF_SUPPORTED.
Packit bd1cd8
//                This ensures that ASSERT_DEATH_IF_SUPPORTED will not
Packit bd1cd8
//                compile inside functions where ASSERT_DEATH doesn't
Packit bd1cd8
//                compile.
Packit bd1cd8
//
Packit bd1cd8
//  The branch that has an always false condition is used to ensure that
Packit bd1cd8
//  statement and regex are compiled (and thus syntactically correct) but
Packit bd1cd8
//  never executed. The unreachable code macro protects the terminator
Packit bd1cd8
//  statement from generating an 'unreachable code' warning in case
Packit bd1cd8
//  statement unconditionally returns or throws. The Message constructor at
Packit bd1cd8
//  the end allows the syntax of streaming additional messages into the
Packit bd1cd8
//  macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.
Packit bd1cd8
# define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \
Packit bd1cd8
    GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
Packit bd1cd8
    if (::testing::internal::AlwaysTrue()) { \
Packit bd1cd8
      GTEST_LOG_(WARNING) \
Packit bd1cd8
          << "Death tests are not supported on this platform.\n" \
Packit bd1cd8
          << "Statement '" #statement "' cannot be verified."; \
Packit bd1cd8
    } else if (::testing::internal::AlwaysFalse()) { \
Packit bd1cd8
      ::testing::internal::RE::PartialMatch(".*", (regex)); \
Packit bd1cd8
      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
Packit bd1cd8
      terminator; \
Packit bd1cd8
    } else \
Packit bd1cd8
      ::testing::Message()
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_HAS_DEATH_TEST
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
}  // namespace testing
Packit bd1cd8
Packit bd1cd8
#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_