|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +import hashlib |
| 6 | +import io |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import stat |
| 10 | +import tarfile |
| 11 | +import tempfile |
| 12 | +import unittest |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from taskgraph.util.archive import ( |
| 17 | + DEFAULT_MTIME, |
| 18 | + create_tar_from_files, |
| 19 | + create_tar_gz_from_files, |
| 20 | +) |
| 21 | + |
| 22 | +MODE_STANDARD = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH |
| 23 | + |
| 24 | + |
| 25 | +def file_hash(path): |
| 26 | + h = hashlib.sha1() |
| 27 | + with open(path, "rb") as fh: |
| 28 | + while True: |
| 29 | + data = fh.read(8192) |
| 30 | + if not data: |
| 31 | + break |
| 32 | + h.update(data) |
| 33 | + |
| 34 | + return h.hexdigest() |
| 35 | + |
| 36 | + |
| 37 | +class TestArchive(unittest.TestCase): |
| 38 | + def _create_files(self, root): |
| 39 | + files = {} |
| 40 | + for i in range(10): |
| 41 | + p = os.path.join(root, "file%02d" % i) |
| 42 | + with open(p, "wb") as fh: |
| 43 | + fh.write(b"file%02d" % i) |
| 44 | + # Need to set permissions or umask may influence testing. |
| 45 | + os.chmod(p, MODE_STANDARD) |
| 46 | + files["file%02d" % i] = p |
| 47 | + |
| 48 | + for i in range(10): |
| 49 | + files["file%02d" % (i + 10)] = io.BytesIO(b"file%02d" % (i + 10)) |
| 50 | + |
| 51 | + return files |
| 52 | + |
| 53 | + def _verify_basic_tarfile(self, tf): |
| 54 | + self.assertEqual(len(tf.getmembers()), 20) |
| 55 | + |
| 56 | + names = ["file%02d" % i for i in range(20)] |
| 57 | + self.assertEqual(tf.getnames(), names) |
| 58 | + |
| 59 | + for ti in tf.getmembers(): |
| 60 | + self.assertEqual(ti.uid, 0) |
| 61 | + self.assertEqual(ti.gid, 0) |
| 62 | + self.assertEqual(ti.uname, "") |
| 63 | + self.assertEqual(ti.gname, "") |
| 64 | + self.assertEqual(ti.mode, MODE_STANDARD) |
| 65 | + self.assertEqual(ti.mtime, DEFAULT_MTIME) |
| 66 | + |
| 67 | + @pytest.mark.xfail( |
| 68 | + reason="ValueError is not thrown despite being provided directory." |
| 69 | + ) |
| 70 | + def test_dirs_refused(self): |
| 71 | + d = tempfile.mkdtemp() |
| 72 | + try: |
| 73 | + tp = os.path.join(d, "test.tar") |
| 74 | + with open(tp, "wb") as fh: |
| 75 | + with self.assertRaisesRegex(ValueError, "not a regular"): |
| 76 | + create_tar_from_files(fh, {"test": d}) |
| 77 | + finally: |
| 78 | + shutil.rmtree(d) |
| 79 | + |
| 80 | + def test_setuid_setgid_refused(self): |
| 81 | + d = tempfile.mkdtemp() |
| 82 | + try: |
| 83 | + uid = os.path.join(d, "setuid") |
| 84 | + gid = os.path.join(d, "setgid") |
| 85 | + with open(uid, "a"): |
| 86 | + pass |
| 87 | + with open(gid, "a"): |
| 88 | + pass |
| 89 | + |
| 90 | + os.chmod(uid, MODE_STANDARD | stat.S_ISUID) |
| 91 | + os.chmod(gid, MODE_STANDARD | stat.S_ISGID) |
| 92 | + |
| 93 | + tp = os.path.join(d, "test.tar") |
| 94 | + with open(tp, "wb") as fh: |
| 95 | + with self.assertRaisesRegex(ValueError, "cannot add file with setuid"): |
| 96 | + create_tar_from_files(fh, {"test": uid}) |
| 97 | + with self.assertRaisesRegex(ValueError, "cannot add file with setuid"): |
| 98 | + create_tar_from_files(fh, {"test": gid}) |
| 99 | + finally: |
| 100 | + shutil.rmtree(d) |
| 101 | + |
| 102 | + def test_create_tar_basic(self): |
| 103 | + d = tempfile.mkdtemp() |
| 104 | + try: |
| 105 | + files = self._create_files(d) |
| 106 | + |
| 107 | + tp = os.path.join(d, "test.tar") |
| 108 | + with open(tp, "wb") as fh: |
| 109 | + create_tar_from_files(fh, files) |
| 110 | + |
| 111 | + # Output should be deterministic. |
| 112 | + self.assertEqual(file_hash(tp), "01cd314e277f060e98c7de6c8ea57f96b3a2065c") |
| 113 | + |
| 114 | + with tarfile.open(tp, "r") as tf: |
| 115 | + self._verify_basic_tarfile(tf) |
| 116 | + |
| 117 | + finally: |
| 118 | + shutil.rmtree(d) |
| 119 | + |
| 120 | + @pytest.mark.xfail(reason="hash mismatch") |
| 121 | + def test_executable_preserved(self): |
| 122 | + d = tempfile.mkdtemp() |
| 123 | + try: |
| 124 | + p = os.path.join(d, "exec") |
| 125 | + with open(p, "wb") as fh: |
| 126 | + fh.write("#!/bin/bash\n") |
| 127 | + os.chmod(p, MODE_STANDARD | stat.S_IXUSR) |
| 128 | + |
| 129 | + tp = os.path.join(d, "test.tar") |
| 130 | + with open(tp, "wb") as fh: |
| 131 | + create_tar_from_files(fh, {"exec": p}) |
| 132 | + |
| 133 | + self.assertEqual(file_hash(tp), "357e1b81c0b6cfdfa5d2d118d420025c3c76ee93") |
| 134 | + |
| 135 | + with tarfile.open(tp, "r") as tf: |
| 136 | + m = tf.getmember("exec") |
| 137 | + self.assertEqual(m.mode, MODE_STANDARD | stat.S_IXUSR) |
| 138 | + |
| 139 | + finally: |
| 140 | + shutil.rmtree(d) |
| 141 | + |
| 142 | + def test_create_tar_gz_basic(self): |
| 143 | + d = tempfile.mkdtemp() |
| 144 | + try: |
| 145 | + files = self._create_files(d) |
| 146 | + |
| 147 | + gp = os.path.join(d, "test.tar.gz") |
| 148 | + with open(gp, "wb") as fh: |
| 149 | + create_tar_gz_from_files(fh, files) |
| 150 | + |
| 151 | + self.assertEqual(file_hash(gp), "7c4da5adc5088cdf00911d5daf9a67b15de714b7") |
| 152 | + |
| 153 | + with tarfile.open(gp, "r:gz") as tf: |
| 154 | + self._verify_basic_tarfile(tf) |
| 155 | + |
| 156 | + finally: |
| 157 | + shutil.rmtree(d) |
| 158 | + |
| 159 | + def test_tar_gz_name(self): |
| 160 | + d = tempfile.mkdtemp() |
| 161 | + try: |
| 162 | + files = self._create_files(d) |
| 163 | + |
| 164 | + gp = os.path.join(d, "test.tar.gz") |
| 165 | + with open(gp, "wb") as fh: |
| 166 | + create_tar_gz_from_files(fh, files, filename="foobar") |
| 167 | + |
| 168 | + self.assertEqual(file_hash(gp), "721e00083c17d16df2edbddf40136298c06d0c49") |
| 169 | + |
| 170 | + with tarfile.open(gp, "r:gz") as tf: |
| 171 | + self._verify_basic_tarfile(tf) |
| 172 | + |
| 173 | + finally: |
| 174 | + shutil.rmtree(d) |
0 commit comments