Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-25 10:04:15

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 #include <string_view>
0047 
0048 #include "gtest/gtest-matchers.h"
0049 #include "gtest/internal/gtest-internal.h"
0050 #include "gtest/internal/gtest-port.h"
0051 
0052 GTEST_DECLARE_string_(internal_run_death_test);
0053 
0054 namespace testing {
0055 namespace internal {
0056 
0057 // Name of the flag (needed for parsing Google Test flag).
0058 const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
0059 
0060 // A string passed to EXPECT_DEATH (etc.) is caught by one of these overloads
0061 // and interpreted as a regex (rather than an Eq matcher) for legacy
0062 // compatibility.
0063 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0064     ::testing::internal::RE regex) {
0065   return ContainsRegex(regex.pattern());
0066 }
0067 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0068     std::string_view regex) {
0069   return ContainsRegex(regex);
0070 }
0071 inline Matcher<const ::std::string&> MakeDeathTestMatcher(const char* regex) {
0072   return ContainsRegex(regex);
0073 }
0074 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0075     const ::std::string& regex) {
0076   return ContainsRegex(regex);
0077 }
0078 
0079 // If a Matcher<const ::std::string&> is passed to EXPECT_DEATH (etc.), it's
0080 // used directly.
0081 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
0082     Matcher<const ::std::string&> matcher) {
0083   return matcher;
0084 }
0085 
0086 #ifdef GTEST_HAS_DEATH_TEST
0087 
0088 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
0089 /* class A needs to have dll-interface to be used by clients of class B */)
0090 
0091 // DeathTest is a class that hides much of the complexity of the
0092 // GTEST_DEATH_TEST_ macro.  It is abstract; its static Create method
0093 // returns a concrete class that depends on the prevailing death test
0094 // style, as defined by the --gtest_death_test_style and/or
0095 // --gtest_internal_run_death_test flags.
0096 
0097 // In describing the results of death tests, these terms are used with
0098 // the corresponding definitions:
0099 //
0100 // exit status:  The integer exit information in the format specified
0101 //               by wait(2)
0102 // exit code:    The integer code passed to exit(3), _Exit(2), or
0103 //               returned from main()
0104 class GTEST_API_ [[nodiscard]] DeathTest {
0105  public:
0106   // Create returns false if there was an error determining the
0107   // appropriate action to take for the current death test; for example,
0108   // if the gtest_death_test_style flag is set to an invalid value.
0109   // The LastMessage method will return a more detailed message in that
0110   // case.  Otherwise, the DeathTest pointer pointed to by the "test"
0111   // argument is set.  If the death test should be skipped, the pointer
0112   // is set to NULL; otherwise, it is set to the address of a new concrete
0113   // DeathTest object that controls the execution of the current test.
0114   static bool Create(const char* statement, Matcher<const std::string&> matcher,
0115                      const char* file, int line, DeathTest** test);
0116   DeathTest();
0117   virtual ~DeathTest() = default;
0118 
0119   // A helper class that aborts a death test when it's deleted.
0120   class ReturnSentinel {
0121    public:
0122     explicit ReturnSentinel(DeathTest* test) : test_(test) {}
0123     ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
0124 
0125    private:
0126     DeathTest* const test_;
0127     ReturnSentinel(const ReturnSentinel&) = delete;
0128     ReturnSentinel& operator=(const ReturnSentinel&) = delete;
0129   };
0130 
0131   // An enumeration of possible roles that may be taken when a death
0132   // test is encountered.  EXECUTE means that the death test logic should
0133   // be executed immediately.  OVERSEE means that the program should prepare
0134   // the appropriate environment for a child process to execute the death
0135   // test, then wait for it to complete.
0136   enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
0137 
0138   // An enumeration of the three reasons that a test might be aborted.
0139   enum AbortReason {
0140     TEST_ENCOUNTERED_RETURN_STATEMENT,
0141     TEST_THREW_EXCEPTION,
0142     TEST_DID_NOT_DIE
0143   };
0144 
0145   // Assumes one of the above roles.
0146   virtual TestRole AssumeRole() = 0;
0147 
0148   // Waits for the death test to finish and returns its status.
0149   virtual int Wait() = 0;
0150 
0151   // Returns true if the death test passed; that is, the test process
0152   // exited during the test, its exit status matches a user-supplied
0153   // predicate, and its stderr output matches a user-supplied regular
0154   // expression.
0155   // The user-supplied predicate may be a macro expression rather
0156   // than a function pointer or functor, or else Wait and Passed could
0157   // be combined.
0158   virtual bool Passed(bool exit_status_ok) = 0;
0159 
0160   // Signals that the death test did not die as expected.
0161   virtual void Abort(AbortReason reason) = 0;
0162 
0163   // Returns a human-readable outcome message regarding the outcome of
0164   // the last death test.
0165   static const char* LastMessage();
0166 
0167   static void set_last_death_test_message(const std::string& message);
0168 
0169  private:
0170   // A string containing a description of the outcome of the last death test.
0171   static std::string last_death_test_message_;
0172 
0173   DeathTest(const DeathTest&) = delete;
0174   DeathTest& operator=(const DeathTest&) = delete;
0175 };
0176 
0177 GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4251
0178 
0179 // Factory interface for death tests.  May be mocked out for testing.
0180 class [[nodiscard]] DeathTestFactory {
0181  public:
0182   virtual ~DeathTestFactory() = default;
0183   virtual bool Create(const char* statement,
0184                       Matcher<const std::string&> matcher, const char* file,
0185                       int line, DeathTest** test) = 0;
0186 };
0187 
0188 // A concrete DeathTestFactory implementation for normal use.
0189 class [[nodiscard]] DefaultDeathTestFactory : public DeathTestFactory {
0190  public:
0191   bool Create(const char* statement, Matcher<const std::string&> matcher,
0192               const char* file, int line, DeathTest** test) override;
0193 };
0194 
0195 // Returns true if exit_status describes a process that was terminated
0196 // by a signal, or exited normally with a nonzero exit code.
0197 GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
0198 
0199 // Traps C++ exceptions escaping statement and reports them as test
0200 // failures. Note that trapping SEH exceptions is not implemented here.
0201 #if GTEST_HAS_EXCEPTIONS
0202 #define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test)           \
0203   try {                                                                      \
0204     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement);               \
0205   } catch (const ::std::exception& gtest_exception) {                        \
0206     fprintf(                                                                 \
0207         stderr,                                                              \
0208         "\n%s: Caught std::exception-derived exception escaping the "        \
0209         "death test statement. Exception message: %s\n",                     \
0210         ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \
0211         gtest_exception.what());                                             \
0212     fflush(stderr);                                                          \
0213     death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
0214   } catch (...) {                                                            \
0215     death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
0216   }
0217 
0218 #else
0219 #define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
0220   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
0221 
0222 #endif
0223 
0224 // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
0225 // ASSERT_EXIT*, and EXPECT_EXIT*.
0226 #define GTEST_DEATH_TEST_(statement, predicate, regex_or_matcher, fail)        \
0227   GTEST_AMBIGUOUS_ELSE_BLOCKER_                                                \
0228   if (::testing::internal::AlwaysTrue()) {                                     \
0229     ::testing::internal::DeathTest* gtest_dt;                                  \
0230     if (!::testing::internal::DeathTest::Create(                               \
0231             #statement,                                                        \
0232             ::testing::internal::MakeDeathTestMatcher(regex_or_matcher),       \
0233             __FILE__, __LINE__, &gtest_dt)) {                                  \
0234       goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__);                        \
0235     }                                                                          \
0236     if (gtest_dt != nullptr) {                                                 \
0237       const std::unique_ptr< ::testing::internal::DeathTest> gtest_dt_ptr(     \
0238           gtest_dt);                                                           \
0239       switch (gtest_dt->AssumeRole()) {                                        \
0240         case ::testing::internal::DeathTest::OVERSEE_TEST:                     \
0241           if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) {                \
0242             goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__);                  \
0243           }                                                                    \
0244           break;                                                               \
0245         case ::testing::internal::DeathTest::EXECUTE_TEST: {                   \
0246           const ::testing::internal::DeathTest::ReturnSentinel gtest_sentinel( \
0247               gtest_dt);                                                       \
0248           GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt);            \
0249           gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE);   \
0250           break;                                                               \
0251         }                                                                      \
0252       }                                                                        \
0253     }                                                                          \
0254   } else                                                                       \
0255     GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__)                                \
0256         : fail(::testing::internal::DeathTest::LastMessage())
0257 // The symbol "fail" here expands to something into which a message
0258 // can be streamed.
0259 
0260 // This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in
0261 // NDEBUG mode. In this case we need the statements to be executed and the macro
0262 // must accept a streamed message even though the message is never printed.
0263 // The regex object is not evaluated, but it is used to prevent "unused"
0264 // warnings and to avoid an expression that doesn't compile in debug mode.
0265 #define GTEST_EXECUTE_STATEMENT_(statement, regex_or_matcher)          \
0266   GTEST_AMBIGUOUS_ELSE_BLOCKER_                                        \
0267   if (::testing::internal::AlwaysTrue()) {                             \
0268     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement);         \
0269   } else if (!::testing::internal::AlwaysTrue()) {                     \
0270     (void)::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \
0271   } else                                                               \
0272     ::testing::Message()
0273 
0274 // A class representing the parsed contents of the
0275 // --gtest_internal_run_death_test flag, as it existed when
0276 // RUN_ALL_TESTS was called.
0277 class [[nodiscard]] InternalRunDeathTestFlag {
0278  public:
0279   InternalRunDeathTestFlag(const std::string& a_file, int a_line, int an_index,
0280                            int a_write_fd)
0281       : file_(a_file), line_(a_line), index_(an_index), write_fd_(a_write_fd) {}
0282 
0283   ~InternalRunDeathTestFlag() {
0284     if (write_fd_ >= 0) posix::Close(write_fd_);
0285   }
0286 
0287   const std::string& file() const { return file_; }
0288   int line() const { return line_; }
0289   int index() const { return index_; }
0290   int write_fd() const { return write_fd_; }
0291 
0292  private:
0293   std::string file_;
0294   int line_;
0295   int index_;
0296   int write_fd_;
0297 
0298   InternalRunDeathTestFlag(const InternalRunDeathTestFlag&) = delete;
0299   InternalRunDeathTestFlag& operator=(const InternalRunDeathTestFlag&) = delete;
0300 };
0301 
0302 // Returns a newly created InternalRunDeathTestFlag object with fields
0303 // initialized from the GTEST_FLAG(internal_run_death_test) flag if
0304 // the flag is specified; otherwise returns NULL.
0305 InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
0306 
0307 #endif  // GTEST_HAS_DEATH_TEST
0308 
0309 }  // namespace internal
0310 }  // namespace testing
0311 
0312 #endif  // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_