• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
34A user can ask Google Test to seg-fault when an assertion fails, using
35either the GTEST_BREAK_ON_FAILURE environment variable or the
36--gtest_break_on_failure flag.  This script tests such functionality
37by invoking gtest_break_on_failure_unittest_ (a program written with
38Google Test) with different environments and command line flags.
39"""
40
41__author__ = 'wan@google.com (Zhanyong Wan)'
42
43import gtest_test_utils
44import os
45import sys
46import unittest
47
48
49# Constants.
50
51# The environment variable for enabling/disabling the break-on-failure mode.
52BREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE'
53
54# The command line flag for enabling/disabling the break-on-failure mode.
55BREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure'
56
57# The environment variable for enabling/disabling the throw-on-failure mode.
58THROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE'
59
60# Path to the gtest_break_on_failure_unittest_ program.
61EXE_PATH = os.path.join(gtest_test_utils.GetBuildDir(),
62                        'gtest_break_on_failure_unittest_')
63
64
65# Utilities.
66
67
68def SetEnvVar(env_var, value):
69  """Sets an environment variable to a given value; unsets it when the
70  given value is None.
71  """
72
73  if value is not None:
74    os.environ[env_var] = value
75  elif env_var in os.environ:
76    del os.environ[env_var]
77
78
79def Run(command):
80  """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise."""
81
82  p = gtest_test_utils.Subprocess(command)
83  if p.terminated_by_signal:
84    return 1
85  else:
86    return 0
87
88
89# The tests.
90
91
92class GTestBreakOnFailureUnitTest(unittest.TestCase):
93  """Tests using the GTEST_BREAK_ON_FAILURE environment variable or
94  the --gtest_break_on_failure flag to turn assertion failures into
95  segmentation faults.
96  """
97
98  def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault):
99    """Runs gtest_break_on_failure_unittest_ and verifies that it does
100    (or does not) have a seg-fault.
101
102    Args:
103      env_var_value:    value of the GTEST_BREAK_ON_FAILURE environment
104                        variable; None if the variable should be unset.
105      flag_value:       value of the --gtest_break_on_failure flag;
106                        None if the flag should not be present.
107      expect_seg_fault: 1 if the program is expected to generate a seg-fault;
108                        0 otherwise.
109    """
110
111    SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value)
112
113    if env_var_value is None:
114      env_var_value_msg = ' is not set'
115    else:
116      env_var_value_msg = '=' + env_var_value
117
118    if flag_value is None:
119      flag = ''
120    elif flag_value == '0':
121      flag = '--%s=0' % BREAK_ON_FAILURE_FLAG
122    else:
123      flag = '--%s' % BREAK_ON_FAILURE_FLAG
124
125    command = [EXE_PATH]
126    if flag:
127      command.append(flag)
128
129    if expect_seg_fault:
130      should_or_not = 'should'
131    else:
132      should_or_not = 'should not'
133
134    has_seg_fault = Run(command)
135
136    SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None)
137
138    msg = ('when %s%s, an assertion failure in "%s" %s cause a seg-fault.' %
139           (BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg, ' '.join(command),
140            should_or_not))
141    self.assert_(has_seg_fault == expect_seg_fault, msg)
142
143  def testDefaultBehavior(self):
144    """Tests the behavior of the default mode."""
145
146    self.RunAndVerify(env_var_value=None,
147                      flag_value=None,
148                      expect_seg_fault=0)
149
150  def testEnvVar(self):
151    """Tests using the GTEST_BREAK_ON_FAILURE environment variable."""
152
153    self.RunAndVerify(env_var_value='0',
154                      flag_value=None,
155                      expect_seg_fault=0)
156    self.RunAndVerify(env_var_value='1',
157                      flag_value=None,
158                      expect_seg_fault=1)
159
160  def testFlag(self):
161    """Tests using the --gtest_break_on_failure flag."""
162
163    self.RunAndVerify(env_var_value=None,
164                      flag_value='0',
165                      expect_seg_fault=0)
166    self.RunAndVerify(env_var_value=None,
167                      flag_value='1',
168                      expect_seg_fault=1)
169
170  def testFlagOverridesEnvVar(self):
171    """Tests that the flag overrides the environment variable."""
172
173    self.RunAndVerify(env_var_value='0',
174                      flag_value='0',
175                      expect_seg_fault=0)
176    self.RunAndVerify(env_var_value='0',
177                      flag_value='1',
178                      expect_seg_fault=1)
179    self.RunAndVerify(env_var_value='1',
180                      flag_value='0',
181                      expect_seg_fault=0)
182    self.RunAndVerify(env_var_value='1',
183                      flag_value='1',
184                      expect_seg_fault=1)
185
186  def testBreakOnFailureOverridesThrowOnFailure(self):
187    """Tests that gtest_break_on_failure overrides gtest_throw_on_failure."""
188
189    SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1')
190    try:
191      self.RunAndVerify(env_var_value=None,
192                        flag_value='1',
193                        expect_seg_fault=1)
194    finally:
195      SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None)
196
197if __name__ == '__main__':
198  gtest_test_utils.Main()
199