-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_SshAgent.py
executable file
·86 lines (64 loc) · 2.65 KB
/
test_SshAgent.py
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
#!/bin/env python
# -*- coding: utf-8 -*-
import unittest
from SshAgent import SshAgentClient, ConfirmationConstraint, LifetimeConstraint
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric import dsa
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
class TestSshAgent(unittest.TestCase):
password = b'12345'
key_files = {
'RSA' : './testdata/embedded_keys/id_rsa',
'DSA' : './testdata/embedded_keys/id_dsa',
'ECDSA': './testdata/embedded_keys/id_ecdsa',
#'ED25519': './embedded_keys/id_ed25519', #Not yet supported by pyca/cryptography
}
def setUp(self):
self.agent = SshAgentClient()
self.keys = dict(self._load_key(f) for f in self.key_files.values())
def tearDown(self):
self.agent.close()
def _load_key(self,key_file):
with open(key_file,'rb') as f:
key = serialization.load_pem_private_key(
f.read(),
backend=default_backend(),
password=self.password)
return (key_file, key)
def _test_key(self,key):
self.agent.add_key(key)
self.assertTrue(self.agent.is_key_active(key))
self.agent.remove_key(key)
self.assertFalse(self.agent.is_key_active(key))
confirm = ConfirmationConstraint()
lifetime = LifetimeConstraint(100)
self.agent.add_key(key, constraints=[confirm])
self.assertTrue(self.agent.is_key_active(key))
self.agent.remove_key(key.public_key())
self.agent.add_key(key, constraints=[lifetime])
self.assertTrue(self.agent.is_key_active(key))
self.agent.remove_key(key.public_key())
self.agent.add_key(key, constraints=[confirm,lifetime])
self.assertTrue(self.agent.is_key_active(key))
self.agent.remove_key(key.public_key())
def test_rsa(self):
key = self.keys[self.key_files['RSA']]
self._test_key(key)
def test_dsa(self):
key = self.keys[self.key_files['DSA']]
self._test_key(key)
def test_ecdsa(self):
key = self.keys[self.key_files['ECDSA']]
self._test_key(key)
def test_clear(self):
for key in self.keys.values():
self.agent.add_key(key)
for key in self.keys.values():
self.assertTrue(self.agent.is_key_active(key))
self.agent.clear_all_keys()
active_keys = list(self.agent.query_active_keys())
self.assertFalse(any(active_keys))
if __name__ == '__main__':
unittest.main()