blob: 75cc0e73d622404f3d8ef4293b266c92713b3494 [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
Antoine Pitrou80e44f82009-11-11 20:57:55 +000030TEMPDIR = os.path.abspath(test_support.TESTFN)
31tarname = test_support.findfile("testtar.tar")
Lars Gustäbelc64e4022007-03-13 10:47:19 +000032gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
33bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
34tmpname = os.path.join(TEMPDIR, "tmp.tar")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000035
Lars Gustäbelc64e4022007-03-13 10:47:19 +000036md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
37md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000038
39
Lars Gustäbelc64e4022007-03-13 10:47:19 +000040class ReadTest(unittest.TestCase):
41
42 tarname = tarname
43 mode = "r:"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000044
45 def setUp(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +000046 self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000047
48 def tearDown(self):
49 self.tar.close()
50
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000051
Lars Gustäbelc64e4022007-03-13 10:47:19 +000052class UstarReadTest(ReadTest):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000053
Lars Gustäbelc64e4022007-03-13 10:47:19 +000054 def test_fileobj_regular_file(self):
55 tarinfo = self.tar.getmember("ustar/regtype")
56 fobj = self.tar.extractfile(tarinfo)
57 data = fobj.read()
58 self.assert_((len(data), md5sum(data)) == (tarinfo.size, md5_regtype),
59 "regular file extraction failed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000060
Lars Gustäbelc64e4022007-03-13 10:47:19 +000061 def test_fileobj_readlines(self):
62 self.tar.extract("ustar/regtype", TEMPDIR)
63 tarinfo = self.tar.getmember("ustar/regtype")
64 fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "rU")
65 fobj2 = self.tar.extractfile(tarinfo)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000066
Lars Gustäbelc64e4022007-03-13 10:47:19 +000067 lines1 = fobj1.readlines()
68 lines2 = fobj2.readlines()
69 self.assert_(lines1 == lines2,
70 "fileobj.readlines() failed")
71 self.assert_(len(lines2) == 114,
72 "fileobj.readlines() failed")
73 self.assert_(lines2[83] == \
74 "I will gladly admit that Python is not the fastest running scripting language.\n",
75 "fileobj.readlines() failed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000076
Lars Gustäbelc64e4022007-03-13 10:47:19 +000077 def test_fileobj_iter(self):
78 self.tar.extract("ustar/regtype", TEMPDIR)
79 tarinfo = self.tar.getmember("ustar/regtype")
80 fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "rU")
81 fobj2 = self.tar.extractfile(tarinfo)
82 lines1 = fobj1.readlines()
83 lines2 = [line for line in fobj2]
84 self.assert_(lines1 == lines2,
85 "fileobj.__iter__() failed")
Martin v. Löwisdf241532005-03-03 08:17:42 +000086
Lars Gustäbelc64e4022007-03-13 10:47:19 +000087 def test_fileobj_seek(self):
88 self.tar.extract("ustar/regtype", TEMPDIR)
89 fobj = open(os.path.join(TEMPDIR, "ustar/regtype"), "rb")
90 data = fobj.read()
Neal Norwitzf3396542005-10-28 05:52:22 +000091 fobj.close()
92
Lars Gustäbelc64e4022007-03-13 10:47:19 +000093 tarinfo = self.tar.getmember("ustar/regtype")
94 fobj = self.tar.extractfile(tarinfo)
95
96 text = fobj.read()
97 fobj.seek(0)
98 self.assert_(0 == fobj.tell(),
99 "seek() to file's start failed")
100 fobj.seek(2048, 0)
101 self.assert_(2048 == fobj.tell(),
102 "seek() to absolute position failed")
103 fobj.seek(-1024, 1)
104 self.assert_(1024 == fobj.tell(),
105 "seek() to negative relative position failed")
106 fobj.seek(1024, 1)
107 self.assert_(2048 == fobj.tell(),
108 "seek() to positive relative position failed")
109 s = fobj.read(10)
110 self.assert_(s == data[2048:2058],
111 "read() after seek failed")
112 fobj.seek(0, 2)
113 self.assert_(tarinfo.size == fobj.tell(),
114 "seek() to file's end failed")
115 self.assert_(fobj.read() == "",
116 "read() at file's end did not return empty string")
117 fobj.seek(-tarinfo.size, 2)
118 self.assert_(0 == fobj.tell(),
119 "relative seek() to file's start failed")
120 fobj.seek(512)
121 s1 = fobj.readlines()
122 fobj.seek(512)
123 s2 = fobj.readlines()
124 self.assert_(s1 == s2,
125 "readlines() after seek failed")
126 fobj.seek(0)
127 self.assert_(len(fobj.readline()) == fobj.tell(),
128 "tell() after readline() failed")
129 fobj.seek(512)
130 self.assert_(len(fobj.readline()) + 512 == fobj.tell(),
131 "tell() after seek() and readline() failed")
132 fobj.seek(0)
133 line = fobj.readline()
134 self.assert_(fobj.read() == data[len(line):],
135 "read() after readline() failed")
136 fobj.close()
137
138
139class MiscReadTest(ReadTest):
140
Lars Gustäbel0f4a14b2007-08-28 12:31:09 +0000141 def test_no_name_argument(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000142 fobj = open(self.tarname, "rb")
143 tar = tarfile.open(fileobj=fobj, mode=self.mode)
144 self.assertEqual(tar.name, os.path.abspath(fobj.name))
145
Lars Gustäbel0f4a14b2007-08-28 12:31:09 +0000146 def test_no_name_attribute(self):
147 data = open(self.tarname, "rb").read()
148 fobj = StringIO.StringIO(data)
149 self.assertRaises(AttributeError, getattr, fobj, "name")
150 tar = tarfile.open(fileobj=fobj, mode=self.mode)
151 self.assertEqual(tar.name, None)
152
153 def test_empty_name_attribute(self):
154 data = open(self.tarname, "rb").read()
155 fobj = StringIO.StringIO(data)
156 fobj.name = ""
157 tar = tarfile.open(fileobj=fobj, mode=self.mode)
158 self.assertEqual(tar.name, None)
159
Lars Gustäbel77b2d632007-12-01 21:02:12 +0000160 def test_fileobj_with_offset(self):
161 # Skip the first member and store values from the second member
162 # of the testtar.
163 tar = tarfile.open(self.tarname, mode=self.mode)
164 tar.next()
165 t = tar.next()
166 name = t.name
167 offset = t.offset
168 data = tar.extractfile(t).read()
169 tar.close()
170
171 # Open the testtar and seek to the offset of the second member.
172 if self.mode.endswith(":gz"):
173 _open = gzip.GzipFile
174 elif self.mode.endswith(":bz2"):
175 _open = bz2.BZ2File
176 else:
177 _open = open
178 fobj = _open(self.tarname, "rb")
179 fobj.seek(offset)
180
181 # Test if the tarfile starts with the second member.
182 tar = tar.open(self.tarname, mode="r:", fileobj=fobj)
183 t = tar.next()
184 self.assertEqual(t.name, name)
185 # Read to the end of fileobj and test if seeking back to the
186 # beginning works.
187 tar.getmembers()
188 self.assertEqual(tar.extractfile(t).read(), data,
189 "seek back did not work")
190 tar.close()
191
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000192 def test_fail_comp(self):
193 # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
194 if self.mode == "r:":
195 return
196 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode)
197 fobj = open(tarname, "rb")
198 self.assertRaises(tarfile.ReadError, tarfile.open, fileobj=fobj, mode=self.mode)
199
200 def test_v7_dirtype(self):
201 # Test old style dirtype member (bug #1336623):
202 # Old V7 tars create directory members using an AREGTYPE
203 # header with a "/" appended to the filename field.
204 tarinfo = self.tar.getmember("misc/dirtype-old-v7")
205 self.assert_(tarinfo.type == tarfile.DIRTYPE,
206 "v7 dirtype failed")
207
Lars Gustäbel6bf51da2008-02-11 19:17:10 +0000208 def test_xstar_type(self):
209 # The xstar format stores extra atime and ctime fields inside the
210 # space reserved for the prefix field. The prefix field must be
211 # ignored in this case, otherwise it will mess up the name.
212 try:
213 self.tar.getmember("misc/regtype-xstar")
214 except KeyError:
215 self.fail("failed to find misc/regtype-xstar (mangled prefix?)")
216
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000217 def test_check_members(self):
218 for tarinfo in self.tar:
219 self.assert_(int(tarinfo.mtime) == 07606136617,
220 "wrong mtime for %s" % tarinfo.name)
221 if not tarinfo.name.startswith("ustar/"):
222 continue
223 self.assert_(tarinfo.uname == "tarfile",
224 "wrong uname for %s" % tarinfo.name)
225
226 def test_find_members(self):
227 self.assert_(self.tar.getmembers()[-1].name == "misc/eof",
228 "could not find all members")
229
230 def test_extract_hardlink(self):
231 # Test hardlink extraction (e.g. bug #857297).
Lars Gustäbela36cde42007-03-13 15:47:07 +0000232 tar = tarfile.open(tarname, errorlevel=1, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000233
234 tar.extract("ustar/regtype", TEMPDIR)
Neal Norwitzf3396542005-10-28 05:52:22 +0000235 try:
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000236 tar.extract("ustar/lnktype", TEMPDIR)
237 except EnvironmentError, e:
238 if e.errno == errno.ENOENT:
239 self.fail("hardlink not extracted properly")
Neal Norwitzf3396542005-10-28 05:52:22 +0000240
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000241 data = open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb").read()
242 self.assertEqual(md5sum(data), md5_regtype)
Neal Norwitzf3396542005-10-28 05:52:22 +0000243
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000244 try:
245 tar.extract("ustar/symtype", TEMPDIR)
246 except EnvironmentError, e:
247 if e.errno == errno.ENOENT:
248 self.fail("symlink not extracted properly")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000249
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000250 data = open(os.path.join(TEMPDIR, "ustar/symtype"), "rb").read()
251 self.assertEqual(md5sum(data), md5_regtype)
252
Lars Gustäbel2ee1c762008-01-04 14:00:33 +0000253 def test_extractall(self):
254 # Test if extractall() correctly restores directory permissions
255 # and times (see issue1735).
Lars Gustäbel2ee1c762008-01-04 14:00:33 +0000256 tar = tarfile.open(tarname, encoding="iso8859-1")
257 directories = [t for t in tar if t.isdir()]
258 tar.extractall(TEMPDIR, directories)
259 for tarinfo in directories:
260 path = os.path.join(TEMPDIR, tarinfo.name)
Lars Gustäbel696d6ba2008-12-12 14:14:42 +0000261 if sys.platform != "win32":
262 # Win32 has no support for fine grained permissions.
263 self.assertEqual(tarinfo.mode & 0777, os.stat(path).st_mode & 0777)
Lars Gustäbel2ee1c762008-01-04 14:00:33 +0000264 self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
265 tar.close()
266
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000267
268class StreamReadTest(ReadTest):
269
270 mode="r|"
271
272 def test_fileobj_regular_file(self):
273 tarinfo = self.tar.next() # get "regtype" (can't use getmember)
274 fobj = self.tar.extractfile(tarinfo)
275 data = fobj.read()
276 self.assert_((len(data), md5sum(data)) == (tarinfo.size, md5_regtype),
277 "regular file extraction failed")
278
279 def test_provoke_stream_error(self):
280 tarinfos = self.tar.getmembers()
281 f = self.tar.extractfile(tarinfos[0]) # read the first member
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000282 self.assertRaises(tarfile.StreamError, f.read)
283
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000284 def test_compare_members(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000285 tar1 = tarfile.open(tarname, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000286 tar2 = self.tar
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000287
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000288 while True:
289 t1 = tar1.next()
290 t2 = tar2.next()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000291 if t1 is None:
292 break
293 self.assert_(t2 is not None, "stream.next() failed.")
294
295 if t2.islnk() or t2.issym():
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000296 self.assertRaises(tarfile.StreamError, tar2.extractfile, t2)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000297 continue
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000298
299 v1 = tar1.extractfile(t1)
300 v2 = tar2.extractfile(t2)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000301 if v1 is None:
302 continue
303 self.assert_(v2 is not None, "stream.extractfile() failed")
304 self.assert_(v1.read() == v2.read(), "stream extraction failed")
305
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000306 tar1.close()
Lars Gustäbela4b23812006-12-23 17:57:23 +0000307
Georg Brandla32e0a02006-10-24 16:54:16 +0000308
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000309class DetectReadTest(unittest.TestCase):
Lars Gustäbel3f8aca12007-02-06 18:38:13 +0000310
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000311 def _testfunc_file(self, name, mode):
312 try:
313 tarfile.open(name, mode)
314 except tarfile.ReadError:
315 self.fail()
Lars Gustäbel3f8aca12007-02-06 18:38:13 +0000316
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000317 def _testfunc_fileobj(self, name, mode):
318 try:
319 tarfile.open(name, mode, fileobj=open(name, "rb"))
320 except tarfile.ReadError:
321 self.fail()
322
323 def _test_modes(self, testfunc):
324 testfunc(tarname, "r")
325 testfunc(tarname, "r:")
326 testfunc(tarname, "r:*")
327 testfunc(tarname, "r|")
328 testfunc(tarname, "r|*")
329
330 if gzip:
331 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:gz")
332 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|gz")
333 self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r:")
334 self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r|")
335
336 testfunc(gzipname, "r")
337 testfunc(gzipname, "r:*")
338 testfunc(gzipname, "r:gz")
339 testfunc(gzipname, "r|*")
340 testfunc(gzipname, "r|gz")
341
342 if bz2:
343 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:bz2")
344 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|bz2")
345 self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r:")
346 self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r|")
347
348 testfunc(bz2name, "r")
349 testfunc(bz2name, "r:*")
350 testfunc(bz2name, "r:bz2")
351 testfunc(bz2name, "r|*")
352 testfunc(bz2name, "r|bz2")
353
354 def test_detect_file(self):
355 self._test_modes(self._testfunc_file)
356
357 def test_detect_fileobj(self):
358 self._test_modes(self._testfunc_fileobj)
359
360
361class MemberReadTest(ReadTest):
362
363 def _test_member(self, tarinfo, chksum=None, **kwargs):
364 if chksum is not None:
365 self.assert_(md5sum(self.tar.extractfile(tarinfo).read()) == chksum,
366 "wrong md5sum for %s" % tarinfo.name)
367
368 kwargs["mtime"] = 07606136617
369 kwargs["uid"] = 1000
370 kwargs["gid"] = 100
371 if "old-v7" not in tarinfo.name:
372 # V7 tar can't handle alphabetic owners.
373 kwargs["uname"] = "tarfile"
374 kwargs["gname"] = "tarfile"
375 for k, v in kwargs.iteritems():
376 self.assert_(getattr(tarinfo, k) == v,
377 "wrong value in %s field of %s" % (k, tarinfo.name))
378
379 def test_find_regtype(self):
380 tarinfo = self.tar.getmember("ustar/regtype")
381 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
382
383 def test_find_conttype(self):
384 tarinfo = self.tar.getmember("ustar/conttype")
385 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
386
387 def test_find_dirtype(self):
388 tarinfo = self.tar.getmember("ustar/dirtype")
389 self._test_member(tarinfo, size=0)
390
391 def test_find_dirtype_with_size(self):
392 tarinfo = self.tar.getmember("ustar/dirtype-with-size")
393 self._test_member(tarinfo, size=255)
394
395 def test_find_lnktype(self):
396 tarinfo = self.tar.getmember("ustar/lnktype")
397 self._test_member(tarinfo, size=0, linkname="ustar/regtype")
398
399 def test_find_symtype(self):
400 tarinfo = self.tar.getmember("ustar/symtype")
401 self._test_member(tarinfo, size=0, linkname="regtype")
402
403 def test_find_blktype(self):
404 tarinfo = self.tar.getmember("ustar/blktype")
405 self._test_member(tarinfo, size=0, devmajor=3, devminor=0)
406
407 def test_find_chrtype(self):
408 tarinfo = self.tar.getmember("ustar/chrtype")
409 self._test_member(tarinfo, size=0, devmajor=1, devminor=3)
410
411 def test_find_fifotype(self):
412 tarinfo = self.tar.getmember("ustar/fifotype")
413 self._test_member(tarinfo, size=0)
414
415 def test_find_sparse(self):
416 tarinfo = self.tar.getmember("ustar/sparse")
417 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
418
419 def test_find_umlauts(self):
420 tarinfo = self.tar.getmember("ustar/umlauts-ÄÖÜäöüß")
421 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
422
423 def test_find_ustar_longname(self):
424 name = "ustar/" + "12345/" * 39 + "1234567/longname"
425 self.assert_(name in self.tar.getnames())
426
427 def test_find_regtype_oldv7(self):
428 tarinfo = self.tar.getmember("misc/regtype-old-v7")
429 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
430
431 def test_find_pax_umlauts(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000432 self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000433 tarinfo = self.tar.getmember("pax/umlauts-ÄÖÜäöüß")
434 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
435
436
437class LongnameTest(ReadTest):
438
439 def test_read_longname(self):
440 # Test reading of longname (bug #1471427).
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000441 longname = self.subdir + "/" + "123/" * 125 + "longname"
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000442 try:
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000443 tarinfo = self.tar.getmember(longname)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000444 except KeyError:
445 self.fail("longname not found")
446 self.assert_(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype")
447
448 def test_read_longlink(self):
449 longname = self.subdir + "/" + "123/" * 125 + "longname"
450 longlink = self.subdir + "/" + "123/" * 125 + "longlink"
451 try:
452 tarinfo = self.tar.getmember(longlink)
453 except KeyError:
454 self.fail("longlink not found")
455 self.assert_(tarinfo.linkname == longname, "linkname wrong")
456
457 def test_truncated_longname(self):
458 longname = self.subdir + "/" + "123/" * 125 + "longname"
459 tarinfo = self.tar.getmember(longname)
460 offset = tarinfo.offset
461 self.tar.fileobj.seek(offset)
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000462 fobj = StringIO.StringIO(self.tar.fileobj.read(3 * 512))
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000463 self.assertRaises(tarfile.ReadError, tarfile.open, name="foo.tar", fileobj=fobj)
464
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000465 def test_header_offset(self):
466 # Test if the start offset of the TarInfo object includes
467 # the preceding extended header.
468 longname = self.subdir + "/" + "123/" * 125 + "longname"
469 offset = self.tar.getmember(longname).offset
470 fobj = open(tarname)
471 fobj.seek(offset)
472 tarinfo = tarfile.TarInfo.frombuf(fobj.read(512))
473 self.assertEqual(tarinfo.type, self.longnametype)
474
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000475
476class GNUReadTest(LongnameTest):
477
478 subdir = "gnu"
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000479 longnametype = tarfile.GNUTYPE_LONGNAME
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000480
481 def test_sparse_file(self):
482 tarinfo1 = self.tar.getmember("ustar/sparse")
483 fobj1 = self.tar.extractfile(tarinfo1)
484 tarinfo2 = self.tar.getmember("gnu/sparse")
485 fobj2 = self.tar.extractfile(tarinfo2)
486 self.assert_(fobj1.read() == fobj2.read(),
487 "sparse file extraction failed")
488
489
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000490class PaxReadTest(LongnameTest):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000491
492 subdir = "pax"
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000493 longnametype = tarfile.XHDTYPE
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000494
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000495 def test_pax_global_headers(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000496 tar = tarfile.open(tarname, encoding="iso8859-1")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000497
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000498 tarinfo = tar.getmember("pax/regtype1")
499 self.assertEqual(tarinfo.uname, "foo")
500 self.assertEqual(tarinfo.gname, "bar")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000501 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000502
503 tarinfo = tar.getmember("pax/regtype2")
504 self.assertEqual(tarinfo.uname, "")
505 self.assertEqual(tarinfo.gname, "bar")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000506 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000507
508 tarinfo = tar.getmember("pax/regtype3")
509 self.assertEqual(tarinfo.uname, "tarfile")
510 self.assertEqual(tarinfo.gname, "tarfile")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000511 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
512
513 def test_pax_number_fields(self):
514 # All following number fields are read from the pax header.
515 tar = tarfile.open(tarname, encoding="iso8859-1")
516 tarinfo = tar.getmember("pax/regtype4")
517 self.assertEqual(tarinfo.size, 7011)
518 self.assertEqual(tarinfo.uid, 123)
519 self.assertEqual(tarinfo.gid, 123)
520 self.assertEqual(tarinfo.mtime, 1041808783.0)
521 self.assertEqual(type(tarinfo.mtime), float)
522 self.assertEqual(float(tarinfo.pax_headers["atime"]), 1041808783.0)
523 self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000524
525
Lars Gustäbelb1a54a32008-05-27 12:39:23 +0000526class WriteTestBase(unittest.TestCase):
527 # Put all write tests in here that are supposed to be tested
528 # in all possible mode combinations.
529
530 def test_fileobj_no_close(self):
531 fobj = StringIO.StringIO()
532 tar = tarfile.open(fileobj=fobj, mode=self.mode)
533 tar.addfile(tarfile.TarInfo("foo"))
534 tar.close()
535 self.assert_(fobj.closed is False, "external fileobjs must never closed")
536
537
538class WriteTest(WriteTestBase):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000539
540 mode = "w:"
541
542 def test_100_char_name(self):
543 # The name field in a tar header stores strings of at most 100 chars.
544 # If a string is shorter than 100 chars it has to be padded with '\0',
545 # which implies that a string of exactly 100 chars is stored without
546 # a trailing '\0'.
547 name = "0123456789" * 10
548 tar = tarfile.open(tmpname, self.mode)
549 t = tarfile.TarInfo(name)
550 tar.addfile(t)
Lars Gustäbel3f8aca12007-02-06 18:38:13 +0000551 tar.close()
552
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000553 tar = tarfile.open(tmpname)
554 self.assert_(tar.getnames()[0] == name,
Georg Brandla32e0a02006-10-24 16:54:16 +0000555 "failed to store 100 char filename")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000556 tar.close()
Georg Brandla32e0a02006-10-24 16:54:16 +0000557
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000558 def test_tar_size(self):
559 # Test for bug #1013882.
560 tar = tarfile.open(tmpname, self.mode)
561 path = os.path.join(TEMPDIR, "file")
562 fobj = open(path, "wb")
563 fobj.write("aaa")
564 fobj.close()
565 tar.add(path)
566 tar.close()
567 self.assert_(os.path.getsize(tmpname) > 0,
568 "tarfile is empty")
Georg Brandla32e0a02006-10-24 16:54:16 +0000569
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000570 # The test_*_size tests test for bug #1167128.
571 def test_file_size(self):
572 tar = tarfile.open(tmpname, self.mode)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000573
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000574 path = os.path.join(TEMPDIR, "file")
575 fobj = open(path, "wb")
576 fobj.close()
577 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000578 self.assertEqual(tarinfo.size, 0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000579
580 fobj = open(path, "wb")
581 fobj.write("aaa")
582 fobj.close()
583 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000584 self.assertEqual(tarinfo.size, 3)
585
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000586 tar.close()
587
588 def test_directory_size(self):
589 path = os.path.join(TEMPDIR, "directory")
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000590 os.mkdir(path)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000591 try:
592 tar = tarfile.open(tmpname, self.mode)
593 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000594 self.assertEqual(tarinfo.size, 0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000595 finally:
596 os.rmdir(path)
597
598 def test_link_size(self):
599 if hasattr(os, "link"):
600 link = os.path.join(TEMPDIR, "link")
601 target = os.path.join(TEMPDIR, "link_target")
602 open(target, "wb").close()
603 os.link(target, link)
604 try:
605 tar = tarfile.open(tmpname, self.mode)
606 tarinfo = tar.gettarinfo(link)
607 self.assertEqual(tarinfo.size, 0)
608 finally:
609 os.remove(target)
610 os.remove(link)
611
612 def test_symlink_size(self):
613 if hasattr(os, "symlink"):
614 path = os.path.join(TEMPDIR, "symlink")
615 os.symlink("link_target", path)
616 try:
617 tar = tarfile.open(tmpname, self.mode)
618 tarinfo = tar.gettarinfo(path)
619 self.assertEqual(tarinfo.size, 0)
620 finally:
621 os.remove(path)
622
623 def test_add_self(self):
624 # Test for #1257255.
625 dstname = os.path.abspath(tmpname)
626
627 tar = tarfile.open(tmpname, self.mode)
628 self.assert_(tar.name == dstname, "archive name must be absolute")
629
630 tar.add(dstname)
631 self.assert_(tar.getnames() == [], "added the archive to itself")
632
633 cwd = os.getcwd()
634 os.chdir(TEMPDIR)
635 tar.add(dstname)
636 os.chdir(cwd)
637 self.assert_(tar.getnames() == [], "added the archive to itself")
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000638
Lars Gustäbel104490e2007-06-18 11:42:11 +0000639 def test_exclude(self):
640 tempdir = os.path.join(TEMPDIR, "exclude")
641 os.mkdir(tempdir)
642 try:
643 for name in ("foo", "bar", "baz"):
644 name = os.path.join(tempdir, name)
645 open(name, "wb").close()
646
647 def exclude(name):
648 return os.path.isfile(name)
649
650 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
651 tar.add(tempdir, arcname="empty_dir", exclude=exclude)
652 tar.close()
653
654 tar = tarfile.open(tmpname, "r")
655 self.assertEqual(len(tar.getmembers()), 1)
656 self.assertEqual(tar.getnames()[0], "empty_dir")
657 finally:
658 shutil.rmtree(tempdir)
659
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000660
Lars Gustäbelb1a54a32008-05-27 12:39:23 +0000661class StreamWriteTest(WriteTestBase):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000662
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000663 mode = "w|"
Neal Norwitz8a519392006-08-21 17:59:46 +0000664
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000665 def test_stream_padding(self):
666 # Test for bug #1543303.
667 tar = tarfile.open(tmpname, self.mode)
668 tar.close()
669
670 if self.mode.endswith("gz"):
671 fobj = gzip.GzipFile(tmpname)
672 data = fobj.read()
673 fobj.close()
674 elif self.mode.endswith("bz2"):
675 dec = bz2.BZ2Decompressor()
676 data = open(tmpname, "rb").read()
677 data = dec.decompress(data)
678 self.assert_(len(dec.unused_data) == 0,
679 "found trailing data")
Neal Norwitz8a519392006-08-21 17:59:46 +0000680 else:
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000681 fobj = open(tmpname, "rb")
682 data = fobj.read()
683 fobj.close()
Neal Norwitz8a519392006-08-21 17:59:46 +0000684
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000685 self.assert_(data.count("\0") == tarfile.RECORDSIZE,
Neal Norwitz8a519392006-08-21 17:59:46 +0000686 "incorrect zero padding")
687
688
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000689class GNUWriteTest(unittest.TestCase):
690 # This testcase checks for correct creation of GNU Longname
691 # and Longlink extended headers (cp. bug #812325).
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000692
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000693 def _length(self, s):
694 blocks, remainder = divmod(len(s) + 1, 512)
695 if remainder:
696 blocks += 1
697 return blocks * 512
698
699 def _calc_size(self, name, link=None):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000700 # Initial tar header
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000701 count = 512
702
703 if len(name) > tarfile.LENGTH_NAME:
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000704 # GNU longname extended header + longname
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000705 count += 512
706 count += self._length(name)
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000707 if link is not None and len(link) > tarfile.LENGTH_LINK:
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000708 # GNU longlink extended header + longlink
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000709 count += 512
710 count += self._length(link)
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000711 return count
712
713 def _test(self, name, link=None):
714 tarinfo = tarfile.TarInfo(name)
715 if link:
716 tarinfo.linkname = link
717 tarinfo.type = tarfile.LNKTYPE
718
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000719 tar = tarfile.open(tmpname, "w")
720 tar.format = tarfile.GNU_FORMAT
Georg Brandl87fa5592006-12-06 22:21:18 +0000721 tar.addfile(tarinfo)
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000722
723 v1 = self._calc_size(name, link)
Georg Brandl87fa5592006-12-06 22:21:18 +0000724 v2 = tar.offset
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000725 self.assert_(v1 == v2, "GNU longname/longlink creation failed")
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000726
Georg Brandl87fa5592006-12-06 22:21:18 +0000727 tar.close()
728
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000729 tar = tarfile.open(tmpname)
Georg Brandl87fa5592006-12-06 22:21:18 +0000730 member = tar.next()
731 self.failIf(member is None, "unable to read longname member")
732 self.assert_(tarinfo.name == member.name and \
733 tarinfo.linkname == member.linkname, \
734 "unable to read longname member")
735
Neal Norwitz0662f8a2004-07-20 21:54:18 +0000736 def test_longname_1023(self):
737 self._test(("longnam/" * 127) + "longnam")
738
739 def test_longname_1024(self):
740 self._test(("longnam/" * 127) + "longname")
741
742 def test_longname_1025(self):
743 self._test(("longnam/" * 127) + "longname_")
744
745 def test_longlink_1023(self):
746 self._test("name", ("longlnk/" * 127) + "longlnk")
747
748 def test_longlink_1024(self):
749 self._test("name", ("longlnk/" * 127) + "longlink")
750
751 def test_longlink_1025(self):
752 self._test("name", ("longlnk/" * 127) + "longlink_")
753
754 def test_longnamelink_1023(self):
755 self._test(("longnam/" * 127) + "longnam",
756 ("longlnk/" * 127) + "longlnk")
757
758 def test_longnamelink_1024(self):
759 self._test(("longnam/" * 127) + "longname",
760 ("longlnk/" * 127) + "longlink")
761
762 def test_longnamelink_1025(self):
763 self._test(("longnam/" * 127) + "longname_",
764 ("longlnk/" * 127) + "longlink_")
765
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000766
767class HardlinkTest(unittest.TestCase):
768 # Test the creation of LNKTYPE (hardlink) members in an archive.
Georg Brandl38c6a222006-05-10 16:26:03 +0000769
770 def setUp(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000771 self.foo = os.path.join(TEMPDIR, "foo")
772 self.bar = os.path.join(TEMPDIR, "bar")
Georg Brandl38c6a222006-05-10 16:26:03 +0000773
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000774 fobj = open(self.foo, "wb")
775 fobj.write("foo")
776 fobj.close()
Georg Brandl38c6a222006-05-10 16:26:03 +0000777
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000778 os.link(self.foo, self.bar)
Georg Brandl38c6a222006-05-10 16:26:03 +0000779
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000780 self.tar = tarfile.open(tmpname, "w")
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000781 self.tar.add(self.foo)
782
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000783 def tearDown(self):
Hirokazu Yamamoto56d380d2008-09-21 11:44:23 +0000784 self.tar.close()
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000785 os.remove(self.foo)
786 os.remove(self.bar)
787
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000788 def test_add_twice(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000789 # The same name will be added as a REGTYPE every
790 # time regardless of st_nlink.
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000791 tarinfo = self.tar.gettarinfo(self.foo)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000792 self.assert_(tarinfo.type == tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000793 "add file as regular failed")
794
795 def test_add_hardlink(self):
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000796 tarinfo = self.tar.gettarinfo(self.bar)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000797 self.assert_(tarinfo.type == tarfile.LNKTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000798 "add file as hardlink failed")
799
800 def test_dereference_hardlink(self):
801 self.tar.dereference = True
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.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +0000804 "dereferencing hardlink failed")
805
Neal Norwitza4f651a2004-07-20 22:07:44 +0000806
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000807class PaxWriteTest(GNUWriteTest):
Martin v. Löwis78be7df2005-03-05 12:47:42 +0000808
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000809 def _test(self, name, link=None):
810 # See GNUWriteTest.
811 tarinfo = tarfile.TarInfo(name)
812 if link:
813 tarinfo.linkname = link
814 tarinfo.type = tarfile.LNKTYPE
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +0000815
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000816 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT)
817 tar.addfile(tarinfo)
818 tar.close()
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +0000819
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000820 tar = tarfile.open(tmpname)
821 if link:
822 l = tar.getmembers()[0].linkname
823 self.assert_(link == l, "PAX longlink creation failed")
824 else:
825 n = tar.getmembers()[0].name
826 self.assert_(name == n, "PAX longname creation failed")
827
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000828 def test_pax_global_header(self):
829 pax_headers = {
830 u"foo": u"bar",
831 u"uid": u"0",
832 u"mtime": u"1.23",
833 u"test": u"äöü",
834 u"äöü": u"test"}
835
836 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, \
837 pax_headers=pax_headers)
838 tar.addfile(tarfile.TarInfo("test"))
839 tar.close()
840
841 # Test if the global header was written correctly.
842 tar = tarfile.open(tmpname, encoding="iso8859-1")
843 self.assertEqual(tar.pax_headers, pax_headers)
844 self.assertEqual(tar.getmembers()[0].pax_headers, pax_headers)
845
846 # Test if all the fields are unicode.
847 for key, val in tar.pax_headers.iteritems():
848 self.assert_(type(key) is unicode)
849 self.assert_(type(val) is unicode)
850 if key in tarfile.PAX_NUMBER_FIELDS:
851 try:
852 tarfile.PAX_NUMBER_FIELDS[key](val)
853 except (TypeError, ValueError):
854 self.fail("unable to convert pax header field")
855
856 def test_pax_extended_header(self):
857 # The fields from the pax header have priority over the
858 # TarInfo.
859 pax_headers = {u"path": u"foo", u"uid": u"123"}
860
861 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1")
862 t = tarfile.TarInfo()
863 t.name = u"äöü" # non-ASCII
864 t.uid = 8**8 # too large
865 t.pax_headers = pax_headers
866 tar.addfile(t)
867 tar.close()
868
869 tar = tarfile.open(tmpname, encoding="iso8859-1")
870 t = tar.getmembers()[0]
871 self.assertEqual(t.pax_headers, pax_headers)
872 self.assertEqual(t.name, "foo")
873 self.assertEqual(t.uid, 123)
874
875
876class UstarUnicodeTest(unittest.TestCase):
877 # All *UnicodeTests FIXME
878
879 format = tarfile.USTAR_FORMAT
880
881 def test_iso8859_1_filename(self):
882 self._test_unicode_filename("iso8859-1")
883
884 def test_utf7_filename(self):
885 self._test_unicode_filename("utf7")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000886
887 def test_utf8_filename(self):
888 self._test_unicode_filename("utf8")
889
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000890 def _test_unicode_filename(self, encoding):
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000891 tar = tarfile.open(tmpname, "w", format=self.format, encoding=encoding, errors="strict")
892 name = u"äöü"
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000893 tar.addfile(tarfile.TarInfo(name))
894 tar.close()
895
896 tar = tarfile.open(tmpname, encoding=encoding)
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000897 self.assert_(type(tar.getnames()[0]) is not unicode)
898 self.assertEqual(tar.getmembers()[0].name, name.encode(encoding))
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000899 tar.close()
900
901 def test_unicode_filename_error(self):
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000902 tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict")
903 tarinfo = tarfile.TarInfo()
904
905 tarinfo.name = "äöü"
906 if self.format == tarfile.PAX_FORMAT:
907 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
908 else:
909 tar.addfile(tarinfo)
910
911 tarinfo.name = u"äöü"
912 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
913
914 tarinfo.name = "foo"
915 tarinfo.uname = u"äöü"
916 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
917
918 def test_unicode_argument(self):
919 tar = tarfile.open(tarname, "r", encoding="iso8859-1", errors="strict")
920 for t in tar:
921 self.assert_(type(t.name) is str)
922 self.assert_(type(t.linkname) is str)
923 self.assert_(type(t.uname) is str)
924 self.assert_(type(t.gname) is str)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000925 tar.close()
926
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000927 def test_uname_unicode(self):
928 for name in (u"äöü", "äöü"):
929 t = tarfile.TarInfo("foo")
930 t.uname = name
931 t.gname = name
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000932
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000933 fobj = StringIO.StringIO()
934 tar = tarfile.open("foo.tar", mode="w", fileobj=fobj, format=self.format, encoding="iso8859-1")
935 tar.addfile(t)
936 tar.close()
937 fobj.seek(0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000938
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000939 tar = tarfile.open("foo.tar", fileobj=fobj, encoding="iso8859-1")
940 t = tar.getmember("foo")
941 self.assertEqual(t.uname, "äöü")
942 self.assertEqual(t.gname, "äöü")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000943
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000944
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000945class GNUUnicodeTest(UstarUnicodeTest):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000946
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000947 format = tarfile.GNU_FORMAT
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000948
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000949
950class PaxUnicodeTest(UstarUnicodeTest):
951
952 format = tarfile.PAX_FORMAT
953
954 def _create_unicode_name(self, name):
955 tar = tarfile.open(tmpname, "w", format=self.format)
956 t = tarfile.TarInfo()
957 t.pax_headers["path"] = name
958 tar.addfile(t)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000959 tar.close()
960
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000961 def test_error_handlers(self):
962 # Test if the unicode error handlers work correctly for characters
963 # that cannot be expressed in a given encoding.
964 self._create_unicode_name(u"äöü")
Georg Brandlded1c4d2006-12-20 11:55:16 +0000965
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000966 for handler, name in (("utf-8", u"äöü".encode("utf8")),
967 ("replace", "???"), ("ignore", "")):
968 tar = tarfile.open(tmpname, format=self.format, encoding="ascii",
969 errors=handler)
970 self.assertEqual(tar.getnames()[0], name)
Georg Brandlded1c4d2006-12-20 11:55:16 +0000971
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000972 self.assertRaises(UnicodeError, tarfile.open, tmpname,
973 encoding="ascii", errors="strict")
974
975 def test_error_handler_utf8(self):
976 # Create a pathname that has one component representable using
977 # iso8859-1 and the other only in iso8859-15.
978 self._create_unicode_name(u"äöü/¤")
979
980 tar = tarfile.open(tmpname, format=self.format, encoding="iso8859-1",
981 errors="utf-8")
982 self.assertEqual(tar.getnames()[0], "äöü/" + u"¤".encode("utf8"))
Georg Brandlded1c4d2006-12-20 11:55:16 +0000983
Georg Brandlded1c4d2006-12-20 11:55:16 +0000984
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000985class AppendTest(unittest.TestCase):
986 # Test append mode (cp. patch #1652681).
Tim Peters8ceefc52004-10-25 03:19:41 +0000987
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000988 def setUp(self):
989 self.tarname = tmpname
990 if os.path.exists(self.tarname):
991 os.remove(self.tarname)
Lars Gustäbela7ba6fc2006-12-27 10:30:46 +0000992
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000993 def _add_testfile(self, fileobj=None):
994 tar = tarfile.open(self.tarname, "a", fileobj=fileobj)
995 tar.addfile(tarfile.TarInfo("bar"))
996 tar.close()
Lars Gustäbela7ba6fc2006-12-27 10:30:46 +0000997
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000998 def _create_testtar(self, mode="w:"):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000999 src = tarfile.open(tarname, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001000 t = src.getmember("ustar/regtype")
1001 t.name = "foo"
1002 f = src.extractfile(t)
1003 tar = tarfile.open(self.tarname, mode)
1004 tar.addfile(t, f)
1005 tar.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001006
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001007 def _test(self, names=["bar"], fileobj=None):
1008 tar = tarfile.open(self.tarname, fileobj=fileobj)
1009 self.assertEqual(tar.getnames(), names)
1010
1011 def test_non_existing(self):
1012 self._add_testfile()
1013 self._test()
1014
1015 def test_empty(self):
1016 open(self.tarname, "w").close()
1017 self._add_testfile()
1018 self._test()
1019
1020 def test_empty_fileobj(self):
1021 fobj = StringIO.StringIO()
1022 self._add_testfile(fobj)
1023 fobj.seek(0)
1024 self._test(fileobj=fobj)
1025
1026 def test_fileobj(self):
1027 self._create_testtar()
1028 data = open(self.tarname).read()
1029 fobj = StringIO.StringIO(data)
1030 self._add_testfile(fobj)
1031 fobj.seek(0)
1032 self._test(names=["foo", "bar"], fileobj=fobj)
1033
1034 def test_existing(self):
1035 self._create_testtar()
1036 self._add_testfile()
1037 self._test(names=["foo", "bar"])
1038
1039 def test_append_gz(self):
1040 if gzip is None:
1041 return
1042 self._create_testtar("w:gz")
1043 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
1044
1045 def test_append_bz2(self):
1046 if bz2 is None:
1047 return
1048 self._create_testtar("w:bz2")
1049 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
1050
1051
1052class LimitsTest(unittest.TestCase):
1053
1054 def test_ustar_limits(self):
1055 # 100 char name
1056 tarinfo = tarfile.TarInfo("0123456789" * 10)
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001057 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001058
1059 # 101 char name that cannot be stored
1060 tarinfo = tarfile.TarInfo("0123456789" * 10 + "0")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001061 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001062
1063 # 256 char name with a slash at pos 156
1064 tarinfo = tarfile.TarInfo("123/" * 62 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001065 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001066
1067 # 256 char name that cannot be stored
1068 tarinfo = tarfile.TarInfo("1234567/" * 31 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001069 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001070
1071 # 512 char name
1072 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001073 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001074
1075 # 512 char linkname
1076 tarinfo = tarfile.TarInfo("longlink")
1077 tarinfo.linkname = "123/" * 126 + "longname"
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001078 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001079
1080 # uid > 8 digits
1081 tarinfo = tarfile.TarInfo("name")
1082 tarinfo.uid = 010000000
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001083 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001084
1085 def test_gnu_limits(self):
1086 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001087 tarinfo.tobuf(tarfile.GNU_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001088
1089 tarinfo = tarfile.TarInfo("longlink")
1090 tarinfo.linkname = "123/" * 126 + "longname"
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001091 tarinfo.tobuf(tarfile.GNU_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001092
1093 # uid >= 256 ** 7
1094 tarinfo = tarfile.TarInfo("name")
1095 tarinfo.uid = 04000000000000000000L
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001096 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.GNU_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001097
1098 def test_pax_limits(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001099 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001100 tarinfo.tobuf(tarfile.PAX_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001101
1102 tarinfo = tarfile.TarInfo("longlink")
1103 tarinfo.linkname = "123/" * 126 + "longname"
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001104 tarinfo.tobuf(tarfile.PAX_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001105
1106 tarinfo = tarfile.TarInfo("name")
1107 tarinfo.uid = 04000000000000000000L
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001108 tarinfo.tobuf(tarfile.PAX_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001109
1110
1111class GzipMiscReadTest(MiscReadTest):
1112 tarname = gzipname
1113 mode = "r:gz"
1114class GzipUstarReadTest(UstarReadTest):
1115 tarname = gzipname
1116 mode = "r:gz"
1117class GzipStreamReadTest(StreamReadTest):
1118 tarname = gzipname
1119 mode = "r|gz"
1120class GzipWriteTest(WriteTest):
1121 mode = "w:gz"
1122class GzipStreamWriteTest(StreamWriteTest):
1123 mode = "w|gz"
1124
1125
1126class Bz2MiscReadTest(MiscReadTest):
1127 tarname = bz2name
1128 mode = "r:bz2"
1129class Bz2UstarReadTest(UstarReadTest):
1130 tarname = bz2name
1131 mode = "r:bz2"
1132class Bz2StreamReadTest(StreamReadTest):
1133 tarname = bz2name
1134 mode = "r|bz2"
1135class Bz2WriteTest(WriteTest):
1136 mode = "w:bz2"
1137class Bz2StreamWriteTest(StreamWriteTest):
1138 mode = "w|bz2"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001139
Lars Gustäbel9e2a09a2009-03-22 20:22:29 +00001140class Bz2PartialReadTest(unittest.TestCase):
1141 # Issue5068: The _BZ2Proxy.read() method loops forever
1142 # on an empty or partial bzipped file.
1143
1144 def _test_partial_input(self, mode):
1145 class MyStringIO(StringIO.StringIO):
1146 hit_eof = False
1147 def read(self, n):
1148 if self.hit_eof:
1149 raise AssertionError("infinite loop detected in tarfile.open()")
1150 self.hit_eof = self.pos == self.len
1151 return StringIO.StringIO.read(self, n)
1152
1153 data = bz2.compress(tarfile.TarInfo("foo").tobuf())
1154 for x in range(len(data) + 1):
1155 tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode)
1156
1157 def test_partial_input(self):
1158 self._test_partial_input("r")
1159
1160 def test_partial_input_bz2(self):
1161 self._test_partial_input("r:bz2")
1162
1163
Neal Norwitz996acf12003-02-17 14:51:41 +00001164def test_main():
Antoine Pitrou80e44f82009-11-11 20:57:55 +00001165 os.makedirs(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001166
Walter Dörwald21d3a322003-05-01 17:45:56 +00001167 tests = [
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001168 UstarReadTest,
1169 MiscReadTest,
1170 StreamReadTest,
1171 DetectReadTest,
1172 MemberReadTest,
1173 GNUReadTest,
1174 PaxReadTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001175 WriteTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001176 StreamWriteTest,
1177 GNUWriteTest,
1178 PaxWriteTest,
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001179 UstarUnicodeTest,
1180 GNUUnicodeTest,
1181 PaxUnicodeTest,
Lars Gustäbel3f8aca12007-02-06 18:38:13 +00001182 AppendTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001183 LimitsTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001184 ]
1185
Neal Norwitza4f651a2004-07-20 22:07:44 +00001186 if hasattr(os, "link"):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001187 tests.append(HardlinkTest)
1188
1189 fobj = open(tarname, "rb")
1190 data = fobj.read()
1191 fobj.close()
Neal Norwitza4f651a2004-07-20 22:07:44 +00001192
Walter Dörwald21d3a322003-05-01 17:45:56 +00001193 if gzip:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001194 # Create testtar.tar.gz and add gzip-specific tests.
1195 tar = gzip.open(gzipname, "wb")
1196 tar.write(data)
1197 tar.close()
1198
1199 tests += [
1200 GzipMiscReadTest,
1201 GzipUstarReadTest,
1202 GzipStreamReadTest,
1203 GzipWriteTest,
1204 GzipStreamWriteTest,
1205 ]
Walter Dörwald21d3a322003-05-01 17:45:56 +00001206
1207 if bz2:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001208 # Create testtar.tar.bz2 and add bz2-specific tests.
1209 tar = bz2.BZ2File(bz2name, "wb")
1210 tar.write(data)
1211 tar.close()
1212
1213 tests += [
1214 Bz2MiscReadTest,
1215 Bz2UstarReadTest,
1216 Bz2StreamReadTest,
1217 Bz2WriteTest,
1218 Bz2StreamWriteTest,
Lars Gustäbel9e2a09a2009-03-22 20:22:29 +00001219 Bz2PartialReadTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001220 ]
1221
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001222 try:
Walter Dörwald21d3a322003-05-01 17:45:56 +00001223 test_support.run_unittest(*tests)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001224 finally:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001225 if os.path.exists(TEMPDIR):
1226 shutil.rmtree(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001227
Neal Norwitz996acf12003-02-17 14:51:41 +00001228if __name__ == "__main__":
1229 test_main()