]> git.proxmox.com Git - ceph.git/blob - ceph/src/s3select/rapidjson/thirdparty/gtest/googletest/test/gtest_break_on_failure_unittest.py
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / s3select / rapidjson / thirdparty / gtest / googletest / test / gtest_break_on_failure_unittest.py
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@google.com (Zhanyong Wan)'
42
43 import os
44 import gtest_test_utils
45
46 # Constants.
47
48 IS_WINDOWS = os.name == 'nt'
49
50 # The environment variable for enabling/disabling the break-on-failure mode.
51 BREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE'
52
53 # The command line flag for enabling/disabling the break-on-failure mode.
54 BREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure'
55
56 # The environment variable for enabling/disabling the throw-on-failure mode.
57 THROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE'
58
59 # The environment variable for enabling/disabling the catch-exceptions mode.
60 CATCH_EXCEPTIONS_ENV_VAR = 'GTEST_CATCH_EXCEPTIONS'
61
62 # Path to the gtest_break_on_failure_unittest_ program.
63 EXE_PATH = gtest_test_utils.GetTestExecutablePath(
64 'gtest_break_on_failure_unittest_')
65
66
67 environ = gtest_test_utils.environ
68 SetEnvVar = gtest_test_utils.SetEnvVar
69
70 # Tests in this file run a Google-Test-based test program and expect it
71 # to terminate prematurely. Therefore they are incompatible with
72 # the premature-exit-file protocol by design. Unset the
73 # premature-exit filepath to prevent Google Test from creating
74 # the file.
75 SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
76
77
78 def Run(command):
79 """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise."""
80
81 p = gtest_test_utils.Subprocess(command, env=environ)
82 if p.terminated_by_signal:
83 return 1
84 else:
85 return 0
86
87
88 # The tests.
89
90
91 class GTestBreakOnFailureUnitTest(gtest_test_utils.TestCase):
92 """Tests using the GTEST_BREAK_ON_FAILURE environment variable or
93 the --gtest_break_on_failure flag to turn assertion failures into
94 segmentation faults.
95 """
96
97 def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault):
98 """Runs gtest_break_on_failure_unittest_ and verifies that it does
99 (or does not) have a seg-fault.
100
101 Args:
102 env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
103 variable; None if the variable should be unset.
104 flag_value: value of the --gtest_break_on_failure flag;
105 None if the flag should not be present.
106 expect_seg_fault: 1 if the program is expected to generate a seg-fault;
107 0 otherwise.
108 """
109
110 SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value)
111
112 if env_var_value is None:
113 env_var_value_msg = ' is not set'
114 else:
115 env_var_value_msg = '=' + env_var_value
116
117 if flag_value is None:
118 flag = ''
119 elif flag_value == '0':
120 flag = '--%s=0' % BREAK_ON_FAILURE_FLAG
121 else:
122 flag = '--%s' % BREAK_ON_FAILURE_FLAG
123
124 command = [EXE_PATH]
125 if flag:
126 command.append(flag)
127
128 if expect_seg_fault:
129 should_or_not = 'should'
130 else:
131 should_or_not = 'should not'
132
133 has_seg_fault = Run(command)
134
135 SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None)
136
137 msg = ('when %s%s, an assertion failure in "%s" %s cause a seg-fault.' %
138 (BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg, ' '.join(command),
139 should_or_not))
140 self.assert_(has_seg_fault == expect_seg_fault, msg)
141
142 def testDefaultBehavior(self):
143 """Tests the behavior of the default mode."""
144
145 self.RunAndVerify(env_var_value=None,
146 flag_value=None,
147 expect_seg_fault=0)
148
149 def testEnvVar(self):
150 """Tests using the GTEST_BREAK_ON_FAILURE environment variable."""
151
152 self.RunAndVerify(env_var_value='0',
153 flag_value=None,
154 expect_seg_fault=0)
155 self.RunAndVerify(env_var_value='1',
156 flag_value=None,
157 expect_seg_fault=1)
158
159 def testFlag(self):
160 """Tests using the --gtest_break_on_failure flag."""
161
162 self.RunAndVerify(env_var_value=None,
163 flag_value='0',
164 expect_seg_fault=0)
165 self.RunAndVerify(env_var_value=None,
166 flag_value='1',
167 expect_seg_fault=1)
168
169 def testFlagOverridesEnvVar(self):
170 """Tests that the flag overrides the environment variable."""
171
172 self.RunAndVerify(env_var_value='0',
173 flag_value='0',
174 expect_seg_fault=0)
175 self.RunAndVerify(env_var_value='0',
176 flag_value='1',
177 expect_seg_fault=1)
178 self.RunAndVerify(env_var_value='1',
179 flag_value='0',
180 expect_seg_fault=0)
181 self.RunAndVerify(env_var_value='1',
182 flag_value='1',
183 expect_seg_fault=1)
184
185 def testBreakOnFailureOverridesThrowOnFailure(self):
186 """Tests that gtest_break_on_failure overrides gtest_throw_on_failure."""
187
188 SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1')
189 try:
190 self.RunAndVerify(env_var_value=None,
191 flag_value='1',
192 expect_seg_fault=1)
193 finally:
194 SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None)
195
196 if IS_WINDOWS:
197 def testCatchExceptionsDoesNotInterfere(self):
198 """Tests that gtest_catch_exceptions doesn't interfere."""
199
200 SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, '1')
201 try:
202 self.RunAndVerify(env_var_value='1',
203 flag_value='1',
204 expect_seg_fault=1)
205 finally:
206 SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, None)
207
208
209 if __name__ == '__main__':
210 gtest_test_utils.Main()