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