Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1 | # encoding: iso8859-1 |
| 2 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 3 | import sys |
| 4 | import os |
| 5 | import shutil |
Brett Cannon | 455ea53 | 2003-06-12 08:01:06 +0000 | [diff] [blame] | 6 | import tempfile |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | import StringIO |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame^] | 8 | from hashlib import md5 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 9 | import errno |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 10 | |
| 11 | import unittest |
| 12 | import tarfile |
| 13 | |
| 14 | from test import test_support |
| 15 | |
| 16 | # Check for our compression modules. |
| 17 | try: |
| 18 | import gzip |
Neal Norwitz | ae32319 | 2003-04-14 01:18:32 +0000 | [diff] [blame] | 19 | gzip.GzipFile |
| 20 | except (ImportError, AttributeError): |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 21 | gzip = None |
| 22 | try: |
| 23 | import bz2 |
| 24 | except ImportError: |
| 25 | bz2 = None |
| 26 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 27 | def md5sum(data): |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame^] | 28 | return md5(data).hexdigest() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 29 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 30 | def path(path): |
| 31 | return test_support.findfile(path) |
| 32 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 33 | TEMPDIR = os.path.join(tempfile.gettempdir(), "test_tarfile_tmp") |
| 34 | tarname = path("testtar.tar") |
| 35 | gzipname = os.path.join(TEMPDIR, "testtar.tar.gz") |
| 36 | bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2") |
| 37 | tmpname = os.path.join(TEMPDIR, "tmp.tar") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 39 | md5_regtype = "65f477c818ad9e15f7feab0c6d37742f" |
| 40 | md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6" |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 41 | |
| 42 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 43 | class ReadTest(unittest.TestCase): |
| 44 | |
| 45 | tarname = tarname |
| 46 | mode = "r:" |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 47 | |
| 48 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 49 | self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 50 | |
| 51 | def tearDown(self): |
| 52 | self.tar.close() |
| 53 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 55 | class UstarReadTest(ReadTest): |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 57 | def test_fileobj_regular_file(self): |
| 58 | tarinfo = self.tar.getmember("ustar/regtype") |
| 59 | fobj = self.tar.extractfile(tarinfo) |
| 60 | data = fobj.read() |
| 61 | self.assert_((len(data), md5sum(data)) == (tarinfo.size, md5_regtype), |
| 62 | "regular file extraction failed") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 64 | def test_fileobj_readlines(self): |
| 65 | self.tar.extract("ustar/regtype", TEMPDIR) |
| 66 | tarinfo = self.tar.getmember("ustar/regtype") |
| 67 | fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "rU") |
| 68 | fobj2 = self.tar.extractfile(tarinfo) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 70 | lines1 = fobj1.readlines() |
| 71 | lines2 = fobj2.readlines() |
| 72 | self.assert_(lines1 == lines2, |
| 73 | "fileobj.readlines() failed") |
| 74 | self.assert_(len(lines2) == 114, |
| 75 | "fileobj.readlines() failed") |
| 76 | self.assert_(lines2[83] == \ |
| 77 | "I will gladly admit that Python is not the fastest running scripting language.\n", |
| 78 | "fileobj.readlines() failed") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 80 | def test_fileobj_iter(self): |
| 81 | self.tar.extract("ustar/regtype", TEMPDIR) |
| 82 | tarinfo = self.tar.getmember("ustar/regtype") |
| 83 | fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "rU") |
| 84 | fobj2 = self.tar.extractfile(tarinfo) |
| 85 | lines1 = fobj1.readlines() |
| 86 | lines2 = [line for line in fobj2] |
| 87 | self.assert_(lines1 == lines2, |
| 88 | "fileobj.__iter__() failed") |
Martin v. Löwis | df24153 | 2005-03-03 08:17:42 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 90 | def test_fileobj_seek(self): |
| 91 | self.tar.extract("ustar/regtype", TEMPDIR) |
| 92 | fobj = open(os.path.join(TEMPDIR, "ustar/regtype"), "rb") |
| 93 | data = fobj.read() |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 94 | fobj.close() |
| 95 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 96 | tarinfo = self.tar.getmember("ustar/regtype") |
| 97 | fobj = self.tar.extractfile(tarinfo) |
| 98 | |
| 99 | text = fobj.read() |
| 100 | fobj.seek(0) |
| 101 | self.assert_(0 == fobj.tell(), |
| 102 | "seek() to file's start failed") |
| 103 | fobj.seek(2048, 0) |
| 104 | self.assert_(2048 == fobj.tell(), |
| 105 | "seek() to absolute position failed") |
| 106 | fobj.seek(-1024, 1) |
| 107 | self.assert_(1024 == fobj.tell(), |
| 108 | "seek() to negative relative position failed") |
| 109 | fobj.seek(1024, 1) |
| 110 | self.assert_(2048 == fobj.tell(), |
| 111 | "seek() to positive relative position failed") |
| 112 | s = fobj.read(10) |
| 113 | self.assert_(s == data[2048:2058], |
| 114 | "read() after seek failed") |
| 115 | fobj.seek(0, 2) |
| 116 | self.assert_(tarinfo.size == fobj.tell(), |
| 117 | "seek() to file's end failed") |
| 118 | self.assert_(fobj.read() == "", |
| 119 | "read() at file's end did not return empty string") |
| 120 | fobj.seek(-tarinfo.size, 2) |
| 121 | self.assert_(0 == fobj.tell(), |
| 122 | "relative seek() to file's start failed") |
| 123 | fobj.seek(512) |
| 124 | s1 = fobj.readlines() |
| 125 | fobj.seek(512) |
| 126 | s2 = fobj.readlines() |
| 127 | self.assert_(s1 == s2, |
| 128 | "readlines() after seek failed") |
| 129 | fobj.seek(0) |
| 130 | self.assert_(len(fobj.readline()) == fobj.tell(), |
| 131 | "tell() after readline() failed") |
| 132 | fobj.seek(512) |
| 133 | self.assert_(len(fobj.readline()) + 512 == fobj.tell(), |
| 134 | "tell() after seek() and readline() failed") |
| 135 | fobj.seek(0) |
| 136 | line = fobj.readline() |
| 137 | self.assert_(fobj.read() == data[len(line):], |
| 138 | "read() after readline() failed") |
| 139 | fobj.close() |
| 140 | |
| 141 | |
| 142 | class MiscReadTest(ReadTest): |
| 143 | |
| 144 | def test_no_filename(self): |
| 145 | fobj = open(self.tarname, "rb") |
| 146 | tar = tarfile.open(fileobj=fobj, mode=self.mode) |
| 147 | self.assertEqual(tar.name, os.path.abspath(fobj.name)) |
| 148 | |
| 149 | def test_fail_comp(self): |
| 150 | # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file. |
| 151 | if self.mode == "r:": |
| 152 | return |
| 153 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode) |
| 154 | fobj = open(tarname, "rb") |
| 155 | self.assertRaises(tarfile.ReadError, tarfile.open, fileobj=fobj, mode=self.mode) |
| 156 | |
| 157 | def test_v7_dirtype(self): |
| 158 | # Test old style dirtype member (bug #1336623): |
| 159 | # Old V7 tars create directory members using an AREGTYPE |
| 160 | # header with a "/" appended to the filename field. |
| 161 | tarinfo = self.tar.getmember("misc/dirtype-old-v7") |
| 162 | self.assert_(tarinfo.type == tarfile.DIRTYPE, |
| 163 | "v7 dirtype failed") |
| 164 | |
| 165 | def test_check_members(self): |
| 166 | for tarinfo in self.tar: |
| 167 | self.assert_(int(tarinfo.mtime) == 07606136617, |
| 168 | "wrong mtime for %s" % tarinfo.name) |
| 169 | if not tarinfo.name.startswith("ustar/"): |
| 170 | continue |
| 171 | self.assert_(tarinfo.uname == "tarfile", |
| 172 | "wrong uname for %s" % tarinfo.name) |
| 173 | |
| 174 | def test_find_members(self): |
| 175 | self.assert_(self.tar.getmembers()[-1].name == "misc/eof", |
| 176 | "could not find all members") |
| 177 | |
| 178 | def test_extract_hardlink(self): |
| 179 | # Test hardlink extraction (e.g. bug #857297). |
| 180 | tar = tarfile.open(tarname, errorlevel=1, encoding="iso8859-1") |
| 181 | |
| 182 | tar.extract("ustar/regtype", TEMPDIR) |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 183 | try: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 184 | tar.extract("ustar/lnktype", TEMPDIR) |
| 185 | except EnvironmentError as e: |
| 186 | if e.errno == errno.ENOENT: |
| 187 | self.fail("hardlink not extracted properly") |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 189 | data = open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb").read() |
| 190 | self.assertEqual(md5sum(data), md5_regtype) |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 191 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 192 | try: |
| 193 | tar.extract("ustar/symtype", TEMPDIR) |
| 194 | except EnvironmentError as e: |
| 195 | if e.errno == errno.ENOENT: |
| 196 | self.fail("symlink not extracted properly") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 198 | data = open(os.path.join(TEMPDIR, "ustar/symtype"), "rb").read() |
| 199 | self.assertEqual(md5sum(data), md5_regtype) |
| 200 | |
| 201 | |
| 202 | class StreamReadTest(ReadTest): |
| 203 | |
| 204 | mode="r|" |
| 205 | |
| 206 | def test_fileobj_regular_file(self): |
| 207 | tarinfo = self.tar.next() # get "regtype" (can't use getmember) |
| 208 | fobj = self.tar.extractfile(tarinfo) |
| 209 | data = fobj.read() |
| 210 | self.assert_((len(data), md5sum(data)) == (tarinfo.size, md5_regtype), |
| 211 | "regular file extraction failed") |
| 212 | |
| 213 | def test_provoke_stream_error(self): |
| 214 | tarinfos = self.tar.getmembers() |
| 215 | f = self.tar.extractfile(tarinfos[0]) # read the first member |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 216 | self.assertRaises(tarfile.StreamError, f.read) |
| 217 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 218 | def test_compare_members(self): |
| 219 | tar1 = tarfile.open(tarname, encoding="iso8859-1") |
| 220 | tar2 = self.tar |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 222 | while True: |
| 223 | t1 = tar1.next() |
| 224 | t2 = tar2.next() |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 225 | if t1 is None: |
| 226 | break |
| 227 | self.assert_(t2 is not None, "stream.next() failed.") |
| 228 | |
| 229 | if t2.islnk() or t2.issym(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 230 | self.assertRaises(tarfile.StreamError, tar2.extractfile, t2) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 231 | continue |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 232 | |
| 233 | v1 = tar1.extractfile(t1) |
| 234 | v2 = tar2.extractfile(t2) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 235 | if v1 is None: |
| 236 | continue |
| 237 | self.assert_(v2 is not None, "stream.extractfile() failed") |
| 238 | self.assert_(v1.read() == v2.read(), "stream extraction failed") |
| 239 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 240 | tar1.close() |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 241 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 243 | class DetectReadTest(unittest.TestCase): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 245 | def _testfunc_file(self, name, mode): |
| 246 | try: |
| 247 | tarfile.open(name, mode) |
| 248 | except tarfile.ReadError: |
| 249 | self.fail() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 251 | def _testfunc_fileobj(self, name, mode): |
| 252 | try: |
| 253 | tarfile.open(name, mode, fileobj=open(name, "rb")) |
| 254 | except tarfile.ReadError: |
| 255 | self.fail() |
| 256 | |
| 257 | def _test_modes(self, testfunc): |
| 258 | testfunc(tarname, "r") |
| 259 | testfunc(tarname, "r:") |
| 260 | testfunc(tarname, "r:*") |
| 261 | testfunc(tarname, "r|") |
| 262 | testfunc(tarname, "r|*") |
| 263 | |
| 264 | if gzip: |
| 265 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:gz") |
| 266 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|gz") |
| 267 | self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r:") |
| 268 | self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r|") |
| 269 | |
| 270 | testfunc(gzipname, "r") |
| 271 | testfunc(gzipname, "r:*") |
| 272 | testfunc(gzipname, "r:gz") |
| 273 | testfunc(gzipname, "r|*") |
| 274 | testfunc(gzipname, "r|gz") |
| 275 | |
| 276 | if bz2: |
| 277 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:bz2") |
| 278 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|bz2") |
| 279 | self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r:") |
| 280 | self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r|") |
| 281 | |
| 282 | testfunc(bz2name, "r") |
| 283 | testfunc(bz2name, "r:*") |
| 284 | testfunc(bz2name, "r:bz2") |
| 285 | testfunc(bz2name, "r|*") |
| 286 | testfunc(bz2name, "r|bz2") |
| 287 | |
| 288 | def test_detect_file(self): |
| 289 | self._test_modes(self._testfunc_file) |
| 290 | |
| 291 | def test_detect_fileobj(self): |
| 292 | self._test_modes(self._testfunc_fileobj) |
| 293 | |
| 294 | |
| 295 | class MemberReadTest(ReadTest): |
| 296 | |
| 297 | def _test_member(self, tarinfo, chksum=None, **kwargs): |
| 298 | if chksum is not None: |
| 299 | self.assert_(md5sum(self.tar.extractfile(tarinfo).read()) == chksum, |
| 300 | "wrong md5sum for %s" % tarinfo.name) |
| 301 | |
| 302 | kwargs["mtime"] = 07606136617 |
| 303 | kwargs["uid"] = 1000 |
| 304 | kwargs["gid"] = 100 |
| 305 | if "old-v7" not in tarinfo.name: |
| 306 | # V7 tar can't handle alphabetic owners. |
| 307 | kwargs["uname"] = "tarfile" |
| 308 | kwargs["gname"] = "tarfile" |
| 309 | for k, v in kwargs.items(): |
| 310 | self.assert_(getattr(tarinfo, k) == v, |
| 311 | "wrong value in %s field of %s" % (k, tarinfo.name)) |
| 312 | |
| 313 | def test_find_regtype(self): |
| 314 | tarinfo = self.tar.getmember("ustar/regtype") |
| 315 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 316 | |
| 317 | def test_find_conttype(self): |
| 318 | tarinfo = self.tar.getmember("ustar/conttype") |
| 319 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 320 | |
| 321 | def test_find_dirtype(self): |
| 322 | tarinfo = self.tar.getmember("ustar/dirtype") |
| 323 | self._test_member(tarinfo, size=0) |
| 324 | |
| 325 | def test_find_dirtype_with_size(self): |
| 326 | tarinfo = self.tar.getmember("ustar/dirtype-with-size") |
| 327 | self._test_member(tarinfo, size=255) |
| 328 | |
| 329 | def test_find_lnktype(self): |
| 330 | tarinfo = self.tar.getmember("ustar/lnktype") |
| 331 | self._test_member(tarinfo, size=0, linkname="ustar/regtype") |
| 332 | |
| 333 | def test_find_symtype(self): |
| 334 | tarinfo = self.tar.getmember("ustar/symtype") |
| 335 | self._test_member(tarinfo, size=0, linkname="regtype") |
| 336 | |
| 337 | def test_find_blktype(self): |
| 338 | tarinfo = self.tar.getmember("ustar/blktype") |
| 339 | self._test_member(tarinfo, size=0, devmajor=3, devminor=0) |
| 340 | |
| 341 | def test_find_chrtype(self): |
| 342 | tarinfo = self.tar.getmember("ustar/chrtype") |
| 343 | self._test_member(tarinfo, size=0, devmajor=1, devminor=3) |
| 344 | |
| 345 | def test_find_fifotype(self): |
| 346 | tarinfo = self.tar.getmember("ustar/fifotype") |
| 347 | self._test_member(tarinfo, size=0) |
| 348 | |
| 349 | def test_find_sparse(self): |
| 350 | tarinfo = self.tar.getmember("ustar/sparse") |
| 351 | self._test_member(tarinfo, size=86016, chksum=md5_sparse) |
| 352 | |
| 353 | def test_find_umlauts(self): |
| 354 | tarinfo = self.tar.getmember("ustar/umlauts-ÄÖÜäöüß") |
| 355 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 356 | |
| 357 | def test_find_ustar_longname(self): |
| 358 | name = "ustar/" + "12345/" * 39 + "1234567/longname" |
| 359 | self.assert_(name in self.tar.getnames()) |
| 360 | |
| 361 | def test_find_regtype_oldv7(self): |
| 362 | tarinfo = self.tar.getmember("misc/regtype-old-v7") |
| 363 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 364 | |
| 365 | def test_find_pax_umlauts(self): |
| 366 | self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1") |
| 367 | tarinfo = self.tar.getmember("pax/umlauts-ÄÖÜäöüß") |
| 368 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 369 | |
| 370 | |
| 371 | class LongnameTest(ReadTest): |
| 372 | |
| 373 | def test_read_longname(self): |
| 374 | # Test reading of longname (bug #1471427). |
| 375 | name = self.subdir + "/" + "123/" * 125 + "longname" |
| 376 | try: |
| 377 | tarinfo = self.tar.getmember(name) |
| 378 | except KeyError: |
| 379 | self.fail("longname not found") |
| 380 | self.assert_(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") |
| 381 | |
| 382 | def test_read_longlink(self): |
| 383 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
| 384 | longlink = self.subdir + "/" + "123/" * 125 + "longlink" |
| 385 | try: |
| 386 | tarinfo = self.tar.getmember(longlink) |
| 387 | except KeyError: |
| 388 | self.fail("longlink not found") |
| 389 | self.assert_(tarinfo.linkname == longname, "linkname wrong") |
| 390 | |
| 391 | def test_truncated_longname(self): |
| 392 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
| 393 | tarinfo = self.tar.getmember(longname) |
| 394 | offset = tarinfo.offset |
| 395 | self.tar.fileobj.seek(offset) |
| 396 | fobj = StringIO.StringIO(self.tar.fileobj.read(1536)) |
| 397 | self.assertRaises(tarfile.ReadError, tarfile.open, name="foo.tar", fileobj=fobj) |
| 398 | |
| 399 | |
| 400 | class GNUReadTest(LongnameTest): |
| 401 | |
| 402 | subdir = "gnu" |
| 403 | |
| 404 | def test_sparse_file(self): |
| 405 | tarinfo1 = self.tar.getmember("ustar/sparse") |
| 406 | fobj1 = self.tar.extractfile(tarinfo1) |
| 407 | tarinfo2 = self.tar.getmember("gnu/sparse") |
| 408 | fobj2 = self.tar.extractfile(tarinfo2) |
| 409 | self.assert_(fobj1.read() == fobj2.read(), |
| 410 | "sparse file extraction failed") |
| 411 | |
| 412 | |
| 413 | class PaxReadTest(ReadTest): |
| 414 | |
| 415 | subdir = "pax" |
| 416 | |
| 417 | def test_pax_globheaders(self): |
| 418 | tar = tarfile.open(tarname, encoding="iso8859-1") |
| 419 | tarinfo = tar.getmember("pax/regtype1") |
| 420 | self.assertEqual(tarinfo.uname, "foo") |
| 421 | self.assertEqual(tarinfo.gname, "bar") |
| 422 | self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), "ÄÖÜäöüß") |
| 423 | |
| 424 | tarinfo = tar.getmember("pax/regtype2") |
| 425 | self.assertEqual(tarinfo.uname, "") |
| 426 | self.assertEqual(tarinfo.gname, "bar") |
| 427 | self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), "ÄÖÜäöüß") |
| 428 | |
| 429 | tarinfo = tar.getmember("pax/regtype3") |
| 430 | self.assertEqual(tarinfo.uname, "tarfile") |
| 431 | self.assertEqual(tarinfo.gname, "tarfile") |
| 432 | self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), "ÄÖÜäöüß") |
| 433 | |
| 434 | |
| 435 | class WriteTest(unittest.TestCase): |
| 436 | |
| 437 | mode = "w:" |
| 438 | |
| 439 | def test_100_char_name(self): |
| 440 | # The name field in a tar header stores strings of at most 100 chars. |
| 441 | # If a string is shorter than 100 chars it has to be padded with '\0', |
| 442 | # which implies that a string of exactly 100 chars is stored without |
| 443 | # a trailing '\0'. |
| 444 | name = "0123456789" * 10 |
| 445 | tar = tarfile.open(tmpname, self.mode) |
| 446 | t = tarfile.TarInfo(name) |
| 447 | tar.addfile(t) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 448 | tar.close() |
| 449 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 450 | tar = tarfile.open(tmpname) |
| 451 | self.assert_(tar.getnames()[0] == name, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 452 | "failed to store 100 char filename") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 453 | tar.close() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 454 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 455 | def test_tar_size(self): |
| 456 | # Test for bug #1013882. |
| 457 | tar = tarfile.open(tmpname, self.mode) |
| 458 | path = os.path.join(TEMPDIR, "file") |
| 459 | fobj = open(path, "wb") |
| 460 | fobj.write("aaa") |
| 461 | fobj.close() |
| 462 | tar.add(path) |
| 463 | tar.close() |
| 464 | self.assert_(os.path.getsize(tmpname) > 0, |
| 465 | "tarfile is empty") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 466 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 467 | # The test_*_size tests test for bug #1167128. |
| 468 | def test_file_size(self): |
| 469 | tar = tarfile.open(tmpname, self.mode) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 470 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 471 | path = os.path.join(TEMPDIR, "file") |
| 472 | fobj = open(path, "wb") |
| 473 | fobj.close() |
| 474 | tarinfo = tar.gettarinfo(path) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 475 | self.assertEqual(tarinfo.size, 0) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 476 | |
| 477 | fobj = open(path, "wb") |
| 478 | fobj.write("aaa") |
| 479 | fobj.close() |
| 480 | tarinfo = tar.gettarinfo(path) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 481 | self.assertEqual(tarinfo.size, 3) |
| 482 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 483 | tar.close() |
| 484 | |
| 485 | def test_directory_size(self): |
| 486 | path = os.path.join(TEMPDIR, "directory") |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 487 | os.mkdir(path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 488 | try: |
| 489 | tar = tarfile.open(tmpname, self.mode) |
| 490 | tarinfo = tar.gettarinfo(path) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 491 | self.assertEqual(tarinfo.size, 0) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 492 | finally: |
| 493 | os.rmdir(path) |
| 494 | |
| 495 | def test_link_size(self): |
| 496 | if hasattr(os, "link"): |
| 497 | link = os.path.join(TEMPDIR, "link") |
| 498 | target = os.path.join(TEMPDIR, "link_target") |
| 499 | open(target, "wb").close() |
| 500 | os.link(target, link) |
| 501 | try: |
| 502 | tar = tarfile.open(tmpname, self.mode) |
| 503 | tarinfo = tar.gettarinfo(link) |
| 504 | self.assertEqual(tarinfo.size, 0) |
| 505 | finally: |
| 506 | os.remove(target) |
| 507 | os.remove(link) |
| 508 | |
| 509 | def test_symlink_size(self): |
| 510 | if hasattr(os, "symlink"): |
| 511 | path = os.path.join(TEMPDIR, "symlink") |
| 512 | os.symlink("link_target", path) |
| 513 | try: |
| 514 | tar = tarfile.open(tmpname, self.mode) |
| 515 | tarinfo = tar.gettarinfo(path) |
| 516 | self.assertEqual(tarinfo.size, 0) |
| 517 | finally: |
| 518 | os.remove(path) |
| 519 | |
| 520 | def test_add_self(self): |
| 521 | # Test for #1257255. |
| 522 | dstname = os.path.abspath(tmpname) |
| 523 | |
| 524 | tar = tarfile.open(tmpname, self.mode) |
| 525 | self.assert_(tar.name == dstname, "archive name must be absolute") |
| 526 | |
| 527 | tar.add(dstname) |
| 528 | self.assert_(tar.getnames() == [], "added the archive to itself") |
| 529 | |
| 530 | cwd = os.getcwd() |
| 531 | os.chdir(TEMPDIR) |
| 532 | tar.add(dstname) |
| 533 | os.chdir(cwd) |
| 534 | self.assert_(tar.getnames() == [], "added the archive to itself") |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 535 | |
| 536 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 537 | class StreamWriteTest(unittest.TestCase): |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 538 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 539 | mode = "w|" |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 540 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 541 | def test_stream_padding(self): |
| 542 | # Test for bug #1543303. |
| 543 | tar = tarfile.open(tmpname, self.mode) |
| 544 | tar.close() |
| 545 | |
| 546 | if self.mode.endswith("gz"): |
| 547 | fobj = gzip.GzipFile(tmpname) |
| 548 | data = fobj.read() |
| 549 | fobj.close() |
| 550 | elif self.mode.endswith("bz2"): |
| 551 | dec = bz2.BZ2Decompressor() |
| 552 | data = open(tmpname, "rb").read() |
| 553 | data = dec.decompress(data) |
| 554 | self.assert_(len(dec.unused_data) == 0, |
| 555 | "found trailing data") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 556 | else: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 557 | fobj = open(tmpname, "rb") |
| 558 | data = fobj.read() |
| 559 | fobj.close() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 560 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 561 | self.assert_(data.count("\0") == tarfile.RECORDSIZE, |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 562 | "incorrect zero padding") |
| 563 | |
| 564 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 565 | class GNUWriteTest(unittest.TestCase): |
| 566 | # This testcase checks for correct creation of GNU Longname |
| 567 | # and Longlink extended headers (cp. bug #812325). |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 568 | |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 569 | def _length(self, s): |
| 570 | blocks, remainder = divmod(len(s) + 1, 512) |
| 571 | if remainder: |
| 572 | blocks += 1 |
| 573 | return blocks * 512 |
| 574 | |
| 575 | def _calc_size(self, name, link=None): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 576 | # Initial tar header |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 577 | count = 512 |
| 578 | |
| 579 | if len(name) > tarfile.LENGTH_NAME: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 580 | # GNU longname extended header + longname |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 581 | count += 512 |
| 582 | count += self._length(name) |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 583 | if link is not None and len(link) > tarfile.LENGTH_LINK: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 584 | # GNU longlink extended header + longlink |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 585 | count += 512 |
| 586 | count += self._length(link) |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 587 | return count |
| 588 | |
| 589 | def _test(self, name, link=None): |
| 590 | tarinfo = tarfile.TarInfo(name) |
| 591 | if link: |
| 592 | tarinfo.linkname = link |
| 593 | tarinfo.type = tarfile.LNKTYPE |
| 594 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 595 | tar = tarfile.open(tmpname, "w") |
| 596 | tar.format = tarfile.GNU_FORMAT |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 597 | tar.addfile(tarinfo) |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 598 | |
| 599 | v1 = self._calc_size(name, link) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 600 | v2 = tar.offset |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 601 | self.assert_(v1 == v2, "GNU longname/longlink creation failed") |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 602 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 603 | tar.close() |
| 604 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 605 | tar = tarfile.open(tmpname) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 606 | member = tar.next() |
| 607 | self.failIf(member is None, "unable to read longname member") |
| 608 | self.assert_(tarinfo.name == member.name and \ |
| 609 | tarinfo.linkname == member.linkname, \ |
| 610 | "unable to read longname member") |
| 611 | |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 612 | def test_longname_1023(self): |
| 613 | self._test(("longnam/" * 127) + "longnam") |
| 614 | |
| 615 | def test_longname_1024(self): |
| 616 | self._test(("longnam/" * 127) + "longname") |
| 617 | |
| 618 | def test_longname_1025(self): |
| 619 | self._test(("longnam/" * 127) + "longname_") |
| 620 | |
| 621 | def test_longlink_1023(self): |
| 622 | self._test("name", ("longlnk/" * 127) + "longlnk") |
| 623 | |
| 624 | def test_longlink_1024(self): |
| 625 | self._test("name", ("longlnk/" * 127) + "longlink") |
| 626 | |
| 627 | def test_longlink_1025(self): |
| 628 | self._test("name", ("longlnk/" * 127) + "longlink_") |
| 629 | |
| 630 | def test_longnamelink_1023(self): |
| 631 | self._test(("longnam/" * 127) + "longnam", |
| 632 | ("longlnk/" * 127) + "longlnk") |
| 633 | |
| 634 | def test_longnamelink_1024(self): |
| 635 | self._test(("longnam/" * 127) + "longname", |
| 636 | ("longlnk/" * 127) + "longlink") |
| 637 | |
| 638 | def test_longnamelink_1025(self): |
| 639 | self._test(("longnam/" * 127) + "longname_", |
| 640 | ("longlnk/" * 127) + "longlink_") |
| 641 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 642 | |
| 643 | class HardlinkTest(unittest.TestCase): |
| 644 | # Test the creation of LNKTYPE (hardlink) members in an archive. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 645 | |
| 646 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 647 | self.foo = os.path.join(TEMPDIR, "foo") |
| 648 | self.bar = os.path.join(TEMPDIR, "bar") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 649 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 650 | fobj = open(self.foo, "wb") |
| 651 | fobj.write("foo") |
| 652 | fobj.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 653 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 654 | os.link(self.foo, self.bar) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 655 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 656 | self.tar = tarfile.open(tmpname, "w") |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 657 | self.tar.add(self.foo) |
| 658 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 659 | def tearDown(self): |
| 660 | os.remove(self.foo) |
| 661 | os.remove(self.bar) |
| 662 | |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 663 | def test_add_twice(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 664 | # The same name will be added as a REGTYPE every |
| 665 | # time regardless of st_nlink. |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 666 | tarinfo = self.tar.gettarinfo(self.foo) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 667 | self.assert_(tarinfo.type == tarfile.REGTYPE, |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 668 | "add file as regular failed") |
| 669 | |
| 670 | def test_add_hardlink(self): |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 671 | tarinfo = self.tar.gettarinfo(self.bar) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 672 | self.assert_(tarinfo.type == tarfile.LNKTYPE, |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 673 | "add file as hardlink failed") |
| 674 | |
| 675 | def test_dereference_hardlink(self): |
| 676 | self.tar.dereference = True |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 677 | tarinfo = self.tar.gettarinfo(self.bar) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 678 | self.assert_(tarinfo.type == tarfile.REGTYPE, |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 679 | "dereferencing hardlink failed") |
| 680 | |
Neal Norwitz | a4f651a | 2004-07-20 22:07:44 +0000 | [diff] [blame] | 681 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 682 | class PaxWriteTest(GNUWriteTest): |
Martin v. Löwis | 78be7df | 2005-03-05 12:47:42 +0000 | [diff] [blame] | 683 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 684 | def _test(self, name, link=None): |
| 685 | # See GNUWriteTest. |
| 686 | tarinfo = tarfile.TarInfo(name) |
| 687 | if link: |
| 688 | tarinfo.linkname = link |
| 689 | tarinfo.type = tarfile.LNKTYPE |
Andrew M. Kuchling | d4f2552 | 2004-10-20 11:47:01 +0000 | [diff] [blame] | 690 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 691 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT) |
| 692 | tar.addfile(tarinfo) |
| 693 | tar.close() |
Andrew M. Kuchling | d4f2552 | 2004-10-20 11:47:01 +0000 | [diff] [blame] | 694 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 695 | tar = tarfile.open(tmpname) |
| 696 | if link: |
| 697 | l = tar.getmembers()[0].linkname |
| 698 | self.assert_(link == l, "PAX longlink creation failed") |
| 699 | else: |
| 700 | n = tar.getmembers()[0].name |
| 701 | self.assert_(name == n, "PAX longname creation failed") |
| 702 | |
| 703 | def test_iso8859_15_filename(self): |
| 704 | self._test_unicode_filename("iso8859-15") |
| 705 | |
| 706 | def test_utf8_filename(self): |
| 707 | self._test_unicode_filename("utf8") |
| 708 | |
| 709 | def test_utf16_filename(self): |
| 710 | self._test_unicode_filename("utf16") |
| 711 | |
| 712 | def _test_unicode_filename(self, encoding): |
| 713 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 714 | name = "\u20ac".encode(encoding) # Euro sign |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 715 | tar.encoding = encoding |
| 716 | tar.addfile(tarfile.TarInfo(name)) |
| 717 | tar.close() |
| 718 | |
| 719 | tar = tarfile.open(tmpname, encoding=encoding) |
| 720 | self.assertEqual(tar.getmembers()[0].name, name) |
| 721 | tar.close() |
| 722 | |
| 723 | def test_unicode_filename_error(self): |
| 724 | # The euro sign filename cannot be translated to iso8859-1 encoding. |
| 725 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="utf8") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 726 | name = "\u20ac".encode("utf8") # Euro sign |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 727 | tar.addfile(tarfile.TarInfo(name)) |
| 728 | tar.close() |
| 729 | |
| 730 | self.assertRaises(UnicodeError, tarfile.open, tmpname, encoding="iso8859-1") |
| 731 | |
| 732 | def test_pax_headers(self): |
| 733 | self._test_pax_headers({"foo": "bar", "uid": 0, "mtime": 1.23}) |
| 734 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 735 | self._test_pax_headers({"euro": "\u20ac".encode("utf8")}) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 736 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 737 | self._test_pax_headers({"euro": "\u20ac"}, |
| 738 | {"euro": "\u20ac".encode("utf8")}) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 739 | |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 740 | self._test_pax_headers({"\u20ac": "euro"}, |
| 741 | {"\u20ac".encode("utf8"): "euro"}) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 742 | |
| 743 | def _test_pax_headers(self, pax_headers, cmp_headers=None): |
| 744 | if cmp_headers is None: |
| 745 | cmp_headers = pax_headers |
| 746 | |
| 747 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, \ |
| 748 | pax_headers=pax_headers, encoding="utf8") |
| 749 | tar.addfile(tarfile.TarInfo("test")) |
| 750 | tar.close() |
| 751 | |
| 752 | tar = tarfile.open(tmpname, encoding="utf8") |
| 753 | self.assertEqual(tar.pax_headers, cmp_headers) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 754 | |
| 755 | def test_truncated_header(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 756 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT) |
| 757 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
| 758 | tar.addfile(tarinfo) |
| 759 | tar.close() |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 761 | # Simulate a premature EOF. |
| 762 | open(tmpname, "rb+").truncate(1536) |
| 763 | tar = tarfile.open(tmpname) |
| 764 | self.assertEqual(tar.getmembers(), []) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 765 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 766 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 767 | class AppendTest(unittest.TestCase): |
| 768 | # Test append mode (cp. patch #1652681). |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 769 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 770 | def setUp(self): |
| 771 | self.tarname = tmpname |
| 772 | if os.path.exists(self.tarname): |
| 773 | os.remove(self.tarname) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 774 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 775 | def _add_testfile(self, fileobj=None): |
| 776 | tar = tarfile.open(self.tarname, "a", fileobj=fileobj) |
| 777 | tar.addfile(tarfile.TarInfo("bar")) |
| 778 | tar.close() |
Tim Peters | 8ceefc5 | 2004-10-25 03:19:41 +0000 | [diff] [blame] | 779 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 780 | def _create_testtar(self, mode="w:"): |
| 781 | src = tarfile.open(tarname, encoding="iso8859-1") |
| 782 | t = src.getmember("ustar/regtype") |
| 783 | t.name = "foo" |
| 784 | f = src.extractfile(t) |
| 785 | tar = tarfile.open(self.tarname, mode) |
| 786 | tar.addfile(t, f) |
| 787 | tar.close() |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 788 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 789 | def _test(self, names=["bar"], fileobj=None): |
| 790 | tar = tarfile.open(self.tarname, fileobj=fileobj) |
| 791 | self.assertEqual(tar.getnames(), names) |
| 792 | |
| 793 | def test_non_existing(self): |
| 794 | self._add_testfile() |
| 795 | self._test() |
| 796 | |
| 797 | def test_empty(self): |
| 798 | open(self.tarname, "w").close() |
| 799 | self._add_testfile() |
| 800 | self._test() |
| 801 | |
| 802 | def test_empty_fileobj(self): |
| 803 | fobj = StringIO.StringIO() |
| 804 | self._add_testfile(fobj) |
| 805 | fobj.seek(0) |
| 806 | self._test(fileobj=fobj) |
| 807 | |
| 808 | def test_fileobj(self): |
| 809 | self._create_testtar() |
| 810 | data = open(self.tarname).read() |
| 811 | fobj = StringIO.StringIO(data) |
| 812 | self._add_testfile(fobj) |
| 813 | fobj.seek(0) |
| 814 | self._test(names=["foo", "bar"], fileobj=fobj) |
| 815 | |
| 816 | def test_existing(self): |
| 817 | self._create_testtar() |
| 818 | self._add_testfile() |
| 819 | self._test(names=["foo", "bar"]) |
| 820 | |
| 821 | def test_append_gz(self): |
| 822 | if gzip is None: |
| 823 | return |
| 824 | self._create_testtar("w:gz") |
| 825 | self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a") |
| 826 | |
| 827 | def test_append_bz2(self): |
| 828 | if bz2 is None: |
| 829 | return |
| 830 | self._create_testtar("w:bz2") |
| 831 | self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a") |
| 832 | |
| 833 | |
| 834 | class LimitsTest(unittest.TestCase): |
| 835 | |
| 836 | def test_ustar_limits(self): |
| 837 | # 100 char name |
| 838 | tarinfo = tarfile.TarInfo("0123456789" * 10) |
| 839 | tarinfo.create_ustar_header() |
| 840 | |
| 841 | # 101 char name that cannot be stored |
| 842 | tarinfo = tarfile.TarInfo("0123456789" * 10 + "0") |
| 843 | self.assertRaises(ValueError, tarinfo.create_ustar_header) |
| 844 | |
| 845 | # 256 char name with a slash at pos 156 |
| 846 | tarinfo = tarfile.TarInfo("123/" * 62 + "longname") |
| 847 | tarinfo.create_ustar_header() |
| 848 | |
| 849 | # 256 char name that cannot be stored |
| 850 | tarinfo = tarfile.TarInfo("1234567/" * 31 + "longname") |
| 851 | self.assertRaises(ValueError, tarinfo.create_ustar_header) |
| 852 | |
| 853 | # 512 char name |
| 854 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
| 855 | self.assertRaises(ValueError, tarinfo.create_ustar_header) |
| 856 | |
| 857 | # 512 char linkname |
| 858 | tarinfo = tarfile.TarInfo("longlink") |
| 859 | tarinfo.linkname = "123/" * 126 + "longname" |
| 860 | self.assertRaises(ValueError, tarinfo.create_ustar_header) |
| 861 | |
| 862 | # uid > 8 digits |
| 863 | tarinfo = tarfile.TarInfo("name") |
| 864 | tarinfo.uid = 010000000 |
| 865 | self.assertRaises(ValueError, tarinfo.create_ustar_header) |
| 866 | |
| 867 | def test_gnu_limits(self): |
| 868 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
| 869 | tarinfo.create_gnu_header() |
| 870 | |
| 871 | tarinfo = tarfile.TarInfo("longlink") |
| 872 | tarinfo.linkname = "123/" * 126 + "longname" |
| 873 | tarinfo.create_gnu_header() |
| 874 | |
| 875 | # uid >= 256 ** 7 |
| 876 | tarinfo = tarfile.TarInfo("name") |
| 877 | tarinfo.uid = 04000000000000000000 |
| 878 | self.assertRaises(ValueError, tarinfo.create_gnu_header) |
| 879 | |
| 880 | def test_pax_limits(self): |
| 881 | # A 256 char name that can be stored without an extended header. |
| 882 | tarinfo = tarfile.TarInfo("123/" * 62 + "longname") |
| 883 | self.assert_(len(tarinfo.create_pax_header("utf8")) == 512, |
| 884 | "create_pax_header attached superfluous extended header") |
| 885 | |
| 886 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
| 887 | tarinfo.create_pax_header("utf8") |
| 888 | |
| 889 | tarinfo = tarfile.TarInfo("longlink") |
| 890 | tarinfo.linkname = "123/" * 126 + "longname" |
| 891 | tarinfo.create_pax_header("utf8") |
| 892 | |
| 893 | tarinfo = tarfile.TarInfo("name") |
| 894 | tarinfo.uid = 04000000000000000000 |
| 895 | tarinfo.create_pax_header("utf8") |
| 896 | |
| 897 | |
| 898 | class GzipMiscReadTest(MiscReadTest): |
| 899 | tarname = gzipname |
| 900 | mode = "r:gz" |
| 901 | class GzipUstarReadTest(UstarReadTest): |
| 902 | tarname = gzipname |
| 903 | mode = "r:gz" |
| 904 | class GzipStreamReadTest(StreamReadTest): |
| 905 | tarname = gzipname |
| 906 | mode = "r|gz" |
| 907 | class GzipWriteTest(WriteTest): |
| 908 | mode = "w:gz" |
| 909 | class GzipStreamWriteTest(StreamWriteTest): |
| 910 | mode = "w|gz" |
| 911 | |
| 912 | |
| 913 | class Bz2MiscReadTest(MiscReadTest): |
| 914 | tarname = bz2name |
| 915 | mode = "r:bz2" |
| 916 | class Bz2UstarReadTest(UstarReadTest): |
| 917 | tarname = bz2name |
| 918 | mode = "r:bz2" |
| 919 | class Bz2StreamReadTest(StreamReadTest): |
| 920 | tarname = bz2name |
| 921 | mode = "r|bz2" |
| 922 | class Bz2WriteTest(WriteTest): |
| 923 | mode = "w:bz2" |
| 924 | class Bz2StreamWriteTest(StreamWriteTest): |
| 925 | mode = "w|bz2" |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 926 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 927 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 928 | if not os.path.exists(TEMPDIR): |
| 929 | os.mkdir(TEMPDIR) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 930 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 931 | tests = [ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 932 | UstarReadTest, |
| 933 | MiscReadTest, |
| 934 | StreamReadTest, |
| 935 | DetectReadTest, |
| 936 | MemberReadTest, |
| 937 | GNUReadTest, |
| 938 | PaxReadTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 939 | WriteTest, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 940 | StreamWriteTest, |
| 941 | GNUWriteTest, |
| 942 | PaxWriteTest, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 943 | AppendTest, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 944 | LimitsTest, |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 945 | ] |
| 946 | |
Neal Norwitz | a4f651a | 2004-07-20 22:07:44 +0000 | [diff] [blame] | 947 | if hasattr(os, "link"): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 948 | tests.append(HardlinkTest) |
| 949 | |
| 950 | fobj = open(tarname, "rb") |
| 951 | data = fobj.read() |
| 952 | fobj.close() |
Neal Norwitz | a4f651a | 2004-07-20 22:07:44 +0000 | [diff] [blame] | 953 | |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 954 | if gzip: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 955 | # Create testtar.tar.gz and add gzip-specific tests. |
| 956 | tar = gzip.open(gzipname, "wb") |
| 957 | tar.write(data) |
| 958 | tar.close() |
| 959 | |
| 960 | tests += [ |
| 961 | GzipMiscReadTest, |
| 962 | GzipUstarReadTest, |
| 963 | GzipStreamReadTest, |
| 964 | GzipWriteTest, |
| 965 | GzipStreamWriteTest, |
| 966 | ] |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 967 | |
| 968 | if bz2: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 969 | # Create testtar.tar.bz2 and add bz2-specific tests. |
| 970 | tar = bz2.BZ2File(bz2name, "wb") |
| 971 | tar.write(data) |
| 972 | tar.close() |
| 973 | |
| 974 | tests += [ |
| 975 | Bz2MiscReadTest, |
| 976 | Bz2UstarReadTest, |
| 977 | Bz2StreamReadTest, |
| 978 | Bz2WriteTest, |
| 979 | Bz2StreamWriteTest, |
| 980 | ] |
| 981 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 982 | try: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 983 | test_support.run_unittest(*tests) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 984 | finally: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 985 | if os.path.exists(TEMPDIR): |
| 986 | shutil.rmtree(TEMPDIR) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 987 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 988 | if __name__ == "__main__": |
| 989 | test_main() |