1 #!/usr/bin/env python2.4
2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, shutil
, time
6 from logging
import getLogger
, DEBUG
, INFO
7 #getLogger().setLevel(DEBUG)
9 sys
.path
.insert(0, '..')
10 from zeroinstall
.injector
import download
, model
, gpg
, trust
11 from zeroinstall
.injector
.namespaces
import *
12 from zeroinstall
.injector
.iface_cache
import iface_cache
, PendingFeed
13 from zeroinstall
.support
import basedir
15 class TestIfaceCache(BaseTest
):
17 self
.assertEquals([], iface_cache
.list_all_interfaces())
18 iface_dir
= basedir
.save_cache_path(config_site
, 'interfaces')
19 file(os
.path
.join(iface_dir
, 'http%3a%2f%2ffoo'), 'w').close()
20 self
.assertEquals(['http://foo'],
21 iface_cache
.list_all_interfaces())
22 # TODO: test overrides
24 def testCheckSigned(self
):
25 trust
.trust_db
.trust_key(
26 '92429807C9853C0744A68B9AAE07828059A53CC1')
27 iface
= iface_cache
.get_interface('http://foo')
28 src
= tempfile
.TemporaryFile()
35 PendingFeed(iface
.uri
, 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
)
53 pending
= PendingFeed(iface
.uri
, src
)
54 raise Exception("Old feeds must be rejected")
55 except model
.SafeException
, ex
:
56 assert str(ex
).startswith("Sorry, "), ex
61 src
.write(data
.foo_signed_xml
)
65 pending
= PendingFeed(iface
.uri
, src
)
66 iface_cache
.add_pending(pending
)
67 assert iface_cache
.update_interface_if_trusted(iface
, pending
.sigs
, pending
.new_xml
)
69 self
.assertEquals(['http://foo'],
70 iface_cache
.list_all_interfaces())
72 self
.assertEquals(1154850229, iface
.last_modified
)
74 def testXMLupdate(self
):
75 trust
.trust_db
.trust_key(
76 '92429807C9853C0744A68B9AAE07828059A53CC1')
77 stream
= tempfile
.TemporaryFile()
78 stream
.write(data
.thomas_key
)
80 gpg
.import_key(stream
)
82 iface
= iface_cache
.get_interface('http://foo')
83 src
= tempfile
.TemporaryFile()
84 src
.write(data
.foo_signed_xml
)
86 pending
= PendingFeed(iface
.uri
, src
)
87 iface_cache
.add_pending(pending
)
88 assert iface_cache
.update_interface_if_trusted(iface
, pending
.sigs
, pending
.new_xml
)
90 iface_cache
.__init
__()
91 iface
= iface_cache
.get_interface('http://foo')
92 assert iface
.last_modified
== 1154850229
94 # mtimes are unreliable because copying often changes them -
95 # check that we extract the time from the signature when upgrading
96 upstream_dir
= basedir
.save_cache_path(config_site
, 'interfaces')
97 cached
= os
.path
.join(upstream_dir
, model
.escape(iface
.uri
))
98 os
.utime(cached
, None)
100 iface_cache
.__init
__()
101 iface
= iface_cache
.get_interface('http://foo')
102 assert iface
.last_modified
> 1154850229
104 src
= tempfile
.TemporaryFile()
105 src
.write(data
.new_foo_signed_xml
)
108 pending
= PendingFeed(iface
.uri
, src
)
109 iface_cache
.add_pending(pending
)
110 assert iface_cache
.update_interface_if_trusted(iface
, pending
.sigs
, pending
.new_xml
)
112 # Can't 'update' to an older copy
113 src
= tempfile
.TemporaryFile()
114 src
.write(data
.foo_signed_xml
)
117 pending
= PendingFeed(iface
.uri
, src
)
118 iface_cache
.add_pending(pending
)
119 assert iface_cache
.update_interface_if_trusted(iface
, pending
.sigs
, pending
.new_xml
)
122 except model
.SafeException
:
126 stream
= tempfile
.TemporaryFile()
127 stream
.write(data
.thomas_key
)
129 gpg
.import_key(stream
)
131 upstream_dir
= basedir
.save_cache_path(config_site
, 'interfaces')
132 cached
= os
.path
.join(upstream_dir
, model
.escape('http://foo'))
134 stream
= file(cached
, 'w')
135 stream
.write(data
.foo_signed_xml
)
138 signed
= iface_cache
._get
_signature
_date
('http://foo')
139 assert signed
== None
141 trust
.trust_db
.trust_key(
142 '92429807C9853C0744A68B9AAE07828059A53CC1')
144 signed
= iface_cache
._get
_signature
_date
('http://foo')
145 assert signed
== 1154850229
147 stream
= file(cached
, 'w+')
149 stream
.write('Hello')
152 # When the signature is invalid, we just return None.
153 # This is because versions < 0.22 used to corrupt the signatue
154 # by adding an attribute to the XML
155 signed
= iface_cache
._get
_signature
_date
('http://foo')
156 assert signed
== None
158 def testCheckAttempt(self
):
159 self
.assertEquals(None, iface_cache
.get_last_check_attempt("http://foo/bar.xml"))
161 start_time
= time
.time() - 5 # Seems to be some odd rounding here
162 iface_cache
.mark_as_checking("http://foo/bar.xml")
163 last_check
= iface_cache
.get_last_check_attempt("http://foo/bar.xml")
165 assert last_check
is not None
166 assert last_check
>= start_time
, (last_check
, start_time
)
168 self
.assertEquals(None, iface_cache
.get_last_check_attempt("http://foo/bar2.xml"))
171 suite
= unittest
.makeSuite(TestIfaceCache
)
172 if __name__
== '__main__':
173 sys
.argv
.append('-v')