1 #!/usr/bin/env python 2 # 3 # Copyright 2006, Google Inc. 4 # All rights reserved. 5 # 6 # Redistribution and use in source and binary forms, with or without 7 # modification, are permitted provided that the following conditions are 8 # met: 9 # 10 # * Redistributions of source code must retain the above copyright 11 # notice, this list of conditions and the following disclaimer. 12 # * Redistributions in binary form must reproduce the above 13 # copyright notice, this list of conditions and the following disclaimer 14 # in the documentation and/or other materials provided with the 15 # distribution. 16 # * Neither the name of Google Inc. nor the names of its 17 # contributors may be used to endorse or promote products derived from 18 # this software without specific prior written permission. 19 # 20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32 """Unit test for Google Test's break-on-failure mode. 33 34 A user can ask Google Test to seg-fault when an assertion fails, using 35 either the GTEST_BREAK_ON_FAILURE environment variable or the 36 --gtest_break_on_failure flag. This script tests such functionality 37 by invoking gtest_break_on_failure_unittest_ (a program written with 38 Google Test) with different environments and command line flags. 39 """ 40 41 __author__ = 'wan (at] google.com (Zhanyong Wan)' 42 43 import gtest_test_utils 44 import os 45 import sys 46 47 48 # Constants. 49 50 IS_WINDOWS = os.name == 'nt' 51 52 # The environment variable for enabling/disabling the break-on-failure mode. 53 BREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE' 54 55 # The command line flag for enabling/disabling the break-on-failure mode. 56 BREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure' 57 58 # The environment variable for enabling/disabling the throw-on-failure mode. 59 THROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE' 60 61 # The environment variable for enabling/disabling the catch-exceptions mode. 62 CATCH_EXCEPTIONS_ENV_VAR = 'GTEST_CATCH_EXCEPTIONS' 63 64 # Path to the gtest_break_on_failure_unittest_ program. 65 EXE_PATH = gtest_test_utils.GetTestExecutablePath( 66 'gtest_break_on_failure_unittest_') 67 68 69 # Utilities. 70 71 72 def SetEnvVar(env_var, value): 73 """Sets an environment variable to a given value; unsets it when the 74 given value is None. 75 """ 76 77 if value is not None: 78 os.environ[env_var] = value 79 elif env_var in os.environ: 80 del os.environ[env_var] 81 82 83 def Run(command): 84 """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise.""" 85 86 p = gtest_test_utils.Subprocess(command) 87 if p.terminated_by_signal: 88 return 1 89 else: 90 return 0 91 92 93 # The tests. 94 95 96 class GTestBreakOnFailureUnitTest(gtest_test_utils.TestCase): 97 """Tests using the GTEST_BREAK_ON_FAILURE environment variable or 98 the --gtest_break_on_failure flag to turn assertion failures into 99 segmentation faults. 100 """ 101 102 def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault): 103 """Runs gtest_break_on_failure_unittest_ and verifies that it does 104 (or does not) have a seg-fault. 105 106 Args: 107 env_var_value: value of the GTEST_BREAK_ON_FAILURE environment 108 variable; None if the variable should be unset. 109 flag_value: value of the --gtest_break_on_failure flag; 110 None if the flag should not be present. 111 expect_seg_fault: 1 if the program is expected to generate a seg-fault; 112 0 otherwise. 113 """ 114 115 SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value) 116 117 if env_var_value is None: 118 env_var_value_msg = ' is not set' 119 else: 120 env_var_value_msg = '=' + env_var_value 121 122 if flag_value is None: 123 flag = '' 124 elif flag_value == '0': 125 flag = '--%s=0' % BREAK_ON_FAILURE_FLAG 126 else: 127 flag = '--%s' % BREAK_ON_FAILURE_FLAG 128 129 command = [EXE_PATH] 130 if flag: 131 command.append(flag) 132 133 if expect_seg_fault: 134 should_or_not = 'should' 135 else: 136 should_or_not = 'should not' 137 138 has_seg_fault = Run(command) 139 140 SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None) 141 142 msg = ('when %s%s, an assertion failure in "%s" %s cause a seg-fault.' % 143 (BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg, ' '.join(command), 144 should_or_not)) 145 self.assert_(has_seg_fault == expect_seg_fault, msg) 146 147 def testDefaultBehavior(self): 148 """Tests the behavior of the default mode.""" 149 150 self.RunAndVerify(env_var_value=None, 151 flag_value=None, 152 expect_seg_fault=0) 153 154 def testEnvVar(self): 155 """Tests using the GTEST_BREAK_ON_FAILURE environment variable.""" 156 157 self.RunAndVerify(env_var_value='0', 158 flag_value=None, 159 expect_seg_fault=0) 160 self.RunAndVerify(env_var_value='1', 161 flag_value=None, 162 expect_seg_fault=1) 163 164 def testFlag(self): 165 """Tests using the --gtest_break_on_failure flag.""" 166 167 self.RunAndVerify(env_var_value=None, 168 flag_value='0', 169 expect_seg_fault=0) 170 self.RunAndVerify(env_var_value=None, 171 flag_value='1', 172 expect_seg_fault=1) 173 174 def testFlagOverridesEnvVar(self): 175 """Tests that the flag overrides the environment variable.""" 176 177 self.RunAndVerify(env_var_value='0', 178 flag_value='0', 179 expect_seg_fault=0) 180 self.RunAndVerify(env_var_value='0', 181 flag_value='1', 182 expect_seg_fault=1) 183 self.RunAndVerify(env_var_value='1', 184 flag_value='0', 185 expect_seg_fault=0) 186 self.RunAndVerify(env_var_value='1', 187 flag_value='1', 188 expect_seg_fault=1) 189 190 def testBreakOnFailureOverridesThrowOnFailure(self): 191 """Tests that gtest_break_on_failure overrides gtest_throw_on_failure.""" 192 193 SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1') 194 try: 195 self.RunAndVerify(env_var_value=None, 196 flag_value='1', 197 expect_seg_fault=1) 198 finally: 199 SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None) 200 201 if IS_WINDOWS: 202 def testCatchExceptionsDoesNotInterfere(self): 203 """Tests that gtest_catch_exceptions doesn't interfere.""" 204 205 SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, '1') 206 try: 207 self.RunAndVerify(env_var_value='1', 208 flag_value='1', 209 expect_seg_fault=1) 210 finally: 211 SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, None) 212 213 214 if __name__ == '__main__': 215 gtest_test_utils.Main() 216