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
8 sys
.path
.insert(0, '..')
10 from zeroinstall
.injector
import model
, autopolicy
, gpg
, iface_cache
, download
, reader
, trust
, handler
11 from zeroinstall
.zerostore
import Store
; Store
._add
_with
_helper
= lambda *unused
: False
12 from zeroinstall
.support
import basedir
18 def __init__(self
, reply
):
24 class DummyHandler(handler
.Handler
):
28 handler
.Handler
.__init
__(self
)
31 def wait_for_blocker(self
, blocker
):
33 handler
.Handler
.wait_for_blocker(self
, blocker
)
37 def report_error(self
, ex
):
38 assert self
.ex
is None, self
.ex
42 #traceback.print_exc()
44 class TestDownload(BaseTest
):
48 stream
= tempfile
.TemporaryFile()
49 stream
.write(data
.thomas_key
)
51 gpg
.import_key(stream
)
54 trust
.trust_db
.watchers
= []
57 BaseTest
.tearDown(self
)
58 if self
.child
is not None:
59 os
.kill(self
.child
, signal
.SIGTERM
)
60 os
.waitpid(self
.child
, 0)
63 def testRejectKey(self
):
66 sys
.stdout
= StringIO()
67 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg')
68 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False,
69 handler
= DummyHandler())
70 assert policy
.need_download()
71 sys
.stdin
= Reply("N\n")
73 policy
.download_and_execute(['Hello'])
75 except model
.SafeException
, ex
:
76 if "Not signed with a trusted key" not in str(ex
):
81 def testRejectKeyXML(self
):
84 sys
.stdout
= StringIO()
85 self
.child
= server
.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg')
86 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello.xml', download_only
= False,
87 handler
= DummyHandler())
88 assert policy
.need_download()
89 sys
.stdin
= Reply("N\n")
91 policy
.download_and_execute(['Hello'])
93 except model
.SafeException
, ex
:
94 if "Not signed with a trusted key" not in str(ex
):
102 from zeroinstall
.injector
import cli
105 rootLogger
= getLogger()
106 rootLogger
.disabled
= True
109 cli
.main(['--import', '-v', 'NO-SUCH-FILE'])
111 except model
.SafeException
, ex
:
112 assert 'NO-SUCH-FILE' in str(ex
)
114 rootLogger
.disabled
= False
115 rootLogger
.setLevel(WARN
)
117 hello
= iface_cache
.iface_cache
.get_interface('http://localhost:8000/Hello')
118 self
.assertEquals(0, len(hello
.implementations
))
120 sys
.stdout
= StringIO()
121 self
.child
= server
.handle_requests('6FCF121BE2390E0B.gpg')
122 sys
.stdin
= Reply("Y\n")
124 assert not trust
.trust_db
.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
125 cli
.main(['--import', 'Hello'])
126 assert trust
.trust_db
.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
128 # Check we imported the interface after trusting the key
129 reader
.update_from_cache(hello
)
130 self
.assertEquals(1, len(hello
.implementations
))
132 # Shouldn't need to prompt the second time
134 cli
.main(['--import', 'Hello'])
138 def testAcceptKey(self
):
141 sys
.stdout
= StringIO()
142 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz')
143 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False,
144 handler
= DummyHandler())
145 assert policy
.need_download()
146 sys
.stdin
= Reply("Y\n")
148 policy
.download_and_execute(['Hello'], main
= 'Missing')
150 except model
.SafeException
, ex
:
151 if "HelloWorld/Missing" not in str(ex
):
156 def testRecipe(self
):
159 sys
.stdout
= StringIO()
160 self
.child
= server
.handle_requests(('HelloWorld.tar.bz2', 'dummy_1-1_all.deb'))
161 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False)
163 policy
.download_and_execute([])
165 except model
.SafeException
, ex
:
166 if "HelloWorld/Missing" not in str(ex
):
171 def testSymlink(self
):
174 sys
.stdout
= StringIO()
175 self
.child
= server
.handle_requests(('HelloWorld.tar.bz2', 'HelloSym.tgz'))
176 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('RecipeSymlink.xml'), download_only
= False,
177 handler
= DummyHandler())
179 policy
.download_and_execute([])
181 except model
.SafeException
, ex
:
182 if 'Attempt to unpack dir over symlink "HelloWorld"' not in str(ex
):
184 self
.assertEquals(None, basedir
.load_first_cache('0install.net', 'implementations', 'main'))
188 def testAutopackage(self
):
191 sys
.stdout
= StringIO()
192 self
.child
= server
.handle_requests('HelloWorld.autopackage')
193 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Autopackage.xml'), download_only
= False)
195 policy
.download_and_execute([])
197 except model
.SafeException
, ex
:
198 if "HelloWorld/Missing" not in str(ex
):
203 def testRecipeFailure(self
):
206 sys
.stdout
= StringIO()
207 self
.child
= server
.handle_requests('*')
208 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False,
209 handler
= DummyHandler())
211 policy
.download_and_execute([])
213 except download
.DownloadError
, ex
:
214 if "Connection" not in str(ex
):
219 suite
= unittest
.makeSuite(TestDownload
)
220 if __name__
== '__main__':
221 sys
.argv
.append('-v')