1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest_catch_exceptions_test.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,223 @@ 1.4 +#!/usr/bin/env python 1.5 +# 1.6 +# Copyright 2010 Google Inc. All Rights Reserved. 1.7 +# 1.8 +# Redistribution and use in source and binary forms, with or without 1.9 +# modification, are permitted provided that the following conditions are 1.10 +# met: 1.11 +# 1.12 +# * Redistributions of source code must retain the above copyright 1.13 +# notice, this list of conditions and the following disclaimer. 1.14 +# * Redistributions in binary form must reproduce the above 1.15 +# copyright notice, this list of conditions and the following disclaimer 1.16 +# in the documentation and/or other materials provided with the 1.17 +# distribution. 1.18 +# * Neither the name of Google Inc. nor the names of its 1.19 +# contributors may be used to endorse or promote products derived from 1.20 +# this software without specific prior written permission. 1.21 +# 1.22 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.23 +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.24 +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.25 +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.26 +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.27 +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.28 +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.29 +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.30 +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.31 +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.32 +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.33 + 1.34 +"""Tests Google Test's exception catching behavior. 1.35 + 1.36 +This script invokes gtest_catch_exceptions_test_ and 1.37 +gtest_catch_exceptions_ex_test_ (programs written with 1.38 +Google Test) and verifies their output. 1.39 +""" 1.40 + 1.41 +__author__ = 'vladl@google.com (Vlad Losev)' 1.42 + 1.43 +import os 1.44 + 1.45 +import gtest_test_utils 1.46 + 1.47 +# Constants. 1.48 +FLAG_PREFIX = '--gtest_' 1.49 +LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests' 1.50 +NO_CATCH_EXCEPTIONS_FLAG = FLAG_PREFIX + 'catch_exceptions=0' 1.51 +FILTER_FLAG = FLAG_PREFIX + 'filter' 1.52 + 1.53 +# Path to the gtest_catch_exceptions_ex_test_ binary, compiled with 1.54 +# exceptions enabled. 1.55 +EX_EXE_PATH = gtest_test_utils.GetTestExecutablePath( 1.56 + 'gtest_catch_exceptions_ex_test_') 1.57 + 1.58 +# Path to the gtest_catch_exceptions_test_ binary, compiled with 1.59 +# exceptions disabled. 1.60 +EXE_PATH = gtest_test_utils.GetTestExecutablePath( 1.61 + 'gtest_catch_exceptions_no_ex_test_') 1.62 + 1.63 +TEST_LIST = gtest_test_utils.Subprocess([EXE_PATH, LIST_TESTS_FLAG]).output 1.64 + 1.65 +SUPPORTS_SEH_EXCEPTIONS = 'ThrowsSehException' in TEST_LIST 1.66 + 1.67 +if SUPPORTS_SEH_EXCEPTIONS: 1.68 + BINARY_OUTPUT = gtest_test_utils.Subprocess([EXE_PATH]).output 1.69 + 1.70 +EX_BINARY_OUTPUT = gtest_test_utils.Subprocess([EX_EXE_PATH]).output 1.71 + 1.72 +# The tests. 1.73 +if SUPPORTS_SEH_EXCEPTIONS: 1.74 + # pylint:disable-msg=C6302 1.75 + class CatchSehExceptionsTest(gtest_test_utils.TestCase): 1.76 + """Tests exception-catching behavior.""" 1.77 + 1.78 + 1.79 + def TestSehExceptions(self, test_output): 1.80 + self.assert_('SEH exception with code 0x2a thrown ' 1.81 + 'in the test fixture\'s constructor' 1.82 + in test_output) 1.83 + self.assert_('SEH exception with code 0x2a thrown ' 1.84 + 'in the test fixture\'s destructor' 1.85 + in test_output) 1.86 + self.assert_('SEH exception with code 0x2a thrown in SetUpTestCase()' 1.87 + in test_output) 1.88 + self.assert_('SEH exception with code 0x2a thrown in TearDownTestCase()' 1.89 + in test_output) 1.90 + self.assert_('SEH exception with code 0x2a thrown in SetUp()' 1.91 + in test_output) 1.92 + self.assert_('SEH exception with code 0x2a thrown in TearDown()' 1.93 + in test_output) 1.94 + self.assert_('SEH exception with code 0x2a thrown in the test body' 1.95 + in test_output) 1.96 + 1.97 + def testCatchesSehExceptionsWithCxxExceptionsEnabled(self): 1.98 + self.TestSehExceptions(EX_BINARY_OUTPUT) 1.99 + 1.100 + def testCatchesSehExceptionsWithCxxExceptionsDisabled(self): 1.101 + self.TestSehExceptions(BINARY_OUTPUT) 1.102 + 1.103 + 1.104 +class CatchCxxExceptionsTest(gtest_test_utils.TestCase): 1.105 + """Tests C++ exception-catching behavior. 1.106 + 1.107 + Tests in this test case verify that: 1.108 + * C++ exceptions are caught and logged as C++ (not SEH) exceptions 1.109 + * Exception thrown affect the remainder of the test work flow in the 1.110 + expected manner. 1.111 + """ 1.112 + 1.113 + def testCatchesCxxExceptionsInFixtureConstructor(self): 1.114 + self.assert_('C++ exception with description ' 1.115 + '"Standard C++ exception" thrown ' 1.116 + 'in the test fixture\'s constructor' 1.117 + in EX_BINARY_OUTPUT) 1.118 + self.assert_('unexpected' not in EX_BINARY_OUTPUT, 1.119 + 'This failure belongs in this test only if ' 1.120 + '"CxxExceptionInConstructorTest" (no quotes) ' 1.121 + 'appears on the same line as words "called unexpectedly"') 1.122 + 1.123 + if ('CxxExceptionInDestructorTest.ThrowsExceptionInDestructor' in 1.124 + EX_BINARY_OUTPUT): 1.125 + 1.126 + def testCatchesCxxExceptionsInFixtureDestructor(self): 1.127 + self.assert_('C++ exception with description ' 1.128 + '"Standard C++ exception" thrown ' 1.129 + 'in the test fixture\'s destructor' 1.130 + in EX_BINARY_OUTPUT) 1.131 + self.assert_('CxxExceptionInDestructorTest::TearDownTestCase() ' 1.132 + 'called as expected.' 1.133 + in EX_BINARY_OUTPUT) 1.134 + 1.135 + def testCatchesCxxExceptionsInSetUpTestCase(self): 1.136 + self.assert_('C++ exception with description "Standard C++ exception"' 1.137 + ' thrown in SetUpTestCase()' 1.138 + in EX_BINARY_OUTPUT) 1.139 + self.assert_('CxxExceptionInConstructorTest::TearDownTestCase() ' 1.140 + 'called as expected.' 1.141 + in EX_BINARY_OUTPUT) 1.142 + self.assert_('CxxExceptionInSetUpTestCaseTest constructor ' 1.143 + 'called as expected.' 1.144 + in EX_BINARY_OUTPUT) 1.145 + self.assert_('CxxExceptionInSetUpTestCaseTest destructor ' 1.146 + 'called as expected.' 1.147 + in EX_BINARY_OUTPUT) 1.148 + self.assert_('CxxExceptionInSetUpTestCaseTest::SetUp() ' 1.149 + 'called as expected.' 1.150 + in EX_BINARY_OUTPUT) 1.151 + self.assert_('CxxExceptionInSetUpTestCaseTest::TearDown() ' 1.152 + 'called as expected.' 1.153 + in EX_BINARY_OUTPUT) 1.154 + self.assert_('CxxExceptionInSetUpTestCaseTest test body ' 1.155 + 'called as expected.' 1.156 + in EX_BINARY_OUTPUT) 1.157 + 1.158 + def testCatchesCxxExceptionsInTearDownTestCase(self): 1.159 + self.assert_('C++ exception with description "Standard C++ exception"' 1.160 + ' thrown in TearDownTestCase()' 1.161 + in EX_BINARY_OUTPUT) 1.162 + 1.163 + def testCatchesCxxExceptionsInSetUp(self): 1.164 + self.assert_('C++ exception with description "Standard C++ exception"' 1.165 + ' thrown in SetUp()' 1.166 + in EX_BINARY_OUTPUT) 1.167 + self.assert_('CxxExceptionInSetUpTest::TearDownTestCase() ' 1.168 + 'called as expected.' 1.169 + in EX_BINARY_OUTPUT) 1.170 + self.assert_('CxxExceptionInSetUpTest destructor ' 1.171 + 'called as expected.' 1.172 + in EX_BINARY_OUTPUT) 1.173 + self.assert_('CxxExceptionInSetUpTest::TearDown() ' 1.174 + 'called as expected.' 1.175 + in EX_BINARY_OUTPUT) 1.176 + self.assert_('unexpected' not in EX_BINARY_OUTPUT, 1.177 + 'This failure belongs in this test only if ' 1.178 + '"CxxExceptionInSetUpTest" (no quotes) ' 1.179 + 'appears on the same line as words "called unexpectedly"') 1.180 + 1.181 + def testCatchesCxxExceptionsInTearDown(self): 1.182 + self.assert_('C++ exception with description "Standard C++ exception"' 1.183 + ' thrown in TearDown()' 1.184 + in EX_BINARY_OUTPUT) 1.185 + self.assert_('CxxExceptionInTearDownTest::TearDownTestCase() ' 1.186 + 'called as expected.' 1.187 + in EX_BINARY_OUTPUT) 1.188 + self.assert_('CxxExceptionInTearDownTest destructor ' 1.189 + 'called as expected.' 1.190 + in EX_BINARY_OUTPUT) 1.191 + 1.192 + def testCatchesCxxExceptionsInTestBody(self): 1.193 + self.assert_('C++ exception with description "Standard C++ exception"' 1.194 + ' thrown in the test body' 1.195 + in EX_BINARY_OUTPUT) 1.196 + self.assert_('CxxExceptionInTestBodyTest::TearDownTestCase() ' 1.197 + 'called as expected.' 1.198 + in EX_BINARY_OUTPUT) 1.199 + self.assert_('CxxExceptionInTestBodyTest destructor ' 1.200 + 'called as expected.' 1.201 + in EX_BINARY_OUTPUT) 1.202 + self.assert_('CxxExceptionInTestBodyTest::TearDown() ' 1.203 + 'called as expected.' 1.204 + in EX_BINARY_OUTPUT) 1.205 + 1.206 + def testCatchesNonStdCxxExceptions(self): 1.207 + self.assert_('Unknown C++ exception thrown in the test body' 1.208 + in EX_BINARY_OUTPUT) 1.209 + 1.210 + def testUnhandledCxxExceptionsAbortTheProgram(self): 1.211 + # Filters out SEH exception tests on Windows. Unhandled SEH exceptions 1.212 + # cause tests to show pop-up windows there. 1.213 + FITLER_OUT_SEH_TESTS_FLAG = FILTER_FLAG + '=-*Seh*' 1.214 + # By default, Google Test doesn't catch the exceptions. 1.215 + uncaught_exceptions_ex_binary_output = gtest_test_utils.Subprocess( 1.216 + [EX_EXE_PATH, 1.217 + NO_CATCH_EXCEPTIONS_FLAG, 1.218 + FITLER_OUT_SEH_TESTS_FLAG]).output 1.219 + 1.220 + self.assert_('Unhandled C++ exception terminating the program' 1.221 + in uncaught_exceptions_ex_binary_output) 1.222 + self.assert_('unexpected' not in uncaught_exceptions_ex_binary_output) 1.223 + 1.224 + 1.225 +if __name__ == '__main__': 1.226 + gtest_test_utils.Main()