3 from __future__
import print_function
5 from basetest
import BaseTest
6 import sys
, tempfile
, os
7 import unittest
, logging
9 sys
.path
.insert(0, '..')
10 from zeroinstall
.zerostore
import unpack
, manifest
, Store
, BadDigest
11 from zeroinstall
import SafeException
, support
12 from zeroinstall
.support
import find_in_path
14 def skipIf(condition
, reason
):
15 def wrapped(underlying
):
17 if hasattr(underlying
, 'func_name'):
18 print("Skipped %s: %s" % (underlying
.func_name
, reason
)) # Python 2
20 print("Skipped %s: %s" % (underlying
.__name
__, reason
)) # Python 3
27 class AbstractTestUnpack():
31 self
.tmpdir
= tempfile
.mkdtemp('-testunpack')
36 BaseTest
.tearDown(self
)
38 support
.ro_rmtree(self
.tmpdir
)
40 assert os
.umask(0o022) == 0o022
44 with
open('HelloWorld.tgz', 'rb') as stream
:
45 unpack
.unpack_archive('ftp://foo/file.foo', stream
, self
.tmpdir
)
47 except SafeException
as ex
:
48 assert 'Unknown extension' in str(ex
)
51 with
open('HelloWorld.tgz', 'rb') as stream
:
52 unpack
.unpack_archive('ftp://foo/file.tgz', stream
, self
.tmpdir
)
53 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
55 @skipIf(not find_in_path('hdiutil'), "not running on MacOS X; no hdiutil")
57 with
open('HelloWorld.dmg', 'rb') as stream
:
58 unpack
.unpack_archive('ftp://foo/file.dmg', stream
, self
.tmpdir
)
59 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
62 with
open('HelloWorld.zip', 'rb') as stream
:
63 unpack
.unpack_archive('ftp://foo/file.zip', stream
, self
.tmpdir
)
64 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
66 def testExtract(self
):
67 with
open('HelloWorld.tgz', 'rb') as stream
:
68 unpack
.unpack_archive('ftp://foo/file.tgz', stream
, self
.tmpdir
, extract
= 'HelloWorld')
69 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
71 def testExtractOver(self
):
72 with
open('HelloWorld.tgz', 'rb') as stream
:
73 unpack
.unpack_archive_over('ftp://foo/file.tgz', stream
, self
.tmpdir
, extract
= 'HelloWorld')
74 self
.assert_manifest('sha1=491678c37f77fadafbaae66b13d48d237773a68f')
76 def testExtractZip(self
):
77 with
open('HelloWorld.zip', 'rb') as stream
:
78 unpack
.unpack_archive('ftp://foo/file.zip', stream
, self
.tmpdir
, extract
= 'HelloWorld')
79 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
81 def testExtractIllegal(self
):
83 with
open('HelloWorld.tgz', 'rb') as stream
:
84 unpack
.unpack_archive('ftp://foo/file.tgz', stream
, self
.tmpdir
, extract
= 'Hello`World`')
86 except SafeException
as ex
:
87 assert 'Illegal' in str(ex
)
89 def testExtractFails(self
):
92 null
= os
.open(os
.devnull
, os
.O_WRONLY
)
96 with
open('HelloWorld.tgz', 'rb') as stream
:
97 unpack
.unpack_archive('ftp://foo/file.tgz', stream
, self
.tmpdir
, extract
= 'HelloWorld2')
99 except SafeException
as ex
:
100 if ('Failed to extract' not in str(ex
) and # GNU tar
101 'Unable to find' not in str(ex
)): # Python tar
107 with
open('HelloWorld.tgz', 'rb') as stream
:
108 unpack
.unpack_archive('ftp://foo/file.tar.GZ', stream
, self
.tmpdir
)
109 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
112 with
open('HelloWorld.tar.bz2', 'rb') as stream
:
113 unpack
.unpack_archive('ftp://foo/file.tar.bz2', stream
, self
.tmpdir
)
114 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
117 with
open('HelloWorld.tar', 'rb') as stream
:
118 unpack
.unpack_archive('ftp://foo/file.tar', stream
, self
.tmpdir
)
119 self
.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
122 with
open('dummy-1-1.noarch.rpm', 'rb') as stream
:
123 unpack
.unpack_archive('ftp://foo/file.rpm', stream
, self
.tmpdir
)
124 self
.assert_manifest('sha1=7be9228c8fe2a1434d4d448c4cf130e3c8a4f53d')
127 with
open('dummy_1-1_all.deb', 'rb') as stream
:
128 unpack
.unpack_archive('ftp://foo/file.deb', stream
, self
.tmpdir
)
129 self
.assert_manifest('sha1new=2c725156ec3832b7980a3de2270b3d8d85d4e3ea')
132 with
open('hello-0.1.gem', 'rb') as stream
:
133 unpack
.unpack_archive('ftp://foo/file.gem', stream
, self
.tmpdir
)
134 self
.assert_manifest('sha1new=fbd4827be7a18f9821790bdfd83132ee60d54647')
136 def testSpecial(self
):
137 os
.chmod(self
.tmpdir
, 0o2755)
138 store
= Store(self
.tmpdir
)
139 with
open('HelloWorld.tgz', 'rb') as stream
:
140 store
.add_archive_to_cache('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a',
142 'http://foo/foo.tgz')
145 logging
.getLogger('').setLevel(logging
.ERROR
)
147 store
= Store(self
.tmpdir
)
149 with
open('HelloWorld.tgz', 'rb') as stream
:
150 store
.add_archive_to_cache('sha1=3ce644dc725f1d21cfcf02562c76f375944b266b',
152 'http://foo/foo.tgz')
157 logging
.getLogger('').setLevel(logging
.INFO
)
159 def assert_manifest(self
, required
):
160 alg_name
= required
.split('=', 1)[0]
161 manifest
.fixup_permissions(self
.tmpdir
)
162 sha1
= alg_name
+ '=' + manifest
.add_manifest_file(self
.tmpdir
, manifest
.get_algorithm(alg_name
)).hexdigest()
163 self
.assertEqual(sha1
, required
)
165 # Check permissions are sensible
166 for root
, dirs
, files
in os
.walk(self
.tmpdir
):
167 for f
in files
+ dirs
:
168 full
= os
.path
.join(root
, f
)
169 if os
.path
.islink(full
): continue
170 full_mode
= os
.stat(full
).st_mode
171 self
.assertEqual(0o444, full_mode
& 0o666) # Must be r-?r-?r-?
173 class TestUnpackPython(AbstractTestUnpack
, BaseTest
):
175 AbstractTestUnpack
.setUp(self
)
176 unpack
._tar
_version
= 'Solaris tar'
177 assert not unpack
._gnu
_tar
()
179 class TestUnpackGNU(AbstractTestUnpack
, BaseTest
):
181 AbstractTestUnpack
.setUp(self
)
182 unpack
._tar
_version
= None
183 assert unpack
._gnu
_tar
()
185 # Only available with GNU tar
187 with
open('HelloWorld.tar.lzma', 'rb') as stream
:
188 unpack
.unpack_archive('ftp://foo/file.tar.lzma', stream
, self
.tmpdir
)
189 self
.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
191 if not unpack
._gnu
_tar
():
192 print("No GNU tar: SKIPPING tests")
193 del globals()['TestUnpackGNU']
195 if __name__
== '__main__':