Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import os |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 3 | import io |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 4 | import shutil |
Brett Cannon | 455ea53 | 2003-06-12 08:01:06 +0000 | [diff] [blame] | 5 | import tempfile |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 6 | import io |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 7 | from hashlib import md5 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 8 | import errno |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 9 | |
| 10 | import unittest |
| 11 | import tarfile |
| 12 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 13 | from test import support |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 14 | |
| 15 | # Check for our compression modules. |
| 16 | try: |
| 17 | import gzip |
Neal Norwitz | ae32319 | 2003-04-14 01:18:32 +0000 | [diff] [blame] | 18 | gzip.GzipFile |
| 19 | except (ImportError, AttributeError): |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 20 | gzip = None |
| 21 | try: |
| 22 | import bz2 |
| 23 | except ImportError: |
| 24 | bz2 = None |
| 25 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 26 | def md5sum(data): |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 27 | return md5(data).hexdigest() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 28 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 29 | def path(path): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 30 | return support.findfile(path) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 32 | TEMPDIR = os.path.join(tempfile.gettempdir(), "test_tarfile_tmp") |
| 33 | tarname = path("testtar.tar") |
| 34 | gzipname = os.path.join(TEMPDIR, "testtar.tar.gz") |
| 35 | bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2") |
| 36 | tmpname = os.path.join(TEMPDIR, "tmp.tar") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 38 | md5_regtype = "65f477c818ad9e15f7feab0c6d37742f" |
| 39 | md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6" |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 40 | |
| 41 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 42 | class ReadTest(unittest.TestCase): |
| 43 | |
| 44 | tarname = tarname |
| 45 | mode = "r:" |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 46 | |
| 47 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 48 | self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 49 | |
| 50 | def tearDown(self): |
| 51 | self.tar.close() |
| 52 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 54 | class UstarReadTest(ReadTest): |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 56 | def test_fileobj_regular_file(self): |
| 57 | tarinfo = self.tar.getmember("ustar/regtype") |
| 58 | fobj = self.tar.extractfile(tarinfo) |
| 59 | data = fobj.read() |
| 60 | self.assert_((len(data), md5sum(data)) == (tarinfo.size, md5_regtype), |
| 61 | "regular file extraction failed") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 63 | def test_fileobj_readlines(self): |
| 64 | self.tar.extract("ustar/regtype", TEMPDIR) |
| 65 | tarinfo = self.tar.getmember("ustar/regtype") |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 66 | fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "r") |
| 67 | fobj2 = io.TextIOWrapper(self.tar.extractfile(tarinfo)) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 69 | lines1 = fobj1.readlines() |
| 70 | lines2 = fobj2.readlines() |
| 71 | self.assert_(lines1 == lines2, |
| 72 | "fileobj.readlines() failed") |
| 73 | self.assert_(len(lines2) == 114, |
| 74 | "fileobj.readlines() failed") |
| 75 | self.assert_(lines2[83] == \ |
| 76 | "I will gladly admit that Python is not the fastest running scripting language.\n", |
| 77 | "fileobj.readlines() failed") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 79 | def test_fileobj_iter(self): |
| 80 | self.tar.extract("ustar/regtype", TEMPDIR) |
| 81 | tarinfo = self.tar.getmember("ustar/regtype") |
| 82 | fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "rU") |
| 83 | fobj2 = self.tar.extractfile(tarinfo) |
| 84 | lines1 = fobj1.readlines() |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 85 | lines2 = list(io.TextIOWrapper(fobj2)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 86 | self.assert_(lines1 == lines2, |
| 87 | "fileobj.__iter__() failed") |
Martin v. Löwis | df24153 | 2005-03-03 08:17:42 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 89 | def test_fileobj_seek(self): |
| 90 | self.tar.extract("ustar/regtype", TEMPDIR) |
| 91 | fobj = open(os.path.join(TEMPDIR, "ustar/regtype"), "rb") |
| 92 | data = fobj.read() |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 93 | fobj.close() |
| 94 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 95 | tarinfo = self.tar.getmember("ustar/regtype") |
| 96 | fobj = self.tar.extractfile(tarinfo) |
| 97 | |
| 98 | text = fobj.read() |
| 99 | fobj.seek(0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 100 | self.assertEqual(0, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 101 | "seek() to file's start failed") |
| 102 | fobj.seek(2048, 0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 103 | self.assertEqual(2048, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 104 | "seek() to absolute position failed") |
| 105 | fobj.seek(-1024, 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 106 | self.assertEqual(1024, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 107 | "seek() to negative relative position failed") |
| 108 | fobj.seek(1024, 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 109 | self.assertEqual(2048, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 110 | "seek() to positive relative position failed") |
| 111 | s = fobj.read(10) |
| 112 | self.assert_(s == data[2048:2058], |
| 113 | "read() after seek failed") |
| 114 | fobj.seek(0, 2) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 115 | self.assertEqual(tarinfo.size, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 116 | "seek() to file's end failed") |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 117 | self.assert_(fobj.read() == b"", |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 118 | "read() at file's end did not return empty string") |
| 119 | fobj.seek(-tarinfo.size, 2) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 120 | self.assertEqual(0, fobj.tell(), |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 121 | "relative seek() to file's end failed") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 122 | fobj.seek(512) |
| 123 | s1 = fobj.readlines() |
| 124 | fobj.seek(512) |
| 125 | s2 = fobj.readlines() |
| 126 | self.assert_(s1 == s2, |
| 127 | "readlines() after seek failed") |
| 128 | fobj.seek(0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 129 | self.assertEqual(len(fobj.readline()), fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 130 | "tell() after readline() failed") |
| 131 | fobj.seek(512) |
| 132 | self.assert_(len(fobj.readline()) + 512 == fobj.tell(), |
| 133 | "tell() after seek() and readline() failed") |
| 134 | fobj.seek(0) |
| 135 | line = fobj.readline() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 136 | self.assertEqual(fobj.read(), data[len(line):], |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 137 | "read() after readline() failed") |
| 138 | fobj.close() |
| 139 | |
| 140 | |
| 141 | class MiscReadTest(ReadTest): |
| 142 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 143 | def test_no_name_argument(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 144 | fobj = open(self.tarname, "rb") |
| 145 | tar = tarfile.open(fileobj=fobj, mode=self.mode) |
| 146 | self.assertEqual(tar.name, os.path.abspath(fobj.name)) |
| 147 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 148 | def test_no_name_attribute(self): |
| 149 | data = open(self.tarname, "rb").read() |
| 150 | fobj = io.BytesIO(data) |
| 151 | self.assertRaises(AttributeError, getattr, fobj, "name") |
| 152 | tar = tarfile.open(fileobj=fobj, mode=self.mode) |
| 153 | self.assertEqual(tar.name, None) |
| 154 | |
| 155 | def test_empty_name_attribute(self): |
| 156 | data = open(self.tarname, "rb").read() |
| 157 | fobj = io.BytesIO(data) |
| 158 | fobj.name = "" |
| 159 | tar = tarfile.open(fileobj=fobj, mode=self.mode) |
| 160 | self.assertEqual(tar.name, None) |
| 161 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 162 | def test_fileobj_with_offset(self): |
| 163 | # Skip the first member and store values from the second member |
| 164 | # of the testtar. |
| 165 | tar = tarfile.open(self.tarname, mode=self.mode) |
| 166 | tar.next() |
| 167 | t = tar.next() |
| 168 | name = t.name |
| 169 | offset = t.offset |
| 170 | data = tar.extractfile(t).read() |
| 171 | tar.close() |
| 172 | |
| 173 | # Open the testtar and seek to the offset of the second member. |
| 174 | if self.mode.endswith(":gz"): |
| 175 | _open = gzip.GzipFile |
| 176 | elif self.mode.endswith(":bz2"): |
| 177 | _open = bz2.BZ2File |
| 178 | else: |
| 179 | _open = open |
| 180 | fobj = _open(self.tarname, "rb") |
| 181 | fobj.seek(offset) |
| 182 | |
| 183 | # Test if the tarfile starts with the second member. |
| 184 | tar = tar.open(self.tarname, mode="r:", fileobj=fobj) |
| 185 | t = tar.next() |
| 186 | self.assertEqual(t.name, name) |
| 187 | # Read to the end of fileobj and test if seeking back to the |
| 188 | # beginning works. |
| 189 | tar.getmembers() |
| 190 | self.assertEqual(tar.extractfile(t).read(), data, |
| 191 | "seek back did not work") |
| 192 | tar.close() |
| 193 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 194 | def test_fail_comp(self): |
| 195 | # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file. |
| 196 | if self.mode == "r:": |
| 197 | return |
| 198 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode) |
| 199 | fobj = open(tarname, "rb") |
| 200 | self.assertRaises(tarfile.ReadError, tarfile.open, fileobj=fobj, mode=self.mode) |
| 201 | |
| 202 | def test_v7_dirtype(self): |
| 203 | # Test old style dirtype member (bug #1336623): |
| 204 | # Old V7 tars create directory members using an AREGTYPE |
| 205 | # header with a "/" appended to the filename field. |
| 206 | tarinfo = self.tar.getmember("misc/dirtype-old-v7") |
| 207 | self.assert_(tarinfo.type == tarfile.DIRTYPE, |
| 208 | "v7 dirtype failed") |
| 209 | |
Christian Heimes | 126d29a | 2008-02-11 22:57:17 +0000 | [diff] [blame] | 210 | def test_xstar_type(self): |
| 211 | # The xstar format stores extra atime and ctime fields inside the |
| 212 | # space reserved for the prefix field. The prefix field must be |
| 213 | # ignored in this case, otherwise it will mess up the name. |
| 214 | try: |
| 215 | self.tar.getmember("misc/regtype-xstar") |
| 216 | except KeyError: |
| 217 | self.fail("failed to find misc/regtype-xstar (mangled prefix?)") |
| 218 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 219 | def test_check_members(self): |
| 220 | for tarinfo in self.tar: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 221 | self.assert_(int(tarinfo.mtime) == 0o7606136617, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 222 | "wrong mtime for %s" % tarinfo.name) |
| 223 | if not tarinfo.name.startswith("ustar/"): |
| 224 | continue |
| 225 | self.assert_(tarinfo.uname == "tarfile", |
| 226 | "wrong uname for %s" % tarinfo.name) |
| 227 | |
| 228 | def test_find_members(self): |
| 229 | self.assert_(self.tar.getmembers()[-1].name == "misc/eof", |
| 230 | "could not find all members") |
| 231 | |
| 232 | def test_extract_hardlink(self): |
| 233 | # Test hardlink extraction (e.g. bug #857297). |
| 234 | tar = tarfile.open(tarname, errorlevel=1, encoding="iso8859-1") |
| 235 | |
| 236 | tar.extract("ustar/regtype", TEMPDIR) |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 237 | try: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 238 | tar.extract("ustar/lnktype", TEMPDIR) |
| 239 | except EnvironmentError as e: |
| 240 | if e.errno == errno.ENOENT: |
| 241 | self.fail("hardlink not extracted properly") |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 243 | data = open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb").read() |
| 244 | self.assertEqual(md5sum(data), md5_regtype) |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 246 | try: |
| 247 | tar.extract("ustar/symtype", TEMPDIR) |
| 248 | except EnvironmentError as e: |
| 249 | if e.errno == errno.ENOENT: |
| 250 | self.fail("symlink not extracted properly") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 252 | data = open(os.path.join(TEMPDIR, "ustar/symtype"), "rb").read() |
| 253 | self.assertEqual(md5sum(data), md5_regtype) |
| 254 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 255 | def test_extractall(self): |
| 256 | # Test if extractall() correctly restores directory permissions |
| 257 | # and times (see issue1735). |
| 258 | if sys.platform == "win32": |
| 259 | # Win32 has no support for utime() on directories or |
| 260 | # fine grained permissions. |
| 261 | return |
| 262 | |
| 263 | tar = tarfile.open(tarname, encoding="iso8859-1") |
| 264 | directories = [t for t in tar if t.isdir()] |
| 265 | tar.extractall(TEMPDIR, directories) |
| 266 | for tarinfo in directories: |
| 267 | path = os.path.join(TEMPDIR, tarinfo.name) |
| 268 | self.assertEqual(tarinfo.mode & 0o777, os.stat(path).st_mode & 0o777) |
| 269 | self.assertEqual(tarinfo.mtime, os.path.getmtime(path)) |
| 270 | tar.close() |
| 271 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 272 | |
| 273 | class StreamReadTest(ReadTest): |
| 274 | |
| 275 | mode="r|" |
| 276 | |
| 277 | def test_fileobj_regular_file(self): |
| 278 | tarinfo = self.tar.next() # get "regtype" (can't use getmember) |
| 279 | fobj = self.tar.extractfile(tarinfo) |
| 280 | data = fobj.read() |
| 281 | self.assert_((len(data), md5sum(data)) == (tarinfo.size, md5_regtype), |
| 282 | "regular file extraction failed") |
| 283 | |
| 284 | def test_provoke_stream_error(self): |
| 285 | tarinfos = self.tar.getmembers() |
| 286 | f = self.tar.extractfile(tarinfos[0]) # read the first member |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 287 | self.assertRaises(tarfile.StreamError, f.read) |
| 288 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 289 | def test_compare_members(self): |
| 290 | tar1 = tarfile.open(tarname, encoding="iso8859-1") |
| 291 | tar2 = self.tar |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 293 | while True: |
| 294 | t1 = tar1.next() |
| 295 | t2 = tar2.next() |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 296 | if t1 is None: |
| 297 | break |
| 298 | self.assert_(t2 is not None, "stream.next() failed.") |
| 299 | |
| 300 | if t2.islnk() or t2.issym(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 301 | self.assertRaises(tarfile.StreamError, tar2.extractfile, t2) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 302 | continue |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 303 | |
| 304 | v1 = tar1.extractfile(t1) |
| 305 | v2 = tar2.extractfile(t2) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 306 | if v1 is None: |
| 307 | continue |
| 308 | self.assert_(v2 is not None, "stream.extractfile() failed") |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 309 | self.assertEqual(v1.read(), v2.read(), "stream extraction failed") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 310 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 311 | tar1.close() |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 312 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 314 | class DetectReadTest(unittest.TestCase): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 316 | def _testfunc_file(self, name, mode): |
| 317 | try: |
| 318 | tarfile.open(name, mode) |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 319 | except tarfile.ReadError as e: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 320 | self.fail() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 322 | def _testfunc_fileobj(self, name, mode): |
| 323 | try: |
| 324 | tarfile.open(name, mode, fileobj=open(name, "rb")) |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 325 | except tarfile.ReadError as e: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 326 | self.fail() |
| 327 | |
| 328 | def _test_modes(self, testfunc): |
| 329 | testfunc(tarname, "r") |
| 330 | testfunc(tarname, "r:") |
| 331 | testfunc(tarname, "r:*") |
| 332 | testfunc(tarname, "r|") |
| 333 | testfunc(tarname, "r|*") |
| 334 | |
| 335 | if gzip: |
| 336 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:gz") |
| 337 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|gz") |
| 338 | self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r:") |
| 339 | self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r|") |
| 340 | |
| 341 | testfunc(gzipname, "r") |
| 342 | testfunc(gzipname, "r:*") |
| 343 | testfunc(gzipname, "r:gz") |
| 344 | testfunc(gzipname, "r|*") |
| 345 | testfunc(gzipname, "r|gz") |
| 346 | |
| 347 | if bz2: |
| 348 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:bz2") |
| 349 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|bz2") |
| 350 | self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r:") |
| 351 | self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r|") |
| 352 | |
| 353 | testfunc(bz2name, "r") |
| 354 | testfunc(bz2name, "r:*") |
| 355 | testfunc(bz2name, "r:bz2") |
| 356 | testfunc(bz2name, "r|*") |
| 357 | testfunc(bz2name, "r|bz2") |
| 358 | |
| 359 | def test_detect_file(self): |
| 360 | self._test_modes(self._testfunc_file) |
| 361 | |
| 362 | def test_detect_fileobj(self): |
| 363 | self._test_modes(self._testfunc_fileobj) |
| 364 | |
| 365 | |
| 366 | class MemberReadTest(ReadTest): |
| 367 | |
| 368 | def _test_member(self, tarinfo, chksum=None, **kwargs): |
| 369 | if chksum is not None: |
| 370 | self.assert_(md5sum(self.tar.extractfile(tarinfo).read()) == chksum, |
| 371 | "wrong md5sum for %s" % tarinfo.name) |
| 372 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 373 | kwargs["mtime"] = 0o7606136617 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 374 | kwargs["uid"] = 1000 |
| 375 | kwargs["gid"] = 100 |
| 376 | if "old-v7" not in tarinfo.name: |
| 377 | # V7 tar can't handle alphabetic owners. |
| 378 | kwargs["uname"] = "tarfile" |
| 379 | kwargs["gname"] = "tarfile" |
| 380 | for k, v in kwargs.items(): |
| 381 | self.assert_(getattr(tarinfo, k) == v, |
| 382 | "wrong value in %s field of %s" % (k, tarinfo.name)) |
| 383 | |
| 384 | def test_find_regtype(self): |
| 385 | tarinfo = self.tar.getmember("ustar/regtype") |
| 386 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 387 | |
| 388 | def test_find_conttype(self): |
| 389 | tarinfo = self.tar.getmember("ustar/conttype") |
| 390 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 391 | |
| 392 | def test_find_dirtype(self): |
| 393 | tarinfo = self.tar.getmember("ustar/dirtype") |
| 394 | self._test_member(tarinfo, size=0) |
| 395 | |
| 396 | def test_find_dirtype_with_size(self): |
| 397 | tarinfo = self.tar.getmember("ustar/dirtype-with-size") |
| 398 | self._test_member(tarinfo, size=255) |
| 399 | |
| 400 | def test_find_lnktype(self): |
| 401 | tarinfo = self.tar.getmember("ustar/lnktype") |
| 402 | self._test_member(tarinfo, size=0, linkname="ustar/regtype") |
| 403 | |
| 404 | def test_find_symtype(self): |
| 405 | tarinfo = self.tar.getmember("ustar/symtype") |
| 406 | self._test_member(tarinfo, size=0, linkname="regtype") |
| 407 | |
| 408 | def test_find_blktype(self): |
| 409 | tarinfo = self.tar.getmember("ustar/blktype") |
| 410 | self._test_member(tarinfo, size=0, devmajor=3, devminor=0) |
| 411 | |
| 412 | def test_find_chrtype(self): |
| 413 | tarinfo = self.tar.getmember("ustar/chrtype") |
| 414 | self._test_member(tarinfo, size=0, devmajor=1, devminor=3) |
| 415 | |
| 416 | def test_find_fifotype(self): |
| 417 | tarinfo = self.tar.getmember("ustar/fifotype") |
| 418 | self._test_member(tarinfo, size=0) |
| 419 | |
| 420 | def test_find_sparse(self): |
| 421 | tarinfo = self.tar.getmember("ustar/sparse") |
| 422 | self._test_member(tarinfo, size=86016, chksum=md5_sparse) |
| 423 | |
| 424 | def test_find_umlauts(self): |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 425 | tarinfo = self.tar.getmember("ustar/umlauts-\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 426 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 427 | |
| 428 | def test_find_ustar_longname(self): |
| 429 | name = "ustar/" + "12345/" * 39 + "1234567/longname" |
| 430 | self.assert_(name in self.tar.getnames()) |
| 431 | |
| 432 | def test_find_regtype_oldv7(self): |
| 433 | tarinfo = self.tar.getmember("misc/regtype-old-v7") |
| 434 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 435 | |
| 436 | def test_find_pax_umlauts(self): |
| 437 | self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1") |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 438 | tarinfo = self.tar.getmember("pax/umlauts-\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 439 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 440 | |
| 441 | |
| 442 | class LongnameTest(ReadTest): |
| 443 | |
| 444 | def test_read_longname(self): |
| 445 | # Test reading of longname (bug #1471427). |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 446 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 447 | try: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 448 | tarinfo = self.tar.getmember(longname) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 449 | except KeyError: |
| 450 | self.fail("longname not found") |
| 451 | self.assert_(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") |
| 452 | |
| 453 | def test_read_longlink(self): |
| 454 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
| 455 | longlink = self.subdir + "/" + "123/" * 125 + "longlink" |
| 456 | try: |
| 457 | tarinfo = self.tar.getmember(longlink) |
| 458 | except KeyError: |
| 459 | self.fail("longlink not found") |
| 460 | self.assert_(tarinfo.linkname == longname, "linkname wrong") |
| 461 | |
| 462 | def test_truncated_longname(self): |
| 463 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
| 464 | tarinfo = self.tar.getmember(longname) |
| 465 | offset = tarinfo.offset |
| 466 | self.tar.fileobj.seek(offset) |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 467 | fobj = io.BytesIO(self.tar.fileobj.read(3 * 512)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 468 | self.assertRaises(tarfile.ReadError, tarfile.open, name="foo.tar", fileobj=fobj) |
| 469 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 470 | def test_header_offset(self): |
| 471 | # Test if the start offset of the TarInfo object includes |
| 472 | # the preceding extended header. |
| 473 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
| 474 | offset = self.tar.getmember(longname).offset |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 475 | fobj = open(tarname, "rb") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 476 | fobj.seek(offset) |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 477 | tarinfo = tarfile.TarInfo.frombuf(fobj.read(512), "iso8859-1", "strict") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 478 | self.assertEqual(tarinfo.type, self.longnametype) |
| 479 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 480 | |
| 481 | class GNUReadTest(LongnameTest): |
| 482 | |
| 483 | subdir = "gnu" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 484 | longnametype = tarfile.GNUTYPE_LONGNAME |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 485 | |
| 486 | def test_sparse_file(self): |
| 487 | tarinfo1 = self.tar.getmember("ustar/sparse") |
| 488 | fobj1 = self.tar.extractfile(tarinfo1) |
| 489 | tarinfo2 = self.tar.getmember("gnu/sparse") |
| 490 | fobj2 = self.tar.extractfile(tarinfo2) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 491 | self.assertEqual(fobj1.read(), fobj2.read(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 492 | "sparse file extraction failed") |
| 493 | |
| 494 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 495 | class PaxReadTest(LongnameTest): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 496 | |
| 497 | subdir = "pax" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 498 | longnametype = tarfile.XHDTYPE |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 499 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 500 | def test_pax_global_headers(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 501 | tar = tarfile.open(tarname, encoding="iso8859-1") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 502 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 503 | tarinfo = tar.getmember("pax/regtype1") |
| 504 | self.assertEqual(tarinfo.uname, "foo") |
| 505 | self.assertEqual(tarinfo.gname, "bar") |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 506 | self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), "\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 507 | |
| 508 | tarinfo = tar.getmember("pax/regtype2") |
| 509 | self.assertEqual(tarinfo.uname, "") |
| 510 | self.assertEqual(tarinfo.gname, "bar") |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 511 | self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), "\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 512 | |
| 513 | tarinfo = tar.getmember("pax/regtype3") |
| 514 | self.assertEqual(tarinfo.uname, "tarfile") |
| 515 | self.assertEqual(tarinfo.gname, "tarfile") |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 516 | self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), "\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 517 | |
| 518 | def test_pax_number_fields(self): |
| 519 | # All following number fields are read from the pax header. |
| 520 | tar = tarfile.open(tarname, encoding="iso8859-1") |
| 521 | tarinfo = tar.getmember("pax/regtype4") |
| 522 | self.assertEqual(tarinfo.size, 7011) |
| 523 | self.assertEqual(tarinfo.uid, 123) |
| 524 | self.assertEqual(tarinfo.gid, 123) |
| 525 | self.assertEqual(tarinfo.mtime, 1041808783.0) |
| 526 | self.assertEqual(type(tarinfo.mtime), float) |
| 527 | self.assertEqual(float(tarinfo.pax_headers["atime"]), 1041808783.0) |
| 528 | self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 529 | |
| 530 | |
| 531 | class WriteTest(unittest.TestCase): |
| 532 | |
| 533 | mode = "w:" |
| 534 | |
| 535 | def test_100_char_name(self): |
| 536 | # The name field in a tar header stores strings of at most 100 chars. |
| 537 | # If a string is shorter than 100 chars it has to be padded with '\0', |
| 538 | # which implies that a string of exactly 100 chars is stored without |
| 539 | # a trailing '\0'. |
| 540 | name = "0123456789" * 10 |
| 541 | tar = tarfile.open(tmpname, self.mode) |
| 542 | t = tarfile.TarInfo(name) |
| 543 | tar.addfile(t) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 544 | tar.close() |
| 545 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 546 | tar = tarfile.open(tmpname) |
| 547 | self.assert_(tar.getnames()[0] == name, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 548 | "failed to store 100 char filename") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 549 | tar.close() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 550 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 551 | def test_tar_size(self): |
| 552 | # Test for bug #1013882. |
| 553 | tar = tarfile.open(tmpname, self.mode) |
| 554 | path = os.path.join(TEMPDIR, "file") |
| 555 | fobj = open(path, "wb") |
Lars Gustäbel | a280ca75 | 2007-08-28 07:34:33 +0000 | [diff] [blame] | 556 | fobj.write(b"aaa") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 557 | fobj.close() |
| 558 | tar.add(path) |
| 559 | tar.close() |
| 560 | self.assert_(os.path.getsize(tmpname) > 0, |
| 561 | "tarfile is empty") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 562 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 563 | # The test_*_size tests test for bug #1167128. |
| 564 | def test_file_size(self): |
| 565 | tar = tarfile.open(tmpname, self.mode) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 566 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 567 | path = os.path.join(TEMPDIR, "file") |
| 568 | fobj = open(path, "wb") |
| 569 | fobj.close() |
| 570 | tarinfo = tar.gettarinfo(path) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 571 | self.assertEqual(tarinfo.size, 0) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 572 | |
| 573 | fobj = open(path, "wb") |
Lars Gustäbel | a280ca75 | 2007-08-28 07:34:33 +0000 | [diff] [blame] | 574 | fobj.write(b"aaa") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 575 | fobj.close() |
| 576 | tarinfo = tar.gettarinfo(path) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 577 | self.assertEqual(tarinfo.size, 3) |
| 578 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 579 | tar.close() |
| 580 | |
| 581 | def test_directory_size(self): |
| 582 | path = os.path.join(TEMPDIR, "directory") |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 583 | os.mkdir(path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 584 | try: |
| 585 | tar = tarfile.open(tmpname, self.mode) |
| 586 | tarinfo = tar.gettarinfo(path) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 587 | self.assertEqual(tarinfo.size, 0) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 588 | finally: |
| 589 | os.rmdir(path) |
| 590 | |
| 591 | def test_link_size(self): |
| 592 | if hasattr(os, "link"): |
| 593 | link = os.path.join(TEMPDIR, "link") |
| 594 | target = os.path.join(TEMPDIR, "link_target") |
| 595 | open(target, "wb").close() |
| 596 | os.link(target, link) |
| 597 | try: |
| 598 | tar = tarfile.open(tmpname, self.mode) |
| 599 | tarinfo = tar.gettarinfo(link) |
| 600 | self.assertEqual(tarinfo.size, 0) |
| 601 | finally: |
| 602 | os.remove(target) |
| 603 | os.remove(link) |
| 604 | |
| 605 | def test_symlink_size(self): |
| 606 | if hasattr(os, "symlink"): |
| 607 | path = os.path.join(TEMPDIR, "symlink") |
| 608 | os.symlink("link_target", path) |
| 609 | try: |
| 610 | tar = tarfile.open(tmpname, self.mode) |
| 611 | tarinfo = tar.gettarinfo(path) |
| 612 | self.assertEqual(tarinfo.size, 0) |
| 613 | finally: |
| 614 | os.remove(path) |
| 615 | |
| 616 | def test_add_self(self): |
| 617 | # Test for #1257255. |
| 618 | dstname = os.path.abspath(tmpname) |
| 619 | |
| 620 | tar = tarfile.open(tmpname, self.mode) |
| 621 | self.assert_(tar.name == dstname, "archive name must be absolute") |
| 622 | |
| 623 | tar.add(dstname) |
| 624 | self.assert_(tar.getnames() == [], "added the archive to itself") |
| 625 | |
| 626 | cwd = os.getcwd() |
| 627 | os.chdir(TEMPDIR) |
| 628 | tar.add(dstname) |
| 629 | os.chdir(cwd) |
| 630 | self.assert_(tar.getnames() == [], "added the archive to itself") |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 632 | def test_exclude(self): |
| 633 | tempdir = os.path.join(TEMPDIR, "exclude") |
| 634 | os.mkdir(tempdir) |
| 635 | try: |
| 636 | for name in ("foo", "bar", "baz"): |
| 637 | name = os.path.join(tempdir, name) |
| 638 | open(name, "wb").close() |
| 639 | |
| 640 | def exclude(name): |
| 641 | return os.path.isfile(name) |
| 642 | |
| 643 | tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1") |
| 644 | tar.add(tempdir, arcname="empty_dir", exclude=exclude) |
| 645 | tar.close() |
| 646 | |
| 647 | tar = tarfile.open(tmpname, "r") |
| 648 | self.assertEqual(len(tar.getmembers()), 1) |
| 649 | self.assertEqual(tar.getnames()[0], "empty_dir") |
| 650 | finally: |
| 651 | shutil.rmtree(tempdir) |
| 652 | |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 653 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 654 | class StreamWriteTest(unittest.TestCase): |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 655 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 656 | mode = "w|" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 657 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 658 | def test_stream_padding(self): |
| 659 | # Test for bug #1543303. |
| 660 | tar = tarfile.open(tmpname, self.mode) |
| 661 | tar.close() |
| 662 | |
| 663 | if self.mode.endswith("gz"): |
| 664 | fobj = gzip.GzipFile(tmpname) |
| 665 | data = fobj.read() |
| 666 | fobj.close() |
| 667 | elif self.mode.endswith("bz2"): |
| 668 | dec = bz2.BZ2Decompressor() |
| 669 | data = open(tmpname, "rb").read() |
| 670 | data = dec.decompress(data) |
| 671 | self.assert_(len(dec.unused_data) == 0, |
| 672 | "found trailing data") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 673 | else: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 674 | fobj = open(tmpname, "rb") |
| 675 | data = fobj.read() |
| 676 | fobj.close() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 677 | |
Lars Gustäbel | a280ca75 | 2007-08-28 07:34:33 +0000 | [diff] [blame] | 678 | self.assert_(data.count(b"\0") == tarfile.RECORDSIZE, |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 679 | "incorrect zero padding") |
| 680 | |
| 681 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 682 | class GNUWriteTest(unittest.TestCase): |
| 683 | # This testcase checks for correct creation of GNU Longname |
| 684 | # and Longlink extended headers (cp. bug #812325). |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 685 | |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 686 | def _length(self, s): |
| 687 | blocks, remainder = divmod(len(s) + 1, 512) |
| 688 | if remainder: |
| 689 | blocks += 1 |
| 690 | return blocks * 512 |
| 691 | |
| 692 | def _calc_size(self, name, link=None): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 693 | # Initial tar header |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 694 | count = 512 |
| 695 | |
| 696 | if len(name) > tarfile.LENGTH_NAME: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 697 | # GNU longname extended header + longname |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 698 | count += 512 |
| 699 | count += self._length(name) |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 700 | if link is not None and len(link) > tarfile.LENGTH_LINK: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 701 | # GNU longlink extended header + longlink |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 702 | count += 512 |
| 703 | count += self._length(link) |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 704 | return count |
| 705 | |
| 706 | def _test(self, name, link=None): |
| 707 | tarinfo = tarfile.TarInfo(name) |
| 708 | if link: |
| 709 | tarinfo.linkname = link |
| 710 | tarinfo.type = tarfile.LNKTYPE |
| 711 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 712 | tar = tarfile.open(tmpname, "w") |
| 713 | tar.format = tarfile.GNU_FORMAT |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 714 | tar.addfile(tarinfo) |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 715 | |
| 716 | v1 = self._calc_size(name, link) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 717 | v2 = tar.offset |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 718 | self.assert_(v1 == v2, "GNU longname/longlink creation failed") |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 719 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 720 | tar.close() |
| 721 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 722 | tar = tarfile.open(tmpname) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 723 | member = tar.next() |
| 724 | self.failIf(member is None, "unable to read longname member") |
| 725 | self.assert_(tarinfo.name == member.name and \ |
| 726 | tarinfo.linkname == member.linkname, \ |
| 727 | "unable to read longname member") |
| 728 | |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 729 | def test_longname_1023(self): |
| 730 | self._test(("longnam/" * 127) + "longnam") |
| 731 | |
| 732 | def test_longname_1024(self): |
| 733 | self._test(("longnam/" * 127) + "longname") |
| 734 | |
| 735 | def test_longname_1025(self): |
| 736 | self._test(("longnam/" * 127) + "longname_") |
| 737 | |
| 738 | def test_longlink_1023(self): |
| 739 | self._test("name", ("longlnk/" * 127) + "longlnk") |
| 740 | |
| 741 | def test_longlink_1024(self): |
| 742 | self._test("name", ("longlnk/" * 127) + "longlink") |
| 743 | |
| 744 | def test_longlink_1025(self): |
| 745 | self._test("name", ("longlnk/" * 127) + "longlink_") |
| 746 | |
| 747 | def test_longnamelink_1023(self): |
| 748 | self._test(("longnam/" * 127) + "longnam", |
| 749 | ("longlnk/" * 127) + "longlnk") |
| 750 | |
| 751 | def test_longnamelink_1024(self): |
| 752 | self._test(("longnam/" * 127) + "longname", |
| 753 | ("longlnk/" * 127) + "longlink") |
| 754 | |
| 755 | def test_longnamelink_1025(self): |
| 756 | self._test(("longnam/" * 127) + "longname_", |
| 757 | ("longlnk/" * 127) + "longlink_") |
| 758 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 759 | |
| 760 | class HardlinkTest(unittest.TestCase): |
| 761 | # Test the creation of LNKTYPE (hardlink) members in an archive. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 762 | |
| 763 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 764 | self.foo = os.path.join(TEMPDIR, "foo") |
| 765 | self.bar = os.path.join(TEMPDIR, "bar") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 766 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 767 | fobj = open(self.foo, "wb") |
Lars Gustäbel | a280ca75 | 2007-08-28 07:34:33 +0000 | [diff] [blame] | 768 | fobj.write(b"foo") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 769 | fobj.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 770 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 771 | os.link(self.foo, self.bar) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 772 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 773 | self.tar = tarfile.open(tmpname, "w") |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 774 | self.tar.add(self.foo) |
| 775 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 776 | def tearDown(self): |
| 777 | os.remove(self.foo) |
| 778 | os.remove(self.bar) |
| 779 | |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 780 | def test_add_twice(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 781 | # The same name will be added as a REGTYPE every |
| 782 | # time regardless of st_nlink. |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 783 | tarinfo = self.tar.gettarinfo(self.foo) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 784 | self.assert_(tarinfo.type == tarfile.REGTYPE, |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 785 | "add file as regular failed") |
| 786 | |
| 787 | def test_add_hardlink(self): |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 788 | tarinfo = self.tar.gettarinfo(self.bar) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 789 | self.assert_(tarinfo.type == tarfile.LNKTYPE, |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 790 | "add file as hardlink failed") |
| 791 | |
| 792 | def test_dereference_hardlink(self): |
| 793 | self.tar.dereference = True |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 794 | tarinfo = self.tar.gettarinfo(self.bar) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 795 | self.assert_(tarinfo.type == tarfile.REGTYPE, |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 796 | "dereferencing hardlink failed") |
| 797 | |
Neal Norwitz | a4f651a | 2004-07-20 22:07:44 +0000 | [diff] [blame] | 798 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 799 | class PaxWriteTest(GNUWriteTest): |
Martin v. Löwis | 78be7df | 2005-03-05 12:47:42 +0000 | [diff] [blame] | 800 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 801 | def _test(self, name, link=None): |
| 802 | # See GNUWriteTest. |
| 803 | tarinfo = tarfile.TarInfo(name) |
| 804 | if link: |
| 805 | tarinfo.linkname = link |
| 806 | tarinfo.type = tarfile.LNKTYPE |
Andrew M. Kuchling | d4f2552 | 2004-10-20 11:47:01 +0000 | [diff] [blame] | 807 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 808 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT) |
| 809 | tar.addfile(tarinfo) |
| 810 | tar.close() |
Andrew M. Kuchling | d4f2552 | 2004-10-20 11:47:01 +0000 | [diff] [blame] | 811 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 812 | tar = tarfile.open(tmpname) |
| 813 | if link: |
| 814 | l = tar.getmembers()[0].linkname |
| 815 | self.assert_(link == l, "PAX longlink creation failed") |
| 816 | else: |
| 817 | n = tar.getmembers()[0].name |
| 818 | self.assert_(name == n, "PAX longname creation failed") |
| 819 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 820 | def test_pax_global_header(self): |
| 821 | pax_headers = { |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 822 | "foo": "bar", |
| 823 | "uid": "0", |
| 824 | "mtime": "1.23", |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 825 | "test": "\xe4\xf6\xfc", |
| 826 | "\xe4\xf6\xfc": "test"} |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 827 | |
| 828 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, \ |
| 829 | pax_headers=pax_headers) |
| 830 | tar.addfile(tarfile.TarInfo("test")) |
| 831 | tar.close() |
| 832 | |
| 833 | # Test if the global header was written correctly. |
| 834 | tar = tarfile.open(tmpname, encoding="iso8859-1") |
| 835 | self.assertEqual(tar.pax_headers, pax_headers) |
| 836 | self.assertEqual(tar.getmembers()[0].pax_headers, pax_headers) |
| 837 | |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 838 | # Test if all the fields are strings. |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 839 | for key, val in tar.pax_headers.items(): |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 840 | self.assert_(type(key) is not bytes) |
| 841 | self.assert_(type(val) is not bytes) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 842 | if key in tarfile.PAX_NUMBER_FIELDS: |
| 843 | try: |
| 844 | tarfile.PAX_NUMBER_FIELDS[key](val) |
| 845 | except (TypeError, ValueError): |
| 846 | self.fail("unable to convert pax header field") |
| 847 | |
| 848 | def test_pax_extended_header(self): |
| 849 | # The fields from the pax header have priority over the |
| 850 | # TarInfo. |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 851 | pax_headers = {"path": "foo", "uid": "123"} |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 852 | |
| 853 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1") |
| 854 | t = tarfile.TarInfo() |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 855 | t.name = "\xe4\xf6\xfc" # non-ASCII |
| 856 | t.uid = 8**8 # too large |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 857 | t.pax_headers = pax_headers |
| 858 | tar.addfile(t) |
| 859 | tar.close() |
| 860 | |
| 861 | tar = tarfile.open(tmpname, encoding="iso8859-1") |
| 862 | t = tar.getmembers()[0] |
| 863 | self.assertEqual(t.pax_headers, pax_headers) |
| 864 | self.assertEqual(t.name, "foo") |
| 865 | self.assertEqual(t.uid, 123) |
| 866 | |
| 867 | |
| 868 | class UstarUnicodeTest(unittest.TestCase): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 869 | |
| 870 | format = tarfile.USTAR_FORMAT |
| 871 | |
| 872 | def test_iso8859_1_filename(self): |
| 873 | self._test_unicode_filename("iso8859-1") |
| 874 | |
| 875 | def test_utf7_filename(self): |
| 876 | self._test_unicode_filename("utf7") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 877 | |
| 878 | def test_utf8_filename(self): |
| 879 | self._test_unicode_filename("utf8") |
| 880 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 881 | def _test_unicode_filename(self, encoding): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 882 | tar = tarfile.open(tmpname, "w", format=self.format, encoding=encoding, errors="strict") |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 883 | name = "\xe4\xf6\xfc" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 884 | tar.addfile(tarfile.TarInfo(name)) |
| 885 | tar.close() |
| 886 | |
| 887 | tar = tarfile.open(tmpname, encoding=encoding) |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 888 | self.assertEqual(tar.getmembers()[0].name, name) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 889 | tar.close() |
| 890 | |
| 891 | def test_unicode_filename_error(self): |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 892 | if self.format == tarfile.PAX_FORMAT: |
| 893 | # PAX_FORMAT ignores encoding in write mode. |
| 894 | return |
| 895 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 896 | tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict") |
| 897 | tarinfo = tarfile.TarInfo() |
| 898 | |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 899 | tarinfo.name = "\xe4\xf6\xfc" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 900 | self.assertRaises(UnicodeError, tar.addfile, tarinfo) |
| 901 | |
| 902 | tarinfo.name = "foo" |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 903 | tarinfo.uname = "\xe4\xf6\xfc" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 904 | self.assertRaises(UnicodeError, tar.addfile, tarinfo) |
| 905 | |
| 906 | def test_unicode_argument(self): |
| 907 | tar = tarfile.open(tarname, "r", encoding="iso8859-1", errors="strict") |
| 908 | for t in tar: |
| 909 | self.assert_(type(t.name) is str) |
| 910 | self.assert_(type(t.linkname) is str) |
| 911 | self.assert_(type(t.uname) is str) |
| 912 | self.assert_(type(t.gname) is str) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 913 | tar.close() |
| 914 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 915 | def test_uname_unicode(self): |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 916 | t = tarfile.TarInfo("foo") |
| 917 | t.uname = "\xe4\xf6\xfc" |
| 918 | t.gname = "\xe4\xf6\xfc" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 919 | |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 920 | tar = tarfile.open(tmpname, mode="w", format=self.format, encoding="iso8859-1") |
| 921 | tar.addfile(t) |
| 922 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 923 | |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 924 | tar = tarfile.open(tmpname, encoding="iso8859-1") |
| 925 | t = tar.getmember("foo") |
| 926 | self.assertEqual(t.uname, "\xe4\xf6\xfc") |
| 927 | self.assertEqual(t.gname, "\xe4\xf6\xfc") |
| 928 | |
| 929 | if self.format != tarfile.PAX_FORMAT: |
| 930 | tar = tarfile.open(tmpname, encoding="ascii") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 931 | t = tar.getmember("foo") |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 932 | self.assertEqual(t.uname, "\ufffd\ufffd\ufffd") |
| 933 | self.assertEqual(t.gname, "\ufffd\ufffd\ufffd") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 934 | |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 935 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 936 | class GNUUnicodeTest(UstarUnicodeTest): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 937 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 938 | format = tarfile.GNU_FORMAT |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 939 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 940 | |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 941 | class PAXUnicodeTest(UstarUnicodeTest): |
| 942 | |
| 943 | format = tarfile.PAX_FORMAT |
| 944 | |
| 945 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 946 | class AppendTest(unittest.TestCase): |
| 947 | # Test append mode (cp. patch #1652681). |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 948 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 949 | def setUp(self): |
| 950 | self.tarname = tmpname |
| 951 | if os.path.exists(self.tarname): |
| 952 | os.remove(self.tarname) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 953 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 954 | def _add_testfile(self, fileobj=None): |
| 955 | tar = tarfile.open(self.tarname, "a", fileobj=fileobj) |
| 956 | tar.addfile(tarfile.TarInfo("bar")) |
| 957 | tar.close() |
Tim Peters | 8ceefc5 | 2004-10-25 03:19:41 +0000 | [diff] [blame] | 958 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 959 | def _create_testtar(self, mode="w:"): |
| 960 | src = tarfile.open(tarname, encoding="iso8859-1") |
| 961 | t = src.getmember("ustar/regtype") |
| 962 | t.name = "foo" |
| 963 | f = src.extractfile(t) |
| 964 | tar = tarfile.open(self.tarname, mode) |
| 965 | tar.addfile(t, f) |
| 966 | tar.close() |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 967 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 968 | def _test(self, names=["bar"], fileobj=None): |
| 969 | tar = tarfile.open(self.tarname, fileobj=fileobj) |
| 970 | self.assertEqual(tar.getnames(), names) |
| 971 | |
| 972 | def test_non_existing(self): |
| 973 | self._add_testfile() |
| 974 | self._test() |
| 975 | |
| 976 | def test_empty(self): |
| 977 | open(self.tarname, "w").close() |
| 978 | self._add_testfile() |
| 979 | self._test() |
| 980 | |
| 981 | def test_empty_fileobj(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 982 | fobj = io.BytesIO() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 983 | self._add_testfile(fobj) |
| 984 | fobj.seek(0) |
| 985 | self._test(fileobj=fobj) |
| 986 | |
| 987 | def test_fileobj(self): |
| 988 | self._create_testtar() |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 989 | data = open(self.tarname, "rb").read() |
| 990 | fobj = io.BytesIO(data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 991 | self._add_testfile(fobj) |
| 992 | fobj.seek(0) |
| 993 | self._test(names=["foo", "bar"], fileobj=fobj) |
| 994 | |
| 995 | def test_existing(self): |
| 996 | self._create_testtar() |
| 997 | self._add_testfile() |
| 998 | self._test(names=["foo", "bar"]) |
| 999 | |
| 1000 | def test_append_gz(self): |
| 1001 | if gzip is None: |
| 1002 | return |
| 1003 | self._create_testtar("w:gz") |
| 1004 | self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a") |
| 1005 | |
| 1006 | def test_append_bz2(self): |
| 1007 | if bz2 is None: |
| 1008 | return |
| 1009 | self._create_testtar("w:bz2") |
| 1010 | self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a") |
| 1011 | |
| 1012 | |
| 1013 | class LimitsTest(unittest.TestCase): |
| 1014 | |
| 1015 | def test_ustar_limits(self): |
| 1016 | # 100 char name |
| 1017 | tarinfo = tarfile.TarInfo("0123456789" * 10) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1018 | tarinfo.tobuf(tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1019 | |
| 1020 | # 101 char name that cannot be stored |
| 1021 | tarinfo = tarfile.TarInfo("0123456789" * 10 + "0") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1022 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1023 | |
| 1024 | # 256 char name with a slash at pos 156 |
| 1025 | tarinfo = tarfile.TarInfo("123/" * 62 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1026 | tarinfo.tobuf(tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1027 | |
| 1028 | # 256 char name that cannot be stored |
| 1029 | tarinfo = tarfile.TarInfo("1234567/" * 31 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1030 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1031 | |
| 1032 | # 512 char name |
| 1033 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1034 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1035 | |
| 1036 | # 512 char linkname |
| 1037 | tarinfo = tarfile.TarInfo("longlink") |
| 1038 | tarinfo.linkname = "123/" * 126 + "longname" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1039 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1040 | |
| 1041 | # uid > 8 digits |
| 1042 | tarinfo = tarfile.TarInfo("name") |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1043 | tarinfo.uid = 0o10000000 |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1044 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1045 | |
| 1046 | def test_gnu_limits(self): |
| 1047 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1048 | tarinfo.tobuf(tarfile.GNU_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1049 | |
| 1050 | tarinfo = tarfile.TarInfo("longlink") |
| 1051 | tarinfo.linkname = "123/" * 126 + "longname" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1052 | tarinfo.tobuf(tarfile.GNU_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1053 | |
| 1054 | # uid >= 256 ** 7 |
| 1055 | tarinfo = tarfile.TarInfo("name") |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1056 | tarinfo.uid = 0o4000000000000000000 |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1057 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.GNU_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1058 | |
| 1059 | def test_pax_limits(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1060 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1061 | tarinfo.tobuf(tarfile.PAX_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1062 | |
| 1063 | tarinfo = tarfile.TarInfo("longlink") |
| 1064 | tarinfo.linkname = "123/" * 126 + "longname" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1065 | tarinfo.tobuf(tarfile.PAX_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1066 | |
| 1067 | tarinfo = tarfile.TarInfo("name") |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1068 | tarinfo.uid = 0o4000000000000000000 |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1069 | tarinfo.tobuf(tarfile.PAX_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1070 | |
| 1071 | |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 1072 | class MiscTest(unittest.TestCase): |
| 1073 | |
| 1074 | def test_char_fields(self): |
| 1075 | self.assertEqual(tarfile.stn("foo", 8, "ascii", "strict"), b"foo\0\0\0\0\0") |
| 1076 | self.assertEqual(tarfile.stn("foobar", 3, "ascii", "strict"), b"foo") |
| 1077 | self.assertEqual(tarfile.nts(b"foo\0\0\0\0\0", "ascii", "strict"), "foo") |
| 1078 | self.assertEqual(tarfile.nts(b"foo\0bar\0", "ascii", "strict"), "foo") |
| 1079 | |
| 1080 | def test_number_fields(self): |
| 1081 | self.assertEqual(tarfile.itn(1), b"0000001\x00") |
| 1082 | self.assertEqual(tarfile.itn(0xffffffff), b"\x80\x00\x00\x00\xff\xff\xff\xff") |
| 1083 | |
| 1084 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1085 | class GzipMiscReadTest(MiscReadTest): |
| 1086 | tarname = gzipname |
| 1087 | mode = "r:gz" |
| 1088 | class GzipUstarReadTest(UstarReadTest): |
| 1089 | tarname = gzipname |
| 1090 | mode = "r:gz" |
| 1091 | class GzipStreamReadTest(StreamReadTest): |
| 1092 | tarname = gzipname |
| 1093 | mode = "r|gz" |
| 1094 | class GzipWriteTest(WriteTest): |
| 1095 | mode = "w:gz" |
| 1096 | class GzipStreamWriteTest(StreamWriteTest): |
| 1097 | mode = "w|gz" |
| 1098 | |
| 1099 | |
| 1100 | class Bz2MiscReadTest(MiscReadTest): |
| 1101 | tarname = bz2name |
| 1102 | mode = "r:bz2" |
| 1103 | class Bz2UstarReadTest(UstarReadTest): |
| 1104 | tarname = bz2name |
| 1105 | mode = "r:bz2" |
| 1106 | class Bz2StreamReadTest(StreamReadTest): |
| 1107 | tarname = bz2name |
| 1108 | mode = "r|bz2" |
| 1109 | class Bz2WriteTest(WriteTest): |
| 1110 | mode = "w:bz2" |
| 1111 | class Bz2StreamWriteTest(StreamWriteTest): |
| 1112 | mode = "w|bz2" |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1113 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 1114 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1115 | if not os.path.exists(TEMPDIR): |
| 1116 | os.mkdir(TEMPDIR) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1117 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1118 | tests = [ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1119 | UstarReadTest, |
| 1120 | MiscReadTest, |
| 1121 | StreamReadTest, |
| 1122 | DetectReadTest, |
| 1123 | MemberReadTest, |
| 1124 | GNUReadTest, |
| 1125 | PaxReadTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1126 | WriteTest, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1127 | StreamWriteTest, |
| 1128 | GNUWriteTest, |
| 1129 | PaxWriteTest, |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1130 | UstarUnicodeTest, |
| 1131 | GNUUnicodeTest, |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 1132 | PAXUnicodeTest, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1133 | AppendTest, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1134 | LimitsTest, |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 1135 | MiscTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1136 | ] |
| 1137 | |
Neal Norwitz | a4f651a | 2004-07-20 22:07:44 +0000 | [diff] [blame] | 1138 | if hasattr(os, "link"): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1139 | tests.append(HardlinkTest) |
| 1140 | |
| 1141 | fobj = open(tarname, "rb") |
| 1142 | data = fobj.read() |
| 1143 | fobj.close() |
Neal Norwitz | a4f651a | 2004-07-20 22:07:44 +0000 | [diff] [blame] | 1144 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1145 | if gzip: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1146 | # Create testtar.tar.gz and add gzip-specific tests. |
| 1147 | tar = gzip.open(gzipname, "wb") |
| 1148 | tar.write(data) |
| 1149 | tar.close() |
| 1150 | |
| 1151 | tests += [ |
| 1152 | GzipMiscReadTest, |
| 1153 | GzipUstarReadTest, |
| 1154 | GzipStreamReadTest, |
| 1155 | GzipWriteTest, |
| 1156 | GzipStreamWriteTest, |
| 1157 | ] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 1158 | |
| 1159 | if bz2: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1160 | # Create testtar.tar.bz2 and add bz2-specific tests. |
| 1161 | tar = bz2.BZ2File(bz2name, "wb") |
| 1162 | tar.write(data) |
| 1163 | tar.close() |
| 1164 | |
| 1165 | tests += [ |
| 1166 | Bz2MiscReadTest, |
| 1167 | Bz2UstarReadTest, |
| 1168 | Bz2StreamReadTest, |
| 1169 | Bz2WriteTest, |
| 1170 | Bz2StreamWriteTest, |
| 1171 | ] |
| 1172 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1173 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1174 | support.run_unittest(*tests) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1175 | finally: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1176 | if os.path.exists(TEMPDIR): |
| 1177 | shutil.rmtree(TEMPDIR) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1178 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 1179 | if __name__ == "__main__": |
| 1180 | test_main() |