Better error handling.
[zeroinstall.git] / tests / testunpack.py
blob0d2b37f4a101ee620197abae7b2ad9b511af3fdc
1 #!/usr/bin/env python2.4
2 from basetest import BaseTest
3 import sys, tempfile, os, shutil, sha
4 import unittest
6 sys.path.insert(0, '..')
7 from zeroinstall.zerostore import unpack, manifest, Store
8 from zeroinstall import SafeException, support
10 class AbstractTestUnpack(BaseTest):
11 def setUp(self):
12 BaseTest.setUp(self)
14 self.tmpdir = tempfile.mkdtemp('-testunpack')
16 os.umask(0022)
18 def tearDown(self):
19 BaseTest.tearDown(self)
21 support.ro_rmtree(self.tmpdir)
23 assert os.umask(0022) == 0022
25 def testBadExt(self):
26 try:
27 unpack.unpack_archive('ftp://foo/file.foo', file('HelloWorld.tgz'), self.tmpdir)
28 assert False
29 except SafeException, ex:
30 assert 'Unknown extension' in str(ex)
32 def testTgz(self):
33 unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir)
34 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
36 def testExtract(self):
37 unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract = 'HelloWorld')
38 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
40 def testExtractIllegal(self):
41 try:
42 unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract = 'Hello`World`')
43 assert False
44 except SafeException, ex:
45 assert 'Illegal' in str(ex)
47 def testExtractFails(self):
48 stderr = os.dup(2)
49 try:
50 null = os.open('/dev/null', os.O_WRONLY)
51 os.close(2)
52 os.dup2(null, 2)
53 try:
54 unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract = 'HelloWorld2')
55 assert False
56 except SafeException, ex:
57 if ('Failed to extract' not in str(ex) and # GNU tar
58 'Unable to find' not in str(ex)): # Python tar
59 raise ex
60 finally:
61 os.dup2(stderr, 2)
63 def testTargz(self):
64 unpack.unpack_archive('ftp://foo/file.tar.GZ', file('HelloWorld.tgz'), self.tmpdir)
65 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
67 def testTbz(self):
68 unpack.unpack_archive('ftp://foo/file.tar.bz2', file('HelloWorld.tar.bz2'), self.tmpdir)
69 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
71 def testTar(self):
72 unpack.unpack_archive('ftp://foo/file.tar', file('HelloWorld.tar'), self.tmpdir)
73 self.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
75 def testRPM(self):
76 unpack.unpack_archive('ftp://foo/file.rpm', file('dummy-1-1.noarch.rpm'), self.tmpdir)
77 self.assert_manifest('sha1=7be9228c8fe2a1434d4d448c4cf130e3c8a4f53d')
79 def testDeb(self):
80 unpack.unpack_archive('ftp://foo/file.deb', file('dummy_1-1_all.deb'), self.tmpdir)
81 self.assert_manifest('sha1new=2c725156ec3832b7980a3de2270b3d8d85d4e3ea')
83 def testSpecial(self):
84 os.chmod(self.tmpdir, 02755)
85 store = Store(self.tmpdir)
86 store.add_archive_to_cache('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a',
87 file('HelloWorld.tgz'),
88 'http://foo/foo.tgz')
90 def assert_manifest(self, required):
91 alg_name = required.split('=', 1)[0]
92 manifest.fixup_permissions(self.tmpdir)
93 sha1 = alg_name + '=' + manifest.add_manifest_file(self.tmpdir, manifest.get_algorithm(alg_name)).hexdigest()
94 self.assertEquals(sha1, required)
96 # Check permissions are sensible
97 for root, dirs, files in os.walk(self.tmpdir):
98 for f in files + dirs:
99 full = os.path.join(root, f)
100 if os.path.islink(full): continue
101 full_mode = os.stat(full).st_mode
102 self.assertEquals(0444, full_mode & 0666) # Must be r-?r-?r-?
104 class TestUnpackPython(AbstractTestUnpack):
105 def setUp(self):
106 AbstractTestUnpack.setUp(self)
107 unpack._tar_version = 'Solaris tar'
108 assert not unpack._gnu_tar()
110 class TestUnpackGNU(AbstractTestUnpack):
111 def setUp(self):
112 AbstractTestUnpack.setUp(self)
113 unpack._tar_version = None
114 assert unpack._gnu_tar()
116 suite = unittest.TestSuite()
117 if unpack._gnu_tar():
118 suite.addTest(unittest.makeSuite(TestUnpackGNU))
119 else:
120 print "No GNU tar: SKIPPING tests"
121 suite.addTest(unittest.makeSuite(TestUnpackPython))
123 if __name__ == '__main__':
124 unittest.TextTestRunner(verbosity=2).run(suite)