Don't use fork() for downloads; Windows doesn't support it
[zeroinstall/zeroinstall-mseaborn.git] / tests / testunpack.py
blob3e894fa40ba8fefa0e17d5e83921670d47f8a76c
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 testZip(self):
37 unpack.unpack_archive('ftp://foo/file.zip', file('HelloWorld.zip'), self.tmpdir)
38 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
40 def testExtract(self):
41 unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract = 'HelloWorld')
42 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
44 def testExtractIllegal(self):
45 try:
46 unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract = 'Hello`World`')
47 assert False
48 except SafeException, ex:
49 assert 'Illegal' in str(ex)
51 def testExtractFails(self):
52 stderr = os.dup(2)
53 try:
54 null = os.open('/dev/null', os.O_WRONLY)
55 os.close(2)
56 os.dup2(null, 2)
57 try:
58 unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract = 'HelloWorld2')
59 assert False
60 except SafeException, ex:
61 if ('Failed to extract' not in str(ex) and # GNU tar
62 'Unable to find' not in str(ex)): # Python tar
63 raise ex
64 finally:
65 os.dup2(stderr, 2)
67 def testTargz(self):
68 unpack.unpack_archive('ftp://foo/file.tar.GZ', file('HelloWorld.tgz'), self.tmpdir)
69 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
71 def testTbz(self):
72 unpack.unpack_archive('ftp://foo/file.tar.bz2', file('HelloWorld.tar.bz2'), self.tmpdir)
73 self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
75 def testTar(self):
76 unpack.unpack_archive('ftp://foo/file.tar', file('HelloWorld.tar'), self.tmpdir)
77 self.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
79 def testRPM(self):
80 unpack.unpack_archive('ftp://foo/file.rpm', file('dummy-1-1.noarch.rpm'), self.tmpdir)
81 self.assert_manifest('sha1=7be9228c8fe2a1434d4d448c4cf130e3c8a4f53d')
83 def testDeb(self):
84 unpack.unpack_archive('ftp://foo/file.deb', file('dummy_1-1_all.deb'), self.tmpdir)
85 self.assert_manifest('sha1new=2c725156ec3832b7980a3de2270b3d8d85d4e3ea')
87 def testSpecial(self):
88 os.chmod(self.tmpdir, 02755)
89 store = Store(self.tmpdir)
90 store.add_archive_to_cache('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a',
91 file('HelloWorld.tgz'),
92 'http://foo/foo.tgz')
94 def assert_manifest(self, required):
95 alg_name = required.split('=', 1)[0]
96 manifest.fixup_permissions(self.tmpdir)
97 sha1 = alg_name + '=' + manifest.add_manifest_file(self.tmpdir, manifest.get_algorithm(alg_name)).hexdigest()
98 self.assertEquals(sha1, required)
100 # Check permissions are sensible
101 for root, dirs, files in os.walk(self.tmpdir):
102 for f in files + dirs:
103 full = os.path.join(root, f)
104 if os.path.islink(full): continue
105 full_mode = os.stat(full).st_mode
106 self.assertEquals(0444, full_mode & 0666) # Must be r-?r-?r-?
108 class TestUnpackPython(AbstractTestUnpack):
109 def setUp(self):
110 AbstractTestUnpack.setUp(self)
111 unpack._tar_version = 'Solaris tar'
112 assert not unpack._gnu_tar()
114 class TestUnpackGNU(AbstractTestUnpack):
115 def setUp(self):
116 AbstractTestUnpack.setUp(self)
117 unpack._tar_version = None
118 assert unpack._gnu_tar()
120 suite = unittest.TestSuite()
121 if unpack._gnu_tar():
122 suite.addTest(unittest.makeSuite(TestUnpackGNU))
123 else:
124 print "No GNU tar: SKIPPING tests"
125 suite.addTest(unittest.makeSuite(TestUnpackPython))
127 if __name__ == '__main__':
128 unittest.TextTestRunner(verbosity=2).run(suite)