Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone 2009, All rights reserved |
| 2 | # Copyright (c) 2001-2009 Twisted Matrix Laboratories. |
| 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 |
| 14 | |
Jean-Paul Calderone | 88f38b2 | 2009-07-16 16:25:19 -0400 | [diff] [blame^] | 15 | from OpenSSL.crypto import Error, _exception_from_error_queue |
| 16 | |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 17 | |
| 18 | class TestCase(TestCase): |
| 19 | """ |
| 20 | L{TestCase} adds useful testing functionality beyond what is available |
| 21 | from the standard library L{unittest.TestCase}. |
| 22 | """ |
| 23 | def tearDown(self): |
| 24 | """ |
| 25 | Clean up any files or directories created using L{TestCase.mktemp}. |
| 26 | Subclasses must invoke this method if they override it or the |
| 27 | cleanup will not occur. |
| 28 | """ |
| 29 | if self._temporaryFiles is not None: |
| 30 | for temp in self._temporaryFiles: |
| 31 | if os.path.isdir(temp): |
| 32 | shutil.rmtree(temp) |
| 33 | elif os.path.exists(temp): |
| 34 | os.unlink(temp) |
Jean-Paul Calderone | 1206daf | 2009-07-16 16:07:42 -0400 | [diff] [blame] | 35 | try: |
| 36 | _exception_from_error_queue() |
| 37 | except Error, e: |
| 38 | if e.args != ([],): |
| 39 | self.fail("Left over errors in OpenSSL error queue: " + repr(e)) |
| 40 | |
Jean-Paul Calderone | 0ef63ed | 2009-07-05 13:05:45 -0400 | [diff] [blame] | 41 | |
| 42 | def failUnlessIdentical(self, first, second, msg=None): |
| 43 | """ |
| 44 | Fail the test if C{first} is not C{second}. This is an |
| 45 | obect-identity-equality test, not an object equality |
| 46 | (i.e. C{__eq__}) test. |
| 47 | |
| 48 | @param msg: if msg is None, then the failure message will be |
| 49 | '%r is not %r' % (first, second) |
| 50 | """ |
| 51 | if first is not second: |
| 52 | raise self.failureException(msg or '%r is not %r' % (first, second)) |
| 53 | return first |
| 54 | assertIdentical = failUnlessIdentical |
| 55 | |
| 56 | |
| 57 | def failIfIdentical(self, first, second, msg=None): |
| 58 | """ |
| 59 | Fail the test if C{first} is C{second}. This is an |
| 60 | obect-identity-equality test, not an object equality |
| 61 | (i.e. C{__eq__}) test. |
| 62 | |
| 63 | @param msg: if msg is None, then the failure message will be |
| 64 | '%r is %r' % (first, second) |
| 65 | """ |
| 66 | if first is second: |
| 67 | raise self.failureException(msg or '%r is %r' % (first, second)) |
| 68 | return first |
| 69 | assertNotIdentical = failIfIdentical |
| 70 | |
| 71 | |
| 72 | def failUnlessRaises(self, exception, f, *args, **kwargs): |
| 73 | """ |
| 74 | Fail the test unless calling the function C{f} with the given |
| 75 | C{args} and C{kwargs} raises C{exception}. The failure will report |
| 76 | the traceback and call stack of the unexpected exception. |
| 77 | |
| 78 | @param exception: exception type that is to be expected |
| 79 | @param f: the function to call |
| 80 | |
| 81 | @return: The raised exception instance, if it is of the given type. |
| 82 | @raise self.failureException: Raised if the function call does |
| 83 | not raise an exception or if it raises an exception of a |
| 84 | different type. |
| 85 | """ |
| 86 | try: |
| 87 | result = f(*args, **kwargs) |
| 88 | except exception, inst: |
| 89 | return inst |
| 90 | except: |
| 91 | raise self.failureException('%s raised instead of %s:\n %s' |
| 92 | % (sys.exc_info()[0], |
| 93 | exception.__name__, |
| 94 | failure.Failure().getTraceback())) |
| 95 | else: |
| 96 | raise self.failureException('%s not raised (%r returned)' |
| 97 | % (exception.__name__, result)) |
| 98 | assertRaises = failUnlessRaises |
| 99 | |
| 100 | |
| 101 | _temporaryFiles = None |
| 102 | def mktemp(self): |
| 103 | """ |
| 104 | Pathetic substitute for twisted.trial.unittest.TestCase.mktemp. |
| 105 | """ |
| 106 | if self._temporaryFiles is None: |
| 107 | self._temporaryFiles = [] |
| 108 | temp = mktemp(dir=".") |
| 109 | self._temporaryFiles.append(temp) |
| 110 | return temp |
| 111 | |
| 112 | |
| 113 | # Python 2.3 compatibility. |
| 114 | def assertTrue(self, *a, **kw): |
| 115 | return self.failUnless(*a, **kw) |
| 116 | |
| 117 | |
| 118 | def assertFalse(self, *a, **kw): |
| 119 | return self.failIf(*a, **kw) |