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
21 fetch
.DEFAULT_KEY_LOOKUP_SERVER
= 'http://localhost:3333/key-info'
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
37 def output_suppressed():
38 old_stdout
= sys
.stdout
39 old_stderr
= sys
.stderr
41 sys
.stdout
= StringIO()
42 sys
.stderr
= StringIO()
45 sys
.stdout
= old_stdout
46 sys
.stderr
= old_stderr
49 def __init__(self
, reply
):
55 class DummyHandler(handler
.Handler
):
56 __slots__
= ['ex', 'tb']
59 handler
.Handler
.__init
__(self
)
62 def wait_for_blocker(self
, blocker
):
64 handler
.Handler
.wait_for_blocker(self
, blocker
)
66 raise self
.ex
, None, self
.tb
68 def report_error(self
, ex
, tb
= None):
69 assert self
.ex
is None, self
.ex
74 #traceback.print_exc()
76 class TestDownload(BaseTest
):
80 stream
= tempfile
.TemporaryFile()
81 stream
.write(data
.thomas_key
)
83 gpg
.import_key(stream
)
86 trust
.trust_db
.watchers
= []
89 BaseTest
.tearDown(self
)
90 if self
.child
is not None:
91 os
.kill(self
.child
, signal
.SIGTERM
)
92 os
.waitpid(self
.child
, 0)
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")
103 policy
.download_and_execute(['Hello'])
105 except model
.SafeException
, ex
:
106 if "Can't find all required implementations" not in str(ex
):
108 if "Not signed with a trusted key" not in str(policy
.handler
.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")
119 policy
.download_and_execute(['Hello'])
121 except model
.SafeException
, ex
:
122 if "Can't find all required implementations" not in str(ex
):
124 if "Not signed with a trusted key" not in str(policy
.handler
.ex
):
127 def testImport(self
):
128 from zeroinstall
.injector
import cli
130 rootLogger
= getLogger()
131 rootLogger
.disabled
= True
134 cli
.main(['--import', '-v', 'NO-SUCH-FILE'])
136 except model
.SafeException
, ex
:
137 assert 'NO-SUCH-FILE' in str(ex
)
139 rootLogger
.disabled
= False
140 rootLogger
.setLevel(WARN
)
142 hello
= iface_cache
.iface_cache
.get_interface('http://localhost:8000/Hello')
143 self
.assertEquals(0, len(hello
.implementations
))
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 reader
.update_from_cache(hello
)
155 self
.assertEquals(1, len(hello
.implementations
))
157 # Shouldn't need to prompt the second time
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
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")
205 policy
.download_and_execute(['Hello'], main
= 'Missing')
207 except model
.SafeException
, ex
:
208 if "HelloWorld/Missing" not in str(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")
220 policy
.download_and_execute(['Hello'], main
= 'Missing')
222 except model
.SafeException
, ex
:
223 if "Downloaded archive has incorrect size" not in str(ex
):
226 def testRecipe(self
):
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)
233 policy
.download_and_execute([])
235 except model
.SafeException
, ex
:
236 if "HelloWorld/Missing" not in str(ex
):
241 def testSymlink(self
):
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())
249 policy
.download_and_execute([])
251 except model
.SafeException
, ex
:
252 if 'Attempt to unpack dir over symlink "HelloWorld"' not in str(ex
):
254 self
.assertEquals(None, basedir
.load_first_cache('0install.net', 'implementations', 'main'))
258 def testAutopackage(self
):
261 sys
.stdout
= StringIO()
262 self
.child
= server
.handle_requests('HelloWorld.autopackage')
263 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Autopackage.xml'), download_only
= False)
265 policy
.download_and_execute([])
267 except model
.SafeException
, ex
:
268 if "HelloWorld/Missing" not in str(ex
):
273 def testRecipeFailure(self
):
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())
281 policy
.download_and_execute([])
283 except download
.DownloadError
, ex
:
284 if "Connection" not in str(ex
):
289 def testMirrors(self
):
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
)
305 def testReplay(self
):
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_interface_from_network(iface
, 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
)
326 policy
.handler
.wait_for_blocker(refreshed
)
327 raise Exception("Should have been rejected!")
328 except model
.SafeException
, ex
:
329 assert "New interface'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
))
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')
340 p
.network_use
= model
.network_minimal
341 p
.solver
.solve(p
.root
, arch
.get_host_architecture())
345 def choose_download(registed_cb
, nid
, actions
):
347 assert actions
== ['download', 'Download'], actions
348 registed_cb(nid
, 'download')
351 traceback
.print_exc()
358 sys
.stdout
= StringIO()
359 self
.child
= server
.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg')
360 my_dbus
.user_callback
= choose_download
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
373 background
.spawn_background_update(p
, verbose
)
375 except SystemExit, ex
:
376 self
.assertEquals(1, ex
.code
)
383 def testBackgroundVerbose(self
):
384 self
.testBackground(verbose
= True)
386 suite
= unittest
.makeSuite(TestDownload
)
387 if __name__
== '__main__':
388 sys
.argv
.append('-v')