You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
9.8 KiB

  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2010 Google Inc. All Rights Reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """Tests Google Test's exception catching behavior.
  31. This script invokes googletest-catch-exceptions-test_ and
  32. googletest-catch-exceptions-ex-test_ (programs written with
  33. Google Test) and verifies their output.
  34. """
  35. import gtest_test_utils
  36. # Constants.
  37. FLAG_PREFIX = '--gtest_'
  38. LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
  39. NO_CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions=0'
  40. FILTER_FLAG = FLAG_PREFIX + 'filter'
  41. # Path to the googletest-catch-exceptions-ex-test_ binary, compiled with
  42. # exceptions enabled.
  43. EX_EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  44. 'googletest-catch-exceptions-ex-test_')
  45. # Path to the googletest-catch-exceptions-test_ binary, compiled with
  46. # exceptions disabled.
  47. EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  48. 'googletest-catch-exceptions-no-ex-test_')
  49. environ = gtest_test_utils.environ
  50. SetEnvVar = gtest_test_utils.SetEnvVar
  51. # Tests in this file run a Google-Test-based test program and expect it
  52. # to terminate prematurely. Therefore they are incompatible with
  53. # the premature-exit-file protocol by design. Unset the
  54. # premature-exit filepath to prevent Google Test from creating
  55. # the file.
  56. SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
  57. TEST_LIST = gtest_test_utils.Subprocess(
  58. [EXE_PATH, LIST_TESTS_FLAG], env=environ).output
  59. SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST
  60. if SUPPORTS_SEH_EXCEPTIONS:
  61. BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH], env=environ).output
  62. EX_BINARY_OUTPUT = gtest_test_utils.Subprocess(
  63. [EX_EXE_PATH], env=environ).output
  64. # The tests.
  65. if SUPPORTS_SEH_EXCEPTIONS:
  66. # pylint:disable-msg=C6302
  67. class CatchSehExceptionsTest(gtest_test_utils.TestCase):
  68. """Tests exception-catching behavior."""
  69. def TestSehExceptions(self, test_output):
  70. self.assert_('SEH exception with code 0x2a thrown '
  71. 'in the test fixture\'s constructor'
  72. in test_output)
  73. self.assert_('SEH exception with code 0x2a thrown '
  74. 'in the test fixture\'s destructor'
  75. in test_output)
  76. self.assert_('SEH exception with code 0x2a thrown in SetUpTestSuite()'
  77. in test_output)
  78. self.assert_('SEH exception with code 0x2a thrown in TearDownTestSuite()'
  79. in test_output)
  80. self.assert_('SEH exception with code 0x2a thrown in SetUp()'
  81. in test_output)
  82. self.assert_('SEH exception with code 0x2a thrown in TearDown()'
  83. in test_output)
  84. self.assert_('SEH exception with code 0x2a thrown in the test body'
  85. in test_output)
  86. def testCatchesSehExceptionsWithCxxExceptionsEnabled(self):
  87. self.TestSehExceptions(EX_BINARY_OUTPUT)
  88. def testCatchesSehExceptionsWithCxxExceptionsDisabled(self):
  89. self.TestSehExceptions(BINARY_OUTPUT)
  90. class CatchCxxExceptionsTest(gtest_test_utils.TestCase):
  91. """Tests C++ exception-catching behavior.
  92. Tests in this test case verify that:
  93. * C++ exceptions are caught and logged as C++ (not SEH) exceptions
  94. * Exception thrown affect the remainder of the test work flow in the
  95. expected manner.
  96. """
  97. def testCatchesCxxExceptionsInFixtureConstructor(self):
  98. self.assertTrue(
  99. 'C++ exception with description '
  100. '"Standard C++ exception" thrown '
  101. 'in the test fixture\'s constructor' in EX_BINARY_OUTPUT,
  102. EX_BINARY_OUTPUT)
  103. self.assert_('unexpected' not in EX_BINARY_OUTPUT,
  104. 'This failure belongs in this test only if '
  105. '"CxxExceptionInConstructorTest" (no quotes) '
  106. 'appears on the same line as words "called unexpectedly"')
  107. if ('CxxExceptionInDestructorTest.ThrowsExceptionInDestructor' in
  108. EX_BINARY_OUTPUT):
  109. def testCatchesCxxExceptionsInFixtureDestructor(self):
  110. self.assertTrue(
  111. 'C++ exception with description '
  112. '"Standard C++ exception" thrown '
  113. 'in the test fixture\'s destructor' in EX_BINARY_OUTPUT,
  114. EX_BINARY_OUTPUT)
  115. self.assertTrue(
  116. 'CxxExceptionInDestructorTest::TearDownTestSuite() '
  117. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  118. def testCatchesCxxExceptionsInSetUpTestCase(self):
  119. self.assertTrue(
  120. 'C++ exception with description "Standard C++ exception"'
  121. ' thrown in SetUpTestSuite()' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  122. self.assertTrue(
  123. 'CxxExceptionInConstructorTest::TearDownTestSuite() '
  124. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  125. self.assertTrue(
  126. 'CxxExceptionInSetUpTestSuiteTest constructor '
  127. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  128. self.assertTrue(
  129. 'CxxExceptionInSetUpTestSuiteTest destructor '
  130. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  131. self.assertTrue(
  132. 'CxxExceptionInSetUpTestSuiteTest::SetUp() '
  133. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  134. self.assertTrue(
  135. 'CxxExceptionInSetUpTestSuiteTest::TearDown() '
  136. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  137. self.assertTrue(
  138. 'CxxExceptionInSetUpTestSuiteTest test body '
  139. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  140. def testCatchesCxxExceptionsInTearDownTestCase(self):
  141. self.assertTrue(
  142. 'C++ exception with description "Standard C++ exception"'
  143. ' thrown in TearDownTestSuite()' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  144. def testCatchesCxxExceptionsInSetUp(self):
  145. self.assertTrue(
  146. 'C++ exception with description "Standard C++ exception"'
  147. ' thrown in SetUp()' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  148. self.assertTrue(
  149. 'CxxExceptionInSetUpTest::TearDownTestSuite() '
  150. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  151. self.assertTrue(
  152. 'CxxExceptionInSetUpTest destructor '
  153. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  154. self.assertTrue(
  155. 'CxxExceptionInSetUpTest::TearDown() '
  156. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  157. self.assert_('unexpected' not in EX_BINARY_OUTPUT,
  158. 'This failure belongs in this test only if '
  159. '"CxxExceptionInSetUpTest" (no quotes) '
  160. 'appears on the same line as words "called unexpectedly"')
  161. def testCatchesCxxExceptionsInTearDown(self):
  162. self.assertTrue(
  163. 'C++ exception with description "Standard C++ exception"'
  164. ' thrown in TearDown()' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  165. self.assertTrue(
  166. 'CxxExceptionInTearDownTest::TearDownTestSuite() '
  167. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  168. self.assertTrue(
  169. 'CxxExceptionInTearDownTest destructor '
  170. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  171. def testCatchesCxxExceptionsInTestBody(self):
  172. self.assertTrue(
  173. 'C++ exception with description "Standard C++ exception"'
  174. ' thrown in the test body' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  175. self.assertTrue(
  176. 'CxxExceptionInTestBodyTest::TearDownTestSuite() '
  177. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  178. self.assertTrue(
  179. 'CxxExceptionInTestBodyTest destructor '
  180. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  181. self.assertTrue(
  182. 'CxxExceptionInTestBodyTest::TearDown() '
  183. 'called as expected.' in EX_BINARY_OUTPUT, EX_BINARY_OUTPUT)
  184. def testCatchesNonStdCxxExceptions(self):
  185. self.assertTrue(
  186. 'Unknown C++ exception thrown in the test body' in EX_BINARY_OUTPUT,
  187. EX_BINARY_OUTPUT)
  188. def testUnhandledCxxExceptionsAbortTheProgram(self):
  189. # Filters out SEH exception tests on Windows. Unhandled SEH exceptions
  190. # cause tests to show pop-up windows there.
  191. FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*'
  192. # By default, Google Test doesn't catch the exceptions.
  193. uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess(
  194. [EX_EXE_PATH,
  195. NO_CATCH_EXCEPTIONS_FLAG,
  196. FITLER_OUT_SEH_TESTS_FLAG],
  197. env=environ).output
  198. self.assert_('Unhandled C++ exception terminating the program'
  199. in uncaught_exceptions_ex_binary_output)
  200. self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output)
  201. if __name__ == '__main__':
  202. gtest_test_utils.Main()