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 print("Skipped %s: %s" % (underlying
.func_name
, reason
))
24 class AbstractTestUnpack():
28 self
.tmpdir
= tempfile
.mkdtemp('-testunpack')
33 BaseTest
.tearDown(self
)
35 support
.ro_rmtree(self
.tmpdir
)
37 assert os
.umask(0o022) == 0o022
41 unpack
.unpack_archive('ftp://foo/file.foo', open('HelloWorld.tgz'), self
.tmpdir
)
43 except SafeException
as ex
:
44 assert 'Unknown extension' in str(ex
)
47 unpack
.unpack_archive('ftp://foo/file.tgz', open('HelloWorld.tgz'), self
.tmpdir
)
48 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
50 @skipIf(not find_in_path('hdiutil'), "not running on MacOS X; no hdiutil")
52 unpack
.unpack_archive('ftp://foo/file.dmg', open('HelloWorld.dmg'), self
.tmpdir
)
53 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
56 unpack
.unpack_archive('ftp://foo/file.zip', open('HelloWorld.zip'), self
.tmpdir
)
57 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
59 def testExtract(self
):
60 unpack
.unpack_archive('ftp://foo/file.tgz', open('HelloWorld.tgz'), self
.tmpdir
, extract
= 'HelloWorld')
61 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
63 def testExtractOver(self
):
64 unpack
.unpack_archive_over('ftp://foo/file.tgz', open('HelloWorld.tgz'), self
.tmpdir
, extract
= 'HelloWorld')
65 self
.assert_manifest('sha1=491678c37f77fadafbaae66b13d48d237773a68f')
67 def testExtractZip(self
):
68 unpack
.unpack_archive('ftp://foo/file.zip', open('HelloWorld.zip'), self
.tmpdir
, extract
= 'HelloWorld')
69 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
71 def testExtractIllegal(self
):
73 unpack
.unpack_archive('ftp://foo/file.tgz', open('HelloWorld.tgz'), self
.tmpdir
, extract
= 'Hello`World`')
75 except SafeException
as ex
:
76 assert 'Illegal' in str(ex
)
78 def testExtractFails(self
):
81 null
= os
.open(os
.devnull
, os
.O_WRONLY
)
85 unpack
.unpack_archive('ftp://foo/file.tgz', open('HelloWorld.tgz'), self
.tmpdir
, extract
= 'HelloWorld2')
87 except SafeException
as ex
:
88 if ('Failed to extract' not in str(ex
) and # GNU tar
89 'Unable to find' not in str(ex
)): # Python tar
95 unpack
.unpack_archive('ftp://foo/file.tar.GZ', open('HelloWorld.tgz'), self
.tmpdir
)
96 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
99 unpack
.unpack_archive('ftp://foo/file.tar.bz2', open('HelloWorld.tar.bz2'), self
.tmpdir
)
100 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
103 unpack
.unpack_archive('ftp://foo/file.tar', open('HelloWorld.tar'), self
.tmpdir
)
104 self
.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
107 unpack
.unpack_archive('ftp://foo/file.rpm', open('dummy-1-1.noarch.rpm'), self
.tmpdir
)
108 self
.assert_manifest('sha1=7be9228c8fe2a1434d4d448c4cf130e3c8a4f53d')
111 unpack
.unpack_archive('ftp://foo/file.deb', open('dummy_1-1_all.deb'), self
.tmpdir
)
112 self
.assert_manifest('sha1new=2c725156ec3832b7980a3de2270b3d8d85d4e3ea')
115 unpack
.unpack_archive('ftp://foo/file.gem', open('hello-0.1.gem'), self
.tmpdir
)
116 self
.assert_manifest('sha1new=fbd4827be7a18f9821790bdfd83132ee60d54647')
118 def testSpecial(self
):
119 os
.chmod(self
.tmpdir
, 0o2755)
120 store
= Store(self
.tmpdir
)
121 store
.add_archive_to_cache('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a',
122 open('HelloWorld.tgz'),
123 'http://foo/foo.tgz')
126 logging
.getLogger('').setLevel(logging
.ERROR
)
128 store
= Store(self
.tmpdir
)
130 store
.add_archive_to_cache('sha1=3ce644dc725f1d21cfcf02562c76f375944b266b',
131 open('HelloWorld.tgz'),
132 'http://foo/foo.tgz')
137 logging
.getLogger('').setLevel(logging
.INFO
)
139 def assert_manifest(self
, required
):
140 alg_name
= required
.split('=', 1)[0]
141 manifest
.fixup_permissions(self
.tmpdir
)
142 sha1
= alg_name
+ '=' + manifest
.add_manifest_file(self
.tmpdir
, manifest
.get_algorithm(alg_name
)).hexdigest()
143 self
.assertEqual(sha1
, required
)
145 # Check permissions are sensible
146 for root
, dirs
, files
in os
.walk(self
.tmpdir
):
147 for f
in files
+ dirs
:
148 full
= os
.path
.join(root
, f
)
149 if os
.path
.islink(full
): continue
150 full_mode
= os
.stat(full
).st_mode
151 self
.assertEqual(0o444, full_mode
& 0o666) # Must be r-?r-?r-?
153 class TestUnpackPython(AbstractTestUnpack
, BaseTest
):
155 AbstractTestUnpack
.setUp(self
)
156 unpack
._tar
_version
= 'Solaris tar'
157 assert not unpack
._gnu
_tar
()
159 class TestUnpackGNU(AbstractTestUnpack
, BaseTest
):
161 AbstractTestUnpack
.setUp(self
)
162 unpack
._tar
_version
= None
163 assert unpack
._gnu
_tar
()
165 # Only available with GNU tar
167 unpack
.unpack_archive('ftp://foo/file.tar.lzma', open('HelloWorld.tar.lzma'), self
.tmpdir
)
168 self
.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
170 if not unpack
._gnu
_tar
():
171 print("No GNU tar: SKIPPING tests")
172 del globals()['TestUnpackGNU']
174 if __name__
== '__main__':