blob: 7e4fd25cf04bc3002b7155ddf53d7124f29118db [file] [log] [blame]
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001# -*- coding: iso-8859-15 -*-
Lars Gustäbelc64e4022007-03-13 10:47:19 +00002
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00003import sys
4import os
5import shutil
Brett Cannon455ea532003-06-12 08:01:06 +00006import tempfile
Georg Brandl38c6a222006-05-10 16:26:03 +00007import StringIO
Brett Cannon7eec2172007-05-30 22:24:28 +00008from hashlib import md5
Lars Gustäbelc64e4022007-03-13 10:47:19 +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
Lars Gustäbelc64e4022007-03-13 10:47:19 +000027def md5sum(data):
Brett Cannon7eec2172007-05-30 22:24:28 +000028 return md5(data).hexdigest()
Lars Gustäbelc64e4022007-03-13 10:47:19 +000029
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000030def path(path):
31 return test_support.findfile(path)
32
Lars Gustäbelc64e4022007-03-13 10:47:19 +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
Lars Gustäbelc64e4022007-03-13 10:47:19 +000039md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
40md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000041
42
Lars Gustäbelc64e4022007-03-13 10:47:19 +000043class ReadTest(unittest.TestCase):
44
45 tarname = tarname
46 mode = "r:"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000047
48 def setUp(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +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
Lars Gustäbelc64e4022007-03-13 10:47:19 +000055class UstarReadTest(ReadTest):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000056
Lars Gustäbelc64e4022007-03-13 10:47:19 +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
Lars Gustäbelc64e4022007-03-13 10:47:19 +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
Lars Gustäbelc64e4022007-03-13 10:47:19 +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
Lars Gustäbelc64e4022007-03-13 10:47:19 +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
Lars Gustäbelc64e4022007-03-13 10:47:19 +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
Lars Gustäbelc64e4022007-03-13 10:47:19 +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
Lars Gustäbel0f4a14b2007-08-28 12:31:09 +0000144 def test_no_name_argument(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000145 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
Lars Gustäbel0f4a14b2007-08-28 12:31:09 +0000149 def test_no_name_attribute(self):
150 data = open(self.tarname, "rb").read()
151 fobj = StringIO.StringIO(data)
152 self.assertRaises(AttributeError, getattr, fobj, "name")
153 tar = tarfile.open(fileobj=fobj, mode=self.mode)
154 self.assertEqual(tar.name, None)
155
156 def test_empty_name_attribute(self):
157 data = open(self.tarname, "rb").read()
158 fobj = StringIO.StringIO(data)
159 fobj.name = ""
160 tar = tarfile.open(fileobj=fobj, mode=self.mode)
161 self.assertEqual(tar.name, None)
162
Lars Gustäbel77b2d632007-12-01 21:02:12 +0000163 def test_fileobj_with_offset(self):
164 # Skip the first member and store values from the second member
165 # of the testtar.
166 tar = tarfile.open(self.tarname, mode=self.mode)
167 tar.next()
168 t = tar.next()
169 name = t.name
170 offset = t.offset
171 data = tar.extractfile(t).read()
172 tar.close()
173
174 # Open the testtar and seek to the offset of the second member.
175 if self.mode.endswith(":gz"):
176 _open = gzip.GzipFile
177 elif self.mode.endswith(":bz2"):
178 _open = bz2.BZ2File
179 else:
180 _open = open
181 fobj = _open(self.tarname, "rb")
182 fobj.seek(offset)
183
184 # Test if the tarfile starts with the second member.
185 tar = tar.open(self.tarname, mode="r:", fileobj=fobj)
186 t = tar.next()
187 self.assertEqual(t.name, name)
188 # Read to the end of fileobj and test if seeking back to the
189 # beginning works.
190 tar.getmembers()
191 self.assertEqual(tar.extractfile(t).read(), data,
192 "seek back did not work")
193 tar.close()
194
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000195 def test_fail_comp(self):
196 # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
197 if self.mode == "r:":
198 return
199 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode)
200 fobj = open(tarname, "rb")
201 self.assertRaises(tarfile.ReadError, tarfile.open, fileobj=fobj, mode=self.mode)
202
203 def test_v7_dirtype(self):
204 # Test old style dirtype member (bug #1336623):
205 # Old V7 tars create directory members using an AREGTYPE
206 # header with a "/" appended to the filename field.
207 tarinfo = self.tar.getmember("misc/dirtype-old-v7")
208 self.assert_(tarinfo.type == tarfile.DIRTYPE,
209 "v7 dirtype failed")
210
Lars Gustäbel6bf51da2008-02-11 19:17:10 +0000211 def test_xstar_type(self):
212 # The xstar format stores extra atime and ctime fields inside the
213 # space reserved for the prefix field. The prefix field must be
214 # ignored in this case, otherwise it will mess up the name.
215 try:
216 self.tar.getmember("misc/regtype-xstar")
217 except KeyError:
218 self.fail("failed to find misc/regtype-xstar (mangled prefix?)")
219
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000220 def test_check_members(self):
221 for tarinfo in self.tar:
222 self.assert_(int(tarinfo.mtime) == 07606136617,
223 "wrong mtime for %s" % tarinfo.name)
224 if not tarinfo.name.startswith("ustar/"):
225 continue
226 self.assert_(tarinfo.uname == "tarfile",
227 "wrong uname for %s" % tarinfo.name)
228
229 def test_find_members(self):
230 self.assert_(self.tar.getmembers()[-1].name == "misc/eof",
231 "could not find all members")
232
233 def test_extract_hardlink(self):
234 # Test hardlink extraction (e.g. bug #857297).
Lars Gustäbela36cde42007-03-13 15:47:07 +0000235 tar = tarfile.open(tarname, errorlevel=1, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000236
237 tar.extract("ustar/regtype", TEMPDIR)
Neal Norwitzf3396542005-10-28 05:52:22 +0000238 try:
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000239 tar.extract("ustar/lnktype", TEMPDIR)
240 except EnvironmentError, e:
241 if e.errno == errno.ENOENT:
242 self.fail("hardlink not extracted properly")
Neal Norwitzf3396542005-10-28 05:52:22 +0000243
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000244 data = open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb").read()
245 self.assertEqual(md5sum(data), md5_regtype)
Neal Norwitzf3396542005-10-28 05:52:22 +0000246
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000247 try:
248 tar.extract("ustar/symtype", TEMPDIR)
249 except EnvironmentError, e:
250 if e.errno == errno.ENOENT:
251 self.fail("symlink not extracted properly")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000252
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000253 data = open(os.path.join(TEMPDIR, "ustar/symtype"), "rb").read()
254 self.assertEqual(md5sum(data), md5_regtype)
255
Lars Gustäbel2ee1c762008-01-04 14:00:33 +0000256 def test_extractall(self):
257 # Test if extractall() correctly restores directory permissions
258 # and times (see issue1735).
259 if sys.platform == "win32":
260 # Win32 has no support for utime() on directories or
261 # fine grained permissions.
262 return
263
264 tar = tarfile.open(tarname, encoding="iso8859-1")
265 directories = [t for t in tar if t.isdir()]
266 tar.extractall(TEMPDIR, directories)
267 for tarinfo in directories:
268 path = os.path.join(TEMPDIR, tarinfo.name)
269 self.assertEqual(tarinfo.mode & 0777, os.stat(path).st_mode & 0777)
270 self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
271 tar.close()
272
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000273
274class StreamReadTest(ReadTest):
275
276 mode="r|"
277
278 def test_fileobj_regular_file(self):
279 tarinfo = self.tar.next() # get "regtype" (can't use getmember)
280 fobj = self.tar.extractfile(tarinfo)
281 data = fobj.read()
282 self.assert_((len(data), md5sum(data)) == (tarinfo.size, md5_regtype),
283 "regular file extraction failed")
284
285 def test_provoke_stream_error(self):
286 tarinfos = self.tar.getmembers()
287 f = self.tar.extractfile(tarinfos[0]) # read the first member
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000288 self.assertRaises(tarfile.StreamError, f.read)
289
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000290 def test_compare_members(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000291 tar1 = tarfile.open(tarname, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000292 tar2 = self.tar
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000293
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000294 while True:
295 t1 = tar1.next()
296 t2 = tar2.next()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000297 if t1 is None:
298 break
299 self.assert_(t2 is not None, "stream.next() failed.")
300
301 if t2.islnk() or t2.issym():
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000302 self.assertRaises(tarfile.StreamError, tar2.extractfile, t2)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000303 continue
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000304
305 v1 = tar1.extractfile(t1)
306 v2 = tar2.extractfile(t2)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000307 if v1 is None:
308 continue
309 self.assert_(v2 is not None, "stream.extractfile() failed")
310 self.assert_(v1.read() == v2.read(), "stream extraction failed")
311
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000312 tar1.close()
Lars Gustäbela4b23812006-12-23 17:57:23 +0000313
Georg Brandla32e0a02006-10-24 16:54:16 +0000314
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000315class DetectReadTest(unittest.TestCase):
Lars Gustäbel3f8aca12007-02-06 18:38:13 +0000316
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000317 def _testfunc_file(self, name, mode):
318 try:
319 tarfile.open(name, mode)
320 except tarfile.ReadError:
321 self.fail()
Lars Gustäbel3f8aca12007-02-06 18:38:13 +0000322
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000323 def _testfunc_fileobj(self, name, mode):
324 try:
325 tarfile.open(name, mode, fileobj=open(name, "rb"))
326 except tarfile.ReadError:
327 self.fail()
328
329 def _test_modes(self, testfunc):
330 testfunc(tarname, "r")
331 testfunc(tarname, "r:")
332 testfunc(tarname, "r:*")
333 testfunc(tarname, "r|")
334 testfunc(tarname, "r|*")
335
336 if gzip:
337 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:gz")
338 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|gz")
339 self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r:")
340 self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r|")
341
342 testfunc(gzipname, "r")
343 testfunc(gzipname, "r:*")
344 testfunc(gzipname, "r:gz")
345 testfunc(gzipname, "r|*")
346 testfunc(gzipname, "r|gz")
347
348 if bz2:
349 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:bz2")
350 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|bz2")
351 self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r:")
352 self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r|")
353
354 testfunc(bz2name, "r")
355 testfunc(bz2name, "r:*")
356 testfunc(bz2name, "r:bz2")
357 testfunc(bz2name, "r|*")
358 testfunc(bz2name, "r|bz2")
359
360 def test_detect_file(self):
361 self._test_modes(self._testfunc_file)
362
363 def test_detect_fileobj(self):
364 self._test_modes(self._testfunc_fileobj)
365
366
367class MemberReadTest(ReadTest):
368
369 def _test_member(self, tarinfo, chksum=None, **kwargs):
370 if chksum is not None:
371 self.assert_(md5sum(self.tar.extractfile(tarinfo).read()) == chksum,
372 "wrong md5sum for %s" % tarinfo.name)
373
374 kwargs["mtime"] = 07606136617
375 kwargs["uid"] = 1000
376 kwargs["gid"] = 100
377 if "old-v7" not in tarinfo.name:
378 # V7 tar can't handle alphabetic owners.
379 kwargs["uname"] = "tarfile"
380 kwargs["gname"] = "tarfile"
381 for k, v in kwargs.iteritems():
382 self.assert_(getattr(tarinfo, k) == v,
383 "wrong value in %s field of %s" % (k, tarinfo.name))
384
385 def test_find_regtype(self):
386 tarinfo = self.tar.getmember("ustar/regtype")
387 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
388
389 def test_find_conttype(self):
390 tarinfo = self.tar.getmember("ustar/conttype")
391 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
392
393 def test_find_dirtype(self):
394 tarinfo = self.tar.getmember("ustar/dirtype")
395 self._test_member(tarinfo, size=0)
396
397 def test_find_dirtype_with_size(self):
398 tarinfo = self.tar.getmember("ustar/dirtype-with-size")
399 self._test_member(tarinfo, size=255)
400
401 def test_find_lnktype(self):
402 tarinfo = self.tar.getmember("ustar/lnktype")
403 self._test_member(tarinfo, size=0, linkname="ustar/regtype")
404
405 def test_find_symtype(self):
406 tarinfo = self.tar.getmember("ustar/symtype")
407 self._test_member(tarinfo, size=0, linkname="regtype")
408
409 def test_find_blktype(self):
410 tarinfo = self.tar.getmember("ustar/blktype")
411 self._test_member(tarinfo, size=0, devmajor=3, devminor=0)
412
413 def test_find_chrtype(self):
414 tarinfo = self.tar.getmember("ustar/chrtype")
415 self._test_member(tarinfo, size=0, devmajor=1, devminor=3)
416
417 def test_find_fifotype(self):
418 tarinfo = self.tar.getmember("ustar/fifotype")
419 self._test_member(tarinfo, size=0)
420
421 def test_find_sparse(self):
422 tarinfo = self.tar.getmember("ustar/sparse")
423 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
424
425 def test_find_umlauts(self):
426 tarinfo = self.tar.getmember("ustar/umlauts-ÄÖÜäöüß")
427 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
428
429 def test_find_ustar_longname(self):
430 name = "ustar/" + "12345/" * 39 + "1234567/longname"
431 self.assert_(name in self.tar.getnames())
432
433 def test_find_regtype_oldv7(self):
434 tarinfo = self.tar.getmember("misc/regtype-old-v7")
435 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
436
437 def test_find_pax_umlauts(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000438 self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000439 tarinfo = self.tar.getmember("pax/umlauts-ÄÖÜäöüß")
440 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
441
442
443class LongnameTest(ReadTest):
444
445 def test_read_longname(self):
446 # Test reading of longname (bug #1471427).
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000447 longname = self.subdir + "/" + "123/" * 125 + "longname"
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000448 try:
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000449 tarinfo = self.tar.getmember(longname)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000450 except KeyError:
451 self.fail("longname not found")
452 self.assert_(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype")
453
454 def test_read_longlink(self):
455 longname = self.subdir + "/" + "123/" * 125 + "longname"
456 longlink = self.subdir + "/" + "123/" * 125 + "longlink"
457 try:
458 tarinfo = self.tar.getmember(longlink)
459 except KeyError:
460 self.fail("longlink not found")
461 self.assert_(tarinfo.linkname == longname, "linkname wrong")
462
463 def test_truncated_longname(self):
464 longname = self.subdir + "/" + "123/" * 125 + "longname"
465 tarinfo = self.tar.getmember(longname)
466 offset = tarinfo.offset
467 self.tar.fileobj.seek(offset)
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000468 fobj = StringIO.StringIO(self.tar.fileobj.read(3 * 512))
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000469 self.assertRaises(tarfile.ReadError, tarfile.open, name="foo.tar", fileobj=fobj)
470
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000471 def test_header_offset(self):
472 # Test if the start offset of the TarInfo object includes
473 # the preceding extended header.
474 longname = self.subdir + "/" + "123/" * 125 + "longname"
475 offset = self.tar.getmember(longname).offset
476 fobj = open(tarname)
477 fobj.seek(offset)
478 tarinfo = tarfile.TarInfo.frombuf(fobj.read(512))
479 self.assertEqual(tarinfo.type, self.longnametype)
480
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000481
482class GNUReadTest(LongnameTest):
483
484 subdir = "gnu"
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000485 longnametype = tarfile.GNUTYPE_LONGNAME
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000486
487 def test_sparse_file(self):
488 tarinfo1 = self.tar.getmember("ustar/sparse")
489 fobj1 = self.tar.extractfile(tarinfo1)
490 tarinfo2 = self.tar.getmember("gnu/sparse")
491 fobj2 = self.tar.extractfile(tarinfo2)
492 self.assert_(fobj1.read() == fobj2.read(),
493 "sparse file extraction failed")
494
495
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000496class PaxReadTest(LongnameTest):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000497
498 subdir = "pax"
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000499 longnametype = tarfile.XHDTYPE
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000500
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000501 def test_pax_global_headers(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000502 tar = tarfile.open(tarname, encoding="iso8859-1")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000503
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000504 tarinfo = tar.getmember("pax/regtype1")
505 self.assertEqual(tarinfo.uname, "foo")
506 self.assertEqual(tarinfo.gname, "bar")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000507 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000508
509 tarinfo = tar.getmember("pax/regtype2")
510 self.assertEqual(tarinfo.uname, "")
511 self.assertEqual(tarinfo.gname, "bar")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000512 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000513
514 tarinfo = tar.getmember("pax/regtype3")
515 self.assertEqual(tarinfo.uname, "tarfile")
516 self.assertEqual(tarinfo.gname, "tarfile")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000517 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
518
519 def test_pax_number_fields(self):
520 # All following number fields are read from the pax header.
521 tar = tarfile.open(tarname, encoding="iso8859-1")
522 tarinfo = tar.getmember("pax/regtype4")
523 self.assertEqual(tarinfo.size, 7011)
524 self.assertEqual(tarinfo.uid, 123)
525 self.assertEqual(tarinfo.gid, 123)
526 self.assertEqual(tarinfo.mtime, 1041808783.0)
527 self.assertEqual(type(tarinfo.mtime), float)
528 self.assertEqual(float(tarinfo.pax_headers["atime"]), 1041808783.0)
529 self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000530
531
Lars Gustäbelb1a54a32008-05-27 12:39:23 +0000532class WriteTestBase(unittest.TestCase):
533 # Put all write tests in here that are supposed to be tested
534 # in all possible mode combinations.
535
536 def test_fileobj_no_close(self):
537 fobj = StringIO.StringIO()
538 tar = tarfile.open(fileobj=fobj, mode=self.mode)
539 tar.addfile(tarfile.TarInfo("foo"))
540 tar.close()
541 self.assert_(fobj.closed is False, "external fileobjs must never closed")
542
543
544class WriteTest(WriteTestBase):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000545
546 mode = "w:"
547
548 def test_100_char_name(self):
549 # The name field in a tar header stores strings of at most 100 chars.
550 # If a string is shorter than 100 chars it has to be padded with '\0',
551 # which implies that a string of exactly 100 chars is stored without
552 # a trailing '\0'.
553 name = "0123456789" * 10
554 tar = tarfile.open(tmpname, self.mode)
555 t = tarfile.TarInfo(name)
556 tar.addfile(t)
Lars Gustäbel3f8aca12007-02-06 18:38:13 +0000557 tar.close()
558
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000559 tar = tarfile.open(tmpname)
560 self.assert_(tar.getnames()[0] == name,
Georg Brandla32e0a02006-10-24 16:54:16 +0000561 "failed to store 100 char filename")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000562 tar.close()
Georg Brandla32e0a02006-10-24 16:54:16 +0000563
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000564 def test_tar_size(self):
565 # Test for bug #1013882.
566 tar = tarfile.open(tmpname, self.mode)
567 path = os.path.join(TEMPDIR, "file")
568 fobj = open(path, "wb")
569 fobj.write("aaa")
570 fobj.close()
571 tar.add(path)
572 tar.close()
573 self.assert_(os.path.getsize(tmpname) > 0,
574 "tarfile is empty")
Georg Brandla32e0a02006-10-24 16:54:16 +0000575
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000576 # The test_*_size tests test for bug #1167128.
577 def test_file_size(self):
578 tar = tarfile.open(tmpname, self.mode)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000579
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000580 path = os.path.join(TEMPDIR, "file")
581 fobj = open(path, "wb")
582 fobj.close()
583 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000584 self.assertEqual(tarinfo.size, 0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000585
586 fobj = open(path, "wb")
587 fobj.write("aaa")
588 fobj.close()
589 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000590 self.assertEqual(tarinfo.size, 3)
591
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000592 tar.close()
593
594 def test_directory_size(self):
595 path = os.path.join(TEMPDIR, "directory")
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000596 os.mkdir(path)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000597 try:
598 tar = tarfile.open(tmpname, self.mode)
599 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000600 self.assertEqual(tarinfo.size, 0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000601 finally:
602 os.rmdir(path)
603
604 def test_link_size(self):
605 if hasattr(os, "link"):
606 link = os.path.join(TEMPDIR, "link")
607 target = os.path.join(TEMPDIR, "link_target")
608 open(target, "wb").close()
609 os.link(target, link)
610 try:
611 tar = tarfile.open(tmpname, self.mode)
612 tarinfo = tar.gettarinfo(link)
613 self.assertEqual(tarinfo.size, 0)
614 finally:
615 os.remove(target)
616 os.remove(link)
617
618 def test_symlink_size(self):
619 if hasattr(os, "symlink"):
620 path = os.path.join(TEMPDIR, "symlink")
621 os.symlink("link_target", path)
622 try:
623 tar = tarfile.open(tmpname, self.mode)
624 tarinfo = tar.gettarinfo(path)
625 self.assertEqual(tarinfo.size, 0)
626 finally:
627 os.remove(path)
628
629 def test_add_self(self):
630 # Test for #1257255.
631 dstname = os.path.abspath(tmpname)
632
633 tar = tarfile.open(tmpname, self.mode)
634 self.assert_(tar.name == dstname, "archive name must be absolute")
635
636 tar.add(dstname)
637 self.assert_(tar.getnames() == [], "added the archive to itself")
638
639 cwd = os.getcwd()
640 os.chdir(TEMPDIR)
641 tar.add(dstname)
642 os.chdir(cwd)
643 self.assert_(tar.getnames() == [], "added the archive to itself")
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000644
Lars Gustäbel104490e2007-06-18 11:42:11 +0000645 def test_exclude(self):
646 tempdir = os.path.join(TEMPDIR, "exclude")
647 os.mkdir(tempdir)
648 try:
649 for name in ("foo", "bar", "baz"):
650 name = os.path.join(tempdir, name)
651 open(name, "wb").close()
652
653 def exclude(name):
654 return os.path.isfile(name)
655
656 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
657 tar.add(tempdir, arcname="empty_dir", exclude=exclude)
658 tar.close()
659
660 tar = tarfile.open(tmpname, "r")
661 self.assertEqual(len(tar.getmembers()), 1)
662 self.assertEqual(tar.getnames()[0], "empty_dir")
663 finally:
664 shutil.rmtree(tempdir)
665
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000666
Lars Gustäbelb1a54a32008-05-27 12:39:23 +0000667class StreamWriteTest(WriteTestBase):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000668
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000669 mode = "w|"
Neal Norwitz8a519392006-08-21 17:59:46 +0000670
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000671 def test_stream_padding(self):
672 # Test for bug #1543303.
673 tar = tarfile.open(tmpname, self.mode)
674 tar.close()
675
676 if self.mode.endswith("gz"):
677 fobj = gzip.GzipFile(tmpname)
678 data = fobj.read()
679 fobj.close()
680 elif self.mode.endswith("bz2"):
681 dec = bz2.BZ2Decompressor()
682 data = open(tmpname, "rb").read()
683 data = dec.decompress(data)
684 self.assert_(len(dec.unused_data) == 0,
685 "found trailing data")
Neal Norwitz8a519392006-08-21 17:59:46 +0000686 else:
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000687 fobj = open(tmpname, "rb")
688 data = fobj.read()
689 fobj.close()
Neal Norwitz8a519392006-08-21 17:59:46 +0000690
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000691 self.assert_(data.count("\0") == tarfile.RECORDSIZE,
Neal Norwitz8a519392006-08-21 17:59:46 +0000692 "incorrect zero padding")
693
694
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000695class GNUWriteTest(unittest.TestCase):
696 # This testcase checks for correct creation of GNU Longname
697 # and Longlink extended headers (cp. bug #812325).
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000698
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000699 def _length(self, s):
700 blocks, remainder = divmod(len(s) + 1, 512)
701 if remainder:
702 blocks += 1
703 return blocks * 512
704
705 def _calc_size(self, name, link=None):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000706 # Initial tar header
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000707 count = 512
708
709 if len(name) > tarfile.LENGTH_NAME:
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000710 # GNU longname extended header + longname
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000711 count += 512
712 count += self._length(name)
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000713 if link is not None and len(link) > tarfile.LENGTH_LINK:
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000714 # GNU longlink extended header + longlink
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000715 count += 512
716 count += self._length(link)
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000717 return count
718
719 def _test(self, name, link=None):
720 tarinfo = tarfile.TarInfo(name)
721 if link:
722 tarinfo.linkname = link
723 tarinfo.type = tarfile.LNKTYPE
724
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000725 tar = tarfile.open(tmpname, "w")
726 tar.format = tarfile.GNU_FORMAT
Georg Brandl87fa5592006-12-06 22:21:18 +0000727 tar.addfile(tarinfo)
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000728
729 v1 = self._calc_size(name, link)
Georg Brandl87fa5592006-12-06 22:21:18 +0000730 v2 = tar.offset
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000731 self.assert_(v1 == v2, "GNU longname/longlink creation failed")
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000732
Georg Brandl87fa5592006-12-06 22:21:18 +0000733 tar.close()
734
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000735 tar = tarfile.open(tmpname)
Georg Brandl87fa5592006-12-06 22:21:18 +0000736 member = tar.next()
737 self.failIf(member is None, "unable to read longname member")
738 self.assert_(tarinfo.name == member.name and \
739 tarinfo.linkname == member.linkname, \
740 "unable to read longname member")
741
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000742 def test_longname_1023(self):
743 self._test(("longnam/" * 127) + "longnam")
744
745 def test_longname_1024(self):
746 self._test(("longnam/" * 127) + "longname")
747
748 def test_longname_1025(self):
749 self._test(("longnam/" * 127) + "longname_")
750
751 def test_longlink_1023(self):
752 self._test("name", ("longlnk/" * 127) + "longlnk")
753
754 def test_longlink_1024(self):
755 self._test("name", ("longlnk/" * 127) + "longlink")
756
757 def test_longlink_1025(self):
758 self._test("name", ("longlnk/" * 127) + "longlink_")
759
760 def test_longnamelink_1023(self):
761 self._test(("longnam/" * 127) + "longnam",
762 ("longlnk/" * 127) + "longlnk")
763
764 def test_longnamelink_1024(self):
765 self._test(("longnam/" * 127) + "longname",
766 ("longlnk/" * 127) + "longlink")
767
768 def test_longnamelink_1025(self):
769 self._test(("longnam/" * 127) + "longname_",
770 ("longlnk/" * 127) + "longlink_")
771
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000772
773class HardlinkTest(unittest.TestCase):
774 # Test the creation of LNKTYPE (hardlink) members in an archive.
Georg Brandl38c6a222006-05-10 16:26:03 +0000775
776 def setUp(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000777 self.foo = os.path.join(TEMPDIR, "foo")
778 self.bar = os.path.join(TEMPDIR, "bar")
Georg Brandl38c6a222006-05-10 16:26:03 +0000779
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000780 fobj = open(self.foo, "wb")
781 fobj.write("foo")
782 fobj.close()
Georg Brandl38c6a222006-05-10 16:26:03 +0000783
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000784 os.link(self.foo, self.bar)
Georg Brandl38c6a222006-05-10 16:26:03 +0000785
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000786 self.tar = tarfile.open(tmpname, "w")
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000787 self.tar.add(self.foo)
788
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000789 def tearDown(self):
Hirokazu Yamamoto56d380d2008-09-21 11:44:23 +0000790 self.tar.close()
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000791 os.remove(self.foo)
792 os.remove(self.bar)
793
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000794 def test_add_twice(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000795 # The same name will be added as a REGTYPE every
796 # time regardless of st_nlink.
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000797 tarinfo = self.tar.gettarinfo(self.foo)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000798 self.assert_(tarinfo.type == tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000799 "add file as regular failed")
800
801 def test_add_hardlink(self):
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000802 tarinfo = self.tar.gettarinfo(self.bar)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000803 self.assert_(tarinfo.type == tarfile.LNKTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000804 "add file as hardlink failed")
805
806 def test_dereference_hardlink(self):
807 self.tar.dereference = True
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000808 tarinfo = self.tar.gettarinfo(self.bar)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000809 self.assert_(tarinfo.type == tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000810 "dereferencing hardlink failed")
811
Neal Norwitza4f651a2004-07-20 22:07:44 +0000812
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000813class PaxWriteTest(GNUWriteTest):
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000814
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000815 def _test(self, name, link=None):
816 # See GNUWriteTest.
817 tarinfo = tarfile.TarInfo(name)
818 if link:
819 tarinfo.linkname = link
820 tarinfo.type = tarfile.LNKTYPE
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +0000821
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000822 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT)
823 tar.addfile(tarinfo)
824 tar.close()
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +0000825
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000826 tar = tarfile.open(tmpname)
827 if link:
828 l = tar.getmembers()[0].linkname
829 self.assert_(link == l, "PAX longlink creation failed")
830 else:
831 n = tar.getmembers()[0].name
832 self.assert_(name == n, "PAX longname creation failed")
833
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000834 def test_pax_global_header(self):
835 pax_headers = {
836 u"foo": u"bar",
837 u"uid": u"0",
838 u"mtime": u"1.23",
839 u"test": u"äöü",
840 u"äöü": u"test"}
841
842 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, \
843 pax_headers=pax_headers)
844 tar.addfile(tarfile.TarInfo("test"))
845 tar.close()
846
847 # Test if the global header was written correctly.
848 tar = tarfile.open(tmpname, encoding="iso8859-1")
849 self.assertEqual(tar.pax_headers, pax_headers)
850 self.assertEqual(tar.getmembers()[0].pax_headers, pax_headers)
851
852 # Test if all the fields are unicode.
853 for key, val in tar.pax_headers.iteritems():
854 self.assert_(type(key) is unicode)
855 self.assert_(type(val) is unicode)
856 if key in tarfile.PAX_NUMBER_FIELDS:
857 try:
858 tarfile.PAX_NUMBER_FIELDS[key](val)
859 except (TypeError, ValueError):
860 self.fail("unable to convert pax header field")
861
862 def test_pax_extended_header(self):
863 # The fields from the pax header have priority over the
864 # TarInfo.
865 pax_headers = {u"path": u"foo", u"uid": u"123"}
866
867 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1")
868 t = tarfile.TarInfo()
869 t.name = u"äöü" # non-ASCII
870 t.uid = 8**8 # too large
871 t.pax_headers = pax_headers
872 tar.addfile(t)
873 tar.close()
874
875 tar = tarfile.open(tmpname, encoding="iso8859-1")
876 t = tar.getmembers()[0]
877 self.assertEqual(t.pax_headers, pax_headers)
878 self.assertEqual(t.name, "foo")
879 self.assertEqual(t.uid, 123)
880
881
882class UstarUnicodeTest(unittest.TestCase):
883 # All *UnicodeTests FIXME
884
885 format = tarfile.USTAR_FORMAT
886
887 def test_iso8859_1_filename(self):
888 self._test_unicode_filename("iso8859-1")
889
890 def test_utf7_filename(self):
891 self._test_unicode_filename("utf7")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000892
893 def test_utf8_filename(self):
894 self._test_unicode_filename("utf8")
895
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000896 def _test_unicode_filename(self, encoding):
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000897 tar = tarfile.open(tmpname, "w", format=self.format, encoding=encoding, errors="strict")
898 name = u"äöü"
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000899 tar.addfile(tarfile.TarInfo(name))
900 tar.close()
901
902 tar = tarfile.open(tmpname, encoding=encoding)
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000903 self.assert_(type(tar.getnames()[0]) is not unicode)
904 self.assertEqual(tar.getmembers()[0].name, name.encode(encoding))
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000905 tar.close()
906
907 def test_unicode_filename_error(self):
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000908 tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict")
909 tarinfo = tarfile.TarInfo()
910
911 tarinfo.name = "äöü"
912 if self.format == tarfile.PAX_FORMAT:
913 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
914 else:
915 tar.addfile(tarinfo)
916
917 tarinfo.name = u"äöü"
918 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
919
920 tarinfo.name = "foo"
921 tarinfo.uname = u"äöü"
922 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
923
924 def test_unicode_argument(self):
925 tar = tarfile.open(tarname, "r", encoding="iso8859-1", errors="strict")
926 for t in tar:
927 self.assert_(type(t.name) is str)
928 self.assert_(type(t.linkname) is str)
929 self.assert_(type(t.uname) is str)
930 self.assert_(type(t.gname) is str)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000931 tar.close()
932
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000933 def test_uname_unicode(self):
934 for name in (u"äöü", "äöü"):
935 t = tarfile.TarInfo("foo")
936 t.uname = name
937 t.gname = name
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000938
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000939 fobj = StringIO.StringIO()
940 tar = tarfile.open("foo.tar", mode="w", fileobj=fobj, format=self.format, encoding="iso8859-1")
941 tar.addfile(t)
942 tar.close()
943 fobj.seek(0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000944
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000945 tar = tarfile.open("foo.tar", fileobj=fobj, encoding="iso8859-1")
946 t = tar.getmember("foo")
947 self.assertEqual(t.uname, "äöü")
948 self.assertEqual(t.gname, "äöü")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000949
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000950
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000951class GNUUnicodeTest(UstarUnicodeTest):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000952
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000953 format = tarfile.GNU_FORMAT
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000954
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000955
956class PaxUnicodeTest(UstarUnicodeTest):
957
958 format = tarfile.PAX_FORMAT
959
960 def _create_unicode_name(self, name):
961 tar = tarfile.open(tmpname, "w", format=self.format)
962 t = tarfile.TarInfo()
963 t.pax_headers["path"] = name
964 tar.addfile(t)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000965 tar.close()
966
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000967 def test_error_handlers(self):
968 # Test if the unicode error handlers work correctly for characters
969 # that cannot be expressed in a given encoding.
970 self._create_unicode_name(u"äöü")
Georg Brandlded1c4d2006-12-20 11:55:16 +0000971
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000972 for handler, name in (("utf-8", u"äöü".encode("utf8")),
973 ("replace", "???"), ("ignore", "")):
974 tar = tarfile.open(tmpname, format=self.format, encoding="ascii",
975 errors=handler)
976 self.assertEqual(tar.getnames()[0], name)
Georg Brandlded1c4d2006-12-20 11:55:16 +0000977
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000978 self.assertRaises(UnicodeError, tarfile.open, tmpname,
979 encoding="ascii", errors="strict")
980
981 def test_error_handler_utf8(self):
982 # Create a pathname that has one component representable using
983 # iso8859-1 and the other only in iso8859-15.
984 self._create_unicode_name(u"äöü/¤")
985
986 tar = tarfile.open(tmpname, format=self.format, encoding="iso8859-1",
987 errors="utf-8")
988 self.assertEqual(tar.getnames()[0], "äöü/" + u"¤".encode("utf8"))
Georg Brandlded1c4d2006-12-20 11:55:16 +0000989
Georg Brandlded1c4d2006-12-20 11:55:16 +0000990
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000991class AppendTest(unittest.TestCase):
992 # Test append mode (cp. patch #1652681).
Tim Peters8ceefc52004-10-25 03:19:41 +0000993
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000994 def setUp(self):
995 self.tarname = tmpname
996 if os.path.exists(self.tarname):
997 os.remove(self.tarname)
Lars Gustäbela7ba6fc2006-12-27 10:30:46 +0000998
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000999 def _add_testfile(self, fileobj=None):
1000 tar = tarfile.open(self.tarname, "a", fileobj=fileobj)
1001 tar.addfile(tarfile.TarInfo("bar"))
1002 tar.close()
Lars Gustäbela7ba6fc2006-12-27 10:30:46 +00001003
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001004 def _create_testtar(self, mode="w:"):
Lars Gustäbela36cde42007-03-13 15:47:07 +00001005 src = tarfile.open(tarname, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001006 t = src.getmember("ustar/regtype")
1007 t.name = "foo"
1008 f = src.extractfile(t)
1009 tar = tarfile.open(self.tarname, mode)
1010 tar.addfile(t, f)
1011 tar.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001012
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001013 def _test(self, names=["bar"], fileobj=None):
1014 tar = tarfile.open(self.tarname, fileobj=fileobj)
1015 self.assertEqual(tar.getnames(), names)
1016
1017 def test_non_existing(self):
1018 self._add_testfile()
1019 self._test()
1020
1021 def test_empty(self):
1022 open(self.tarname, "w").close()
1023 self._add_testfile()
1024 self._test()
1025
1026 def test_empty_fileobj(self):
1027 fobj = StringIO.StringIO()
1028 self._add_testfile(fobj)
1029 fobj.seek(0)
1030 self._test(fileobj=fobj)
1031
1032 def test_fileobj(self):
1033 self._create_testtar()
1034 data = open(self.tarname).read()
1035 fobj = StringIO.StringIO(data)
1036 self._add_testfile(fobj)
1037 fobj.seek(0)
1038 self._test(names=["foo", "bar"], fileobj=fobj)
1039
1040 def test_existing(self):
1041 self._create_testtar()
1042 self._add_testfile()
1043 self._test(names=["foo", "bar"])
1044
1045 def test_append_gz(self):
1046 if gzip is None:
1047 return
1048 self._create_testtar("w:gz")
1049 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
1050
1051 def test_append_bz2(self):
1052 if bz2 is None:
1053 return
1054 self._create_testtar("w:bz2")
1055 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
1056
1057
1058class LimitsTest(unittest.TestCase):
1059
1060 def test_ustar_limits(self):
1061 # 100 char name
1062 tarinfo = tarfile.TarInfo("0123456789" * 10)
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001063 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001064
1065 # 101 char name that cannot be stored
1066 tarinfo = tarfile.TarInfo("0123456789" * 10 + "0")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001067 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001068
1069 # 256 char name with a slash at pos 156
1070 tarinfo = tarfile.TarInfo("123/" * 62 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001071 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001072
1073 # 256 char name that cannot be stored
1074 tarinfo = tarfile.TarInfo("1234567/" * 31 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001075 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001076
1077 # 512 char name
1078 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001079 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001080
1081 # 512 char linkname
1082 tarinfo = tarfile.TarInfo("longlink")
1083 tarinfo.linkname = "123/" * 126 + "longname"
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001084 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001085
1086 # uid > 8 digits
1087 tarinfo = tarfile.TarInfo("name")
1088 tarinfo.uid = 010000000
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001089 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001090
1091 def test_gnu_limits(self):
1092 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001093 tarinfo.tobuf(tarfile.GNU_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001094
1095 tarinfo = tarfile.TarInfo("longlink")
1096 tarinfo.linkname = "123/" * 126 + "longname"
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001097 tarinfo.tobuf(tarfile.GNU_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001098
1099 # uid >= 256 ** 7
1100 tarinfo = tarfile.TarInfo("name")
1101 tarinfo.uid = 04000000000000000000L
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001102 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.GNU_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001103
1104 def test_pax_limits(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001105 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001106 tarinfo.tobuf(tarfile.PAX_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001107
1108 tarinfo = tarfile.TarInfo("longlink")
1109 tarinfo.linkname = "123/" * 126 + "longname"
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001110 tarinfo.tobuf(tarfile.PAX_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001111
1112 tarinfo = tarfile.TarInfo("name")
1113 tarinfo.uid = 04000000000000000000L
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001114 tarinfo.tobuf(tarfile.PAX_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001115
1116
1117class GzipMiscReadTest(MiscReadTest):
1118 tarname = gzipname
1119 mode = "r:gz"
1120class GzipUstarReadTest(UstarReadTest):
1121 tarname = gzipname
1122 mode = "r:gz"
1123class GzipStreamReadTest(StreamReadTest):
1124 tarname = gzipname
1125 mode = "r|gz"
1126class GzipWriteTest(WriteTest):
1127 mode = "w:gz"
1128class GzipStreamWriteTest(StreamWriteTest):
1129 mode = "w|gz"
1130
1131
1132class Bz2MiscReadTest(MiscReadTest):
1133 tarname = bz2name
1134 mode = "r:bz2"
1135class Bz2UstarReadTest(UstarReadTest):
1136 tarname = bz2name
1137 mode = "r:bz2"
1138class Bz2StreamReadTest(StreamReadTest):
1139 tarname = bz2name
1140 mode = "r|bz2"
1141class Bz2WriteTest(WriteTest):
1142 mode = "w:bz2"
1143class Bz2StreamWriteTest(StreamWriteTest):
1144 mode = "w|bz2"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001145
Neal Norwitz996acf12003-02-17 14:51:41 +00001146def test_main():
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001147 if not os.path.exists(TEMPDIR):
1148 os.mkdir(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001149
Walter Dörwald21d3a322003-05-01 17:45:56 +00001150 tests = [
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001151 UstarReadTest,
1152 MiscReadTest,
1153 StreamReadTest,
1154 DetectReadTest,
1155 MemberReadTest,
1156 GNUReadTest,
1157 PaxReadTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001158 WriteTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001159 StreamWriteTest,
1160 GNUWriteTest,
1161 PaxWriteTest,
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001162 UstarUnicodeTest,
1163 GNUUnicodeTest,
1164 PaxUnicodeTest,
Lars Gustäbel3f8aca12007-02-06 18:38:13 +00001165 AppendTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001166 LimitsTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001167 ]
1168
Neal Norwitza4f651a2004-07-20 22:07:44 +00001169 if hasattr(os, "link"):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001170 tests.append(HardlinkTest)
1171
1172 fobj = open(tarname, "rb")
1173 data = fobj.read()
1174 fobj.close()
Neal Norwitza4f651a2004-07-20 22:07:44 +00001175
Walter Dörwald21d3a322003-05-01 17:45:56 +00001176 if gzip:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001177 # Create testtar.tar.gz and add gzip-specific tests.
1178 tar = gzip.open(gzipname, "wb")
1179 tar.write(data)
1180 tar.close()
1181
1182 tests += [
1183 GzipMiscReadTest,
1184 GzipUstarReadTest,
1185 GzipStreamReadTest,
1186 GzipWriteTest,
1187 GzipStreamWriteTest,
1188 ]
Walter Dörwald21d3a322003-05-01 17:45:56 +00001189
1190 if bz2:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001191 # Create testtar.tar.bz2 and add bz2-specific tests.
1192 tar = bz2.BZ2File(bz2name, "wb")
1193 tar.write(data)
1194 tar.close()
1195
1196 tests += [
1197 Bz2MiscReadTest,
1198 Bz2UstarReadTest,
1199 Bz2StreamReadTest,
1200 Bz2WriteTest,
1201 Bz2StreamWriteTest,
1202 ]
1203
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001204 try:
Walter Dörwald21d3a322003-05-01 17:45:56 +00001205 test_support.run_unittest(*tests)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001206 finally:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001207 if os.path.exists(TEMPDIR):
1208 shutil.rmtree(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001209
Neal Norwitz996acf12003-02-17 14:51:41 +00001210if __name__ == "__main__":
1211 test_main()