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
, basedir
, autopolicy
, gpg
, iface_cache
, download
, reader
, trust
, handler
11 from zeroinstall
.zerostore
import Store
; Store
._add
_with
_helper
= lambda *unused
: False
17 def __init__(self
, reply
):
23 class DummyHandler(handler
.Handler
):
27 handler
.Handler
.__init
__(self
)
30 def wait_for_blocker(self
, blocker
):
32 handler
.Handler
.wait_for_blocker(self
, blocker
)
36 def report_error(self
, ex
):
37 assert self
.ex
is None, self
.ex
41 #traceback.print_exc()
43 class TestDownload(BaseTest
):
47 stream
= tempfile
.TemporaryFile()
48 stream
.write(data
.thomas_key
)
50 gpg
.import_key(stream
)
53 trust
.trust_db
.watchers
= []
56 BaseTest
.tearDown(self
)
57 if self
.child
is not None:
58 os
.kill(self
.child
, signal
.SIGTERM
)
59 os
.waitpid(self
.child
, 0)
62 def testRejectKey(self
):
65 sys
.stdout
= StringIO()
66 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg')
67 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False,
68 handler
= DummyHandler())
69 assert policy
.need_download()
70 sys
.stdin
= Reply("N\n")
72 policy
.download_and_execute(['Hello'])
74 except model
.SafeException
, ex
:
75 if "Not signed with a trusted key" not in str(ex
):
80 def testRejectKeyXML(self
):
83 sys
.stdout
= StringIO()
84 self
.child
= server
.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg')
85 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello.xml', download_only
= False,
86 handler
= DummyHandler())
87 assert policy
.need_download()
88 sys
.stdin
= Reply("N\n")
90 policy
.download_and_execute(['Hello'])
92 except model
.SafeException
, ex
:
93 if "Not signed with a trusted key" not in str(ex
):
101 from zeroinstall
.injector
import cli
104 rootLogger
= getLogger()
105 rootLogger
.disabled
= True
108 cli
.main(['--import', '-v', 'NO-SUCH-FILE'])
110 except model
.SafeException
, ex
:
111 assert 'NO-SUCH-FILE' in str(ex
)
113 rootLogger
.disabled
= False
114 rootLogger
.setLevel(WARN
)
116 hello
= iface_cache
.iface_cache
.get_interface('http://localhost:8000/Hello')
117 self
.assertEquals(0, len(hello
.implementations
))
119 sys
.stdout
= StringIO()
120 self
.child
= server
.handle_requests('6FCF121BE2390E0B.gpg')
121 sys
.stdin
= Reply("Y\n")
123 assert not trust
.trust_db
.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
124 cli
.main(['--import', 'Hello'])
125 assert trust
.trust_db
.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
127 # Check we imported the interface after trusting the key
128 reader
.update_from_cache(hello
)
129 self
.assertEquals(1, len(hello
.implementations
))
131 # Shouldn't need to prompt the second time
133 cli
.main(['--import', 'Hello'])
137 def testAcceptKey(self
):
140 sys
.stdout
= StringIO()
141 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz')
142 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False,
143 handler
= DummyHandler())
144 assert policy
.need_download()
145 sys
.stdin
= Reply("Y\n")
147 policy
.download_and_execute(['Hello'], main
= 'Missing')
149 except model
.SafeException
, ex
:
150 if "HelloWorld/Missing" not in str(ex
):
155 def testRecipe(self
):
158 sys
.stdout
= StringIO()
159 self
.child
= server
.handle_requests(('HelloWorld.tar.bz2', 'dummy_1-1_all.deb'))
160 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False)
162 policy
.download_and_execute([])
164 except model
.SafeException
, ex
:
165 if "HelloWorld/Missing" not in str(ex
):
170 def testSymlink(self
):
173 sys
.stdout
= StringIO()
174 self
.child
= server
.handle_requests(('HelloWorld.tar.bz2', 'HelloSym.tgz'))
175 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('RecipeSymlink.xml'), download_only
= False,
176 handler
= DummyHandler())
178 policy
.download_and_execute([])
180 except model
.SafeException
, ex
:
181 if 'Attempt to unpack dir over symlink "HelloWorld"' not in str(ex
):
183 self
.assertEquals(None, basedir
.load_first_cache('0install.net', 'implementations', 'main'))
187 def testAutopackage(self
):
190 sys
.stdout
= StringIO()
191 self
.child
= server
.handle_requests('HelloWorld.autopackage')
192 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Autopackage.xml'), download_only
= False)
194 policy
.download_and_execute([])
196 except model
.SafeException
, ex
:
197 if "HelloWorld/Missing" not in str(ex
):
202 def testRecipeFailure(self
):
205 sys
.stdout
= StringIO()
206 self
.child
= server
.handle_requests('*')
207 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False,
208 handler
= DummyHandler())
210 policy
.download_and_execute([])
212 except download
.DownloadError
, ex
:
213 if "Connection" not in str(ex
):
218 suite
= unittest
.makeSuite(TestDownload
)
219 if __name__
== '__main__':
220 sys
.argv
.append('-v')