blob: d195d952c8487ce0df8492378547ef813907f93f [file] [log] [blame]
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -04001# Copyright (C) Jean-Paul Calderone 2009, All rights reserved
2# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
3# See LICENSE for details.
4
5"""
6Helpers for the OpenSSL test suite, largely copied from
7U{Twisted<http://twistedmatrix.com/>}.
8"""
9
10import shutil
11import os, os.path
12from tempfile import mktemp
13from unittest import TestCase
Rick Dean47262da2009-07-08 16:17:17 -050014import sys
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040015
Jean-Paul Calderone88f38b22009-07-16 16:25:19 -040016from OpenSSL.crypto import Error, _exception_from_error_queue
17
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040018
19class 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 """
30 if self._temporaryFiles is not None:
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 Calderone1206daf2009-07-16 16:07:42 -040036 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 Calderone0ef63ed2009-07-05 13:05:45 -040042
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 Dean47262da2009-07-08 16:17:17 -050092 raise self.failureException('%s raised instead of %s'
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040093 % (sys.exc_info()[0],
94 exception.__name__,
Rick Dean47262da2009-07-08 16:17:17 -050095 ))
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040096 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 Calderone68649052009-07-17 21:14:27 -0400121
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)