/
test_Keychain.py
78 lines (58 loc) · 2.69 KB
/
test_Keychain.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
#!/usr/bin/env python
# encoding: utf-8
"""
Keychain.py
Created by Chris Adams on 2009-01-06.
"""
import unittest
from PyMacAdmin.Security.Keychain import Keychain, GenericPassword, InternetPassword
class KeychainTests(unittest.TestCase):
"""Unit test for the Keychain module"""
def setUp(self):
pass
def test_load_default_keychain(self):
k = Keychain()
self.failIfEqual(k, None)
def test_load_system_keychain(self):
k = Keychain('/Library/Keychains/System.keychain')
self.failIfEqual(k, None)
def test_find_airport_password(self):
system_keychain = Keychain("/Library/Keychains/System.keychain")
# BUG: Most people probably have this - but not everyone?
system_keychain.find_generic_password(account_name="linksys")
def test_find_nonexistent_generic_password(self):
import uuid
system_keychain = Keychain("/Library/Keychains/System.keychain")
self.assertRaises(KeyError, system_keychain.find_generic_password, **{ 'account_name': "NonExistantGenericPassword-%s" % uuid.uuid4() })
def test_add_and_remove_generic_password(self):
import uuid
k = Keychain()
service_name = "PyMacAdmin Keychain Unit Test"
account_name = str(uuid.uuid4())
password = str(uuid.uuid4())
i = GenericPassword(service_name=service_name, account_name=account_name, password=password)
k.add(i)
self.assertEquals(i.password, k.find_generic_password(service_name, account_name).password)
k.remove(i)
self.assertRaises(KeyError, k.find_generic_password, **{"service_name": service_name, "account_name": account_name})
def test_find_internet_password(self):
keychain = Keychain()
i = keychain.find_internet_password(server_name="connect.apple.com") # BUG: Most people probably have this - but not everyone?
self.failIfEqual(i, None)
def test_add_and_remove_internet_password(self):
import uuid
k = Keychain()
kwargs = {
'server_name': "pymacadmin.googlecode.com",
'account_name': "unittest",
'protocol_type': 'http',
'authentication_type': 'http',
'password': str(uuid.uuid4())
}
i = InternetPassword(**kwargs)
k.add(i)
self.assertEquals(i.password, k.find_internet_password(server_name=kwargs['server_name'], account_name=kwargs['account_name']).password)
k.remove(i)
self.assertRaises(KeyError, k.find_internet_password, **{"server_name": kwargs['server_name'], "account_name": kwargs['account_name']})
if __name__ == '__main__':
unittest.main()