Fix to previous commit
[zeroinstall/solver.git] / tests / testtrust.py
blobaceb16580a6ec667fff87967e10e1b1fd44f348e
1 #!/usr/bin/env python
2 from basetest import BaseTest
3 import sys
4 import unittest
6 thomas_fingerprint = "92429807C9853C0744A68B9AAE07828059A53CC1"
8 sys.path.insert(0, '..')
9 from zeroinstall.injector import trust
10 from zeroinstall import SafeException
12 class TestTrust(BaseTest):
13 def testInit(self):
14 assert not trust.trust_db.is_trusted(thomas_fingerprint)
15 assert not trust.trust_db.is_trusted("1234")
16 assert len(trust.trust_db.keys) == 0
18 def testAddInvalid(self):
19 try:
20 trust.trust_db.trust_key("hello")
21 assert 0
22 except ValueError:
23 pass
25 def testAdd(self):
26 assert not trust.trust_db.is_trusted("1234")
27 trust.trust_db.trust_key("1234")
28 assert trust.trust_db.is_trusted("1234")
29 assert not trust.trust_db.is_trusted("1236")
31 trust.trust_db.untrust_key("1234")
32 assert not trust.trust_db.is_trusted("1234")
34 def testAddDomain(self):
35 assert not trust.trust_db.is_trusted("1234", "0install.net")
36 trust.trust_db.trust_key("1234")
37 self.assertEqual(set(['*']), trust.trust_db.get_trust_domains("1234"))
38 self.assertEqual(set(['1234']), trust.trust_db.get_keys_for_domain("*"))
39 self.assertEqual(set(), trust.trust_db.get_trust_domains("bob"))
41 assert trust.trust_db.is_trusted("1234")
42 assert trust.trust_db.is_trusted("1234", "0install.net")
43 assert trust.trust_db.is_trusted("1234", "rox.sourceforge.net")
44 assert not trust.trust_db.is_trusted("1236")
46 trust.trust_db.untrust_key("1234")
47 assert not trust.trust_db.is_trusted("1234")
48 assert not trust.trust_db.is_trusted("1234", "rox.sourceforge.net")
50 trust.trust_db.trust_key("1234", "0install.net")
51 trust.trust_db.trust_key("1234", "gimp.org")
52 trust.trust_db.trust_key("1236", "gimp.org")
53 assert trust.trust_db.is_trusted("1234")
54 assert trust.trust_db.is_trusted("1234", "0install.net")
55 assert trust.trust_db.is_trusted("1234", "gimp.org")
56 assert not trust.trust_db.is_trusted("1234", "rox.sourceforge.net")
58 self.assertEqual(set(['1234', '1236']),
59 trust.trust_db.get_keys_for_domain("gimp.org"))
61 self.assertEqual(set(), trust.trust_db.get_trust_domains("99877"))
62 self.assertEqual(set(['0install.net', 'gimp.org']), trust.trust_db.get_trust_domains("1234"))
64 def testParallel(self):
65 a = trust.TrustDB()
66 b = trust.TrustDB()
67 a.trust_key("1")
68 assert b.is_trusted("1")
69 b.trust_key("2")
70 a.untrust_key("1")
71 assert not a.is_trusted("1")
72 assert a.is_trusted("2")
74 def testDomain(self):
75 self.assertEqual("example.com", trust.domain_from_url('http://example.com/foo'))
76 self.assertRaises(SafeException, lambda: trust.domain_from_url('/tmp/feed.xml'))
77 self.assertRaises(SafeException, lambda: trust.domain_from_url('http:///foo'))
78 self.assertRaises(SafeException, lambda: trust.domain_from_url('http://*/foo'))
79 self.assertRaises(SafeException, lambda: trust.domain_from_url(''))
82 if __name__ == '__main__':
83 unittest.main()