Support extra restrictions passed in by user
[zeroinstall/solver.git] / tests / testtrust.py
blob2e712e07ce343dac068f8613db47388b7a53e235
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 trust.trust_db.untrust_key(thomas_fingerprint, domain = '0install.net') # Gets added by default
15 assert not trust.trust_db.is_trusted(thomas_fingerprint)
16 assert not trust.trust_db.is_trusted("1234")
17 assert len(trust.trust_db.keys) == 0
19 def testAddInvalid(self):
20 try:
21 trust.trust_db.trust_key("hello")
22 assert 0
23 except ValueError:
24 pass
26 def testAdd(self):
27 assert not trust.trust_db.is_trusted("1234")
28 trust.trust_db.trust_key("1234")
29 assert trust.trust_db.is_trusted("1234")
30 assert not trust.trust_db.is_trusted("1236")
32 trust.trust_db.untrust_key("1234")
33 assert not trust.trust_db.is_trusted("1234")
35 def testAddDomain(self):
36 assert not trust.trust_db.is_trusted("1234", "0install.net")
37 trust.trust_db.trust_key("1234")
38 self.assertEquals(set(['*']), trust.trust_db.get_trust_domains("1234"))
39 self.assertEquals(set(['1234']), trust.trust_db.get_keys_for_domain("*"))
40 self.assertEquals(set(), trust.trust_db.get_trust_domains("bob"))
42 assert trust.trust_db.is_trusted("1234")
43 assert trust.trust_db.is_trusted("1234", "0install.net")
44 assert trust.trust_db.is_trusted("1234", "rox.sourceforge.net")
45 assert not trust.trust_db.is_trusted("1236")
47 trust.trust_db.untrust_key("1234")
48 assert not trust.trust_db.is_trusted("1234")
49 assert not trust.trust_db.is_trusted("1234", "rox.sourceforge.net")
51 trust.trust_db.trust_key("1234", "0install.net")
52 trust.trust_db.trust_key("1234", "gimp.org")
53 trust.trust_db.trust_key("1236", "gimp.org")
54 assert trust.trust_db.is_trusted("1234")
55 assert trust.trust_db.is_trusted("1234", "0install.net")
56 assert trust.trust_db.is_trusted("1234", "gimp.org")
57 assert not trust.trust_db.is_trusted("1234", "rox.sourceforge.net")
59 self.assertEquals(set(['1234', '1236']),
60 trust.trust_db.get_keys_for_domain("gimp.org"))
62 self.assertEquals(set(), trust.trust_db.get_trust_domains("99877"))
63 self.assertEquals(set(['0install.net', 'gimp.org']), trust.trust_db.get_trust_domains("1234"))
65 def testParallel(self):
66 a = trust.TrustDB()
67 b = trust.TrustDB()
68 a.trust_key("1")
69 assert b.is_trusted("1")
70 b.trust_key("2")
71 a.untrust_key("1")
72 assert not a.is_trusted("1")
73 assert a.is_trusted("2")
75 def testDomain(self):
76 self.assertEquals("example.com", trust.domain_from_url('http://example.com/foo'))
77 self.assertRaises(SafeException, lambda: trust.domain_from_url('/tmp/feed.xml'))
78 self.assertRaises(SafeException, lambda: trust.domain_from_url('http:///foo'))
79 self.assertRaises(SafeException, lambda: trust.domain_from_url('http://*/foo'))
80 self.assertRaises(SafeException, lambda: trust.domain_from_url(''))
83 suite = unittest.makeSuite(TestTrust)
84 if __name__ == '__main__':
85 sys.argv.append('-v')
86 unittest.main()