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 |
| 11 | import os, os.path |
| 12 | from tempfile import mktemp |
| 13 | from unittest import TestCase |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 14 | import sys |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 15 | |
Jean-Paul Calderone | 88f38b2 | 2009-07-16 16:25:19 -0400 | [diff] [blame] | 16 | from OpenSSL.crypto import Error, _exception_from_error_queue |
| 17 | |
Jean-Paul Calderone | ea9c8a3 | 2011-04-01 18:26:37 -0400 | [diff] [blame] | 18 | if sys.version_info < (3, 0): |
Jean-Paul Calderone | 9e4eeae | 2010-08-22 21:32:52 -0400 | [diff] [blame] | 19 | def b(s): |
| 20 | return s |
| 21 | bytes = str |
| 22 | else: |
| 23 | def b(s): |
Jean-Paul Calderone | 7776960 | 2011-04-06 18:20:10 -0400 | [diff] [blame] | 24 | return s.encode("charmap") |
Jean-Paul Calderone | ea9c8a3 | 2011-04-01 18:26:37 -0400 | [diff] [blame] | 25 | bytes = bytes |
Jean-Paul Calderone | 9e4eeae | 2010-08-22 21:32:52 -0400 | [diff] [blame] | 26 | |
| 27 | |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 28 | class TestCase(TestCase): |
| 29 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 30 | :py:class:`TestCase` adds useful testing functionality beyond what is available |
| 31 | from the standard library :py:class:`unittest.TestCase`. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 32 | """ |
| 33 | def tearDown(self): |
| 34 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 35 | Clean up any files or directories created using :py:meth:`TestCase.mktemp`. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 36 | Subclasses must invoke this method if they override it or the |
| 37 | cleanup will not occur. |
| 38 | """ |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 39 | if False and self._temporaryFiles is not None: |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 40 | for temp in self._temporaryFiles: |
| 41 | if os.path.isdir(temp): |
| 42 | shutil.rmtree(temp) |
| 43 | elif os.path.exists(temp): |
| 44 | os.unlink(temp) |
Jean-Paul Calderone | 1206daf | 2009-07-16 16:07:42 -0400 | [diff] [blame] | 45 | try: |
| 46 | _exception_from_error_queue() |
Jean-Paul Calderone | 24b6459 | 2010-08-12 10:43:09 -0400 | [diff] [blame] | 47 | except Error: |
| 48 | e = sys.exc_info()[1] |
Jean-Paul Calderone | 1206daf | 2009-07-16 16:07:42 -0400 | [diff] [blame] | 49 | if e.args != ([],): |
| 50 | self.fail("Left over errors in OpenSSL error queue: " + repr(e)) |
| 51 | |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 52 | |
Jean-Paul Calderone | abfbab6 | 2013-02-09 21:25:02 -0800 | [diff] [blame^] | 53 | |
Jean-Paul Calderone | 060a57e | 2011-05-04 18:02:49 -0400 | [diff] [blame] | 54 | def failUnlessIn(self, containee, container, msg=None): |
| 55 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 56 | 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] | 57 | |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 58 | :param containee: the value that should be in :py:class:`container` |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 59 | :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] | 60 | will follow semantics of 'if key in dict.keys()' |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 61 | :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] | 62 | '%r not in %r' % (first, second) |
| 63 | """ |
| 64 | if containee not in container: |
| 65 | raise self.failureException(msg or "%r not in %r" |
| 66 | % (containee, container)) |
| 67 | return containee |
| 68 | assertIn = failUnlessIn |
| 69 | |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 70 | def failUnlessIdentical(self, first, second, msg=None): |
| 71 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 72 | 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] | 73 | obect-identity-equality test, not an object equality |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 74 | (i.e. :py:func:`__eq__`) test. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 75 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 76 | :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] | 77 | '%r is not %r' % (first, second) |
| 78 | """ |
| 79 | if first is not second: |
| 80 | raise self.failureException(msg or '%r is not %r' % (first, second)) |
| 81 | return first |
| 82 | assertIdentical = failUnlessIdentical |
| 83 | |
| 84 | |
| 85 | def failIfIdentical(self, first, second, msg=None): |
| 86 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 87 | 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] | 88 | obect-identity-equality test, not an object equality |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 89 | (i.e. :py:func:`__eq__`) test. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 90 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 91 | :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] | 92 | '%r is %r' % (first, second) |
| 93 | """ |
| 94 | if first is second: |
| 95 | raise self.failureException(msg or '%r is %r' % (first, second)) |
| 96 | return first |
| 97 | assertNotIdentical = failIfIdentical |
| 98 | |
| 99 | |
| 100 | def failUnlessRaises(self, exception, f, *args, **kwargs): |
| 101 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 102 | Fail the test unless calling the function :py:data:`f` with the given |
| 103 | :py:data:`args` and :py:data:`kwargs` raises :py:data:`exception`. The |
| 104 | failure will report the traceback and call stack of the unexpected |
| 105 | exception. |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 106 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 107 | :param exception: exception type that is to be expected |
| 108 | :param f: the function to call |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 109 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 110 | :return: The raised exception instance, if it is of the given type. |
| 111 | :raise self.failureException: Raised if the function call does |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 112 | not raise an exception or if it raises an exception of a |
| 113 | different type. |
| 114 | """ |
| 115 | try: |
| 116 | result = f(*args, **kwargs) |
Jean-Paul Calderone | 24b6459 | 2010-08-12 10:43:09 -0400 | [diff] [blame] | 117 | except exception: |
| 118 | inst = sys.exc_info()[1] |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 119 | return inst |
| 120 | except: |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 121 | raise self.failureException('%s raised instead of %s' |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 122 | % (sys.exc_info()[0], |
| 123 | exception.__name__, |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 124 | )) |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 125 | else: |
| 126 | raise self.failureException('%s not raised (%r returned)' |
| 127 | % (exception.__name__, result)) |
| 128 | assertRaises = failUnlessRaises |
| 129 | |
| 130 | |
| 131 | _temporaryFiles = None |
| 132 | def mktemp(self): |
| 133 | """ |
| 134 | Pathetic substitute for twisted.trial.unittest.TestCase.mktemp. |
| 135 | """ |
| 136 | if self._temporaryFiles is None: |
| 137 | self._temporaryFiles = [] |
| 138 | temp = mktemp(dir=".") |
| 139 | self._temporaryFiles.append(temp) |
| 140 | return temp |
| 141 | |
| 142 | |
| 143 | # Python 2.3 compatibility. |
| 144 | def assertTrue(self, *a, **kw): |
| 145 | return self.failUnless(*a, **kw) |
| 146 | |
| 147 | |
| 148 | def assertFalse(self, *a, **kw): |
| 149 | return self.failIf(*a, **kw) |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 150 | |
| 151 | |
| 152 | # Other stuff |
| 153 | def assertConsistentType(self, theType, name, *constructionArgs): |
| 154 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 155 | 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] | 156 | well-defined type. This is useful for extension types, where it's |
| 157 | pretty easy to do something wacky. If something about the type is |
| 158 | unusual, an exception will be raised. |
| 159 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 160 | :param theType: The type object about which to make assertions. |
| 161 | :param name: A string giving the name of the type. |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 162 | :param constructionArgs: Positional arguments to use with :py:data:`theType` to |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 163 | create an instance of it. |
| 164 | """ |
| 165 | self.assertEqual(theType.__name__, name) |
| 166 | self.assertTrue(isinstance(theType, type)) |
| 167 | instance = theType(*constructionArgs) |
| 168 | self.assertIdentical(type(instance), theType) |