API changes: use Feed rather than Interface in many places
[zeroinstall/zeroinstall-afb.git] / tests / testdownload.py
blobae60b3937b41c3a09225ee175d2cbefc8096bf0f
1 #!/usr/bin/env python
2 from __future__ import with_statement
3 from basetest import BaseTest
4 import sys, tempfile, os
5 from StringIO import StringIO
6 import unittest, signal
7 from logging import getLogger, WARN, ERROR
8 from contextlib import contextmanager
10 sys.path.insert(0, '..')
12 os.environ["http_proxy"] = "localhost:8000"
14 from zeroinstall.injector import model, autopolicy, gpg, iface_cache, download, reader, trust, handler, background, arch, selections, qdom
15 from zeroinstall.zerostore import Store; Store._add_with_helper = lambda *unused: False
16 from zeroinstall.support import basedir, tasks
17 from zeroinstall.injector import fetch
18 import data
19 import my_dbus
21 fetch.DEFAULT_KEY_LOOKUP_SERVER = 'http://localhost:3333/key-info'
23 import server
25 ran_gui = False
26 def raise_gui(*args):
27 global ran_gui
28 ran_gui = True
29 background._detach = lambda: False
30 background._exec_gui = raise_gui
31 sys.modules['dbus'] = my_dbus
32 sys.modules['dbus.glib'] = my_dbus
33 my_dbus.types = my_dbus
34 sys.modules['dbus.types'] = my_dbus
36 @contextmanager
37 def output_suppressed():
38 old_stdout = sys.stdout
39 old_stderr = sys.stderr
40 try:
41 sys.stdout = StringIO()
42 sys.stderr = StringIO()
43 yield
44 finally:
45 sys.stdout = old_stdout
46 sys.stderr = old_stderr
48 class Reply:
49 def __init__(self, reply):
50 self.reply = reply
52 def readline(self):
53 return self.reply
55 class DummyHandler(handler.Handler):
56 __slots__ = ['ex', 'tb']
58 def __init__(self):
59 handler.Handler.__init__(self)
60 self.ex = None
62 def wait_for_blocker(self, blocker):
63 self.ex = None
64 handler.Handler.wait_for_blocker(self, blocker)
65 if self.ex:
66 raise self.ex, None, self.tb
68 def report_error(self, ex, tb = None):
69 assert self.ex is None, self.ex
70 self.ex = ex
71 self.tb = tb
73 #import traceback
74 #traceback.print_exc()
76 class TestDownload(BaseTest):
77 def setUp(self):
78 BaseTest.setUp(self)
80 stream = tempfile.TemporaryFile()
81 stream.write(data.thomas_key)
82 stream.seek(0)
83 gpg.import_key(stream)
84 self.child = None
86 trust.trust_db.watchers = []
88 def tearDown(self):
89 BaseTest.tearDown(self)
90 if self.child is not None:
91 os.kill(self.child, signal.SIGTERM)
92 os.waitpid(self.child, 0)
93 self.child = None
95 def testRejectKey(self):
96 with output_suppressed():
97 self.child = server.handle_requests('Hello', '6FCF121BE2390E0B.gpg', '/key-info/key/DE937DD411906ACF7C263B396FCF121BE2390E0B')
98 policy = autopolicy.AutoPolicy('http://localhost:8000/Hello', download_only = False,
99 handler = DummyHandler())
100 assert policy.need_download()
101 sys.stdin = Reply("N\n")
102 try:
103 policy.download_and_execute(['Hello'])
104 assert 0
105 except model.SafeException, ex:
106 if "Can't find all required implementations" not in str(ex):
107 raise ex
108 if "Not signed with a trusted key" not in str(policy.handler.ex):
109 raise ex
111 def testRejectKeyXML(self):
112 with output_suppressed():
113 self.child = server.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg', '/key-info/key/DE937DD411906ACF7C263B396FCF121BE2390E0B')
114 policy = autopolicy.AutoPolicy('http://example.com:8000/Hello.xml', download_only = False,
115 handler = DummyHandler())
116 assert policy.need_download()
117 sys.stdin = Reply("N\n")
118 try:
119 policy.download_and_execute(['Hello'])
120 assert 0
121 except model.SafeException, ex:
122 if "Can't find all required implementations" not in str(ex):
123 raise ex
124 if "Not signed with a trusted key" not in str(policy.handler.ex):
125 raise
127 def testImport(self):
128 from zeroinstall.injector import cli
130 rootLogger = getLogger()
131 rootLogger.disabled = True
132 try:
133 try:
134 cli.main(['--import', '-v', 'NO-SUCH-FILE'])
135 assert 0
136 except model.SafeException, ex:
137 assert 'NO-SUCH-FILE' in str(ex)
138 finally:
139 rootLogger.disabled = False
140 rootLogger.setLevel(WARN)
142 hello = iface_cache.iface_cache.get_feed('http://localhost:8000/Hello')
143 self.assertEquals(None, hello)
145 with output_suppressed():
146 self.child = server.handle_requests('6FCF121BE2390E0B.gpg')
147 sys.stdin = Reply("Y\n")
149 assert not trust.trust_db.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
150 cli.main(['--import', 'Hello'])
151 assert trust.trust_db.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
153 # Check we imported the interface after trusting the key
154 hello = iface_cache.iface_cache.get_feed('http://localhost:8000/Hello', force = True)
155 self.assertEquals(1, len(hello.implementations))
157 # Shouldn't need to prompt the second time
158 sys.stdin = None
159 cli.main(['--import', 'Hello'])
161 def testSelections(self):
162 from zeroinstall.injector.cli import _download_missing_selections
163 root = qdom.parse(file("selections.xml"))
164 sels = selections.Selections(root)
165 class Options: dry_run = False
167 with output_suppressed():
168 self.child = server.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg', '/key-info/key/DE937DD411906ACF7C263B396FCF121BE2390E0B', 'HelloWorld.tgz')
169 sys.stdin = Reply("Y\n")
170 _download_missing_selections(Options(), sels)
171 path = iface_cache.iface_cache.stores.lookup_any(sels.selections['http://example.com:8000/Hello.xml'].digests)
172 assert os.path.exists(os.path.join(path, 'HelloWorld', 'main'))
174 assert sels.download_missing(iface_cache.iface_cache, None) is None
176 def testSelectionsWithFeed(self):
177 from zeroinstall.injector.cli import _download_missing_selections
178 root = qdom.parse(file("selections.xml"))
179 sels = selections.Selections(root)
180 class Options: dry_run = False
182 with output_suppressed():
183 self.child = server.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg', '/key-info/key/DE937DD411906ACF7C263B396FCF121BE2390E0B', 'HelloWorld.tgz')
184 sys.stdin = Reply("Y\n")
186 from zeroinstall.injector.handler import Handler
187 handler = Handler()
188 fetcher = fetch.Fetcher(handler)
189 handler.wait_for_blocker(fetcher.download_and_import_feed('http://example.com:8000/Hello.xml', iface_cache.iface_cache))
191 _download_missing_selections(Options(), sels)
192 path = iface_cache.iface_cache.stores.lookup_any(sels.selections['http://example.com:8000/Hello.xml'].digests)
193 assert os.path.exists(os.path.join(path, 'HelloWorld', 'main'))
195 assert sels.download_missing(iface_cache.iface_cache, None) is None
197 def testAcceptKey(self):
198 with output_suppressed():
199 self.child = server.handle_requests('Hello', '6FCF121BE2390E0B.gpg', '/key-info/key/DE937DD411906ACF7C263B396FCF121BE2390E0B', 'HelloWorld.tgz')
200 policy = autopolicy.AutoPolicy('http://localhost:8000/Hello', download_only = False,
201 handler = DummyHandler())
202 assert policy.need_download()
203 sys.stdin = Reply("Y\n")
204 try:
205 policy.download_and_execute(['Hello'], main = 'Missing')
206 assert 0
207 except model.SafeException, ex:
208 if "HelloWorld/Missing" not in str(ex):
209 raise ex
211 def testWrongSize(self):
212 with output_suppressed():
213 self.child = server.handle_requests('Hello-wrong-size', '6FCF121BE2390E0B.gpg',
214 '/key-info/key/DE937DD411906ACF7C263B396FCF121BE2390E0B', 'HelloWorld.tgz')
215 policy = autopolicy.AutoPolicy('http://localhost:8000/Hello-wrong-size', download_only = False,
216 handler = DummyHandler())
217 assert policy.need_download()
218 sys.stdin = Reply("Y\n")
219 try:
220 policy.download_and_execute(['Hello'], main = 'Missing')
221 assert 0
222 except model.SafeException, ex:
223 if "Downloaded archive has incorrect size" not in str(ex):
224 raise ex
226 def testRecipe(self):
227 old_out = sys.stdout
228 try:
229 sys.stdout = StringIO()
230 self.child = server.handle_requests(('HelloWorld.tar.bz2', 'dummy_1-1_all.deb'))
231 policy = autopolicy.AutoPolicy(os.path.abspath('Recipe.xml'), download_only = False)
232 try:
233 policy.download_and_execute([])
234 assert False
235 except model.SafeException, ex:
236 if "HelloWorld/Missing" not in str(ex):
237 raise ex
238 finally:
239 sys.stdout = old_out
241 def testSymlink(self):
242 old_out = sys.stdout
243 try:
244 sys.stdout = StringIO()
245 self.child = server.handle_requests(('HelloWorld.tar.bz2', 'HelloSym.tgz'))
246 policy = autopolicy.AutoPolicy(os.path.abspath('RecipeSymlink.xml'), download_only = False,
247 handler = DummyHandler())
248 try:
249 policy.download_and_execute([])
250 assert False
251 except model.SafeException, ex:
252 if 'Attempt to unpack dir over symlink "HelloWorld"' not in str(ex):
253 raise ex
254 self.assertEquals(None, basedir.load_first_cache('0install.net', 'implementations', 'main'))
255 finally:
256 sys.stdout = old_out
258 def testAutopackage(self):
259 old_out = sys.stdout
260 try:
261 sys.stdout = StringIO()
262 self.child = server.handle_requests('HelloWorld.autopackage')
263 policy = autopolicy.AutoPolicy(os.path.abspath('Autopackage.xml'), download_only = False)
264 try:
265 policy.download_and_execute([])
266 assert False
267 except model.SafeException, ex:
268 if "HelloWorld/Missing" not in str(ex):
269 raise
270 finally:
271 sys.stdout = old_out
273 def testRecipeFailure(self):
274 old_out = sys.stdout
275 try:
276 sys.stdout = StringIO()
277 self.child = server.handle_requests('*')
278 policy = autopolicy.AutoPolicy(os.path.abspath('Recipe.xml'), download_only = False,
279 handler = DummyHandler())
280 try:
281 policy.download_and_execute([])
282 assert False
283 except download.DownloadError, ex:
284 if "Connection" not in str(ex):
285 raise
286 finally:
287 sys.stdout = old_out
289 def testMirrors(self):
290 old_out = sys.stdout
291 try:
292 sys.stdout = StringIO()
293 getLogger().setLevel(ERROR)
294 trust.trust_db.trust_key('DE937DD411906ACF7C263B396FCF121BE2390E0B', 'example.com:8000')
295 self.child = server.handle_requests(server.Give404('/Hello.xml'), 'latest.xml', '/0mirror/keys/6FCF121BE2390E0B.gpg')
296 policy = autopolicy.AutoPolicy('http://example.com:8000/Hello.xml', download_only = False)
297 policy.fetcher.feed_mirror = 'http://example.com:8000/0mirror'
299 refreshed = policy.solve_with_downloads()
300 policy.handler.wait_for_blocker(refreshed)
301 assert policy.ready
302 finally:
303 sys.stdout = old_out
305 def testReplay(self):
306 old_out = sys.stdout
307 try:
308 sys.stdout = StringIO()
309 getLogger().setLevel(ERROR)
310 iface = iface_cache.iface_cache.get_interface('http://example.com:8000/Hello.xml')
311 mtime = int(os.stat('Hello-new.xml').st_mtime)
312 iface_cache.iface_cache.update_feed_from_network(iface.uri, file('Hello-new.xml').read(), mtime + 10000)
314 trust.trust_db.trust_key('DE937DD411906ACF7C263B396FCF121BE2390E0B', 'example.com:8000')
315 self.child = server.handle_requests(server.Give404('/Hello.xml'), 'latest.xml', '/0mirror/keys/6FCF121BE2390E0B.gpg', 'Hello.xml')
316 policy = autopolicy.AutoPolicy('http://example.com:8000/Hello.xml', download_only = False)
317 policy.fetcher.feed_mirror = 'http://example.com:8000/0mirror'
319 # Update from mirror (should ignore out-of-date timestamp)
320 refreshed = policy.fetcher.download_and_import_feed(iface.uri, iface_cache.iface_cache)
321 policy.handler.wait_for_blocker(refreshed)
323 # Update from upstream (should report an error)
324 refreshed = policy.fetcher.download_and_import_feed(iface.uri, iface_cache.iface_cache)
325 try:
326 policy.handler.wait_for_blocker(refreshed)
327 raise Exception("Should have been rejected!")
328 except model.SafeException, ex:
329 assert "New feed's modification time is before old version" in str(ex)
331 # Must finish with the newest version
332 self.assertEquals(1235911552, iface_cache.iface_cache._get_signature_date(iface.uri))
333 finally:
334 sys.stdout = old_out
336 def testBackground(self, verbose = False):
337 p = autopolicy.AutoPolicy('http://example.com:8000/Hello.xml')
338 reader.update(iface_cache.iface_cache.get_interface(p.root), 'Hello.xml')
339 p.freshness = 0
340 p.network_use = model.network_minimal
341 p.solver.solve(p.root, arch.get_host_architecture())
342 assert p.ready
344 @tasks.async
345 def choose_download(registed_cb, nid, actions):
346 try:
347 assert actions == ['download', 'Download'], actions
348 registed_cb(nid, 'download')
349 except:
350 import traceback
351 traceback.print_exc()
352 yield None
354 global ran_gui
355 ran_gui = False
356 old_out = sys.stdout
357 try:
358 sys.stdout = StringIO()
359 self.child = server.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg')
360 my_dbus.user_callback = choose_download
361 pid = os.getpid()
362 old_exit = os._exit
363 def my_exit(code):
364 # The background handler runs in the same process
365 # as the tests, so don't let it abort.
366 if os.getpid() == pid:
367 raise SystemExit(code)
368 # But, child download processes are OK
369 old_exit(code)
370 key_info = fetch.DEFAULT_KEY_LOOKUP_SERVER
371 fetch.DEFAULT_KEY_LOOKUP_SERVER = None
372 try:
373 try:
374 os._exit = my_exit
375 background.spawn_background_update(p, verbose)
376 assert False
377 except SystemExit, ex:
378 self.assertEquals(1, ex.code)
379 finally:
380 os._exit = old_exit
381 fetch.DEFAULT_KEY_LOOKUP_SERVER = key_info
382 finally:
383 sys.stdout = old_out
384 assert ran_gui
386 def testBackgroundVerbose(self):
387 self.testBackground(verbose = True)
389 if __name__ == '__main__':
390 unittest.main()