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