Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:18:07

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 #include <string>
0046 
0047 #include "gtest/gtest-matchers.h"
0048 #include "gtest/internal/gtest-internal.h"
0049 #include "gtest/internal/gtest-port.h"
0050 
0051 GTEST_DECLARE_string_(internal_run_death_test);
0052 
0053 namespace testing {
0054 namespace internal {
0055 
0056 // Name of the flag (needed for parsing Google Test flag).
0057 const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
0058 
0059 // A string passed to EXPECT_DEATH (etc.) is caught by one of these overloads
0060 // and interpreted as a regex (rather than an Eq matcher) for legacy
0061 // compatibility.
0062 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0063     ::testing::internal::RE regex) {
0064   return ContainsRegex(regex.pattern());
0065 }
0066 inline Matcher<const ::std::string&> MakeDeathTestMatcher(const char* regex) {
0067   return ContainsRegex(regex);
0068 }
0069 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0070     const ::std::string& regex) {
0071   return ContainsRegex(regex);
0072 }
0073 
0074 // If a Matcher<const ::std::string&> is passed to EXPECT_DEATH (etc.), it's
0075 // used directly.
0076 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0077     Matcher<const ::std::string&> matcher) {
0078   return matcher;
0079 }
0080 
0081 #ifdef GTEST_HAS_DEATH_TEST
0082 
0083 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0084 /* class A needs to have dll-interface to be used by clients of class B */)
0085 
0086 // DeathTest is a class that hides much of the complexity of the
0087 // GTEST_DEATH_TEST_ macro.  It is abstract; its static Create method
0088 // returns a concrete class that depends on the prevailing death test
0089 // style, as defined by the --gtest_death_test_style and/or
0090 // --gtest_internal_run_death_test flags.
0091 
0092 // In describing the results of death tests, these terms are used with
0093 // the corresponding definitions:
0094 //
0095 // exit status:  The integer exit information in the format specified
0096 //               by wait(2)
0097 // exit code:    The integer code passed to exit(3), _Exit(2), or
0098 //               returned from main()
0099 class GTEST_API_ DeathTest {
0100  public:
0101   // Create returns false if there was an error determining the
0102   // appropriate action to take for the current death test; for example,
0103   // if the gtest_death_test_style flag is set to an invalid value.
0104   // The LastMessage method will return a more detailed message in that
0105   // case.  Otherwise, the DeathTest pointer pointed to by the "test"
0106   // argument is set.  If the death test should be skipped, the pointer
0107   // is set to NULL; otherwise, it is set to the address of a new concrete
0108   // DeathTest object that controls the execution of the current test.
0109   static bool Create(const char* statement, Matcher<const std::string&> matcher,
0110                      const char* file, int line, DeathTest** test);
0111   DeathTest();
0112   virtual ~DeathTest() = default;
0113 
0114   // A helper class that aborts a death test when it's deleted.
0115   class ReturnSentinel {
0116    public:
0117     explicit ReturnSentinel(DeathTest* test) : test_(test) {}
0118     ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
0119 
0120    private:
0121     DeathTest* const test_;
0122     ReturnSentinel(const ReturnSentinel&) = delete;
0123     ReturnSentinel& operator=(const ReturnSentinel&) = delete;
0124   };
0125 
0126   // An enumeration of possible roles that may be taken when a death
0127   // test is encountered.  EXECUTE means that the death test logic should
0128   // be executed immediately.  OVERSEE means that the program should prepare
0129   // the appropriate environment for a child process to execute the death
0130   // test, then wait for it to complete.
0131   enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
0132 
0133   // An enumeration of the three reasons that a test might be aborted.
0134   enum AbortReason {
0135     TEST_ENCOUNTERED_RETURN_STATEMENT,
0136     TEST_THREW_EXCEPTION,
0137     TEST_DID_NOT_DIE
0138   };
0139 
0140   // Assumes one of the above roles.
0141   virtual TestRole AssumeRole() = 0;
0142 
0143   // Waits for the death test to finish and returns its status.
0144   virtual int Wait() = 0;
0145 
0146   // Returns true if the death test passed; that is, the test process
0147   // exited during the test, its exit status matches a user-supplied
0148   // predicate, and its stderr output matches a user-supplied regular
0149   // expression.
0150   // The user-supplied predicate may be a macro expression rather
0151   // than a function pointer or functor, or else Wait and Passed could
0152   // be combined.
0153   virtual bool Passed(bool exit_status_ok) = 0;
0154 
0155   // Signals that the death test did not die as expected.
0156   virtual void Abort(AbortReason reason) = 0;
0157 
0158   // Returns a human-readable outcome message regarding the outcome of
0159   // the last death test.
0160   static const char* LastMessage();
0161 
0162   static void set_last_death_test_message(const std::string& message);
0163 
0164  private:
0165   // A string containing a description of the outcome of the last death test.
0166   static std::string last_death_test_message_;
0167 
0168   DeathTest(const DeathTest&) = delete;
0169   DeathTest& operator=(const DeathTest&) = delete;
0170 };
0171 
0172 GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
0173 
0174 // Factory interface for death tests.  May be mocked out for testing.
0175 class DeathTestFactory {
0176  public:
0177   virtual ~DeathTestFactory() = default;
0178   virtual bool Create(const char* statement,
0179                       Matcher<const std::string&> matcher, const char* file,
0180                       int line, DeathTest** test) = 0;
0181 };
0182 
0183 // A concrete DeathTestFactory implementation for normal use.
0184 class DefaultDeathTestFactory : public DeathTestFactory {
0185  public:
0186   bool Create(const char* statement, Matcher<const std::string&> matcher,
0187               const char* file, int line, DeathTest** test) override;
0188 };
0189 
0190 // Returns true if exit_status describes a process that was terminated
0191 // by a signal, or exited normally with a nonzero exit code.
0192 GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
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           const ::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_