blob: b9dd562daafc16dc8f9a393143be5140924f5e2a [file] [log] [blame]
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -05001
2"""
3Unit tests for L{OpenSSL.crypto}.
4"""
5
6from unittest import TestCase
7
8from OpenSSL.crypto import TYPE_RSA, TYPE_DSA, Error, PKey, PKeyType
9
10
11class PKeyTests(TestCase):
12 """
13 Unit tests for L{OpenSSL.crypto.PKey}.
14 """
15 def test_construction(self):
16 """
17 L{PKey} takes no arguments and returns a new L{PKeyType} instance.
18 """
19 self.assertRaises(TypeError, PKey, None)
20 key = PKey()
21 self.assertTrue(
22 isinstance(key, PKeyType),
23 "%r is of type %r, should be %r" % (key, type(key), PKeyType))
24
25
26 def test_pregeneration(self):
27 """
28 L{PKeyType.bits} and L{PKeyType.type} return C{0} before the key is
29 generated.
30 """
31 key = PKey()
32 self.assertEqual(key.type(), 0)
33 self.assertEqual(key.bits(), 0)
34
35
36 def test_failedGeneration(self):
37 """
38 L{PKeyType.generate_key} takes two arguments, the first giving the
39 key type as one of L{TYPE_RSA} or L{TYPE_DSA} and the second giving
40 the number of bits to generate. If an invalid type is specified or
41 generation fails, L{Error} is raised.
42 """
43 key = PKey()
44 self.assertRaises(TypeError, key.generate_key)
45 self.assertRaises(TypeError, key.generate_key, 1, 2, 3)
46 self.assertRaises(TypeError, key.generate_key, "foo", "bar")
47 self.assertRaises(Error, key.generate_key, -1, 0)
48 self.assertRaises(Error, key.generate_key, TYPE_RSA, 0)
49
50 # XXX DSA generation seems happy with any number of bits. The DSS
51 # says bits must be between 512 and 1024 inclusive. OpenSSL's DSA
52 # generator doesn't seem to care about the upper limit at all. For
53 # the lower limit, it uses 512 if anything smaller is specified.
54 # So, it doesn't seem possible to make generate_key fail for
55 # TYPE_DSA with a bits argument which is at least an int.
56
57 # self.assertRaises(Error, key.generate_key, TYPE_DSA, -7)
58
59
60 def test_rsaGeneration(self):
61 """
62 L{PKeyType.generate_key} generates an RSA key when passed
63 L{TYPE_RSA} as a type and a reasonable number of bits.
64 """
65 bits = 128
66 key = PKey()
67 key.generate_key(TYPE_RSA, bits)
68 self.assertEqual(key.type(), TYPE_RSA)
69 self.assertEqual(key.bits(), bits)
70
71
72 def test_dsaGeneration(self):
73 """
74 L{PKeyType.generate_key} generates a DSA key when passed
75 L{TYPE_DSA} as a type and a reasonable number of bits.
76 """
77 # 512 is a magic number. The DSS (Digital Signature Standard)
78 # allows a minimum of 512 bits for DSA. DSA_generate_parameters
79 # will silently promote any value below 512 to 512.
80 bits = 512
81 key = PKey()
82 key.generate_key(TYPE_DSA, bits)
83 self.assertEqual(key.type(), TYPE_DSA)
84 self.assertEqual(key.bits(), bits)
85
86
87 def test_regeneration(self):
88 """
89 L{PKeyType.generate_key} can be called multiple times on the same
90 key to generate new keys.
91 """
92 key = PKey()
93 for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]:
94 key.generate_key(type, bits)
95 self.assertEqual(key.type(), type)
96 self.assertEqual(key.bits(), bits)