Added 'type' and 'start-offset' attributes to <archive> elements.
[zeroinstall.git] / tests / testdownload.py
blobb6e7b157bfb693aaa0fb4f9dd4e32d463b03e661
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
11 import data
13 import server
15 class Reply:
16 def __init__(self, reply):
17 self.reply = reply
19 def readline(self):
20 return self.reply
22 class TestDownload(unittest.TestCase):
23 def setUp(self):
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
28 reload(basedir)
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)
39 stream.seek(0)
40 gpg.import_key(stream)
41 iface_cache.iface_cache.__init__()
42 download._downloads = {}
43 self.child = None
45 def tearDown(self):
46 if self.child is not None:
47 os.kill(self.child, signal.SIGTERM)
48 os.waitpid(self.child, 0)
49 self.child = None
50 shutil.rmtree(self.config_home)
51 shutil.rmtree(self.cache_home)
52 shutil.rmtree(self.gnupg_home)
54 def testRejectKey(self):
55 old_out = sys.stdout
56 try:
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")
62 try:
63 policy.download_and_execute(['Hello'])
64 assert 0
65 except model.SafeException, ex:
66 if "Not signed with a trusted key" not in str(ex):
67 raise ex
68 finally:
69 sys.stdout = old_out
71 def testAcceptKey(self):
72 old_out = sys.stdout
73 try:
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")
79 try:
80 policy.download_and_execute(['Hello'], main = 'Missing')
81 assert 0
82 except model.SafeException, ex:
83 if "HelloWorld/Missing" not in str(ex):
84 raise ex
85 finally:
86 sys.stdout = old_out
88 def testRecipe(self):
89 old_out = sys.stdout
90 try:
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)
94 try:
95 policy.download_and_execute([])
96 except model.SafeException, ex:
97 if "HelloWorld/Missing" not in str(ex):
98 raise ex
99 finally:
100 sys.stdout = old_out
102 def testAutopackage(self):
103 old_out = sys.stdout
104 try:
105 sys.stdout = StringIO()
106 self.child = server.handle_requests('HelloWorld.autopackage')
107 policy = autopolicy.AutoPolicy(os.path.abspath('Autopackage.xml'), download_only = False)
108 try:
109 policy.download_and_execute([])
110 except model.SafeException, ex:
111 if "HelloWorld/Missing" not in str(ex):
112 raise ex
113 finally:
114 sys.stdout = old_out
116 def testRecipeFailure(self):
117 old_out = sys.stdout
118 try:
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)
122 try:
123 policy.download_and_execute([])
124 except download.DownloadError, ex:
125 if "Connection" not in str(ex):
126 raise ex
127 finally:
128 sys.stdout = old_out
130 suite = unittest.makeSuite(TestDownload)
131 if __name__ == '__main__':
132 sys.argv.append('-v')
133 unittest.main()