2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, time
7 sys
.path
.insert(0, '..')
8 from zeroinstall
.injector
import model
, gpg
, trust
9 from zeroinstall
.injector
.namespaces
import config_site
10 from zeroinstall
.injector
.iface_cache
import PendingFeed
11 from zeroinstall
.support
import basedir
13 class TestIfaceCache(BaseTest
):
15 iface_cache
= self
.config
.iface_cache
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 iface_cache
= self
.config
.iface_cache
25 trust
.trust_db
.trust_key(
26 '92429807C9853C0744A68B9AAE07828059A53CC1')
27 feed_url
= 'http://foo'
28 src
= tempfile
.TemporaryFile()
35 PendingFeed(feed_url
, src
)
37 except model
.SafeException
:
40 stream
= tempfile
.TemporaryFile()
41 stream
.write(data
.thomas_key
)
44 gpg
.import_key(stream
)
48 src
.write(data
.foo_signed_xml
)
52 pending
= PendingFeed(feed_url
, src
)
53 assert iface_cache
.update_feed_if_trusted(feed_url
, pending
.sigs
, pending
.new_xml
)
55 self
.assertEquals(['http://foo'],
56 iface_cache
.list_all_interfaces())
58 feed
= iface_cache
.get_feed(feed_url
)
60 self
.assertEquals(1154850229, feed
.last_modified
)
62 def testXMLupdate(self
):
63 iface_cache
= self
.config
.iface_cache
64 trust
.trust_db
.trust_key(
65 '92429807C9853C0744A68B9AAE07828059A53CC1')
66 stream
= tempfile
.TemporaryFile()
67 stream
.write(data
.thomas_key
)
69 gpg
.import_key(stream
)
71 iface
= iface_cache
.get_interface('http://foo')
72 src
= tempfile
.TemporaryFile()
73 src
.write(data
.foo_signed_xml
)
75 pending
= PendingFeed(iface
.uri
, src
)
76 assert iface_cache
.update_feed_if_trusted(iface
.uri
, pending
.sigs
, pending
.new_xml
)
78 iface_cache
.__init
__()
79 feed
= iface_cache
.get_feed('http://foo')
80 assert feed
.last_modified
== 1154850229
82 # mtimes are unreliable because copying often changes them -
83 # check that we extract the time from the signature when upgrading
84 upstream_dir
= basedir
.save_cache_path(config_site
, 'interfaces')
85 cached
= os
.path
.join(upstream_dir
, model
.escape(feed
.url
))
86 os
.utime(cached
, None)
88 iface_cache
.__init
__()
89 feed
= iface_cache
.get_feed('http://foo')
90 assert feed
.last_modified
> 1154850229
92 src
= tempfile
.TemporaryFile()
93 src
.write(data
.new_foo_signed_xml
)
96 pending
= PendingFeed(feed
.url
, src
)
97 assert iface_cache
.update_feed_if_trusted(feed
.url
, pending
.sigs
, pending
.new_xml
)
99 # Can't 'update' to an older copy
100 src
= tempfile
.TemporaryFile()
101 src
.write(data
.foo_signed_xml
)
104 pending
= PendingFeed(feed
.url
, src
)
105 assert iface_cache
.update_feed_if_trusted(feed
.url
, pending
.sigs
, pending
.new_xml
)
108 except model
.SafeException
:
112 iface_cache
= self
.config
.iface_cache
113 stream
= tempfile
.TemporaryFile()
114 stream
.write(data
.thomas_key
)
116 gpg
.import_key(stream
)
118 upstream_dir
= basedir
.save_cache_path(config_site
, 'interfaces')
119 cached
= os
.path
.join(upstream_dir
, model
.escape('http://foo'))
121 stream
= file(cached
, 'w')
122 stream
.write(data
.foo_signed_xml
)
125 signed
= iface_cache
._get
_signature
_date
('http://foo')
126 assert signed
== None
128 trust
.trust_db
.trust_key(
129 '92429807C9853C0744A68B9AAE07828059A53CC1')
131 signed
= iface_cache
._get
_signature
_date
('http://foo')
132 assert signed
== 1154850229
134 stream
= file(cached
, 'w+')
136 stream
.write('Hello')
139 # When the signature is invalid, we just return None.
140 # This is because versions < 0.22 used to corrupt the signatue
141 # by adding an attribute to the XML
142 signed
= iface_cache
._get
_signature
_date
('http://foo')
143 assert signed
== None
145 def testCheckAttempt(self
):
146 iface_cache
= self
.config
.iface_cache
147 self
.assertEquals(None, iface_cache
.get_last_check_attempt("http://foo/bar.xml"))
149 start_time
= time
.time() - 5 # Seems to be some odd rounding here
150 iface_cache
.mark_as_checking("http://foo/bar.xml")
151 last_check
= iface_cache
.get_last_check_attempt("http://foo/bar.xml")
153 assert last_check
is not None
154 assert last_check
>= start_time
, (last_check
, start_time
)
156 self
.assertEquals(None, iface_cache
.get_last_check_attempt("http://foo/bar2.xml"))
159 if __name__
== '__main__':