140.1.4
by Jean-Paul Calderone
Update copyright headers; remove pointless dates, add a pointer to the LICENSE file. |
1 |
# Copyright (C) Jean-Paul Calderone
|
2 |
# Copyright (C) Twisted Matrix Laboratories.
|
|
111
by Jean-Paul Calderone
Actually add util.py :/ And move mktemp into it, and add cleanup code to tearDown |
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 |
|
112.4.1
by Rick Dean
Add subject and issuer parameters to X509Extension(). Fix bug in OpenSSL.test.util.failUnlessRaises(). |
14 |
import sys |
111
by Jean-Paul Calderone
Actually add util.py :/ And move mktemp into it, and add cleanup code to tearDown |
15 |
|
114.1.4
by Jean-Paul Calderone
minor tweaks |
16 |
from OpenSSL.crypto import Error, _exception_from_error_queue |
17 |
||
142.1.6
by Jean-Paul Calderone
Make this bytes thing actually work :/ |
18 |
if sys.version_info < (3, 0): |
132.2.38
by Jean-Paul Calderone
Fix the import_crypto_module helper and factor some str/unicode helpers into the test util module |
19 |
def b(s): |
20 |
return s |
|
21 |
bytes = str |
|
22 |
else: |
|
23 |
def b(s): |
|
142.1.10
by Jean-Paul Calderone
encode text to bytes using charmap instead of ascii; charmap can actually represent every possible byte in a byte string |
24 |
return s.encode("charmap") |
142.1.6
by Jean-Paul Calderone
Make this bytes thing actually work :/ |
25 |
bytes = bytes |
132.2.38
by Jean-Paul Calderone
Fix the import_crypto_module helper and factor some str/unicode helpers into the test util module |
26 |
|
27 |
||
111
by Jean-Paul Calderone
Actually add util.py :/ And move mktemp into it, and add cleanup code to tearDown |
28 |
class TestCase(TestCase): |
29 |
"""
|
|
30 |
L{TestCase} adds useful testing functionality beyond what is available
|
|
31 |
from the standard library L{unittest.TestCase}.
|
|
32 |
"""
|
|
33 |
def tearDown(self): |
|
34 |
"""
|
|
35 |
Clean up any files or directories created using L{TestCase.mktemp}.
|
|
36 |
Subclasses must invoke this method if they override it or the
|
|
37 |
cleanup will not occur.
|
|
38 |
"""
|
|
132.1.34
by Jean-Paul Calderone
Try for a real test of Context.add_extra_chain_cert; not working |
39 |
if False and self._temporaryFiles is not None: |
111
by Jean-Paul Calderone
Actually add util.py :/ And move mktemp into it, and add cleanup code to tearDown |
40 |
for temp in self._temporaryFiles: |
41 |
if os.path.isdir(temp): |
|
42 |
shutil.rmtree(temp) |
|
43 |
elif os.path.exists(temp): |
|
44 |
os.unlink(temp) |
|
114.1.1
by Jean-Paul Calderone
Expose exception_from_error_queue in the most inelegant way; use this in the base TestCase to make sure no tests leave garbage errors lying around |
45 |
try: |
46 |
_exception_from_error_queue() |
|
132.2.21
by Jean-Paul Calderone
py3k syntax compat |
47 |
except Error: |
48 |
e = sys.exc_info()[1] |
|
114.1.1
by Jean-Paul Calderone
Expose exception_from_error_queue in the most inelegant way; use this in the base TestCase to make sure no tests leave garbage errors lying around |
49 |
if e.args != ([],): |
50 |
self.fail("Left over errors in OpenSSL error queue: " + repr(e)) |
|
51 |
||
111
by Jean-Paul Calderone
Actually add util.py :/ And move mktemp into it, and add cleanup code to tearDown |
52 |
|
53 |
def failUnlessIdentical(self, first, second, msg=None): |
|
54 |
"""
|
|
55 |
Fail the test if C{first} is not C{second}. This is an
|
|
56 |
obect-identity-equality test, not an object equality
|
|
57 |
(i.e. C{__eq__}) test.
|
|
58 |
||
59 |
@param msg: if msg is None, then the failure message will be
|
|
60 |
'%r is not %r' % (first, second)
|
|
61 |
"""
|
|
62 |
if first is not second: |
|
63 |
raise self.failureException(msg or '%r is not %r' % (first, second)) |
|
64 |
return first |
|
65 |
assertIdentical = failUnlessIdentical |
|
66 |
||
67 |
||
68 |
def failIfIdentical(self, first, second, msg=None): |
|
69 |
"""
|
|
70 |
Fail the test if C{first} is C{second}. This is an
|
|
71 |
obect-identity-equality test, not an object equality
|
|
72 |
(i.e. C{__eq__}) test.
|
|
73 |
||
74 |
@param msg: if msg is None, then the failure message will be
|
|
75 |
'%r is %r' % (first, second)
|
|
76 |
"""
|
|
77 |
if first is second: |
|
78 |
raise self.failureException(msg or '%r is %r' % (first, second)) |
|
79 |
return first |
|
80 |
assertNotIdentical = failIfIdentical |
|
81 |
||
82 |
||
83 |
def failUnlessRaises(self, exception, f, *args, **kwargs): |
|
84 |
"""
|
|
85 |
Fail the test unless calling the function C{f} with the given
|
|
86 |
C{args} and C{kwargs} raises C{exception}. The failure will report
|
|
87 |
the traceback and call stack of the unexpected exception.
|
|
88 |
||
89 |
@param exception: exception type that is to be expected
|
|
90 |
@param f: the function to call
|
|
91 |
||
92 |
@return: The raised exception instance, if it is of the given type.
|
|
93 |
@raise self.failureException: Raised if the function call does
|
|
94 |
not raise an exception or if it raises an exception of a
|
|
95 |
different type.
|
|
96 |
"""
|
|
97 |
try: |
|
98 |
result = f(*args, **kwargs) |
|
132.2.21
by Jean-Paul Calderone
py3k syntax compat |
99 |
except exception: |
100 |
inst = sys.exc_info()[1] |
|
111
by Jean-Paul Calderone
Actually add util.py :/ And move mktemp into it, and add cleanup code to tearDown |
101 |
return inst |
102 |
except: |
|
112.4.1
by Rick Dean
Add subject and issuer parameters to X509Extension(). Fix bug in OpenSSL.test.util.failUnlessRaises(). |
103 |
raise self.failureException('%s raised instead of %s' |
111
by Jean-Paul Calderone
Actually add util.py :/ And move mktemp into it, and add cleanup code to tearDown |
104 |
% (sys.exc_info()[0], |
105 |
exception.__name__, |
|
112.4.1
by Rick Dean
Add subject and issuer parameters to X509Extension(). Fix bug in OpenSSL.test.util.failUnlessRaises(). |
106 |
))
|
111
by Jean-Paul Calderone
Actually add util.py :/ And move mktemp into it, and add cleanup code to tearDown |
107 |
else: |
108 |
raise self.failureException('%s not raised (%r returned)' |
|
109 |
% (exception.__name__, result)) |
|
110 |
assertRaises = failUnlessRaises |
|
111 |
||
112 |
||
113 |
_temporaryFiles = None |
|
114 |
def mktemp(self): |
|
115 |
"""
|
|
116 |
Pathetic substitute for twisted.trial.unittest.TestCase.mktemp.
|
|
117 |
"""
|
|
118 |
if self._temporaryFiles is None: |
|
119 |
self._temporaryFiles = [] |
|
120 |
temp = mktemp(dir=".") |
|
121 |
self._temporaryFiles.append(temp) |
|
122 |
return temp |
|
123 |
||
124 |
||
125 |
# Python 2.3 compatibility.
|
|
126 |
def assertTrue(self, *a, **kw): |
|
127 |
return self.failUnless(*a, **kw) |
|
128 |
||
129 |
||
130 |
def assertFalse(self, *a, **kw): |
|
131 |
return self.failIf(*a, **kw) |
|
119
by Jean-Paul Calderone
Add a bunch of tests which assert that extension types are defined nicely. |
132 |
|
133 |
||
134 |
# Other stuff
|
|
135 |
def assertConsistentType(self, theType, name, *constructionArgs): |
|
136 |
"""
|
|
137 |
Perform various assertions about C{theType} to ensure that it is a
|
|
138 |
well-defined type. This is useful for extension types, where it's
|
|
139 |
pretty easy to do something wacky. If something about the type is
|
|
140 |
unusual, an exception will be raised.
|
|
141 |
||
142 |
@param theType: The type object about which to make assertions.
|
|
143 |
@param name: A string giving the name of the type.
|
|
144 |
@param constructionArgs: Positional arguments to use with C{theType} to
|
|
145 |
create an instance of it.
|
|
146 |
"""
|
|
147 |
self.assertEqual(theType.__name__, name) |
|
148 |
self.assertTrue(isinstance(theType, type)) |
|
149 |
instance = theType(*constructionArgs) |
|
150 |
self.assertIdentical(type(instance), theType) |