Allow overrides of native packages.
[zeroinstall.git] / tests / testifacecache.py
blob73a28369b4dca31dc7a27115a8bd97cb576c4cd0
1 #!/usr/bin/env python2.4
2 from basetest import BaseTest
3 import sys, tempfile, os, shutil
4 import unittest
5 import data
6 from logging import getLogger, DEBUG, INFO
7 #getLogger().setLevel(DEBUG)
9 sys.path.insert(0, '..')
10 from zeroinstall.injector import basedir, download, model, gpg, trust
11 from zeroinstall.injector.namespaces import *
12 from zeroinstall.injector.iface_cache import iface_cache, PendingFeed
14 class TestIfaceCache(BaseTest):
15 def testList(self):
16 self.assertEquals([], iface_cache.list_all_interfaces())
17 iface_dir = basedir.save_cache_path(config_site, 'interfaces')
18 file(os.path.join(iface_dir, 'http%3a%2f%2ffoo'), 'w').close()
19 self.assertEquals(['http://foo'],
20 iface_cache.list_all_interfaces())
21 # TODO: test overrides
23 def testCheckSigned(self):
24 trust.trust_db.trust_key(
25 '92429807C9853C0744A68B9AAE07828059A53CC1')
26 iface = iface_cache.get_interface('http://foo')
27 src = tempfile.TemporaryFile()
29 # Unsigned
30 src.write("hello")
31 src.flush()
32 src.seek(0)
33 try:
34 PendingFeed(iface.uri, src)
35 assert 0
36 except model.SafeException:
37 pass
39 stream = tempfile.TemporaryFile()
40 stream.write(data.thomas_key)
41 stream.seek(0)
43 gpg.import_key(stream)
45 # Signed
46 src.seek(0)
47 src.write(data.foo_signed)
48 src.flush()
49 src.seek(0)
51 pending = PendingFeed(iface.uri, src)
52 iface_cache.add_pending(pending)
53 assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
55 self.assertEquals(['http://foo'],
56 iface_cache.list_all_interfaces())
58 self.assertEquals(1116788178, iface.last_modified)
60 # mtimes are unreliable because copying often changes them -
61 # check that we stored the mtime in an attribute
62 upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
63 cached = os.path.join(upstream_dir, model.escape(iface.uri))
64 os.utime(cached, None)
66 iface_cache.__init__()
67 iface = iface_cache.get_interface('http://foo')
68 self.assertEquals(1116788178, iface.last_modified)
70 def testXMLupdate(self):
71 trust.trust_db.trust_key(
72 '92429807C9853C0744A68B9AAE07828059A53CC1')
73 stream = tempfile.TemporaryFile()
74 stream.write(data.thomas_key)
75 stream.seek(0)
76 gpg.import_key(stream)
78 iface = iface_cache.get_interface('http://foo')
79 src = tempfile.TemporaryFile()
80 src.write(data.foo_signed_xml)
81 src.seek(0)
82 pending = PendingFeed(iface.uri, src)
83 iface_cache.add_pending(pending)
84 assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
86 iface_cache.__init__()
87 iface = iface_cache.get_interface('http://foo')
88 assert iface.last_modified == 1154850229
90 # mtimes are unreliable because copying often changes them -
91 # check that we extract the time from the signature when upgrading
92 upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
93 cached = os.path.join(upstream_dir, model.escape(iface.uri))
94 os.utime(cached, None)
96 iface_cache.__init__()
97 iface = iface_cache.get_interface('http://foo')
98 assert iface.last_modified > 1154850229
100 src = tempfile.TemporaryFile()
101 src.write(data.new_foo_signed_xml)
102 src.seek(0)
104 pending = PendingFeed(iface.uri, src)
105 iface_cache.add_pending(pending)
106 assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
108 # Can't 'update' to an older copy
109 src = tempfile.TemporaryFile()
110 src.write(data.foo_signed_xml)
111 src.seek(0)
112 try:
113 pending = PendingFeed(iface.uri, src)
114 iface_cache.add_pending(pending)
115 assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
117 assert 0
118 except model.SafeException:
119 pass
121 def testTimes(self):
122 stream = tempfile.TemporaryFile()
123 stream.write(data.thomas_key)
124 stream.seek(0)
125 gpg.import_key(stream)
127 upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
128 cached = os.path.join(upstream_dir, model.escape('http://foo'))
130 stream = file(cached, 'w')
131 stream.write(data.foo_signed_xml)
132 stream.close()
134 signed = iface_cache._get_signature_date('http://foo')
135 assert signed == None
137 trust.trust_db.trust_key(
138 '92429807C9853C0744A68B9AAE07828059A53CC1')
140 signed = iface_cache._get_signature_date('http://foo')
141 assert signed == 1154850229
143 stream = file(cached, 'w+')
144 stream.seek(0)
145 stream.write('Hello')
146 stream.close()
148 # When the signature is invalid, we just return None.
149 # This is because versions < 0.22 used to corrupt the signatue
150 # by adding an attribute to the XML
151 signed = iface_cache._get_signature_date('http://foo')
152 assert signed == None
154 suite = unittest.makeSuite(TestIfaceCache)
155 if __name__ == '__main__':
156 sys.argv.append('-v')
157 unittest.main()