1 #!/usr/bin/env python2.3
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
7 #getLogger().setLevel(DEBUG)
9 sys
.path
.insert(0, '..')
11 from zeroinstall
.injector
import model
, basedir
, autopolicy
, gpg
, iface_cache
, download
, reader
, trust
12 from zeroinstall
.zerostore
import Store
; Store
._add
_with
_helper
= lambda *unused
: False
18 def __init__(self
, reply
):
24 class TestDownload(BaseTest
):
28 stream
= tempfile
.TemporaryFile()
29 stream
.write(data
.thomas_key
)
31 gpg
.import_key(stream
)
34 trust
.trust_db
.watchers
= []
37 BaseTest
.tearDown(self
)
38 if self
.child
is not None:
39 os
.kill(self
.child
, signal
.SIGTERM
)
40 os
.waitpid(self
.child
, 0)
43 def testRejectKey(self
):
46 sys
.stdout
= StringIO()
47 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg')
48 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False)
49 assert policy
.need_download()
50 sys
.stdin
= Reply("N\n")
52 policy
.download_and_execute(['Hello'])
54 except model
.SafeException
, ex
:
55 if "Not signed with a trusted key" not in str(ex
):
60 def testRejectKeyXML(self
):
63 sys
.stdout
= StringIO()
64 self
.child
= server
.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg')
65 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello.xml', download_only
= False)
66 assert policy
.need_download()
67 sys
.stdin
= Reply("N\n")
69 policy
.download_and_execute(['Hello'])
71 except model
.SafeException
, ex
:
72 if "Not signed with a trusted key" not in str(ex
):
80 from zeroinstall
.injector
import cli
83 rootLogger
= getLogger()
84 rootLogger
.disabled
= True
87 cli
.main(['--import', '-v', 'NO-SUCH-FILE'])
89 except model
.SafeException
, ex
:
90 assert 'NO-SUCH-FILE' in str(ex
)
92 rootLogger
.disabled
= False
93 rootLogger
.setLevel(WARN
)
95 hello
= iface_cache
.iface_cache
.get_interface('http://localhost:8000/Hello')
96 self
.assertEquals(0, len(hello
.implementations
))
98 sys
.stdout
= StringIO()
99 self
.child
= server
.handle_requests('6FCF121BE2390E0B.gpg', 'HelloWorld.tgz')
100 sys
.stdin
= Reply("Y\n")
102 assert not trust
.trust_db
.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
103 cli
.main(['--import', 'Hello'])
104 assert trust
.trust_db
.is_trusted('DE937DD411906ACF7C263B396FCF121BE2390E0B')
106 # Check we imported the interface after trusting the key
107 reader
.update_from_cache(hello
)
108 self
.assertEquals(1, len(hello
.implementations
))
110 # Shouldn't need to prompt the second time
112 cli
.main(['--import', 'Hello'])
116 def testAcceptKey(self
):
119 sys
.stdout
= StringIO()
120 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz')
121 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False)
122 assert policy
.need_download()
123 sys
.stdin
= Reply("Y\n")
125 policy
.download_and_execute(['Hello'], main
= 'Missing')
127 except model
.SafeException
, ex
:
128 if "HelloWorld/Missing" not in str(ex
):
133 def testRecipe(self
):
136 sys
.stdout
= StringIO()
137 self
.child
= server
.handle_requests(('HelloWorld.tar.bz2', 'dummy_1-1_all.deb'))
138 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False)
140 policy
.download_and_execute([])
142 except model
.SafeException
, ex
:
143 if "HelloWorld/Missing" not in str(ex
):
148 def testSymlink(self
):
151 sys
.stdout
= StringIO()
152 self
.child
= server
.handle_requests(('HelloWorld.tar.bz2', 'HelloSym.tgz'))
153 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('RecipeSymlink.xml'), download_only
= False)
155 policy
.download_and_execute([])
157 except model
.SafeException
, ex
:
158 if 'Attempt to unpack dir over symlink "HelloWorld"' not in str(ex
):
160 self
.assertEquals(None, basedir
.load_first_cache('0install.net', 'implementations', 'main'))
164 def testAutopackage(self
):
167 sys
.stdout
= StringIO()
168 self
.child
= server
.handle_requests('HelloWorld.autopackage')
169 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Autopackage.xml'), download_only
= False)
171 policy
.download_and_execute([])
173 except model
.SafeException
, ex
:
174 if "HelloWorld/Missing" not in str(ex
):
179 def testRecipeFailure(self
):
182 sys
.stdout
= StringIO()
183 self
.child
= server
.handle_requests('*')
184 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False)
186 policy
.download_and_execute([])
188 except download
.DownloadError
, ex
:
189 if "Connection" not in str(ex
):
194 suite
= unittest
.makeSuite(TestDownload
)
195 if __name__
== '__main__':
196 sys
.argv
.append('-v')