blob: e5b5dc359c6676b033ca078fa5bec9430a271a1e [file] [log] [blame]
Jean-Paul Calderone8671c852011-03-02 19:26:20 -05001# Copyright (C) Jean-Paul Calderone
2# Copyright (C) Twisted Matrix Laboratories.
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -04003# 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
Jean-Paul Calderone9e4eeae2010-08-22 21:32:52 -040019try:
20 bytes = bytes
21except NameError:
22 def b(s):
23 return s
24 bytes = str
25else:
26 def b(s):
27 return s.encode("ascii")
28
29
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040030class 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 Calderonebf37f0f2010-07-31 14:56:20 -040041 if False and self._temporaryFiles is not None:
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040042 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 Calderone1206daf2009-07-16 16:07:42 -040047 try:
48 _exception_from_error_queue()
Jean-Paul Calderone24b64592010-08-12 10:43:09 -040049 except Error:
50 e = sys.exc_info()[1]
Jean-Paul Calderone1206daf2009-07-16 16:07:42 -040051 if e.args != ([],):
52 self.fail("Left over errors in OpenSSL error queue: " + repr(e))
53
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -040054
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 Calderone24b64592010-08-12 10:43:09 -0400101 except exception:
102 inst = sys.exc_info()[1]
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -0400103 return inst
104 except:
Rick Dean47262da2009-07-08 16:17:17 -0500105 raise self.failureException('%s raised instead of %s'
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -0400106 % (sys.exc_info()[0],
107 exception.__name__,
Rick Dean47262da2009-07-08 16:17:17 -0500108 ))
Jean-Paul Calderone0ef63ed2009-07-05 13:05:45 -0400109 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 Calderone68649052009-07-17 21:14:27 -0400134
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)