2 from basetest
import BaseTest
3 import sys
, tempfile
, os
4 import unittest
, logging
6 sys
.path
.insert(0, '..')
7 from zeroinstall
.zerostore
import unpack
, manifest
, Store
, BadDigest
8 from zeroinstall
import SafeException
, support
9 from zeroinstall
.support
import find_in_path
11 def skipIf(condition
, reason
):
12 def wrapped(underlying
):
14 print "Skipped %s: %s" % (underlying
.func_name
, reason
)
21 class AbstractTestUnpack():
25 self
.tmpdir
= tempfile
.mkdtemp('-testunpack')
30 BaseTest
.tearDown(self
)
32 support
.ro_rmtree(self
.tmpdir
)
34 assert os
.umask(0o022) == 0o022
38 unpack
.unpack_archive('ftp://foo/file.foo', file('HelloWorld.tgz'), self
.tmpdir
)
40 except SafeException
as ex
:
41 assert 'Unknown extension' in str(ex
)
44 unpack
.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self
.tmpdir
)
45 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
47 @skipIf(not find_in_path('hdiutil'), "not running on MacOS X; no hdiutil")
49 unpack
.unpack_archive('ftp://foo/file.dmg', file('HelloWorld.dmg'), self
.tmpdir
)
50 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
53 unpack
.unpack_archive('ftp://foo/file.zip', file('HelloWorld.zip'), self
.tmpdir
)
54 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
56 def testExtract(self
):
57 unpack
.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self
.tmpdir
, extract
= 'HelloWorld')
58 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
60 def testExtractOver(self
):
61 unpack
.unpack_archive_over('ftp://foo/file.tgz', file('HelloWorld.tgz'), self
.tmpdir
, extract
= 'HelloWorld')
62 self
.assert_manifest('sha1=491678c37f77fadafbaae66b13d48d237773a68f')
64 def testExtractZip(self
):
65 unpack
.unpack_archive('ftp://foo/file.zip', file('HelloWorld.zip'), self
.tmpdir
, extract
= 'HelloWorld')
66 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
68 def testExtractIllegal(self
):
70 unpack
.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self
.tmpdir
, extract
= 'Hello`World`')
72 except SafeException
as ex
:
73 assert 'Illegal' in str(ex
)
75 def testExtractFails(self
):
78 null
= os
.open('/dev/null', os
.O_WRONLY
)
82 unpack
.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self
.tmpdir
, extract
= 'HelloWorld2')
84 except SafeException
as ex
:
85 if ('Failed to extract' not in str(ex
) and # GNU tar
86 'Unable to find' not in str(ex
)): # Python tar
92 unpack
.unpack_archive('ftp://foo/file.tar.GZ', file('HelloWorld.tgz'), self
.tmpdir
)
93 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
96 unpack
.unpack_archive('ftp://foo/file.tar.bz2', file('HelloWorld.tar.bz2'), self
.tmpdir
)
97 self
.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
100 unpack
.unpack_archive('ftp://foo/file.tar', file('HelloWorld.tar'), self
.tmpdir
)
101 self
.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
104 unpack
.unpack_archive('ftp://foo/file.rpm', file('dummy-1-1.noarch.rpm'), self
.tmpdir
)
105 self
.assert_manifest('sha1=7be9228c8fe2a1434d4d448c4cf130e3c8a4f53d')
108 unpack
.unpack_archive('ftp://foo/file.deb', file('dummy_1-1_all.deb'), self
.tmpdir
)
109 self
.assert_manifest('sha1new=2c725156ec3832b7980a3de2270b3d8d85d4e3ea')
112 unpack
.unpack_archive('ftp://foo/file.gem', file('hello-0.1.gem'), self
.tmpdir
)
113 self
.assert_manifest('sha1new=fbd4827be7a18f9821790bdfd83132ee60d54647')
115 def testSpecial(self
):
116 os
.chmod(self
.tmpdir
, 0o2755)
117 store
= Store(self
.tmpdir
)
118 store
.add_archive_to_cache('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a',
119 file('HelloWorld.tgz'),
120 'http://foo/foo.tgz')
123 logging
.getLogger('').setLevel(logging
.ERROR
)
125 store
= Store(self
.tmpdir
)
127 store
.add_archive_to_cache('sha1=3ce644dc725f1d21cfcf02562c76f375944b266b',
128 file('HelloWorld.tgz'),
129 'http://foo/foo.tgz')
134 logging
.getLogger('').setLevel(logging
.INFO
)
136 def assert_manifest(self
, required
):
137 alg_name
= required
.split('=', 1)[0]
138 manifest
.fixup_permissions(self
.tmpdir
)
139 sha1
= alg_name
+ '=' + manifest
.add_manifest_file(self
.tmpdir
, manifest
.get_algorithm(alg_name
)).hexdigest()
140 self
.assertEquals(sha1
, required
)
142 # Check permissions are sensible
143 for root
, dirs
, files
in os
.walk(self
.tmpdir
):
144 for f
in files
+ dirs
:
145 full
= os
.path
.join(root
, f
)
146 if os
.path
.islink(full
): continue
147 full_mode
= os
.stat(full
).st_mode
148 self
.assertEquals(0o444, full_mode
& 0o666) # Must be r-?r-?r-?
150 class TestUnpackPython(AbstractTestUnpack
, BaseTest
):
152 AbstractTestUnpack
.setUp(self
)
153 unpack
._tar
_version
= 'Solaris tar'
154 assert not unpack
._gnu
_tar
()
156 class TestUnpackGNU(AbstractTestUnpack
, BaseTest
):
158 AbstractTestUnpack
.setUp(self
)
159 unpack
._tar
_version
= None
160 assert unpack
._gnu
_tar
()
162 # Only available with GNU tar
164 unpack
.unpack_archive('ftp://foo/file.tar.lzma', file('HelloWorld.tar.lzma'), self
.tmpdir
)
165 self
.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
167 if not unpack
._gnu
_tar
():
168 print "No GNU tar: SKIPPING tests"
169 del globals()['TestUnpackGNU']
171 if __name__
== '__main__':