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.

208 lines
7.1 KiB

  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. """Unit test for Google Test's break-on-failure mode.
  32. A user can ask Google Test to seg-fault when an assertion fails, using
  33. either the GTEST_BREAK_ON_FAILURE environment variable or the
  34. --gtest_break_on_failure flag. This script tests such functionality
  35. by invoking googletest-break-on-failure-unittest_ (a program written with
  36. Google Test) with different environments and command line flags.
  37. """
  38. import os
  39. import gtest_test_utils
  40. # Constants.
  41. IS_WINDOWS = os.name == 'nt'
  42. # The environment variable for enabling/disabling the break-on-failure mode.
  43. BREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE'
  44. # The command line flag for enabling/disabling the break-on-failure mode.
  45. BREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure'
  46. # The environment variable for enabling/disabling the throw-on-failure mode.
  47. THROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE'
  48. # The environment variable for enabling/disabling the catch-exceptions mode.
  49. CATCH_EXCEPTIONS_ENV_VAR = 'GTEST_CATCH_EXCEPTIONS'
  50. # Path to the googletest-break-on-failure-unittest_ program.
  51. EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  52. 'googletest-break-on-failure-unittest_')
  53. environ = gtest_test_utils.environ
  54. SetEnvVar = gtest_test_utils.SetEnvVar
  55. # Tests in this file run a Google-Test-based test program and expect it
  56. # to terminate prematurely. Therefore they are incompatible with
  57. # the premature-exit-file protocol by design. Unset the
  58. # premature-exit filepath to prevent Google Test from creating
  59. # the file.
  60. SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
  61. def Run(command):
  62. """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise."""
  63. p = gtest_test_utils.Subprocess(command, env=environ)
  64. if p.terminated_by_signal:
  65. return 1
  66. else:
  67. return 0
  68. # The tests.
  69. class GTestBreakOnFailureUnitTest(gtest_test_utils.TestCase):
  70. """Tests using the GTEST_BREAK_ON_FAILURE environment variable or
  71. the --gtest_break_on_failure flag to turn assertion failures into
  72. segmentation faults.
  73. """
  74. def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault):
  75. """Runs googletest-break-on-failure-unittest_ and verifies that it does
  76. (or does not) have a seg-fault.
  77. Args:
  78. env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
  79. variable; None if the variable should be unset.
  80. flag_value: value of the --gtest_break_on_failure flag;
  81. None if the flag should not be present.
  82. expect_seg_fault: 1 if the program is expected to generate a seg-fault;
  83. 0 otherwise.
  84. """
  85. SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value)
  86. if env_var_value is None:
  87. env_var_value_msg = ' is not set'
  88. else:
  89. env_var_value_msg = '=' + env_var_value
  90. if flag_value is None:
  91. flag = ''
  92. elif flag_value == '0':
  93. flag = '--%s=0' % BREAK_ON_FAILURE_FLAG
  94. else:
  95. flag = '--%s' % BREAK_ON_FAILURE_FLAG
  96. command = [EXE_PATH]
  97. if flag:
  98. command.append(flag)
  99. if expect_seg_fault:
  100. should_or_not = 'should'
  101. else:
  102. should_or_not = 'should not'
  103. has_seg_fault = Run(command)
  104. SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None)
  105. msg = ('when %s%s, an assertion failure in "%s" %s cause a seg-fault.' %
  106. (BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg, ' '.join(command),
  107. should_or_not))
  108. self.assert_(has_seg_fault == expect_seg_fault, msg)
  109. def testDefaultBehavior(self):
  110. """Tests the behavior of the default mode."""
  111. self.RunAndVerify(env_var_value=None,
  112. flag_value=None,
  113. expect_seg_fault=0)
  114. def testEnvVar(self):
  115. """Tests using the GTEST_BREAK_ON_FAILURE environment variable."""
  116. self.RunAndVerify(env_var_value='0',
  117. flag_value=None,
  118. expect_seg_fault=0)
  119. self.RunAndVerify(env_var_value='1',
  120. flag_value=None,
  121. expect_seg_fault=1)
  122. def testFlag(self):
  123. """Tests using the --gtest_break_on_failure flag."""
  124. self.RunAndVerify(env_var_value=None,
  125. flag_value='0',
  126. expect_seg_fault=0)
  127. self.RunAndVerify(env_var_value=None,
  128. flag_value='1',
  129. expect_seg_fault=1)
  130. def testFlagOverridesEnvVar(self):
  131. """Tests that the flag overrides the environment variable."""
  132. self.RunAndVerify(env_var_value='0',
  133. flag_value='0',
  134. expect_seg_fault=0)
  135. self.RunAndVerify(env_var_value='0',
  136. flag_value='1',
  137. expect_seg_fault=1)
  138. self.RunAndVerify(env_var_value='1',
  139. flag_value='0',
  140. expect_seg_fault=0)
  141. self.RunAndVerify(env_var_value='1',
  142. flag_value='1',
  143. expect_seg_fault=1)
  144. def testBreakOnFailureOverridesThrowOnFailure(self):
  145. """Tests that gtest_break_on_failure overrides gtest_throw_on_failure."""
  146. SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1')
  147. try:
  148. self.RunAndVerify(env_var_value=None,
  149. flag_value='1',
  150. expect_seg_fault=1)
  151. finally:
  152. SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None)
  153. if IS_WINDOWS:
  154. def testCatchExceptionsDoesNotInterfere(self):
  155. """Tests that gtest_catch_exceptions doesn't interfere."""
  156. SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, '1')
  157. try:
  158. self.RunAndVerify(env_var_value='1',
  159. flag_value='1',
  160. expect_seg_fault=1)
  161. finally:
  162. SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, None)
  163. if __name__ == '__main__':
  164. gtest_test_utils.Main()