2008-07-03 22:38:12 +00:00
|
|
|
// Copyright 2005, Google Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
|
|
|
|
//
|
|
|
|
// The Google C++ Testing Framework (Google Test)
|
|
|
|
//
|
|
|
|
// This header file defines internal utilities needed for implementing
|
|
|
|
// death tests. They are subject to change without notice.
|
|
|
|
|
|
|
|
#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
|
|
|
|
#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
|
|
|
|
|
|
|
|
#include <gtest/internal/gtest-internal.h>
|
|
|
|
|
|
|
|
namespace testing {
|
|
|
|
namespace internal {
|
|
|
|
|
2008-10-11 07:20:02 +00:00
|
|
|
GTEST_DECLARE_string_(internal_run_death_test);
|
2008-07-03 22:38:12 +00:00
|
|
|
|
|
|
|
// Names of the flags (needed for parsing Google Test flags).
|
|
|
|
const char kDeathTestStyleFlag[] = "death_test_style";
|
2009-01-08 01:10:31 +00:00
|
|
|
const char kDeathTestUseFork[] = "death_test_use_fork";
|
2008-07-03 22:38:12 +00:00
|
|
|
const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
|
|
|
|
|
2009-02-23 23:21:55 +00:00
|
|
|
#if GTEST_HAS_DEATH_TEST
|
2008-07-03 22:38:12 +00:00
|
|
|
|
|
|
|
// DeathTest is a class that hides much of the complexity of the
|
2008-10-11 07:20:02 +00:00
|
|
|
// GTEST_DEATH_TEST_ macro. It is abstract; its static Create method
|
2008-07-03 22:38:12 +00:00
|
|
|
// returns a concrete class that depends on the prevailing death test
|
|
|
|
// style, as defined by the --gtest_death_test_style and/or
|
|
|
|
// --gtest_internal_run_death_test flags.
|
|
|
|
|
|
|
|
// In describing the results of death tests, these terms are used with
|
|
|
|
// the corresponding definitions:
|
|
|
|
//
|
|
|
|
// exit status: The integer exit information in the format specified
|
|
|
|
// by wait(2)
|
|
|
|
// exit code: The integer code passed to exit(3), _exit(2), or
|
|
|
|
// returned from main()
|
|
|
|
class DeathTest {
|
|
|
|
public:
|
|
|
|
// Create returns false if there was an error determining the
|
|
|
|
// appropriate action to take for the current death test; for example,
|
|
|
|
// if the gtest_death_test_style flag is set to an invalid value.
|
|
|
|
// The LastMessage method will return a more detailed message in that
|
|
|
|
// case. Otherwise, the DeathTest pointer pointed to by the "test"
|
|
|
|
// argument is set. If the death test should be skipped, the pointer
|
|
|
|
// is set to NULL; otherwise, it is set to the address of a new concrete
|
|
|
|
// DeathTest object that controls the execution of the current test.
|
|
|
|
static bool Create(const char* statement, const RE* regex,
|
|
|
|
const char* file, int line, DeathTest** test);
|
|
|
|
DeathTest();
|
|
|
|
virtual ~DeathTest() { }
|
|
|
|
|
|
|
|
// A helper class that aborts a death test when it's deleted.
|
|
|
|
class ReturnSentinel {
|
|
|
|
public:
|
|
|
|
explicit ReturnSentinel(DeathTest* test) : test_(test) { }
|
|
|
|
~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
|
|
|
|
private:
|
|
|
|
DeathTest* const test_;
|
2008-10-11 07:20:02 +00:00
|
|
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);
|
|
|
|
} GTEST_ATTRIBUTE_UNUSED_;
|
2008-07-03 22:38:12 +00:00
|
|
|
|
|
|
|
// An enumeration of possible roles that may be taken when a death
|
|
|
|
// test is encountered. EXECUTE means that the death test logic should
|
|
|
|
// be executed immediately. OVERSEE means that the program should prepare
|
|
|
|
// the appropriate environment for a child process to execute the death
|
|
|
|
// test, then wait for it to complete.
|
|
|
|
enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
|
|
|
|
|
|
|
|
// An enumeration of the two reasons that a test might be aborted.
|
|
|
|
enum AbortReason { TEST_ENCOUNTERED_RETURN_STATEMENT, TEST_DID_NOT_DIE };
|
|
|
|
|
|
|
|
// Assumes one of the above roles.
|
|
|
|
virtual TestRole AssumeRole() = 0;
|
|
|
|
|
|
|
|
// Waits for the death test to finish and returns its status.
|
|
|
|
virtual int Wait() = 0;
|
|
|
|
|
|
|
|
// Returns true if the death test passed; that is, the test process
|
|
|
|
// exited during the test, its exit status matches a user-supplied
|
|
|
|
// predicate, and its stderr output matches a user-supplied regular
|
|
|
|
// expression.
|
|
|
|
// The user-supplied predicate may be a macro expression rather
|
|
|
|
// than a function pointer or functor, or else Wait and Passed could
|
|
|
|
// be combined.
|
|
|
|
virtual bool Passed(bool exit_status_ok) = 0;
|
|
|
|
|
|
|
|
// Signals that the death test did not die as expected.
|
|
|
|
virtual void Abort(AbortReason reason) = 0;
|
|
|
|
|
|
|
|
// Returns a human-readable outcome message regarding the outcome of
|
|
|
|
// the last death test.
|
|
|
|
static const char* LastMessage();
|
|
|
|
|
2009-03-06 01:20:15 +00:00
|
|
|
static void set_last_death_test_message(const String& message);
|
|
|
|
|
2008-07-03 22:38:12 +00:00
|
|
|
private:
|
2009-03-06 01:20:15 +00:00
|
|
|
// A string containing a description of the outcome of the last death test.
|
|
|
|
static String last_death_test_message_;
|
|
|
|
|
2008-10-11 07:20:02 +00:00
|
|
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest);
|
2008-07-03 22:38:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Factory interface for death tests. May be mocked out for testing.
|
|
|
|
class DeathTestFactory {
|
|
|
|
public:
|
|
|
|
virtual ~DeathTestFactory() { }
|
|
|
|
virtual bool Create(const char* statement, const RE* regex,
|
|
|
|
const char* file, int line, DeathTest** test) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A concrete DeathTestFactory implementation for normal use.
|
|
|
|
class DefaultDeathTestFactory : public DeathTestFactory {
|
|
|
|
public:
|
|
|
|
virtual bool Create(const char* statement, const RE* regex,
|
|
|
|
const char* file, int line, DeathTest** test);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns true if exit_status describes a process that was terminated
|
|
|
|
// by a signal, or exited normally with a nonzero exit code.
|
|
|
|
bool ExitedUnsuccessfully(int exit_status);
|
|
|
|
|
|
|
|
// This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
|
|
|
|
// ASSERT_EXIT*, and EXPECT_EXIT*.
|
2008-10-11 07:20:02 +00:00
|
|
|
#define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \
|
|
|
|
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
2008-07-03 22:38:12 +00:00
|
|
|
if (true) { \
|
|
|
|
const ::testing::internal::RE& gtest_regex = (regex); \
|
|
|
|
::testing::internal::DeathTest* gtest_dt; \
|
|
|
|
if (!::testing::internal::DeathTest::Create(#statement, >est_regex, \
|
|
|
|
__FILE__, __LINE__, >est_dt)) { \
|
2008-10-11 07:20:02 +00:00
|
|
|
goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
|
2008-07-03 22:38:12 +00:00
|
|
|
} \
|
|
|
|
if (gtest_dt != NULL) { \
|
|
|
|
::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \
|
|
|
|
gtest_dt_ptr(gtest_dt); \
|
|
|
|
switch (gtest_dt->AssumeRole()) { \
|
|
|
|
case ::testing::internal::DeathTest::OVERSEE_TEST: \
|
|
|
|
if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
|
2008-10-11 07:20:02 +00:00
|
|
|
goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
|
2008-07-03 22:38:12 +00:00
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
case ::testing::internal::DeathTest::EXECUTE_TEST: { \
|
|
|
|
::testing::internal::DeathTest::ReturnSentinel \
|
|
|
|
gtest_sentinel(gtest_dt); \
|
2009-03-06 01:20:15 +00:00
|
|
|
GTEST_HIDE_UNREACHABLE_CODE_(statement); \
|
2008-07-03 22:38:12 +00:00
|
|
|
gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} else \
|
2008-10-11 07:20:02 +00:00
|
|
|
GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \
|
2008-07-03 22:38:12 +00:00
|
|
|
fail(::testing::internal::DeathTest::LastMessage())
|
|
|
|
// The symbol "fail" here expands to something into which a message
|
|
|
|
// can be streamed.
|
|
|
|
|
2009-03-06 01:20:15 +00:00
|
|
|
// A class representing the parsed contents of the
|
2008-07-03 22:38:12 +00:00
|
|
|
// --gtest_internal_run_death_test flag, as it existed when
|
|
|
|
// RUN_ALL_TESTS was called.
|
2009-03-06 01:20:15 +00:00
|
|
|
class InternalRunDeathTestFlag {
|
|
|
|
public:
|
|
|
|
InternalRunDeathTestFlag(const String& file,
|
|
|
|
int line,
|
|
|
|
int index,
|
2009-03-24 20:39:44 +00:00
|
|
|
int write_fd)
|
|
|
|
: file_(file), line_(line), index_(index), write_fd_(write_fd) {}
|
2009-03-06 01:20:15 +00:00
|
|
|
|
|
|
|
~InternalRunDeathTestFlag() {
|
2009-03-24 20:39:44 +00:00
|
|
|
if (write_fd_ >= 0)
|
2009-03-26 19:03:47 +00:00
|
|
|
posix::close(write_fd_);
|
2009-03-06 01:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String file() const { return file_; }
|
|
|
|
int line() const { return line_; }
|
|
|
|
int index() const { return index_; }
|
2009-03-24 20:39:44 +00:00
|
|
|
int write_fd() const { return write_fd_; }
|
2009-03-06 01:20:15 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
String file_;
|
|
|
|
int line_;
|
|
|
|
int index_;
|
2009-03-24 20:39:44 +00:00
|
|
|
int write_fd_;
|
2009-03-06 01:20:15 +00:00
|
|
|
|
|
|
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag);
|
2008-07-03 22:38:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Returns a newly created InternalRunDeathTestFlag object with fields
|
|
|
|
// initialized from the GTEST_FLAG(internal_run_death_test) flag if
|
|
|
|
// the flag is specified; otherwise returns NULL.
|
|
|
|
InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
|
|
|
|
|
|
|
|
#endif // GTEST_HAS_DEATH_TEST
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace testing
|
|
|
|
|
|
|
|
#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
|