forked from xmlsec/python-xmlsec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_keys.py
More file actions
134 lines (103 loc) · 5.32 KB
/
test_keys.py
File metadata and controls
134 lines (103 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from tests import base
import copy
import xmlsec
consts = xmlsec.constants
class TestKeys(base.TestMemoryLeaks):
def test_key_from_memory(self):
key = xmlsec.Key.from_memory(self.load("rsakey.pem"), format=consts.KeyDataFormatPem)
self.assertIsNotNone(key)
def test_key_from_memory_with_bad_args(self):
with self.assertRaises(TypeError):
xmlsec.Key.from_memory(1, format="")
def test_key_from_file(self):
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
self.assertIsNotNone(key)
def test_key_from_file_with_bad_args(self):
with self.assertRaises(TypeError):
xmlsec.Key.from_file(1, format="")
def test_key_from_fileobj(self):
with open(self.path("rsakey.pem"), "rb") as fobj:
key = xmlsec.Key.from_file(fobj, format=consts.KeyDataFormatPem)
self.assertIsNotNone(key)
def test_generate(self):
key = xmlsec.Key.generate(klass=consts.KeyDataAes, size=256, type=consts.KeyDataTypeSession)
self.assertIsNotNone(key)
def test_generate_with_bad_args(self):
with self.assertRaises(TypeError):
xmlsec.Key.generate(klass="", size="", type="")
def test_from_binary_file(self):
key = xmlsec.Key.from_binary_file(klass=consts.KeyDataDes, filename=self.path("deskey.bin"))
self.assertIsNotNone(key)
def test_from_binary_file_with_bad_args(self):
with self.assertRaises(TypeError):
xmlsec.Key.from_binary_file(klass="", filename=1)
def test_from_binary_data(self):
key = xmlsec.Key.from_binary_data(klass=consts.KeyDataDes, data=self.load("deskey.bin"))
self.assertIsNotNone(key)
def test_from_binary_data_with_bad_args(self):
with self.assertRaises(TypeError):
xmlsec.Key.from_binary_data(klass="", data=1)
def test_load_cert_from_file(self):
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
self.assertIsNotNone(key)
key.load_cert_from_file(self.path("rsacert.pem"), format=consts.KeyDataFormatPem)
def test_load_cert_from_file_with_bad_args(self):
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
self.assertIsNotNone(key)
with self.assertRaises(TypeError):
key.load_cert_from_file(1, format="")
def test_load_cert_from_fileobj(self):
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
self.assertIsNotNone(key)
with open(self.path("rsacert.pem"), "rb") as fobj:
key.load_cert_from_file(fobj, format=consts.KeyDataFormatPem)
def test_load_cert_from_memory(self):
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
self.assertIsNotNone(key)
key.load_cert_from_memory(self.load("rsacert.pem"), format=consts.KeyDataFormatPem)
def test_load_cert_from_memory_with_bad_args(self):
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
self.assertIsNotNone(key)
with self.assertRaises(TypeError):
key.load_cert_from_memory(1, format="")
def test_name(self):
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
self.assertIsNone(key.name)
key.name = "rsakey"
self.assertEqual("rsakey", key.name)
def test_copy(self):
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
key2 = copy.copy(key)
del key
key2.load_cert_from_file(self.path("rsacert.pem"), format=consts.KeyDataFormatPem)
class TestKeysManager(base.TestMemoryLeaks):
def test_add_key(self):
key = xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem)
mngr = xmlsec.KeysManager()
mngr.add_key(key)
def test_add_key_with_bad_args(self):
mngr = xmlsec.KeysManager()
with self.assertRaises(TypeError):
mngr.add_key("")
def test_load_cert(self):
mngr = xmlsec.KeysManager()
mngr.add_key(xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem))
mngr.load_cert(self.path("rsacert.pem"), format=consts.KeyDataFormatPem, type=consts.KeyDataTypeTrusted)
def test_load_cert_with_bad_args(self):
mngr = xmlsec.KeysManager()
mngr.add_key(xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem))
with self.assertRaises(TypeError):
mngr.load_cert(1, format="", type="")
def test_load_cert_from_memory(self):
mngr = xmlsec.KeysManager()
mngr.add_key(xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem))
mngr.load_cert_from_memory(self.load("rsacert.pem"), format=consts.KeyDataFormatPem, type=consts.KeyDataTypeTrusted)
def test_load_cert_from_memory_with_bad_args(self):
mngr = xmlsec.KeysManager()
mngr.add_key(xmlsec.Key.from_file(self.path("rsakey.pem"), format=consts.KeyDataFormatPem))
with self.assertRaises(TypeError):
mngr.load_cert_from_memory(1, format="", type="")
def test_load_invalid_key(self):
mngr = xmlsec.KeysManager()
with self.assertRaises(ValueError):
mngr.add_key(xmlsec.Key())