1 #!/usr/bin/env python2.4
2 from basetest
import BaseTest
3 import sys
, tempfile
, os
, shutil
4 from StringIO
import StringIO
5 import unittest
, signal
6 from logging
import getLogger
, DEBUG
, INFO
, WARN
, ERROR
8 sys
.path
.insert(0, '..')
10 from zeroinstall
.injector
import model
, autopolicy
, gpg
, iface_cache
, download
, reader
, trust
, handler
, background
, arch
, selections
, qdom
11 from zeroinstall
.zerostore
import Store
; Store
._add
_with
_helper
= lambda *unused
: False
12 from zeroinstall
.support
import basedir
, tasks
22 background
._detach
= lambda: False
23 background
._exec
_gui
= raise_gui
24 sys
.modules
['dbus'] = my_dbus
25 sys
.modules
['dbus.glib'] = my_dbus
26 my_dbus
.types
= my_dbus
27 sys
.modules
['dbus.types'] = my_dbus
30 def __init__(self
, reply
):
36 class DummyHandler(handler
.Handler
):
37 __slots__
= ['ex', 'tb']
40 handler
.Handler
.__init
__(self
)
43 def wait_for_blocker(self
, blocker
):
45 handler
.Handler
.wait_for_blocker(self
, blocker
)
47 raise self
.ex
, None, self
.tb
49 def report_error(self
, ex
, tb
= None):
50 assert self
.ex
is None, self
.ex
55 #traceback.print_exc()
57 class TestDownload(BaseTest
):
61 stream
= tempfile
.TemporaryFile()
62 stream
.write(data
.thomas_key
)
64 gpg
.import_key(stream
)
67 trust
.trust_db
.watchers
= []
70 BaseTest
.tearDown(self
)
71 if self
.child
is not None:
72 os
.kill(self
.child
, signal
.SIGTERM
)
73 os
.waitpid(self
.child
, 0)
76 def testRejectKey(self
):
79 sys
.stdout
= StringIO()
80 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg')
81 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False,
82 handler
= DummyHandler())
83 assert policy
.need_download()
84 sys
.stdin
= Reply("N\n")
86 policy
.download_and_execute(['Hello'])
88 except model
.SafeException
, ex
:
89 if "Not signed with a trusted key" not in str(ex
):
94 def testRejectKeyXML(self
):
97 sys
.stdout
= StringIO()
98 self
.child
= server
.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg')
99 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello.xml', download_only
= False,
100 handler
= DummyHandler())
101 assert policy
.need_download()
102 sys
.stdin
= Reply("N\n")
104 policy
.download_and_execute(['Hello'])
106 except model
.SafeException
, ex
:
107 if "Not signed with a trusted key" not in str(ex
):
112 def testImport(self
):
115 from zeroinstall
.injector
import cli
118 rootLogger
= getLogger()
119 rootLogger
.disabled
= True
122 cli
.main(['--import', '-v', 'NO-SUCH-FILE'])
124 except model
.SafeException
, ex
:
125 assert 'NO-SUCH-FILE' in str(ex
)
127 rootLogger
.disabled
= False
128 rootLogger
.setLevel(WARN
)
130 hello
= iface_cache
.iface_cache
.get_interface('http://localhost:8000/Hello')
131 self
.assertEquals(0, len(hello
.implementations
))
133 sys
.stdout
= StringIO()
134 self
.child
= server
.handle_requests('6FCF121BE2390E0B.gpg')
135 sys
.stdin
= Reply("Y\n")
137 assert not trust
.trust_db
.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
138 cli
.main(['--import', 'Hello'])
139 assert trust
.trust_db
.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
141 # Check we imported the interface after trusting the key
142 reader
.update_from_cache(hello
)
143 self
.assertEquals(1, len(hello
.implementations
))
145 # Shouldn't need to prompt the second time
147 cli
.main(['--import', 'Hello'])
151 def testSelections(self
):
152 from zeroinstall
.injector
.cli
import _download_missing_selections
153 root
= qdom
.parse(file("selections.xml"))
154 sels
= selections
.Selections(root
)
155 class Options
: dry_run
= False
159 sys
.stdout
= StringIO()
160 self
.child
= server
.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz')
161 sys
.stdin
= Reply("Y\n")
162 _download_missing_selections(Options(), sels
)
163 path
= iface_cache
.iface_cache
.stores
.lookup(sels
.selections
['http://localhost:8000/Hello.xml'].id)
164 assert os
.path
.exists(os
.path
.join(path
, 'HelloWorld', 'main'))
166 assert sels
.download_missing(iface_cache
.iface_cache
, None) is None
170 def testAcceptKey(self
):
173 sys
.stdout
= StringIO()
174 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz')
175 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False,
176 handler
= DummyHandler())
177 assert policy
.need_download()
178 sys
.stdin
= Reply("Y\n")
180 policy
.download_and_execute(['Hello'], main
= 'Missing')
182 except model
.SafeException
, ex
:
183 if "HelloWorld/Missing" not in str(ex
):
188 def testRecipe(self
):
191 sys
.stdout
= StringIO()
192 self
.child
= server
.handle_requests(('HelloWorld.tar.bz2', 'dummy_1-1_all.deb'))
193 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False)
195 policy
.download_and_execute([])
197 except model
.SafeException
, ex
:
198 if "HelloWorld/Missing" not in str(ex
):
203 def testSymlink(self
):
206 sys
.stdout
= StringIO()
207 self
.child
= server
.handle_requests(('HelloWorld.tar.bz2', 'HelloSym.tgz'))
208 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('RecipeSymlink.xml'), download_only
= False,
209 handler
= DummyHandler())
211 policy
.download_and_execute([])
213 except model
.SafeException
, ex
:
214 if 'Attempt to unpack dir over symlink "HelloWorld"' not in str(ex
):
216 self
.assertEquals(None, basedir
.load_first_cache('0install.net', 'implementations', 'main'))
220 def testAutopackage(self
):
223 sys
.stdout
= StringIO()
224 self
.child
= server
.handle_requests('HelloWorld.autopackage')
225 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Autopackage.xml'), download_only
= False)
227 policy
.download_and_execute([])
229 except model
.SafeException
, ex
:
230 if "HelloWorld/Missing" not in str(ex
):
235 def testRecipeFailure(self
):
238 sys
.stdout
= StringIO()
239 self
.child
= server
.handle_requests('*')
240 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False,
241 handler
= DummyHandler())
243 policy
.download_and_execute([])
245 except download
.DownloadError
, ex
:
246 if "Connection" not in str(ex
):
251 def testMirrors(self
):
254 sys
.stdout
= StringIO()
255 getLogger().setLevel(ERROR
)
256 trust
.trust_db
.trust_key('DE937DD411906ACF7C263B396FCF121BE2390E0B', 'localhost:8000')
257 self
.child
= server
.handle_requests(server
.Give404('/Hello.xml'), 'latest.xml', '/0mirror/keys/6FCF121BE2390E0B.gpg')
258 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello.xml', download_only
= False)
259 policy
.fetcher
.feed_mirror
= 'http://localhost:8000/0mirror'
261 refreshed
= policy
.solve_with_downloads()
262 policy
.handler
.wait_for_blocker(refreshed
)
267 def testReplay(self
):
270 sys
.stdout
= StringIO()
271 getLogger().setLevel(ERROR
)
272 iface
= iface_cache
.iface_cache
.get_interface('http://localhost:8000/Hello.xml')
273 mtime
= int(os
.stat('Hello-new.xml').st_mtime
)
274 iface_cache
.iface_cache
.update_interface_from_network(iface
, file('Hello-new.xml').read(), mtime
+ 10000)
276 trust
.trust_db
.trust_key('DE937DD411906ACF7C263B396FCF121BE2390E0B', 'localhost:8000')
277 self
.child
= server
.handle_requests(server
.Give404('/Hello.xml'), 'latest.xml', '/0mirror/keys/6FCF121BE2390E0B.gpg', 'Hello.xml')
278 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello.xml', download_only
= False)
279 policy
.fetcher
.feed_mirror
= 'http://localhost:8000/0mirror'
281 # Update from mirror (should ignore out-of-date timestamp)
282 refreshed
= policy
.fetcher
.download_and_import_feed(iface
.uri
, iface_cache
.iface_cache
)
283 policy
.handler
.wait_for_blocker(refreshed
)
285 # Update from upstream (should report an error)
286 refreshed
= policy
.fetcher
.download_and_import_feed(iface
.uri
, iface_cache
.iface_cache
)
288 policy
.handler
.wait_for_blocker(refreshed
)
289 raise Exception("Should have been rejected!")
290 except model
.SafeException
, ex
:
291 assert "New interface's modification time is before old version" in str(ex
)
293 # Must finish with the newest version
294 self
.assertEquals(1209206132, iface_cache
.iface_cache
._get
_signature
_date
(iface
.uri
))
298 def testBackground(self
, verbose
= False):
299 p
= autopolicy
.AutoPolicy('http://localhost:8000/Hello.xml')
300 reader
.update(iface_cache
.iface_cache
.get_interface(p
.root
), 'Hello.xml')
302 p
.network_use
= model
.network_minimal
303 p
.solver
.solve(p
.root
, arch
.get_host_architecture())
307 def choose_download(registed_cb
, nid
, actions
):
309 assert actions
== ['download', 'Download'], actions
310 registed_cb(nid
, 'download')
313 traceback
.print_exc()
320 sys
.stdout
= StringIO()
321 self
.child
= server
.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg')
322 my_dbus
.user_callback
= choose_download
326 # The background handler runs in the same process
327 # as the tests, so don't let it abort.
328 if os
.getpid() == pid
:
329 raise SystemExit(code
)
330 # But, child download processes are OK
335 background
.spawn_background_update(p
, verbose
)
337 except SystemExit, ex
:
338 self
.assertEquals(1, ex
.code
)
345 def testBackgroundVerbose(self
):
346 self
.testBackground(verbose
= True)
348 suite
= unittest
.makeSuite(TestDownload
)
349 if __name__
== '__main__':
350 sys
.argv
.append('-v')