Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:04

0001 // Copyright 2005, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 
0030 // The Google C++ Testing and Mocking Framework (Google Test)
0031 //
0032 // This header file defines internal utilities needed for implementing
0033 // death tests.  They are subject to change without notice.
0034 
0035 // IWYU pragma: private, include "gtest/gtest.h"
0036 // IWYU pragma: friend gtest/.*
0037 // IWYU pragma: friend gmock/.*
0038 
0039 #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
0040 #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
0041 
0042 #include <stdio.h>
0043 
0044 #include <memory>
0045 
0046 #include "gtest/gtest-matchers.h"
0047 #include "gtest/internal/gtest-internal.h"
0048 
0049 GTEST_DECLARE_string_(internal_run_death_test);
0050 
0051 namespace testing {
0052 namespace internal {
0053 
0054 // Names of the flags (needed for parsing Google Test flags).
0055 const char kDeathTestStyleFlag[] = "death_test_style";
0056 const char kDeathTestUseFork[] = "death_test_use_fork";
0057 const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
0058 
0059 #if GTEST_HAS_DEATH_TEST
0060 
0061 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0062 /* class A needs to have dll-interface to be used by clients of class B */)
0063 
0064 // DeathTest is a class that hides much of the complexity of the
0065 // GTEST_DEATH_TEST_ macro.  It is abstract; its static Create method
0066 // returns a concrete class that depends on the prevailing death test
0067 // style, as defined by the --gtest_death_test_style and/or
0068 // --gtest_internal_run_death_test flags.
0069 
0070 // In describing the results of death tests, these terms are used with
0071 // the corresponding definitions:
0072 //
0073 // exit status:  The integer exit information in the format specified
0074 //               by wait(2)
0075 // exit code:    The integer code passed to exit(3), _exit(2), or
0076 //               returned from main()
0077 class GTEST_API_ DeathTest {
0078  public:
0079   // Create returns false if there was an error determining the
0080   // appropriate action to take for the current death test; for example,
0081   // if the gtest_death_test_style flag is set to an invalid value.
0082   // The LastMessage method will return a more detailed message in that
0083   // case.  Otherwise, the DeathTest pointer pointed to by the "test"
0084   // argument is set.  If the death test should be skipped, the pointer
0085   // is set to NULL; otherwise, it is set to the address of a new concrete
0086   // DeathTest object that controls the execution of the current test.
0087   static bool Create(const char* statement, Matcher<const std::string&> matcher,
0088                      const char* file, int line, DeathTest** test);
0089   DeathTest();
0090   virtual ~DeathTest() {}
0091 
0092   // A helper class that aborts a death test when it's deleted.
0093   class ReturnSentinel {
0094    public:
0095     explicit ReturnSentinel(DeathTest* test) : test_(test) {}
0096     ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
0097 
0098    private:
0099     DeathTest* const test_;
0100     ReturnSentinel(const ReturnSentinel&) = delete;
0101     ReturnSentinel& operator=(const ReturnSentinel&) = delete;
0102   } GTEST_ATTRIBUTE_UNUSED_;
0103 
0104   // An enumeration of possible roles that may be taken when a death
0105   // test is encountered.  EXECUTE means that the death test logic should
0106   // be executed immediately.  OVERSEE means that the program should prepare
0107   // the appropriate environment for a child process to execute the death
0108   // test, then wait for it to complete.
0109   enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
0110 
0111   // An enumeration of the three reasons that a test might be aborted.
0112   enum AbortReason {
0113     TEST_ENCOUNTERED_RETURN_STATEMENT,
0114     TEST_THREW_EXCEPTION,
0115     TEST_DID_NOT_DIE
0116   };
0117 
0118   // Assumes one of the above roles.
0119   virtual TestRole AssumeRole() = 0;
0120 
0121   // Waits for the death test to finish and returns its status.
0122   virtual int Wait() = 0;
0123 
0124   // Returns true if the death test passed; that is, the test process
0125   // exited during the test, its exit status matches a user-supplied
0126   // predicate, and its stderr output matches a user-supplied regular
0127   // expression.
0128   // The user-supplied predicate may be a macro expression rather
0129   // than a function pointer or functor, or else Wait and Passed could
0130   // be combined.
0131   virtual bool Passed(bool exit_status_ok) = 0;
0132 
0133   // Signals that the death test did not die as expected.
0134   virtual void Abort(AbortReason reason) = 0;
0135 
0136   // Returns a human-readable outcome message regarding the outcome of
0137   // the last death test.
0138   static const char* LastMessage();
0139 
0140   static void set_last_death_test_message(const std::string& message);
0141 
0142  private:
0143   // A string containing a description of the outcome of the last death test.
0144   static std::string last_death_test_message_;
0145 
0146   DeathTest(const DeathTest&) = delete;
0147   DeathTest& operator=(const DeathTest&) = delete;
0148 };
0149 
0150 GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
0151 
0152 // Factory interface for death tests.  May be mocked out for testing.
0153 class DeathTestFactory {
0154  public:
0155   virtual ~DeathTestFactory() {}
0156   virtual bool Create(const char* statement,
0157                       Matcher<const std::string&> matcher, const char* file,
0158                       int line, DeathTest** test) = 0;
0159 };
0160 
0161 // A concrete DeathTestFactory implementation for normal use.
0162 class DefaultDeathTestFactory : public DeathTestFactory {
0163  public:
0164   bool Create(const char* statement, Matcher<const std::string&> matcher,
0165               const char* file, int line, DeathTest** test) override;
0166 };
0167 
0168 // Returns true if exit_status describes a process that was terminated
0169 // by a signal, or exited normally with a nonzero exit code.
0170 GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
0171 
0172 // A string passed to EXPECT_DEATH (etc.) is caught by one of these overloads
0173 // and interpreted as a regex (rather than an Eq matcher) for legacy
0174 // compatibility.
0175 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0176     ::testing::internal::RE regex) {
0177   return ContainsRegex(regex.pattern());
0178 }
0179 inline Matcher<const ::std::string&> MakeDeathTestMatcher(const char* regex) {
0180   return ContainsRegex(regex);
0181 }
0182 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0183     const ::std::string& regex) {
0184   return ContainsRegex(regex);
0185 }
0186 
0187 // If a Matcher<const ::std::string&> is passed to EXPECT_DEATH (etc.), it's
0188 // used directly.
0189 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0190     Matcher<const ::std::string&> matcher) {
0191   return matcher;
0192 }
0193 
0194 // Traps C++ exceptions escaping statement and reports them as test
0195 // failures. Note that trapping SEH exceptions is not implemented here.
0196 #if GTEST_HAS_EXCEPTIONS
0197 #define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test)           \
0198   try {                                                                      \
0199     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement);               \
0200   } catch (const ::std::exception& gtest_exception) {                        \
0201     fprintf(                                                                 \
0202         stderr,                                                              \
0203         "\n%s: Caught std::exception-derived exception escaping the "        \
0204         "death test statement. Exception message: %s\n",                     \
0205         ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \
0206         gtest_exception.what());                                             \
0207     fflush(stderr);                                                          \
0208     death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
0209   } catch (...) {                                                            \
0210     death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
0211   }
0212 
0213 #else
0214 #define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
0215   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
0216 
0217 #endif
0218 
0219 // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
0220 // ASSERT_EXIT*, and EXPECT_EXIT*.
0221 #define GTEST_DEATH_TEST_(statement, predicate, regex_or_matcher, fail)        \
0222   GTEST_AMBIGUOUS_ELSE_BLOCKER_                                                \
0223   if (::testing::internal::AlwaysTrue()) {                                     \
0224     ::testing::internal::DeathTest* gtest_dt;                                  \
0225     if (!::testing::internal::DeathTest::Create(                               \
0226             #statement,                                                        \
0227             ::testing::internal::MakeDeathTestMatcher(regex_or_matcher),       \
0228             __FILE__, __LINE__, &gtest_dt)) {                                  \
0229       goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__);                        \
0230     }                                                                          \
0231     if (gtest_dt != nullptr) {                                                 \
0232       std::unique_ptr< ::testing::internal::DeathTest> gtest_dt_ptr(gtest_dt); \
0233       switch (gtest_dt->AssumeRole()) {                                        \
0234         case ::testing::internal::DeathTest::OVERSEE_TEST:                     \
0235           if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) {                \
0236             goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__);                  \
0237           }                                                                    \
0238           break;                                                               \
0239         case ::testing::internal::DeathTest::EXECUTE_TEST: {                   \
0240           ::testing::internal::DeathTest::ReturnSentinel gtest_sentinel(       \
0241               gtest_dt);                                                       \
0242           GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt);            \
0243           gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE);   \
0244           break;                                                               \
0245         }                                                                      \
0246       }                                                                        \
0247     }                                                                          \
0248   } else                                                                       \
0249     GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__)                                \
0250         : fail(::testing::internal::DeathTest::LastMessage())
0251 // The symbol "fail" here expands to something into which a message
0252 // can be streamed.
0253 
0254 // This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in
0255 // NDEBUG mode. In this case we need the statements to be executed and the macro
0256 // must accept a streamed message even though the message is never printed.
0257 // The regex object is not evaluated, but it is used to prevent "unused"
0258 // warnings and to avoid an expression that doesn't compile in debug mode.
0259 #define GTEST_EXECUTE_STATEMENT_(statement, regex_or_matcher)    \
0260   GTEST_AMBIGUOUS_ELSE_BLOCKER_                                  \
0261   if (::testing::internal::AlwaysTrue()) {                       \
0262     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement);   \
0263   } else if (!::testing::internal::AlwaysTrue()) {               \
0264     ::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \
0265   } else                                                         \
0266     ::testing::Message()
0267 
0268 // A class representing the parsed contents of the
0269 // --gtest_internal_run_death_test flag, as it existed when
0270 // RUN_ALL_TESTS was called.
0271 class InternalRunDeathTestFlag {
0272  public:
0273   InternalRunDeathTestFlag(const std::string& a_file, int a_line, int an_index,
0274                            int a_write_fd)
0275       : file_(a_file), line_(a_line), index_(an_index), write_fd_(a_write_fd) {}
0276 
0277   ~InternalRunDeathTestFlag() {
0278     if (write_fd_ >= 0) posix::Close(write_fd_);
0279   }
0280 
0281   const std::string& file() const { return file_; }
0282   int line() const { return line_; }
0283   int index() const { return index_; }
0284   int write_fd() const { return write_fd_; }
0285 
0286  private:
0287   std::string file_;
0288   int line_;
0289   int index_;
0290   int write_fd_;
0291 
0292   InternalRunDeathTestFlag(const InternalRunDeathTestFlag&) = delete;
0293   InternalRunDeathTestFlag& operator=(const InternalRunDeathTestFlag&) = delete;
0294 };
0295 
0296 // Returns a newly created InternalRunDeathTestFlag object with fields
0297 // initialized from the GTEST_FLAG(internal_run_death_test) flag if
0298 // the flag is specified; otherwise returns NULL.
0299 InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
0300 
0301 #endif  // GTEST_HAS_DEATH_TEST
0302 
0303 }  // namespace internal
0304 }  // namespace testing
0305 
0306 #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_