When checking a feed, check that the key is valid specifically for the feed's
[zeroinstall/zeroinstall-rsl.git] / tests / testtrust.py
blobdfb273bdd6165dcca36c463418ccd69f6529a791
1 #!/usr/bin/env python2.3
2 from basetest import BaseTest
3 import sys, tempfile, os, shutil
4 import unittest
5 from sets import Set
7 thomas_fingerprint = "92429807C9853C0744A68B9AAE07828059A53CC1"
9 sys.path.insert(0, '..')
10 from zeroinstall.injector import trust, basedir
11 from zeroinstall import SafeException
13 class TestTrust(BaseTest):
14 def testInit(self):
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"))
40 assert trust.trust_db.is_trusted("1234")
41 assert trust.trust_db.is_trusted("1234", "0install.net")
42 assert trust.trust_db.is_trusted("1234", "rox.sourceforge.net")
43 assert not trust.trust_db.is_trusted("1236")
45 trust.trust_db.untrust_key("1234")
46 assert not trust.trust_db.is_trusted("1234")
47 assert not trust.trust_db.is_trusted("1234", "rox.sourceforge.net")
49 trust.trust_db.trust_key("1234", "0install.net")
50 trust.trust_db.trust_key("1234", "gimp.org")
51 assert trust.trust_db.is_trusted("1234")
52 assert trust.trust_db.is_trusted("1234", "0install.net")
53 assert trust.trust_db.is_trusted("1234", "gimp.org")
54 assert not trust.trust_db.is_trusted("1234", "rox.sourceforge.net")
56 self.assertEquals(Set(), trust.trust_db.get_trust_domains("99877"))
57 self.assertEquals(Set(['0install.net', 'gimp.org']), trust.trust_db.get_trust_domains("1234"))
59 def testParallel(self):
60 a = trust.TrustDB()
61 b = trust.TrustDB()
62 a.trust_key("1")
63 assert b.is_trusted("1")
64 b.trust_key("2")
65 a.untrust_key("1")
66 assert not a.is_trusted("1")
67 assert a.is_trusted("2")
69 def testDomain(self):
70 self.assertEquals("example.com", trust.domain_from_url('http://example.com/foo'))
71 self.assertRaises(SafeException, lambda: trust.domain_from_url('/tmp/feed.xml'))
72 self.assertRaises(SafeException, lambda: trust.domain_from_url('http:///foo'))
73 self.assertRaises(SafeException, lambda: trust.domain_from_url('http://*/foo'))
74 self.assertRaises(SafeException, lambda: trust.domain_from_url(''))
77 suite = unittest.makeSuite(TestTrust)
78 if __name__ == '__main__':
79 sys.argv.append('-v')
80 unittest.main()