• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
31 //
32 // The Google C++ Testing Framework (Google Test)
33 //
34 // This header file defines internal utilities needed for implementing
35 // death tests.  They are subject to change without notice.
36 
37 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
38 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
39 
40 #include <gtest/internal/gtest-internal.h>
41 
42 #if GTEST_HAS_DEATH_TEST && GTEST_OS_WINDOWS
43 #include <io.h>
44 #endif  // GTEST_HAS_DEATH_TEST && GTEST_OS_WINDOWS
45 
46 namespace testing {
47 namespace internal {
48 
49 GTEST_DECLARE_string_(internal_run_death_test);
50 
51 // Names of the flags (needed for parsing Google Test flags).
52 const char kDeathTestStyleFlag[] = "death_test_style";
53 const char kDeathTestUseFork[] = "death_test_use_fork";
54 const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
55 
56 #if GTEST_HAS_DEATH_TEST
57 
58 // DeathTest is a class that hides much of the complexity of the
59 // GTEST_DEATH_TEST_ macro.  It is abstract; its static Create method
60 // returns a concrete class that depends on the prevailing death test
61 // style, as defined by the --gtest_death_test_style and/or
62 // --gtest_internal_run_death_test flags.
63 
64 // In describing the results of death tests, these terms are used with
65 // the corresponding definitions:
66 //
67 // exit status:  The integer exit information in the format specified
68 //               by wait(2)
69 // exit code:    The integer code passed to exit(3), _exit(2), or
70 //               returned from main()
71 class DeathTest {
72  public:
73   // Create returns false if there was an error determining the
74   // appropriate action to take for the current death test; for example,
75   // if the gtest_death_test_style flag is set to an invalid value.
76   // The LastMessage method will return a more detailed message in that
77   // case.  Otherwise, the DeathTest pointer pointed to by the "test"
78   // argument is set.  If the death test should be skipped, the pointer
79   // is set to NULL; otherwise, it is set to the address of a new concrete
80   // DeathTest object that controls the execution of the current test.
81   static bool Create(const char* statement, const RE* regex,
82                      const char* file, int line, DeathTest** test);
83   DeathTest();
~DeathTest()84   virtual ~DeathTest() { }
85 
86   // A helper class that aborts a death test when it's deleted.
87   class ReturnSentinel {
88    public:
ReturnSentinel(DeathTest * test)89     explicit ReturnSentinel(DeathTest* test) : test_(test) { }
~ReturnSentinel()90     ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
91    private:
92     DeathTest* const test_;
93     GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);
94   } GTEST_ATTRIBUTE_UNUSED_;
95 
96   // An enumeration of possible roles that may be taken when a death
97   // test is encountered.  EXECUTE means that the death test logic should
98   // be executed immediately.  OVERSEE means that the program should prepare
99   // the appropriate environment for a child process to execute the death
100   // test, then wait for it to complete.
101   enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
102 
103   // An enumeration of the two reasons that a test might be aborted.
104   enum AbortReason { TEST_ENCOUNTERED_RETURN_STATEMENT, TEST_DID_NOT_DIE };
105 
106   // Assumes one of the above roles.
107   virtual TestRole AssumeRole() = 0;
108 
109   // Waits for the death test to finish and returns its status.
110   virtual int Wait() = 0;
111 
112   // Returns true if the death test passed; that is, the test process
113   // exited during the test, its exit status matches a user-supplied
114   // predicate, and its stderr output matches a user-supplied regular
115   // expression.
116   // The user-supplied predicate may be a macro expression rather
117   // than a function pointer or functor, or else Wait and Passed could
118   // be combined.
119   virtual bool Passed(bool exit_status_ok) = 0;
120 
121   // Signals that the death test did not die as expected.
122   virtual void Abort(AbortReason reason) = 0;
123 
124   // Returns a human-readable outcome message regarding the outcome of
125   // the last death test.
126   static const char* LastMessage();
127 
128   static void set_last_death_test_message(const String& message);
129 
130  private:
131   // A string containing a description of the outcome of the last death test.
132   static String last_death_test_message_;
133 
134   GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest);
135 };
136 
137 // Factory interface for death tests.  May be mocked out for testing.
138 class DeathTestFactory {
139  public:
~DeathTestFactory()140   virtual ~DeathTestFactory() { }
141   virtual bool Create(const char* statement, const RE* regex,
142                       const char* file, int line, DeathTest** test) = 0;
143 };
144 
145 // A concrete DeathTestFactory implementation for normal use.
146 class DefaultDeathTestFactory : public DeathTestFactory {
147  public:
148   virtual bool Create(const char* statement, const RE* regex,
149                       const char* file, int line, DeathTest** test);
150 };
151 
152 // Returns true if exit_status describes a process that was terminated
153 // by a signal, or exited normally with a nonzero exit code.
154 bool ExitedUnsuccessfully(int exit_status);
155 
156 // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
157 // ASSERT_EXIT*, and EXPECT_EXIT*.
158 #define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \
159   GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
160   if (true) { \
161     const ::testing::internal::RE& gtest_regex = (regex); \
162     ::testing::internal::DeathTest* gtest_dt; \
163     if (!::testing::internal::DeathTest::Create(#statement, &gtest_regex, \
164         __FILE__, __LINE__, &gtest_dt)) { \
165       goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
166     } \
167     if (gtest_dt != NULL) { \
168       ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \
169           gtest_dt_ptr(gtest_dt); \
170       switch (gtest_dt->AssumeRole()) { \
171         case ::testing::internal::DeathTest::OVERSEE_TEST: \
172           if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
173             goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
174           } \
175           break; \
176         case ::testing::internal::DeathTest::EXECUTE_TEST: { \
177           ::testing::internal::DeathTest::ReturnSentinel \
178               gtest_sentinel(gtest_dt); \
179           GTEST_HIDE_UNREACHABLE_CODE_(statement); \
180           gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
181           break; \
182         } \
183       } \
184     } \
185   } else \
186     GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \
187       fail(::testing::internal::DeathTest::LastMessage())
188 // The symbol "fail" here expands to something into which a message
189 // can be streamed.
190 
191 // A class representing the parsed contents of the
192 // --gtest_internal_run_death_test flag, as it existed when
193 // RUN_ALL_TESTS was called.
194 class InternalRunDeathTestFlag {
195  public:
InternalRunDeathTestFlag(const String & file,int line,int index,int status_fd)196   InternalRunDeathTestFlag(const String& file,
197                            int line,
198                            int index,
199                            int status_fd)
200       : file_(file), line_(line), index_(index), status_fd_(status_fd) {}
201 
~InternalRunDeathTestFlag()202   ~InternalRunDeathTestFlag() {
203     if (status_fd_ >= 0)
204 // Suppress MSVC complaints about POSIX functions.
205 #ifdef _MSC_VER
206 #pragma warning(push)
207 #pragma warning(disable: 4996)
208 #endif  // _MSC_VER
209       close(status_fd_);
210 #ifdef _MSC_VER
211 #pragma warning(pop)
212 #endif  // _MSC_VER
213   }
214 
file()215   String file() const { return file_; }
line()216   int line() const { return line_; }
index()217   int index() const { return index_; }
status_fd()218   int status_fd() const { return status_fd_; }
219 
220  private:
221   String file_;
222   int line_;
223   int index_;
224   int status_fd_;
225 
226   GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag);
227 };
228 
229 // Returns a newly created InternalRunDeathTestFlag object with fields
230 // initialized from the GTEST_FLAG(internal_run_death_test) flag if
231 // the flag is specified; otherwise returns NULL.
232 InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
233 
234 #endif  // GTEST_HAS_DEATH_TEST
235 
236 }  // namespace internal
237 }  // namespace testing
238 
239 #endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
240