Jean-Paul Calderone | 8671c85 | 2011-03-02 19:26:20 -0500 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone |
| 2 | # Copyright (C) Twisted Matrix Laboratories. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 3 | # See LICENSE for details. |
| 4 | |
| 5 | """ |
| 6 | Helpers for the OpenSSL test suite, largely copied from |
| 7 | U{Twisted<http://twistedmatrix.com/>}. |
| 8 | """ |
| 9 | |
| 10 | import shutil |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 11 | import sys |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 12 | import traceback |
| 13 | |
| 14 | from tempfile import mktemp, mkdtemp |
| 15 | from unittest import TestCase |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 16 | |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 17 | from six import PY3 |
| 18 | |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 19 | from OpenSSL._util import exception_from_error_queue |
| 20 | from OpenSSL.crypto import Error |
Jean-Paul Calderone | 88f38b2 | 2009-07-16 16:25:19 -0400 | [diff] [blame] | 21 | |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 22 | |
Alex Gaynor | 7e9de8e | 2015-09-04 08:34:07 -0400 | [diff] [blame] | 23 | import memdbg |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 24 | |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 25 | from OpenSSL._util import ffi, lib, byte_string as b |
Jean-Paul Calderone | 9e4eeae | 2010-08-22 21:32:52 -0400 | [diff] [blame] | 26 | |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 27 | |
| 28 | # This is the UTF-8 encoding of the SNOWMAN unicode code point. |
| 29 | NON_ASCII = b("\xe2\x98\x83").decode("utf-8") |
| 30 | |
| 31 | |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 32 | class TestCase(TestCase): |
| 33 | """ |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 34 | :py:class:`TestCase` adds useful testing functionality beyond what is |
| 35 | available from the standard library :py:class:`unittest.TestCase`. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 36 | """ |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 37 | def run(self, result): |
Jean-Paul Calderone | 68703ed | 2013-03-04 12:23:44 -0800 | [diff] [blame] | 38 | run = super(TestCase, self).run |
| 39 | if memdbg.heap is None: |
| 40 | return run(result) |
| 41 | |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 42 | # Run the test as usual |
| 43 | before = set(memdbg.heap) |
Jean-Paul Calderone | 68703ed | 2013-03-04 12:23:44 -0800 | [diff] [blame] | 44 | run(result) |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 45 | |
| 46 | # Clean up some long-lived allocations so they won't be reported as |
| 47 | # memory leaks. |
Jean-Paul Calderone | 9227c47 | 2013-12-31 13:47:36 -0500 | [diff] [blame] | 48 | lib.CRYPTO_cleanup_all_ex_data() |
| 49 | lib.ERR_remove_thread_state(ffi.NULL) |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 50 | after = set(memdbg.heap) |
| 51 | |
| 52 | if not after - before: |
| 53 | # No leaks, fast succeed |
| 54 | return |
| 55 | |
| 56 | if result.wasSuccessful(): |
| 57 | # If it passed, run it again with memory debugging |
| 58 | before = set(memdbg.heap) |
Jean-Paul Calderone | 68703ed | 2013-03-04 12:23:44 -0800 | [diff] [blame] | 59 | run(result) |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 60 | |
| 61 | # Clean up some long-lived allocations so they won't be reported as |
| 62 | # memory leaks. |
Jean-Paul Calderone | 3f93d21 | 2014-01-01 12:36:53 -0500 | [diff] [blame] | 63 | lib.CRYPTO_cleanup_all_ex_data() |
| 64 | lib.ERR_remove_thread_state(ffi.NULL) |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 65 | |
| 66 | after = set(memdbg.heap) |
| 67 | |
| 68 | self._reportLeaks(after - before, result) |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 69 | |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 70 | def _reportLeaks(self, leaks, result): |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 71 | def format_leak(p): |
| 72 | stacks = memdbg.heap[p] |
| 73 | # Eventually look at multiple stacks for the realloc() case. For |
| 74 | # now just look at the original allocation location. |
Jean-Paul Calderone | c2e8b41 | 2013-03-02 16:27:55 -0800 | [diff] [blame] | 75 | (size, python_stack, c_stack) = stacks[0] |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 76 | |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 77 | stack = traceback.format_list(python_stack)[:-1] |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 78 | |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 79 | # c_stack looks something like this (interesting parts indicated |
| 80 | # with inserted arrows not part of the data): |
| 81 | # |
| 82 | # /home/exarkun/Projects/pyOpenSSL/branches/use-opentls/__pycache__/_cffi__x89095113xb9185b9b.so(+0x12cf) [0x7fe2e20582cf] |
| 83 | # /home/exarkun/Projects/cpython/2.7/python(PyCFunction_Call+0x8b) [0x56265a] |
| 84 | # /home/exarkun/Projects/cpython/2.7/python() [0x4d5f52] |
| 85 | # /home/exarkun/Projects/cpython/2.7/python(PyEval_EvalFrameEx+0x753b) [0x4d0e1e] |
| 86 | # /home/exarkun/Projects/cpython/2.7/python() [0x4d6419] |
| 87 | # /home/exarkun/Projects/cpython/2.7/python() [0x4d6129] |
| 88 | # /home/exarkun/Projects/cpython/2.7/python(PyEval_EvalFrameEx+0x753b) [0x4d0e1e] |
| 89 | # /home/exarkun/Projects/cpython/2.7/python(PyEval_EvalCodeEx+0x1043) [0x4d3726] |
| 90 | # /home/exarkun/Projects/cpython/2.7/python() [0x55fd51] |
| 91 | # /home/exarkun/Projects/cpython/2.7/python(PyObject_Call+0x7e) [0x420ee6] |
| 92 | # /home/exarkun/Projects/cpython/2.7/python(PyEval_CallObjectWithKeywords+0x158) [0x4d56ec] |
| 93 | # /home/exarkun/.local/lib/python2.7/site-packages/cffi-0.5-py2.7-linux-x86_64.egg/_cffi_backend.so(+0xe96e) [0x7fe2e38be96e] |
| 94 | # /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_closure_unix64_inner+0x1b9) [0x7fe2e36ad819] |
| 95 | # /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_closure_unix64+0x46) [0x7fe2e36adb7c] |
| 96 | # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(CRYPTO_malloc+0x64) [0x7fe2e1cef784] <------ end interesting |
| 97 | # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(lh_insert+0x16b) [0x7fe2e1d6a24b] . |
| 98 | # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(+0x61c18) [0x7fe2e1cf0c18] . |
| 99 | # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(+0x625ec) [0x7fe2e1cf15ec] . |
| 100 | # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(DSA_new_method+0xe6) [0x7fe2e1d524d6] . |
| 101 | # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(DSA_generate_parameters+0x3a) [0x7fe2e1d5364a] <------ begin interesting |
| 102 | # /home/exarkun/Projects/opentls/trunk/tls/c/__pycache__/_cffi__x305d4698xb539baaa.so(+0x1f397) [0x7fe2df84d397] |
| 103 | # /home/exarkun/Projects/cpython/2.7/python(PyCFunction_Call+0x8b) [0x56265a] |
| 104 | # /home/exarkun/Projects/cpython/2.7/python() [0x4d5f52] |
| 105 | # /home/exarkun/Projects/cpython/2.7/python(PyEval_EvalFrameEx+0x753b) [0x4d0e1e] |
| 106 | # /home/exarkun/Projects/cpython/2.7/python() [0x4d6419] |
| 107 | # ... |
| 108 | # |
| 109 | # Notice the stack is upside down compared to a Python traceback. |
Alex Gaynor | 75690d9 | 2015-09-05 10:14:52 -0400 | [diff] [blame^] | 110 | # Identify the start and end of interesting bits and stuff it into |
| 111 | # the stack we report. |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 112 | |
Jean-Paul Calderone | c2e8b41 | 2013-03-02 16:27:55 -0800 | [diff] [blame] | 113 | saved = list(c_stack) |
| 114 | |
Alex Gaynor | 75690d9 | 2015-09-05 10:14:52 -0400 | [diff] [blame^] | 115 | # Figure the first interesting frame will be after a the |
| 116 | # cffi-compiled module |
Jean-Paul Calderone | c2e8b41 | 2013-03-02 16:27:55 -0800 | [diff] [blame] | 117 | while c_stack and '/__pycache__/_cffi__' not in c_stack[-1]: |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 118 | c_stack.pop() |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 119 | |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 120 | # Figure the last interesting frame will always be CRYPTO_malloc, |
| 121 | # since that's where we hooked in to things. |
Jean-Paul Calderone | c2e8b41 | 2013-03-02 16:27:55 -0800 | [diff] [blame] | 122 | while c_stack and 'CRYPTO_malloc' not in c_stack[0] and 'CRYPTO_realloc' not in c_stack[0]: |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 123 | c_stack.pop(0) |
| 124 | |
Jean-Paul Calderone | c2e8b41 | 2013-03-02 16:27:55 -0800 | [diff] [blame] | 125 | if c_stack: |
| 126 | c_stack.reverse() |
| 127 | else: |
| 128 | c_stack = saved[::-1] |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 129 | stack.extend([frame + "\n" for frame in c_stack]) |
| 130 | |
Jean-Paul Calderone | 2beac53 | 2013-03-03 17:30:36 -0800 | [diff] [blame] | 131 | stack.insert(0, "Leaked (%s) at:\n") |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 132 | return "".join(stack) |
| 133 | |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 134 | if leaks: |
Jean-Paul Calderone | 2beac53 | 2013-03-03 17:30:36 -0800 | [diff] [blame] | 135 | unique_leaks = {} |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 136 | for p in leaks: |
Jean-Paul Calderone | 2beac53 | 2013-03-03 17:30:36 -0800 | [diff] [blame] | 137 | size = memdbg.heap[p][-1][0] |
| 138 | new_leak = format_leak(p) |
| 139 | if new_leak not in unique_leaks: |
| 140 | unique_leaks[new_leak] = [(size, p)] |
| 141 | else: |
| 142 | unique_leaks[new_leak].append((size, p)) |
| 143 | memdbg.free(p) |
| 144 | |
| 145 | for (stack, allocs) in unique_leaks.iteritems(): |
| 146 | allocs_accum = [] |
| 147 | for (size, pointer) in allocs: |
| 148 | |
Jean-Paul Calderone | 9227c47 | 2013-12-31 13:47:36 -0500 | [diff] [blame] | 149 | addr = int(ffi.cast('uintptr_t', pointer)) |
Jean-Paul Calderone | 2beac53 | 2013-03-03 17:30:36 -0800 | [diff] [blame] | 150 | allocs_accum.append("%d@0x%x" % (size, addr)) |
| 151 | allocs_report = ", ".join(sorted(allocs_accum)) |
| 152 | |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 153 | result.addError( |
| 154 | self, |
Jean-Paul Calderone | 2beac53 | 2013-03-03 17:30:36 -0800 | [diff] [blame] | 155 | (None, Exception(stack % (allocs_report,)), None)) |
Jean-Paul Calderone | f6745b3 | 2013-03-01 15:08:46 -0800 | [diff] [blame] | 156 | |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 157 | _tmpdir = None |
| 158 | |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 159 | @property |
| 160 | def tmpdir(self): |
| 161 | """ |
| 162 | On demand create a temporary directory. |
| 163 | """ |
| 164 | if self._tmpdir is not None: |
| 165 | return self._tmpdir |
| 166 | |
| 167 | self._tmpdir = mkdtemp(dir=".") |
| 168 | return self._tmpdir |
| 169 | |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 170 | def tearDown(self): |
| 171 | """ |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 172 | Clean up any files or directories created using |
| 173 | :py:meth:`TestCase.mktemp`. Subclasses must invoke this method if they |
| 174 | override it or the cleanup will not occur. |
Jean-Paul Calderone | 855331d | 2013-03-03 10:21:43 -0800 | [diff] [blame] | 175 | """ |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 176 | if self._tmpdir is not None: |
| 177 | shutil.rmtree(self._tmpdir) |
| 178 | |
Jean-Paul Calderone | 1206daf | 2009-07-16 16:07:42 -0400 | [diff] [blame] | 179 | try: |
Jean-Paul Calderone | c86bb7d | 2013-12-29 10:25:59 -0500 | [diff] [blame] | 180 | exception_from_error_queue(Error) |
Jean-Paul Calderone | 24b6459 | 2010-08-12 10:43:09 -0400 | [diff] [blame] | 181 | except Error: |
| 182 | e = sys.exc_info()[1] |
Jean-Paul Calderone | 1206daf | 2009-07-16 16:07:42 -0400 | [diff] [blame] | 183 | if e.args != ([],): |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 184 | self.fail( |
| 185 | "Left over errors in OpenSSL error queue: " + repr(e) |
| 186 | ) |
Jean-Paul Calderone | 1206daf | 2009-07-16 16:07:42 -0400 | [diff] [blame] | 187 | |
Jean-Paul Calderone | 8fb5318 | 2013-12-30 08:35:49 -0500 | [diff] [blame] | 188 | def assertIsInstance(self, instance, classOrTuple, message=None): |
| 189 | """ |
| 190 | Fail if C{instance} is not an instance of the given class or of |
| 191 | one of the given classes. |
| 192 | |
| 193 | @param instance: the object to test the type (first argument of the |
| 194 | C{isinstance} call). |
| 195 | @type instance: any. |
| 196 | @param classOrTuple: the class or classes to test against (second |
| 197 | argument of the C{isinstance} call). |
| 198 | @type classOrTuple: class, type, or tuple. |
| 199 | |
| 200 | @param message: Custom text to include in the exception text if the |
| 201 | assertion fails. |
| 202 | """ |
| 203 | if not isinstance(instance, classOrTuple): |
| 204 | if message is None: |
| 205 | suffix = "" |
| 206 | else: |
| 207 | suffix = ": " + message |
| 208 | self.fail("%r is not an instance of %s%s" % ( |
Alex Gaynor | 75690d9 | 2015-09-05 10:14:52 -0400 | [diff] [blame^] | 209 | instance, classOrTuple, suffix)) |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame] | 210 | |
Jean-Paul Calderone | 060a57e | 2011-05-04 18:02:49 -0400 | [diff] [blame] | 211 | def failUnlessIn(self, containee, container, msg=None): |
| 212 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 213 | Fail the test if :py:data:`containee` is not found in :py:data:`container`. |
Jean-Paul Calderone | 060a57e | 2011-05-04 18:02:49 -0400 | [diff] [blame] | 214 | |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 215 | :param containee: the value that should be in :py:class:`container` |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 216 | :param container: a sequence type, or in the case of a mapping type, |
Jean-Paul Calderone | 060a57e | 2011-05-04 18:02:49 -0400 | [diff] [blame] | 217 | will follow semantics of 'if key in dict.keys()' |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 218 | :param msg: if msg is None, then the failure message will be |
Jean-Paul Calderone | 060a57e | 2011-05-04 18:02:49 -0400 | [diff] [blame] | 219 | '%r not in %r' % (first, second) |
| 220 | """ |
| 221 | if containee not in container: |
| 222 | raise self.failureException(msg or "%r not in %r" |
| 223 | % (containee, container)) |
| 224 | return containee |
| 225 | assertIn = failUnlessIn |
| 226 | |
Jean-Paul Calderone | 15f3644 | 2014-05-01 07:58:02 -0400 | [diff] [blame] | 227 | def assertNotIn(self, containee, container, msg=None): |
| 228 | """ |
| 229 | Fail the test if C{containee} is found in C{container}. |
| 230 | |
| 231 | @param containee: the value that should not be in C{container} |
| 232 | @param container: a sequence type, or in the case of a mapping type, |
| 233 | will follow semantics of 'if key in dict.keys()' |
| 234 | @param msg: if msg is None, then the failure message will be |
| 235 | '%r in %r' % (first, second) |
| 236 | """ |
| 237 | if containee in container: |
| 238 | raise self.failureException(msg or "%r in %r" |
| 239 | % (containee, container)) |
| 240 | return containee |
| 241 | failIfIn = assertNotIn |
| 242 | |
Jean-Paul Calderone | 77b3d08 | 2014-12-12 20:04:35 -0500 | [diff] [blame] | 243 | def assertIs(self, first, second, msg=None): |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 244 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 245 | Fail the test if :py:data:`first` is not :py:data:`second`. This is an |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 246 | obect-identity-equality test, not an object equality |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 247 | (i.e. :py:func:`__eq__`) test. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 248 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 249 | :param msg: if msg is None, then the failure message will be |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 250 | '%r is not %r' % (first, second) |
| 251 | """ |
| 252 | if first is not second: |
| 253 | raise self.failureException(msg or '%r is not %r' % (first, second)) |
| 254 | return first |
Jean-Paul Calderone | 77b3d08 | 2014-12-12 20:04:35 -0500 | [diff] [blame] | 255 | assertIdentical = failUnlessIdentical = assertIs |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 256 | |
Jean-Paul Calderone | 77b3d08 | 2014-12-12 20:04:35 -0500 | [diff] [blame] | 257 | def assertIsNot(self, first, second, msg=None): |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 258 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 259 | Fail the test if :py:data:`first` is :py:data:`second`. This is an |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 260 | obect-identity-equality test, not an object equality |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 261 | (i.e. :py:func:`__eq__`) test. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 262 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 263 | :param msg: if msg is None, then the failure message will be |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 264 | '%r is %r' % (first, second) |
| 265 | """ |
| 266 | if first is second: |
| 267 | raise self.failureException(msg or '%r is %r' % (first, second)) |
| 268 | return first |
Jean-Paul Calderone | 77b3d08 | 2014-12-12 20:04:35 -0500 | [diff] [blame] | 269 | assertNotIdentical = failIfIdentical = assertIsNot |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 270 | |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 271 | def failUnlessRaises(self, exception, f, *args, **kwargs): |
| 272 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 273 | Fail the test unless calling the function :py:data:`f` with the given |
| 274 | :py:data:`args` and :py:data:`kwargs` raises :py:data:`exception`. The |
| 275 | failure will report the traceback and call stack of the unexpected |
| 276 | exception. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 277 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 278 | :param exception: exception type that is to be expected |
| 279 | :param f: the function to call |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 280 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 281 | :return: The raised exception instance, if it is of the given type. |
| 282 | :raise self.failureException: Raised if the function call does |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 283 | not raise an exception or if it raises an exception of a |
| 284 | different type. |
| 285 | """ |
| 286 | try: |
| 287 | result = f(*args, **kwargs) |
Jean-Paul Calderone | 24b6459 | 2010-08-12 10:43:09 -0400 | [diff] [blame] | 288 | except exception: |
| 289 | inst = sys.exc_info()[1] |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 290 | return inst |
| 291 | except: |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 292 | raise self.failureException('%s raised instead of %s' |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 293 | % (sys.exc_info()[0], |
| 294 | exception.__name__, |
Alex Gaynor | 75690d9 | 2015-09-05 10:14:52 -0400 | [diff] [blame^] | 295 | )) |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 296 | else: |
| 297 | raise self.failureException('%s not raised (%r returned)' |
| 298 | % (exception.__name__, result)) |
| 299 | assertRaises = failUnlessRaises |
| 300 | |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 301 | def mktemp(self): |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 302 | """ |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 303 | Return UTF-8-encoded bytes of a path to a tmp file. |
| 304 | |
| 305 | The file will be cleaned up after the test run. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 306 | """ |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 307 | return mktemp(dir=self.tmpdir).encode("utf-8") |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 308 | |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 309 | # Other stuff |
| 310 | def assertConsistentType(self, theType, name, *constructionArgs): |
| 311 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 312 | Perform various assertions about :py:data:`theType` to ensure that it is a |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 313 | well-defined type. This is useful for extension types, where it's |
| 314 | pretty easy to do something wacky. If something about the type is |
| 315 | unusual, an exception will be raised. |
| 316 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 317 | :param theType: The type object about which to make assertions. |
| 318 | :param name: A string giving the name of the type. |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 319 | :param constructionArgs: Positional arguments to use with :py:data:`theType` to |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 320 | create an instance of it. |
| 321 | """ |
| 322 | self.assertEqual(theType.__name__, name) |
| 323 | self.assertTrue(isinstance(theType, type)) |
| 324 | instance = theType(*constructionArgs) |
| 325 | self.assertIdentical(type(instance), theType) |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 326 | |
| 327 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 328 | class EqualityTestsMixin(object): |
| 329 | """ |
| 330 | A mixin defining tests for the standard implementation of C{==} and C{!=}. |
| 331 | """ |
| 332 | def anInstance(self): |
| 333 | """ |
| 334 | Return an instance of the class under test. Each call to this method |
| 335 | must return a different object. All objects returned must be equal to |
| 336 | each other. |
| 337 | """ |
| 338 | raise NotImplementedError() |
| 339 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 340 | def anotherInstance(self): |
| 341 | """ |
| 342 | Return an instance of the class under test. Each call to this method |
| 343 | must return a different object. The objects must not be equal to the |
| 344 | objects returned by C{anInstance}. They may or may not be equal to |
| 345 | each other (they will not be compared against each other). |
| 346 | """ |
| 347 | raise NotImplementedError() |
| 348 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 349 | def test_identicalEq(self): |
| 350 | """ |
| 351 | An object compares equal to itself using the C{==} operator. |
| 352 | """ |
| 353 | o = self.anInstance() |
| 354 | self.assertTrue(o == o) |
| 355 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 356 | def test_identicalNe(self): |
| 357 | """ |
| 358 | An object doesn't compare not equal to itself using the C{!=} operator. |
| 359 | """ |
| 360 | o = self.anInstance() |
| 361 | self.assertFalse(o != o) |
| 362 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 363 | def test_sameEq(self): |
| 364 | """ |
| 365 | Two objects that are equal to each other compare equal to each other |
| 366 | using the C{==} operator. |
| 367 | """ |
| 368 | a = self.anInstance() |
| 369 | b = self.anInstance() |
| 370 | self.assertTrue(a == b) |
| 371 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 372 | def test_sameNe(self): |
| 373 | """ |
| 374 | Two objects that are equal to each other do not compare not equal to |
| 375 | each other using the C{!=} operator. |
| 376 | """ |
| 377 | a = self.anInstance() |
| 378 | b = self.anInstance() |
| 379 | self.assertFalse(a != b) |
| 380 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 381 | def test_differentEq(self): |
| 382 | """ |
| 383 | Two objects that are not equal to each other do not compare equal to |
| 384 | each other using the C{==} operator. |
| 385 | """ |
| 386 | a = self.anInstance() |
| 387 | b = self.anotherInstance() |
| 388 | self.assertFalse(a == b) |
| 389 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 390 | def test_differentNe(self): |
| 391 | """ |
| 392 | Two objects that are not equal to each other compare not equal to each |
| 393 | other using the C{!=} operator. |
| 394 | """ |
| 395 | a = self.anInstance() |
| 396 | b = self.anotherInstance() |
| 397 | self.assertTrue(a != b) |
| 398 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 399 | def test_anotherTypeEq(self): |
| 400 | """ |
| 401 | The object does not compare equal to an object of an unrelated type |
| 402 | (which does not implement the comparison) using the C{==} operator. |
| 403 | """ |
| 404 | a = self.anInstance() |
| 405 | b = object() |
| 406 | self.assertFalse(a == b) |
| 407 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 408 | def test_anotherTypeNe(self): |
| 409 | """ |
| 410 | The object compares not equal to an object of an unrelated type (which |
| 411 | does not implement the comparison) using the C{!=} operator. |
| 412 | """ |
| 413 | a = self.anInstance() |
| 414 | b = object() |
| 415 | self.assertTrue(a != b) |
| 416 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 417 | def test_delegatedEq(self): |
| 418 | """ |
| 419 | The result of comparison using C{==} is delegated to the right-hand |
| 420 | operand if it is of an unrelated type. |
| 421 | """ |
| 422 | class Delegate(object): |
| 423 | def __eq__(self, other): |
| 424 | # Do something crazy and obvious. |
| 425 | return [self] |
| 426 | |
| 427 | a = self.anInstance() |
| 428 | b = Delegate() |
| 429 | self.assertEqual(a == b, [b]) |
| 430 | |
Jean-Paul Calderone | 9c7f069 | 2014-04-30 18:17:19 -0400 | [diff] [blame] | 431 | def test_delegateNe(self): |
| 432 | """ |
| 433 | The result of comparison using C{!=} is delegated to the right-hand |
| 434 | operand if it is of an unrelated type. |
| 435 | """ |
| 436 | class Delegate(object): |
| 437 | def __ne__(self, other): |
| 438 | # Do something crazy and obvious. |
| 439 | return [self] |
| 440 | |
| 441 | a = self.anInstance() |
| 442 | b = Delegate() |
| 443 | self.assertEqual(a != b, [b]) |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 444 | |
| 445 | |
| 446 | # The type name expected in warnings about using the wrong string type. |
| 447 | if PY3: |
| 448 | WARNING_TYPE_EXPECTED = "str" |
| 449 | else: |
| 450 | WARNING_TYPE_EXPECTED = "unicode" |