1
#!/usr/bin/env python2.3
2
import sys, tempfile, os, shutil
5
from logging import getLogger, DEBUG, INFO
6
#getLogger().setLevel(DEBUG)
8
sys.path.insert(0, '..')
9
from zeroinstall.injector import basedir, download, model, gpg, trust
10
from zeroinstall.injector.namespaces import *
11
from zeroinstall.injector.iface_cache import iface_cache, PendingFeed
13
class TestIfaceCache(unittest.TestCase):
15
self.config_home = tempfile.mktemp()
16
self.cache_home = tempfile.mktemp()
17
self.gnupg_home = tempfile.mktemp()
18
os.environ['XDG_CONFIG_HOME'] = self.config_home
19
os.environ['XDG_CACHE_HOME'] = self.cache_home
20
os.environ['XDG_CACHE_DIRS'] = ''
21
os.environ['GNUPGHOME'] = self.gnupg_home
24
os.mkdir(self.config_home, 0700)
25
os.mkdir(self.cache_home, 0700)
26
os.mkdir(self.gnupg_home, 0700)
28
iface_cache.__init__()
31
shutil.rmtree(self.config_home)
32
shutil.rmtree(self.cache_home)
33
shutil.rmtree(self.gnupg_home)
36
self.assertEquals([], iface_cache.list_all_interfaces())
37
iface_dir = basedir.save_cache_path(config_site, 'interfaces')
38
file(os.path.join(iface_dir, 'http%3a%2f%2ffoo'), 'w').close()
39
self.assertEquals(['http://foo'],
40
iface_cache.list_all_interfaces())
41
# TODO: test overrides
43
def testCheckSigned(self):
44
trust.trust_db.trust_key(
45
'92429807C9853C0744A68B9AAE07828059A53CC1')
46
iface = iface_cache.get_interface('http://foo')
47
src = tempfile.TemporaryFile()
54
PendingFeed(iface.uri, src)
56
except model.SafeException:
59
stream = tempfile.TemporaryFile()
60
stream.write(data.thomas_key)
63
gpg.import_key(stream)
67
src.write(data.foo_signed)
71
pending = PendingFeed(iface.uri, src)
72
iface_cache.add_pending(pending)
73
assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
75
self.assertEquals(['http://foo'],
76
iface_cache.list_all_interfaces())
78
self.assertEquals(1116788178, iface.last_modified)
80
# mtimes are unreliable because copying often changes them -
81
# check that we stored the mtime in an attribute
82
upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
83
cached = os.path.join(upstream_dir, model.escape(iface.uri))
84
os.utime(cached, None)
86
iface_cache.__init__()
87
iface = iface_cache.get_interface('http://foo')
88
self.assertEquals(1116788178, iface.last_modified)
90
def testXMLupdate(self):
91
trust.trust_db.trust_key(
92
'92429807C9853C0744A68B9AAE07828059A53CC1')
93
stream = tempfile.TemporaryFile()
94
stream.write(data.thomas_key)
96
gpg.import_key(stream)
98
iface = iface_cache.get_interface('http://foo')
99
src = tempfile.TemporaryFile()
100
src.write(data.foo_signed_xml)
102
pending = PendingFeed(iface.uri, src)
103
iface_cache.add_pending(pending)
104
assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
106
iface_cache.__init__()
107
iface = iface_cache.get_interface('http://foo')
108
assert iface.last_modified == 1154850229
110
# mtimes are unreliable because copying often changes them -
111
# check that we extract the time from the signature when upgrading
112
upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
113
cached = os.path.join(upstream_dir, model.escape(iface.uri))
114
os.utime(cached, None)
116
iface_cache.__init__()
117
iface = iface_cache.get_interface('http://foo')
118
assert iface.last_modified > 1154850229
120
src = tempfile.TemporaryFile()
121
src.write(data.new_foo_signed_xml)
124
pending = PendingFeed(iface.uri, src)
125
iface_cache.add_pending(pending)
126
assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
128
# Can't 'update' to an older copy
129
src = tempfile.TemporaryFile()
130
src.write(data.foo_signed_xml)
133
pending = PendingFeed(iface.uri, src)
134
iface_cache.add_pending(pending)
135
assert iface_cache.update_interface_if_trusted(iface, pending.sigs, pending.new_xml)
138
except model.SafeException:
142
stream = tempfile.TemporaryFile()
143
stream.write(data.thomas_key)
145
gpg.import_key(stream)
147
upstream_dir = basedir.save_cache_path(config_site, 'interfaces')
148
cached = os.path.join(upstream_dir, model.escape('http://foo'))
150
stream = file(cached, 'w')
151
stream.write(data.foo_signed_xml)
154
signed = iface_cache._get_signature_date('http://foo')
155
assert signed == None
157
trust.trust_db.trust_key(
158
'92429807C9853C0744A68B9AAE07828059A53CC1')
160
signed = iface_cache._get_signature_date('http://foo')
161
assert signed == 1154850229
163
stream = file(cached, 'w+')
165
stream.write('Hello')
168
# When the signature is invalid, we just return None.
169
# This is because versions < 0.22 used to corrupt the signatue
170
# by adding an attribute to the XML
171
signed = iface_cache._get_signature_date('http://foo')
172
assert signed == None
174
suite = unittest.makeSuite(TestIfaceCache)
175
if __name__ == '__main__':
176
sys.argv.append('-v')