0store-secure-add: ensure files are world readable
[zeroinstall/solver.git] / tests / testunpack.py
blobb35c20a0a4da8f13f8b6fb98becc84a85fba0fcc
1 #!/usr/bin/env python
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):
13 if condition:
14 print "Skipped %s: %s" % (underlying.func_name, reason)
15 def run(self): pass
16 return run
17 else:
18 return underlying
19 return wrapped
21 class AbstractTestUnpack():
22 def setUp(self):
23 BaseTest.setUp(self)
25 self.tmpdir = tempfile.mkdtemp('-testunpack')
27 os.umask(0o022)
29 def tearDown(self):
30 BaseTest.tearDown(self)
32 support.ro_rmtree(self.tmpdir)
34 assert os.umask(0o022) == 0o022
36 def testBadExt(self):
37 try:
38 unpack.unpack_archive('ftp://foo/file.foo', file('HelloWorld.tgz'), self.tmpdir)
39 assert False
40 except SafeException as ex:
41 assert 'Unknown extension' in str(ex)
43 def testTgz(self):
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")
48 def testDmg(self):
49 unpack.unpack_archive('ftp://foo/file.dmg', file('HelloWorld.dmg'), self.tmpdir)
50 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
52 def testZip(self):
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):
69 try:
70 unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract = 'Hello`World`')
71 assert False
72 except SafeException as ex:
73 assert 'Illegal' in str(ex)
75 def testExtractFails(self):
76 stderr = os.dup(2)
77 try:
78 null = os.open('/dev/null', os.O_WRONLY)
79 os.close(2)
80 os.dup2(null, 2)
81 try:
82 unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract = 'HelloWorld2')
83 assert False
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
87 raise ex
88 finally:
89 os.dup2(stderr, 2)
91 def testTargz(self):
92 unpack.unpack_archive('ftp://foo/file.tar.GZ', file('HelloWorld.tgz'), self.tmpdir)
93 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
95 def testTbz(self):
96 unpack.unpack_archive('ftp://foo/file.tar.bz2', file('HelloWorld.tar.bz2'), self.tmpdir)
97 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
99 def testTar(self):
100 unpack.unpack_archive('ftp://foo/file.tar', file('HelloWorld.tar'), self.tmpdir)
101 self.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
103 def testRPM(self):
104 unpack.unpack_archive('ftp://foo/file.rpm', file('dummy-1-1.noarch.rpm'), self.tmpdir)
105 self.assert_manifest('sha1=7be9228c8fe2a1434d4d448c4cf130e3c8a4f53d')
107 def testDeb(self):
108 unpack.unpack_archive('ftp://foo/file.deb', file('dummy_1-1_all.deb'), self.tmpdir)
109 self.assert_manifest('sha1new=2c725156ec3832b7980a3de2270b3d8d85d4e3ea')
111 def testGem(self):
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')
122 def testBad(self):
123 logging.getLogger('').setLevel(logging.ERROR)
125 store = Store(self.tmpdir)
126 try:
127 store.add_archive_to_cache('sha1=3ce644dc725f1d21cfcf02562c76f375944b266b',
128 file('HelloWorld.tgz'),
129 'http://foo/foo.tgz')
130 assert 0
131 except BadDigest:
132 pass
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):
151 def setUp(self):
152 AbstractTestUnpack.setUp(self)
153 unpack._tar_version = 'Solaris tar'
154 assert not unpack._gnu_tar()
156 class TestUnpackGNU(AbstractTestUnpack, BaseTest):
157 def setUp(self):
158 AbstractTestUnpack.setUp(self)
159 unpack._tar_version = None
160 assert unpack._gnu_tar()
162 # Only available with GNU tar
163 def testLzma(self):
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__':
172 unittest.main()