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 31"""Tests Google Test's exception catching behavior. 32 33This script invokes googletest-catch-exceptions-test_ and 34googletest-catch-exceptions-ex-test_ (programs written with 35Google Test) and verifies their output. 36""" 37 38import gtest_test_utils 39 40# Constants. 41FLAG_PREFIX = '--gtest_' 42LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' 43NO_CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions=0' 44FILTER_FLAG = FLAG_PREFIX + 'filter' 45 46# Path to the googletest-catch-exceptions-ex-test_ binary, compiled with 47# exceptions enabled. 48EX_EXE_PATH = gtest_test_utils.GetTestExecutablePath( 49 'googletest-catch-exceptions-ex-test_') 50 51# Path to the googletest-catch-exceptions-test_ binary, compiled with 52# exceptions disabled. 53EXE_PATH = gtest_test_utils.GetTestExecutablePath( 54 'googletest-catch-exceptions-no-ex-test_') 55 56environ = gtest_test_utils.environ 57SetEnvVar = gtest_test_utils.SetEnvVar 58 59# Tests in this file run a Google-Test-based test program and expect it 60# to terminate prematurely. Therefore they are incompatible with 61# the premature-exit-file protocol by design. Unset the 62# premature-exit filepath to prevent Google Test from creating 63# the file. 64SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None) 65 66TEST_LIST = gtest_test_utils.Subprocess( 67 [EXE_PATH, LIST_TESTS_FLAG], env=environ).output 68 69SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST 70 71if SUPPORTS_SEH_EXCEPTIONS: 72 BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH], env=environ).output 73 74EX_BINARY_OUTPUT = gtest_test_utils.Subprocess( 75 [EX_EXE_PATH], env=environ).output 76 77 78# The tests. 79if SUPPORTS_SEH_EXCEPTIONS: 80 # pylint:disable-msg=C6302 81 class CatchSehExceptionsTest(gtest_test_utils.TestCase): 82 """Tests exception-catching behavior.""" 83 84 85 def TestSehExceptions(self, test_output): 86 self.assert_('SEH exception with code 0x2a thrown ' 87 'in the test fixture\'s constructor' 88 in test_output) 89 self.assert_('SEH exception with code 0x2a thrown ' 90 'in the test fixture\'s destructor' 91 in test_output) 92 self.assert_('SEH exception with code 0x2a thrown in SetUpTestCase()' 93 in test_output) 94 self.assert_('SEH exception with code 0x2a thrown in TearDownTestCase()' 95 in test_output) 96 self.assert_('SEH exception with code 0x2a thrown in SetUp()' 97 in test_output) 98 self.assert_('SEH exception with code 0x2a thrown in TearDown()' 99 in test_output) 100 self.assert_('SEH exception with code 0x2a thrown in the test body' 101 in test_output) 102 103 def testCatchesSehExceptionsWithCxxExceptionsEnabled(self): 104 self.TestSehExceptions(EX_BINARY_OUTPUT) 105 106 def testCatchesSehExceptionsWithCxxExceptionsDisabled(self): 107 self.TestSehExceptions(BINARY_OUTPUT) 108 109 110class CatchCxxExceptionsTest(gtest_test_utils.TestCase): 111 """Tests C++ exception-catching behavior. 112 113 Tests in this test case verify that: 114 * C++ exceptions are caught and logged as C++ (not SEH) exceptions 115 * Exception thrown affect the remainder of the test work flow in the 116 expected manner. 117 """ 118 119 def testCatchesCxxExceptionsInFixtureConstructor(self): 120 self.assert_('C++ exception with description ' 121 '"Standard C++ exception" thrown ' 122 'in the test fixture\'s constructor' 123 in EX_BINARY_OUTPUT) 124 self.assert_('unexpected' not in EX_BINARY_OUTPUT, 125 'This failure belongs in this test only if ' 126 '"CxxExceptionInConstructorTest" (no quotes) ' 127 'appears on the same line as words "called unexpectedly"') 128 129 if ('CxxExceptionInDestructorTest.ThrowsExceptionInDestructor' in 130 EX_BINARY_OUTPUT): 131 132 def testCatchesCxxExceptionsInFixtureDestructor(self): 133 self.assert_('C++ exception with description ' 134 '"Standard C++ exception" thrown ' 135 'in the test fixture\'s destructor' 136 in EX_BINARY_OUTPUT) 137 self.assert_('CxxExceptionInDestructorTest::TearDownTestCase() ' 138 'called as expected.' 139 in EX_BINARY_OUTPUT) 140 141 def testCatchesCxxExceptionsInSetUpTestCase(self): 142 self.assert_('C++ exception with description "Standard C++ exception"' 143 ' thrown in SetUpTestCase()' 144 in EX_BINARY_OUTPUT) 145 self.assert_('CxxExceptionInConstructorTest::TearDownTestCase() ' 146 'called as expected.' 147 in EX_BINARY_OUTPUT) 148 self.assert_('CxxExceptionInSetUpTestCaseTest constructor ' 149 'called as expected.' 150 in EX_BINARY_OUTPUT) 151 self.assert_('CxxExceptionInSetUpTestCaseTest destructor ' 152 'called as expected.' 153 in EX_BINARY_OUTPUT) 154 self.assert_('CxxExceptionInSetUpTestCaseTest::SetUp() ' 155 'called as expected.' 156 in EX_BINARY_OUTPUT) 157 self.assert_('CxxExceptionInSetUpTestCaseTest::TearDown() ' 158 'called as expected.' 159 in EX_BINARY_OUTPUT) 160 self.assert_('CxxExceptionInSetUpTestCaseTest test body ' 161 'called as expected.' 162 in EX_BINARY_OUTPUT) 163 164 def testCatchesCxxExceptionsInTearDownTestCase(self): 165 self.assert_('C++ exception with description "Standard C++ exception"' 166 ' thrown in TearDownTestCase()' 167 in EX_BINARY_OUTPUT) 168 169 def testCatchesCxxExceptionsInSetUp(self): 170 self.assert_('C++ exception with description "Standard C++ exception"' 171 ' thrown in SetUp()' 172 in EX_BINARY_OUTPUT) 173 self.assert_('CxxExceptionInSetUpTest::TearDownTestCase() ' 174 'called as expected.' 175 in EX_BINARY_OUTPUT) 176 self.assert_('CxxExceptionInSetUpTest destructor ' 177 'called as expected.' 178 in EX_BINARY_OUTPUT) 179 self.assert_('CxxExceptionInSetUpTest::TearDown() ' 180 'called as expected.' 181 in EX_BINARY_OUTPUT) 182 self.assert_('unexpected' not in EX_BINARY_OUTPUT, 183 'This failure belongs in this test only if ' 184 '"CxxExceptionInSetUpTest" (no quotes) ' 185 'appears on the same line as words "called unexpectedly"') 186 187 def testCatchesCxxExceptionsInTearDown(self): 188 self.assert_('C++ exception with description "Standard C++ exception"' 189 ' thrown in TearDown()' 190 in EX_BINARY_OUTPUT) 191 self.assert_('CxxExceptionInTearDownTest::TearDownTestCase() ' 192 'called as expected.' 193 in EX_BINARY_OUTPUT) 194 self.assert_('CxxExceptionInTearDownTest destructor ' 195 'called as expected.' 196 in EX_BINARY_OUTPUT) 197 198 def testCatchesCxxExceptionsInTestBody(self): 199 self.assert_('C++ exception with description "Standard C++ exception"' 200 ' thrown in the test body' 201 in EX_BINARY_OUTPUT) 202 self.assert_('CxxExceptionInTestBodyTest::TearDownTestCase() ' 203 'called as expected.' 204 in EX_BINARY_OUTPUT) 205 self.assert_('CxxExceptionInTestBodyTest destructor ' 206 'called as expected.' 207 in EX_BINARY_OUTPUT) 208 self.assert_('CxxExceptionInTestBodyTest::TearDown() ' 209 'called as expected.' 210 in EX_BINARY_OUTPUT) 211 212 def testCatchesNonStdCxxExceptions(self): 213 self.assert_('Unknown C++ exception thrown in the test body' 214 in EX_BINARY_OUTPUT) 215 216 def testUnhandledCxxExceptionsAbortTheProgram(self): 217 # Filters out SEH exception tests on Windows. Unhandled SEH exceptions 218 # cause tests to show pop-up windows there. 219 FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*' 220 # By default, Google Test doesn't catch the exceptions. 221 uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess( 222 [EX_EXE_PATH, 223 NO_CATCH_EXCEPTIONS_FLAG, 224 FITLER_OUT_SEH_TESTS_FLAG], 225 env=environ).output 226 227 self.assert_('Unhandled C++ exception terminating the program' 228 in uncaught_exceptions_ex_binary_output) 229 self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output) 230 231 232if __name__ == '__main__': 233 gtest_test_utils.Main() 234