1 #!/usr/bin/env python2.3
2 import sys
, tempfile
, os
, shutil
3 from StringIO
import StringIO
4 import unittest
, signal
5 from logging
import getLogger
, DEBUG
, INFO
6 #getLogger().setLevel(DEBUG)
8 sys
.path
.insert(0, '..')
10 from zeroinstall
.injector
import model
, basedir
, autopolicy
, gpg
, iface_cache
, download
16 def __init__(self
, reply
):
22 class TestDownload(unittest
.TestCase
):
24 self
.config_home
= tempfile
.mktemp()
25 self
.cache_home
= tempfile
.mktemp()
26 os
.environ
['XDG_CONFIG_HOME'] = self
.config_home
27 os
.environ
['XDG_CACHE_HOME'] = self
.cache_home
30 os
.mkdir(self
.config_home
, 0700)
31 os
.mkdir(self
.cache_home
, 0700)
32 if os
.environ
.has_key('DISPLAY'):
33 del os
.environ
['DISPLAY']
34 self
.gnupg_home
= tempfile
.mktemp()
35 os
.environ
['GNUPGHOME'] = self
.gnupg_home
36 os
.mkdir(self
.gnupg_home
, 0700)
37 stream
= tempfile
.TemporaryFile()
38 stream
.write(data
.thomas_key
)
40 gpg
.import_key(stream
)
41 iface_cache
.iface_cache
.__init
__()
42 download
._downloads
= {}
46 if self
.child
is not None:
47 os
.kill(self
.child
, signal
.SIGTERM
)
48 os
.waitpid(self
.child
, 0)
50 shutil
.rmtree(self
.config_home
)
51 shutil
.rmtree(self
.cache_home
)
52 shutil
.rmtree(self
.gnupg_home
)
54 def testRejectKey(self
):
57 sys
.stdout
= StringIO()
58 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg')
59 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False)
60 assert policy
.need_download()
61 sys
.stdin
= Reply("N\n")
63 policy
.download_and_execute(['Hello'])
65 except model
.SafeException
, ex
:
66 if "Not signed with a trusted key" not in str(ex
):
71 def testAcceptKey(self
):
74 sys
.stdout
= StringIO()
75 self
.child
= server
.handle_requests('Hello', '6FCF121BE2390E0B.gpg', 'HelloWorld.tgz')
76 policy
= autopolicy
.AutoPolicy('http://localhost:8000/Hello', download_only
= False)
77 assert policy
.need_download()
78 sys
.stdin
= Reply("Y\n")
80 policy
.download_and_execute(['Hello'], main
= 'Missing')
82 except model
.SafeException
, ex
:
83 if "HelloWorld/Missing" not in str(ex
):
91 sys
.stdout
= StringIO()
92 self
.child
= server
.handle_requests('HelloWorld.tar.bz2', 'dummy_1-1_all.deb')
93 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False)
95 policy
.download_and_execute([])
96 except model
.SafeException
, ex
:
97 if "HelloWorld/Missing" not in str(ex
):
102 def testAutopackage(self
):
105 sys
.stdout
= StringIO()
106 self
.child
= server
.handle_requests('HelloWorld.autopackage')
107 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Autopackage.xml'), download_only
= False)
109 policy
.download_and_execute([])
110 except model
.SafeException
, ex
:
111 if "HelloWorld/Missing" not in str(ex
):
116 def testRecipeFailure(self
):
119 sys
.stdout
= StringIO()
120 self
.child
= server
.handle_requests('HelloWorld.tar.bz2')
121 policy
= autopolicy
.AutoPolicy(os
.path
.abspath('Recipe.xml'), download_only
= False)
123 policy
.download_and_execute([])
124 except download
.DownloadError
, ex
:
125 if "Connection" not in str(ex
):
130 suite
= unittest
.makeSuite(TestDownload
)
131 if __name__
== '__main__':
132 sys
.argv
.append('-v')