blob: 312050bef991b9a86e4074166bc2fe8939d996bd [file] [log] [blame]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001# encoding: iso8859-1
2
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00003import sys
4import os
5import shutil
Brett Cannon455ea532003-06-12 08:01:06 +00006import tempfile
Thomas Wouters477c8d52006-05-27 19:21:47 +00007import StringIO
Guido van Rossuma8add0e2007-05-14 22:03:55 +00008from hashlib import md5
Guido van Rossumd8faa362007-04-27 19:54:29 +00009import errno
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000010
11import unittest
12import tarfile
13
14from test import test_support
15
16# Check for our compression modules.
17try:
18 import gzip
Neal Norwitzae323192003-04-14 01:18:32 +000019 gzip.GzipFile
20except (ImportError, AttributeError):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000021 gzip = None
22try:
23 import bz2
24except ImportError:
25 bz2 = None
26
Guido van Rossumd8faa362007-04-27 19:54:29 +000027def md5sum(data):
Guido van Rossuma8add0e2007-05-14 22:03:55 +000028 return md5(data).hexdigest()
Guido van Rossumd8faa362007-04-27 19:54:29 +000029
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000030def path(path):
31 return test_support.findfile(path)
32
Guido van Rossumd8faa362007-04-27 19:54:29 +000033TEMPDIR = os.path.join(tempfile.gettempdir(), "test_tarfile_tmp")
34tarname = path("testtar.tar")
35gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
36bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
37tmpname = os.path.join(TEMPDIR, "tmp.tar")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000038
Guido van Rossumd8faa362007-04-27 19:54:29 +000039md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
40md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000041
42
Guido van Rossumd8faa362007-04-27 19:54:29 +000043class ReadTest(unittest.TestCase):
44
45 tarname = tarname
46 mode = "r:"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000047
48 def setUp(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +000049 self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000050
51 def tearDown(self):
52 self.tar.close()
53
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000054
Guido van Rossumd8faa362007-04-27 19:54:29 +000055class UstarReadTest(ReadTest):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000056
Guido van Rossumd8faa362007-04-27 19:54:29 +000057 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 Norwitzb9ef4ae2003-01-05 23:19:43 +000063
Guido van Rossumd8faa362007-04-27 19:54:29 +000064 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 Norwitzb9ef4ae2003-01-05 23:19:43 +000069
Guido van Rossumd8faa362007-04-27 19:54:29 +000070 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 Norwitzb9ef4ae2003-01-05 23:19:43 +000079
Guido van Rossumd8faa362007-04-27 19:54:29 +000080 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öwisdf241532005-03-03 08:17:42 +000089
Guido van Rossumd8faa362007-04-27 19:54:29 +000090 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 Norwitzf3396542005-10-28 05:52:22 +000094 fobj.close()
95
Guido van Rossumd8faa362007-04-27 19:54:29 +000096 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
142class 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 Norwitzf3396542005-10-28 05:52:22 +0000183 try:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000184 tar.extract("ustar/lnktype", TEMPDIR)
185 except EnvironmentError as e:
186 if e.errno == errno.ENOENT:
187 self.fail("hardlink not extracted properly")
Neal Norwitzf3396542005-10-28 05:52:22 +0000188
Guido van Rossumd8faa362007-04-27 19:54:29 +0000189 data = open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb").read()
190 self.assertEqual(md5sum(data), md5_regtype)
Neal Norwitzf3396542005-10-28 05:52:22 +0000191
Guido van Rossumd8faa362007-04-27 19:54:29 +0000192 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 Norwitzb9ef4ae2003-01-05 23:19:43 +0000197
Guido van Rossumd8faa362007-04-27 19:54:29 +0000198 data = open(os.path.join(TEMPDIR, "ustar/symtype"), "rb").read()
199 self.assertEqual(md5sum(data), md5_regtype)
200
201
202class 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 Norwitzb9ef4ae2003-01-05 23:19:43 +0000216 self.assertRaises(tarfile.StreamError, f.read)
217
Guido van Rossumd8faa362007-04-27 19:54:29 +0000218 def test_compare_members(self):
219 tar1 = tarfile.open(tarname, encoding="iso8859-1")
220 tar2 = self.tar
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000221
Guido van Rossumd8faa362007-04-27 19:54:29 +0000222 while True:
223 t1 = tar1.next()
224 t2 = tar2.next()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000225 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 Rossumd8faa362007-04-27 19:54:29 +0000230 self.assertRaises(tarfile.StreamError, tar2.extractfile, t2)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000231 continue
Guido van Rossumd8faa362007-04-27 19:54:29 +0000232
233 v1 = tar1.extractfile(t1)
234 v2 = tar2.extractfile(t2)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000235 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 Rossumd8faa362007-04-27 19:54:29 +0000240 tar1.close()
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000241
Thomas Wouters89f507f2006-12-13 04:49:30 +0000242
Guido van Rossumd8faa362007-04-27 19:54:29 +0000243class DetectReadTest(unittest.TestCase):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000244
Guido van Rossumd8faa362007-04-27 19:54:29 +0000245 def _testfunc_file(self, name, mode):
246 try:
247 tarfile.open(name, mode)
248 except tarfile.ReadError:
249 self.fail()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000250
Guido van Rossumd8faa362007-04-27 19:54:29 +0000251 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
295class 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
371class 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
400class 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
413class 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
435class 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 Wouterscf297e42007-02-23 15:07:44 +0000448 tar.close()
449
Guido van Rossumd8faa362007-04-27 19:54:29 +0000450 tar = tarfile.open(tmpname)
451 self.assert_(tar.getnames()[0] == name,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000452 "failed to store 100 char filename")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000453 tar.close()
Thomas Wouters89f507f2006-12-13 04:49:30 +0000454
Guido van Rossumd8faa362007-04-27 19:54:29 +0000455 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 Wouters89f507f2006-12-13 04:49:30 +0000466
Guido van Rossumd8faa362007-04-27 19:54:29 +0000467 # The test_*_size tests test for bug #1167128.
468 def test_file_size(self):
469 tar = tarfile.open(tmpname, self.mode)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000470
Guido van Rossumd8faa362007-04-27 19:54:29 +0000471 path = os.path.join(TEMPDIR, "file")
472 fobj = open(path, "wb")
473 fobj.close()
474 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000475 self.assertEqual(tarinfo.size, 0)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000476
477 fobj = open(path, "wb")
478 fobj.write("aaa")
479 fobj.close()
480 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000481 self.assertEqual(tarinfo.size, 3)
482
Guido van Rossumd8faa362007-04-27 19:54:29 +0000483 tar.close()
484
485 def test_directory_size(self):
486 path = os.path.join(TEMPDIR, "directory")
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000487 os.mkdir(path)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000488 try:
489 tar = tarfile.open(tmpname, self.mode)
490 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000491 self.assertEqual(tarinfo.size, 0)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000492 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öwis5dbdc592005-08-27 10:07:56 +0000535
536
Guido van Rossumd8faa362007-04-27 19:54:29 +0000537class StreamWriteTest(unittest.TestCase):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000538
Guido van Rossumd8faa362007-04-27 19:54:29 +0000539 mode = "w|"
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000540
Guido van Rossumd8faa362007-04-27 19:54:29 +0000541 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 Wouters00ee7ba2006-08-21 19:07:27 +0000556 else:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000557 fobj = open(tmpname, "rb")
558 data = fobj.read()
559 fobj.close()
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000560
Guido van Rossumd8faa362007-04-27 19:54:29 +0000561 self.assert_(data.count("\0") == tarfile.RECORDSIZE,
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000562 "incorrect zero padding")
563
564
Guido van Rossumd8faa362007-04-27 19:54:29 +0000565class GNUWriteTest(unittest.TestCase):
566 # This testcase checks for correct creation of GNU Longname
567 # and Longlink extended headers (cp. bug #812325).
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000568
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000569 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 Rossumd8faa362007-04-27 19:54:29 +0000576 # Initial tar header
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000577 count = 512
578
579 if len(name) > tarfile.LENGTH_NAME:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000580 # GNU longname extended header + longname
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000581 count += 512
582 count += self._length(name)
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000583 if link is not None and len(link) > tarfile.LENGTH_LINK:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000584 # GNU longlink extended header + longlink
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000585 count += 512
586 count += self._length(link)
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000587 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 Rossumd8faa362007-04-27 19:54:29 +0000595 tar = tarfile.open(tmpname, "w")
596 tar.format = tarfile.GNU_FORMAT
Thomas Wouters89f507f2006-12-13 04:49:30 +0000597 tar.addfile(tarinfo)
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000598
599 v1 = self._calc_size(name, link)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000600 v2 = tar.offset
Guido van Rossumd8faa362007-04-27 19:54:29 +0000601 self.assert_(v1 == v2, "GNU longname/longlink creation failed")
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000602
Thomas Wouters89f507f2006-12-13 04:49:30 +0000603 tar.close()
604
Guido van Rossumd8faa362007-04-27 19:54:29 +0000605 tar = tarfile.open(tmpname)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000606 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 Norwitz0662f8a2004-07-20 21:54:18 +0000612 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 Rossumd8faa362007-04-27 19:54:29 +0000642
643class HardlinkTest(unittest.TestCase):
644 # Test the creation of LNKTYPE (hardlink) members in an archive.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000645
646 def setUp(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000647 self.foo = os.path.join(TEMPDIR, "foo")
648 self.bar = os.path.join(TEMPDIR, "bar")
Thomas Wouters477c8d52006-05-27 19:21:47 +0000649
Guido van Rossumd8faa362007-04-27 19:54:29 +0000650 fobj = open(self.foo, "wb")
651 fobj.write("foo")
652 fobj.close()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653
Guido van Rossumd8faa362007-04-27 19:54:29 +0000654 os.link(self.foo, self.bar)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000655
Guido van Rossumd8faa362007-04-27 19:54:29 +0000656 self.tar = tarfile.open(tmpname, "w")
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000657 self.tar.add(self.foo)
658
Guido van Rossumd8faa362007-04-27 19:54:29 +0000659 def tearDown(self):
660 os.remove(self.foo)
661 os.remove(self.bar)
662
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000663 def test_add_twice(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000664 # The same name will be added as a REGTYPE every
665 # time regardless of st_nlink.
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000666 tarinfo = self.tar.gettarinfo(self.foo)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000667 self.assert_(tarinfo.type == tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000668 "add file as regular failed")
669
670 def test_add_hardlink(self):
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000671 tarinfo = self.tar.gettarinfo(self.bar)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000672 self.assert_(tarinfo.type == tarfile.LNKTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000673 "add file as hardlink failed")
674
675 def test_dereference_hardlink(self):
676 self.tar.dereference = True
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000677 tarinfo = self.tar.gettarinfo(self.bar)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000678 self.assert_(tarinfo.type == tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000679 "dereferencing hardlink failed")
680
Neal Norwitza4f651a2004-07-20 22:07:44 +0000681
Guido van Rossumd8faa362007-04-27 19:54:29 +0000682class PaxWriteTest(GNUWriteTest):
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000683
Guido van Rossumd8faa362007-04-27 19:54:29 +0000684 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. Kuchlingd4f25522004-10-20 11:47:01 +0000690
Guido van Rossumd8faa362007-04-27 19:54:29 +0000691 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT)
692 tar.addfile(tarinfo)
693 tar.close()
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +0000694
Guido van Rossumd8faa362007-04-27 19:54:29 +0000695 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 Rossumef87d6e2007-05-02 19:09:54 +0000714 name = "\u20ac".encode(encoding) # Euro sign
Guido van Rossumd8faa362007-04-27 19:54:29 +0000715 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 Rossumef87d6e2007-05-02 19:09:54 +0000726 name = "\u20ac".encode("utf8") # Euro sign
Guido van Rossumd8faa362007-04-27 19:54:29 +0000727 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 Rossumef87d6e2007-05-02 19:09:54 +0000735 self._test_pax_headers({"euro": "\u20ac".encode("utf8")})
Guido van Rossumd8faa362007-04-27 19:54:29 +0000736
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000737 self._test_pax_headers({"euro": "\u20ac"},
738 {"euro": "\u20ac".encode("utf8")})
Guido van Rossumd8faa362007-04-27 19:54:29 +0000739
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000740 self._test_pax_headers({"\u20ac": "euro"},
741 {"\u20ac".encode("utf8"): "euro"})
Guido van Rossumd8faa362007-04-27 19:54:29 +0000742
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 Wouters902d6eb2007-01-09 23:18:33 +0000754
755 def test_truncated_header(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000756 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT)
757 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
758 tar.addfile(tarinfo)
759 tar.close()
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000760
Guido van Rossumd8faa362007-04-27 19:54:29 +0000761 # Simulate a premature EOF.
762 open(tmpname, "rb+").truncate(1536)
763 tar = tarfile.open(tmpname)
764 self.assertEqual(tar.getmembers(), [])
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000765
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000766
Guido van Rossumd8faa362007-04-27 19:54:29 +0000767class AppendTest(unittest.TestCase):
768 # Test append mode (cp. patch #1652681).
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000769
Guido van Rossumd8faa362007-04-27 19:54:29 +0000770 def setUp(self):
771 self.tarname = tmpname
772 if os.path.exists(self.tarname):
773 os.remove(self.tarname)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000774
Guido van Rossumd8faa362007-04-27 19:54:29 +0000775 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 Peters8ceefc52004-10-25 03:19:41 +0000779
Guido van Rossumd8faa362007-04-27 19:54:29 +0000780 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 Norwitzb9ef4ae2003-01-05 23:19:43 +0000788
Guido van Rossumd8faa362007-04-27 19:54:29 +0000789 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
834class 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
898class GzipMiscReadTest(MiscReadTest):
899 tarname = gzipname
900 mode = "r:gz"
901class GzipUstarReadTest(UstarReadTest):
902 tarname = gzipname
903 mode = "r:gz"
904class GzipStreamReadTest(StreamReadTest):
905 tarname = gzipname
906 mode = "r|gz"
907class GzipWriteTest(WriteTest):
908 mode = "w:gz"
909class GzipStreamWriteTest(StreamWriteTest):
910 mode = "w|gz"
911
912
913class Bz2MiscReadTest(MiscReadTest):
914 tarname = bz2name
915 mode = "r:bz2"
916class Bz2UstarReadTest(UstarReadTest):
917 tarname = bz2name
918 mode = "r:bz2"
919class Bz2StreamReadTest(StreamReadTest):
920 tarname = bz2name
921 mode = "r|bz2"
922class Bz2WriteTest(WriteTest):
923 mode = "w:bz2"
924class Bz2StreamWriteTest(StreamWriteTest):
925 mode = "w|bz2"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000926
Neal Norwitz996acf12003-02-17 14:51:41 +0000927def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +0000928 if not os.path.exists(TEMPDIR):
929 os.mkdir(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000930
Walter Dörwald21d3a322003-05-01 17:45:56 +0000931 tests = [
Guido van Rossumd8faa362007-04-27 19:54:29 +0000932 UstarReadTest,
933 MiscReadTest,
934 StreamReadTest,
935 DetectReadTest,
936 MemberReadTest,
937 GNUReadTest,
938 PaxReadTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000939 WriteTest,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000940 StreamWriteTest,
941 GNUWriteTest,
942 PaxWriteTest,
Thomas Wouterscf297e42007-02-23 15:07:44 +0000943 AppendTest,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000944 LimitsTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000945 ]
946
Neal Norwitza4f651a2004-07-20 22:07:44 +0000947 if hasattr(os, "link"):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000948 tests.append(HardlinkTest)
949
950 fobj = open(tarname, "rb")
951 data = fobj.read()
952 fobj.close()
Neal Norwitza4f651a2004-07-20 22:07:44 +0000953
Walter Dörwald21d3a322003-05-01 17:45:56 +0000954 if gzip:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000955 # 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örwald21d3a322003-05-01 17:45:56 +0000967
968 if bz2:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000969 # 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 Norwitzb9ef4ae2003-01-05 23:19:43 +0000982 try:
Walter Dörwald21d3a322003-05-01 17:45:56 +0000983 test_support.run_unittest(*tests)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000984 finally:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000985 if os.path.exists(TEMPDIR):
986 shutil.rmtree(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000987
Neal Norwitz996acf12003-02-17 14:51:41 +0000988if __name__ == "__main__":
989 test_main()