1 #!/usr/bin/env python2.3
2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, shutil
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
):
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()
34 PendingFeed(iface
.uri
, src
)
36 except model
.SafeException
:
39 stream
= tempfile
.TemporaryFile()
40 stream
.write(data
.thomas_key
)
43 gpg
.import_key(stream
)
47 src
.write(data
.foo_signed
)
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
)
76 gpg
.import_key(stream
)
78 iface
= iface_cache
.get_interface('http://foo')
79 src
= tempfile
.TemporaryFile()
80 src
.write(data
.foo_signed_xml
)
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
)
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
)
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
)
118 except model
.SafeException
:
122 stream
= tempfile
.TemporaryFile()
123 stream
.write(data
.thomas_key
)
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
)
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+')
145 stream
.write('Hello')
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')