Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | import mozfile |
michael@0 | 4 | import os |
michael@0 | 5 | import shutil |
michael@0 | 6 | import tarfile |
michael@0 | 7 | import tempfile |
michael@0 | 8 | import stubs |
michael@0 | 9 | import unittest |
michael@0 | 10 | import zipfile |
michael@0 | 11 | |
michael@0 | 12 | |
michael@0 | 13 | class TestExtract(unittest.TestCase): |
michael@0 | 14 | """test extracting archives""" |
michael@0 | 15 | |
michael@0 | 16 | def ensure_directory_contents(self, directory): |
michael@0 | 17 | """ensure the directory contents match""" |
michael@0 | 18 | for f in stubs.files: |
michael@0 | 19 | path = os.path.join(directory, *f) |
michael@0 | 20 | exists = os.path.exists(path) |
michael@0 | 21 | if not exists: |
michael@0 | 22 | print "%s does not exist" % (os.path.join(f)) |
michael@0 | 23 | self.assertTrue(exists) |
michael@0 | 24 | if exists: |
michael@0 | 25 | contents = file(path).read().strip() |
michael@0 | 26 | self.assertTrue(contents == f[-1]) |
michael@0 | 27 | |
michael@0 | 28 | def test_extract_zipfile(self): |
michael@0 | 29 | """test extracting a zipfile""" |
michael@0 | 30 | _zipfile = self.create_zip() |
michael@0 | 31 | self.assertTrue(os.path.exists(_zipfile)) |
michael@0 | 32 | try: |
michael@0 | 33 | dest = tempfile.mkdtemp() |
michael@0 | 34 | try: |
michael@0 | 35 | mozfile.extract_zip(_zipfile, dest) |
michael@0 | 36 | self.ensure_directory_contents(dest) |
michael@0 | 37 | finally: |
michael@0 | 38 | shutil.rmtree(dest) |
michael@0 | 39 | finally: |
michael@0 | 40 | os.remove(_zipfile) |
michael@0 | 41 | |
michael@0 | 42 | def test_extract_tarball(self): |
michael@0 | 43 | """test extracting a tarball""" |
michael@0 | 44 | tarball = self.create_tarball() |
michael@0 | 45 | self.assertTrue(os.path.exists(tarball)) |
michael@0 | 46 | try: |
michael@0 | 47 | dest = tempfile.mkdtemp() |
michael@0 | 48 | try: |
michael@0 | 49 | mozfile.extract_tarball(tarball, dest) |
michael@0 | 50 | self.ensure_directory_contents(dest) |
michael@0 | 51 | finally: |
michael@0 | 52 | shutil.rmtree(dest) |
michael@0 | 53 | finally: |
michael@0 | 54 | os.remove(tarball) |
michael@0 | 55 | |
michael@0 | 56 | def test_extract(self): |
michael@0 | 57 | """test the generalized extract function""" |
michael@0 | 58 | |
michael@0 | 59 | # test extracting a tarball |
michael@0 | 60 | tarball = self.create_tarball() |
michael@0 | 61 | self.assertTrue(os.path.exists(tarball)) |
michael@0 | 62 | try: |
michael@0 | 63 | dest = tempfile.mkdtemp() |
michael@0 | 64 | try: |
michael@0 | 65 | mozfile.extract(tarball, dest) |
michael@0 | 66 | self.ensure_directory_contents(dest) |
michael@0 | 67 | finally: |
michael@0 | 68 | shutil.rmtree(dest) |
michael@0 | 69 | finally: |
michael@0 | 70 | os.remove(tarball) |
michael@0 | 71 | |
michael@0 | 72 | # test extracting a zipfile |
michael@0 | 73 | _zipfile = self.create_zip() |
michael@0 | 74 | self.assertTrue(os.path.exists(_zipfile)) |
michael@0 | 75 | try: |
michael@0 | 76 | dest = tempfile.mkdtemp() |
michael@0 | 77 | try: |
michael@0 | 78 | mozfile.extract_zip(_zipfile, dest) |
michael@0 | 79 | self.ensure_directory_contents(dest) |
michael@0 | 80 | finally: |
michael@0 | 81 | shutil.rmtree(dest) |
michael@0 | 82 | finally: |
michael@0 | 83 | os.remove(_zipfile) |
michael@0 | 84 | |
michael@0 | 85 | # test extracting some non-archive; this should fail |
michael@0 | 86 | fd, filename = tempfile.mkstemp() |
michael@0 | 87 | os.write(fd, 'This is not a zipfile or tarball') |
michael@0 | 88 | os.close(fd) |
michael@0 | 89 | exception = None |
michael@0 | 90 | try: |
michael@0 | 91 | dest = tempfile.mkdtemp() |
michael@0 | 92 | mozfile.extract(filename, dest) |
michael@0 | 93 | except Exception, exception: |
michael@0 | 94 | pass |
michael@0 | 95 | finally: |
michael@0 | 96 | os.remove(filename) |
michael@0 | 97 | os.rmdir(dest) |
michael@0 | 98 | self.assertTrue(isinstance(exception, Exception)) |
michael@0 | 99 | |
michael@0 | 100 | ### utility functions |
michael@0 | 101 | |
michael@0 | 102 | def create_tarball(self): |
michael@0 | 103 | """create a stub tarball for testing""" |
michael@0 | 104 | tempdir = stubs.create_stub() |
michael@0 | 105 | filename = tempfile.mktemp(suffix='.tar') |
michael@0 | 106 | archive = tarfile.TarFile(filename, mode='w') |
michael@0 | 107 | try: |
michael@0 | 108 | for path in stubs.files: |
michael@0 | 109 | archive.add(os.path.join(tempdir, *path), arcname=os.path.join(*path)) |
michael@0 | 110 | except: |
michael@0 | 111 | os.remove(archive) |
michael@0 | 112 | raise |
michael@0 | 113 | finally: |
michael@0 | 114 | shutil.rmtree(tempdir) |
michael@0 | 115 | archive.close() |
michael@0 | 116 | return filename |
michael@0 | 117 | |
michael@0 | 118 | def create_zip(self): |
michael@0 | 119 | """create a stub zipfile for testing""" |
michael@0 | 120 | |
michael@0 | 121 | tempdir = stubs.create_stub() |
michael@0 | 122 | filename = tempfile.mktemp(suffix='.zip') |
michael@0 | 123 | archive = zipfile.ZipFile(filename, mode='w') |
michael@0 | 124 | try: |
michael@0 | 125 | for path in stubs.files: |
michael@0 | 126 | archive.write(os.path.join(tempdir, *path), arcname=os.path.join(*path)) |
michael@0 | 127 | except: |
michael@0 | 128 | os.remove(filename) |
michael@0 | 129 | raise |
michael@0 | 130 | finally: |
michael@0 | 131 | shutil.rmtree(tempdir) |
michael@0 | 132 | archive.close() |
michael@0 | 133 | return filename |