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