blob: 9d318100fa87fe44e2916a672193829752ba8a47 [file] [log] [blame]
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001import sys
2import os
Lars Gustäbelb506dc32007-08-07 18:36:16 +00003import io
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00004import shutil
Guido van Rossuma8add0e2007-05-14 22:03:55 +00005from hashlib import md5
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00006
7import unittest
8import tarfile
9
Serhiy Storchakad27b4552013-11-24 01:53:29 +020010from test import support, script_helper
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000011
12# Check for our compression modules.
13try:
14 import gzip
Brett Cannon260fbe82013-07-04 18:16:15 -040015except ImportError:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000016 gzip = None
17try:
18 import bz2
Brett Cannon260fbe82013-07-04 18:16:15 -040019except ImportError:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000020 bz2 = None
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +010021try:
22 import lzma
Brett Cannon260fbe82013-07-04 18:16:15 -040023except ImportError:
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +010024 lzma = None
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000025
Guido van Rossumd8faa362007-04-27 19:54:29 +000026def md5sum(data):
Guido van Rossuma8add0e2007-05-14 22:03:55 +000027 return md5(data).hexdigest()
Guido van Rossumd8faa362007-04-27 19:54:29 +000028
Antoine Pitrouab58b5f2010-09-23 19:39:35 +000029TEMPDIR = os.path.abspath(support.TESTFN) + "-tardir"
Serhiy Storchakad27b4552013-11-24 01:53:29 +020030tarextdir = TEMPDIR + '-extract-test'
Antoine Pitrou941ee882009-11-11 20:59:38 +000031tarname = support.findfile("testtar.tar")
Guido van Rossumd8faa362007-04-27 19:54:29 +000032gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
33bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +010034xzname = os.path.join(TEMPDIR, "testtar.tar.xz")
Guido van Rossumd8faa362007-04-27 19:54:29 +000035tmpname = os.path.join(TEMPDIR, "tmp.tar")
Serhiy Storchakad27b4552013-11-24 01:53:29 +020036dotlessname = os.path.join(TEMPDIR, "testtar")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000037
Guido van Rossumd8faa362007-04-27 19:54:29 +000038md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
39md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000040
41
Serhiy Storchaka8b562922013-06-17 15:38:50 +030042class TarTest:
Guido van Rossumd8faa362007-04-27 19:54:29 +000043 tarname = tarname
Serhiy Storchaka8b562922013-06-17 15:38:50 +030044 suffix = ''
45 open = io.FileIO
46
47 @property
48 def mode(self):
49 return self.prefix + self.suffix
50
51@support.requires_gzip
52class GzipTest:
53 tarname = gzipname
54 suffix = 'gz'
55 open = gzip.GzipFile if gzip else None
56
57@support.requires_bz2
58class Bz2Test:
59 tarname = bz2name
60 suffix = 'bz2'
61 open = bz2.BZ2File if bz2 else None
62
63@support.requires_lzma
64class LzmaTest:
65 tarname = xzname
66 suffix = 'xz'
67 open = lzma.LZMAFile if lzma else None
68
69
70class ReadTest(TarTest):
71
72 prefix = "r:"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000073
74 def setUp(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +030075 self.tar = tarfile.open(self.tarname, mode=self.mode,
76 encoding="iso8859-1")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000077
78 def tearDown(self):
79 self.tar.close()
80
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000081
Serhiy Storchaka8b562922013-06-17 15:38:50 +030082class UstarReadTest(ReadTest, unittest.TestCase):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000083
Guido van Rossumd8faa362007-04-27 19:54:29 +000084 def test_fileobj_regular_file(self):
85 tarinfo = self.tar.getmember("ustar/regtype")
Lars Gustäbel7a919e92012-05-05 18:15:03 +020086 with self.tar.extractfile(tarinfo) as fobj:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +000087 data = fobj.read()
Serhiy Storchaka8b562922013-06-17 15:38:50 +030088 self.assertEqual(len(data), tarinfo.size,
89 "regular file extraction failed")
90 self.assertEqual(md5sum(data), md5_regtype,
Antoine Pitroue1eca4e2010-10-29 23:49:49 +000091 "regular file extraction failed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000092
Guido van Rossumd8faa362007-04-27 19:54:29 +000093 def test_fileobj_readlines(self):
94 self.tar.extract("ustar/regtype", TEMPDIR)
95 tarinfo = self.tar.getmember("ustar/regtype")
Antoine Pitrou95f55602010-09-23 18:36:46 +000096 with open(os.path.join(TEMPDIR, "ustar/regtype"), "r") as fobj1:
97 lines1 = fobj1.readlines()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000098
Lars Gustäbel7a919e92012-05-05 18:15:03 +020099 with self.tar.extractfile(tarinfo) as fobj:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000100 fobj2 = io.TextIOWrapper(fobj)
101 lines2 = fobj2.readlines()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300102 self.assertEqual(lines1, lines2,
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000103 "fileobj.readlines() failed")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300104 self.assertEqual(len(lines2), 114,
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000105 "fileobj.readlines() failed")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300106 self.assertEqual(lines2[83],
107 "I will gladly admit that Python is not the fastest "
108 "running scripting language.\n",
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000109 "fileobj.readlines() failed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000110
Guido van Rossumd8faa362007-04-27 19:54:29 +0000111 def test_fileobj_iter(self):
112 self.tar.extract("ustar/regtype", TEMPDIR)
113 tarinfo = self.tar.getmember("ustar/regtype")
Victor Stinner4e86d5b2011-05-04 13:55:36 +0200114 with open(os.path.join(TEMPDIR, "ustar/regtype"), "r") as fobj1:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000115 lines1 = fobj1.readlines()
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200116 with self.tar.extractfile(tarinfo) as fobj2:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000117 lines2 = list(io.TextIOWrapper(fobj2))
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300118 self.assertEqual(lines1, lines2,
119 "fileobj.__iter__() failed")
Martin v. Löwisdf241532005-03-03 08:17:42 +0000120
Guido van Rossumd8faa362007-04-27 19:54:29 +0000121 def test_fileobj_seek(self):
122 self.tar.extract("ustar/regtype", TEMPDIR)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000123 with open(os.path.join(TEMPDIR, "ustar/regtype"), "rb") as fobj:
124 data = fobj.read()
Neal Norwitzf3396542005-10-28 05:52:22 +0000125
Guido van Rossumd8faa362007-04-27 19:54:29 +0000126 tarinfo = self.tar.getmember("ustar/regtype")
127 fobj = self.tar.extractfile(tarinfo)
128
129 text = fobj.read()
130 fobj.seek(0)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000131 self.assertEqual(0, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000132 "seek() to file's start failed")
133 fobj.seek(2048, 0)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000134 self.assertEqual(2048, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000135 "seek() to absolute position failed")
136 fobj.seek(-1024, 1)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000137 self.assertEqual(1024, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000138 "seek() to negative relative position failed")
139 fobj.seek(1024, 1)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000140 self.assertEqual(2048, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000141 "seek() to positive relative position failed")
142 s = fobj.read(10)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300143 self.assertEqual(s, data[2048:2058],
Guido van Rossumd8faa362007-04-27 19:54:29 +0000144 "read() after seek failed")
145 fobj.seek(0, 2)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000146 self.assertEqual(tarinfo.size, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000147 "seek() to file's end failed")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300148 self.assertEqual(fobj.read(), b"",
Guido van Rossumd8faa362007-04-27 19:54:29 +0000149 "read() at file's end did not return empty string")
150 fobj.seek(-tarinfo.size, 2)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000151 self.assertEqual(0, fobj.tell(),
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000152 "relative seek() to file's end failed")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000153 fobj.seek(512)
154 s1 = fobj.readlines()
155 fobj.seek(512)
156 s2 = fobj.readlines()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300157 self.assertEqual(s1, s2,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000158 "readlines() after seek failed")
159 fobj.seek(0)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000160 self.assertEqual(len(fobj.readline()), fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000161 "tell() after readline() failed")
162 fobj.seek(512)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300163 self.assertEqual(len(fobj.readline()) + 512, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000164 "tell() after seek() and readline() failed")
165 fobj.seek(0)
166 line = fobj.readline()
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000167 self.assertEqual(fobj.read(), data[len(line):],
Guido van Rossumd8faa362007-04-27 19:54:29 +0000168 "read() after readline() failed")
169 fobj.close()
170
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200171 def test_fileobj_text(self):
172 with self.tar.extractfile("ustar/regtype") as fobj:
173 fobj = io.TextIOWrapper(fobj)
174 data = fobj.read().encode("iso8859-1")
175 self.assertEqual(md5sum(data), md5_regtype)
176 try:
177 fobj.seek(100)
178 except AttributeError:
179 # Issue #13815: seek() complained about a missing
180 # flush() method.
181 self.fail("seeking failed in text mode")
182
Lars Gustäbel1b512722010-06-03 12:45:16 +0000183 # Test if symbolic and hard links are resolved by extractfile(). The
184 # test link members each point to a regular member whose data is
185 # supposed to be exported.
186 def _test_fileobj_link(self, lnktype, regtype):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300187 with self.tar.extractfile(lnktype) as a, \
188 self.tar.extractfile(regtype) as b:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000189 self.assertEqual(a.name, b.name)
Lars Gustäbel1b512722010-06-03 12:45:16 +0000190
191 def test_fileobj_link1(self):
192 self._test_fileobj_link("ustar/lnktype", "ustar/regtype")
193
194 def test_fileobj_link2(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300195 self._test_fileobj_link("./ustar/linktest2/lnktype",
196 "ustar/linktest1/regtype")
Lars Gustäbel1b512722010-06-03 12:45:16 +0000197
198 def test_fileobj_symlink1(self):
199 self._test_fileobj_link("ustar/symtype", "ustar/regtype")
200
201 def test_fileobj_symlink2(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300202 self._test_fileobj_link("./ustar/linktest2/symtype",
203 "ustar/linktest1/regtype")
Lars Gustäbel1b512722010-06-03 12:45:16 +0000204
Lars Gustäbel1ef9eda2012-04-24 21:04:40 +0200205 def test_issue14160(self):
206 self._test_fileobj_link("symtype2", "ustar/regtype")
207
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300208class GzipUstarReadTest(GzipTest, UstarReadTest):
209 pass
210
211class Bz2UstarReadTest(Bz2Test, UstarReadTest):
212 pass
213
214class LzmaUstarReadTest(LzmaTest, UstarReadTest):
215 pass
216
Guido van Rossumd8faa362007-04-27 19:54:29 +0000217
Lars Gustäbel9520a432009-11-22 18:48:49 +0000218class CommonReadTest(ReadTest):
219
220 def test_empty_tarfile(self):
221 # Test for issue6123: Allow opening empty archives.
222 # This test checks if tarfile.open() is able to open an empty tar
223 # archive successfully. Note that an empty tar archive is not the
224 # same as an empty file!
Antoine Pitrou95f55602010-09-23 18:36:46 +0000225 with tarfile.open(tmpname, self.mode.replace("r", "w")):
226 pass
Lars Gustäbel9520a432009-11-22 18:48:49 +0000227 try:
228 tar = tarfile.open(tmpname, self.mode)
229 tar.getnames()
230 except tarfile.ReadError:
231 self.fail("tarfile.open() failed on empty archive")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000232 else:
233 self.assertListEqual(tar.getmembers(), [])
234 finally:
235 tar.close()
Lars Gustäbel9520a432009-11-22 18:48:49 +0000236
237 def test_null_tarfile(self):
238 # Test for issue6123: Allow opening empty archives.
239 # This test guarantees that tarfile.open() does not treat an empty
240 # file as an empty tar archive.
Antoine Pitrou95f55602010-09-23 18:36:46 +0000241 with open(tmpname, "wb"):
242 pass
Lars Gustäbel9520a432009-11-22 18:48:49 +0000243 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, self.mode)
244 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname)
245
246 def test_ignore_zeros(self):
247 # Test TarFile's ignore_zeros option.
Lars Gustäbel9520a432009-11-22 18:48:49 +0000248 for char in (b'\0', b'a'):
249 # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
250 # are ignored correctly.
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300251 with self.open(tmpname, "w") as fobj:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000252 fobj.write(char * 1024)
253 fobj.write(tarfile.TarInfo("foo").tobuf())
Lars Gustäbel9520a432009-11-22 18:48:49 +0000254
255 tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000256 try:
257 self.assertListEqual(tar.getnames(), ["foo"],
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300258 "ignore_zeros=True should have skipped the %r-blocks" %
259 char)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000260 finally:
261 tar.close()
Lars Gustäbel9520a432009-11-22 18:48:49 +0000262
263
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300264class MiscReadTestBase(CommonReadTest):
Thomas Woutersed03b412007-08-28 21:37:11 +0000265 def test_no_name_argument(self):
Antoine Pitrou95f55602010-09-23 18:36:46 +0000266 with open(self.tarname, "rb") as fobj:
267 tar = tarfile.open(fileobj=fobj, mode=self.mode)
268 self.assertEqual(tar.name, os.path.abspath(fobj.name))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000269
Thomas Woutersed03b412007-08-28 21:37:11 +0000270 def test_no_name_attribute(self):
Antoine Pitrou95f55602010-09-23 18:36:46 +0000271 with open(self.tarname, "rb") as fobj:
272 data = fobj.read()
Thomas Woutersed03b412007-08-28 21:37:11 +0000273 fobj = io.BytesIO(data)
274 self.assertRaises(AttributeError, getattr, fobj, "name")
275 tar = tarfile.open(fileobj=fobj, mode=self.mode)
276 self.assertEqual(tar.name, None)
277
278 def test_empty_name_attribute(self):
Antoine Pitrou95f55602010-09-23 18:36:46 +0000279 with open(self.tarname, "rb") as fobj:
280 data = fobj.read()
Thomas Woutersed03b412007-08-28 21:37:11 +0000281 fobj = io.BytesIO(data)
282 fobj.name = ""
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000283 with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
284 self.assertEqual(tar.name, None)
Thomas Woutersed03b412007-08-28 21:37:11 +0000285
Christian Heimesd8654cf2007-12-02 15:22:16 +0000286 def test_fileobj_with_offset(self):
287 # Skip the first member and store values from the second member
288 # of the testtar.
289 tar = tarfile.open(self.tarname, mode=self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000290 try:
291 tar.next()
292 t = tar.next()
293 name = t.name
294 offset = t.offset
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200295 with tar.extractfile(t) as f:
296 data = f.read()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000297 finally:
298 tar.close()
Christian Heimesd8654cf2007-12-02 15:22:16 +0000299
300 # Open the testtar and seek to the offset of the second member.
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300301 with self.open(self.tarname) as fobj:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000302 fobj.seek(offset)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000303
Antoine Pitrou95f55602010-09-23 18:36:46 +0000304 # Test if the tarfile starts with the second member.
305 tar = tar.open(self.tarname, mode="r:", fileobj=fobj)
306 t = tar.next()
307 self.assertEqual(t.name, name)
308 # Read to the end of fileobj and test if seeking back to the
309 # beginning works.
310 tar.getmembers()
311 self.assertEqual(tar.extractfile(t).read(), data,
312 "seek back did not work")
313 tar.close()
Christian Heimesd8654cf2007-12-02 15:22:16 +0000314
Guido van Rossumd8faa362007-04-27 19:54:29 +0000315 def test_fail_comp(self):
316 # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000317 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000318 with open(tarname, "rb") as fobj:
319 self.assertRaises(tarfile.ReadError, tarfile.open,
320 fileobj=fobj, mode=self.mode)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000321
322 def test_v7_dirtype(self):
323 # Test old style dirtype member (bug #1336623):
324 # Old V7 tars create directory members using an AREGTYPE
325 # header with a "/" appended to the filename field.
326 tarinfo = self.tar.getmember("misc/dirtype-old-v7")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300327 self.assertEqual(tarinfo.type, tarfile.DIRTYPE,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000328 "v7 dirtype failed")
329
Christian Heimes126d29a2008-02-11 22:57:17 +0000330 def test_xstar_type(self):
331 # The xstar format stores extra atime and ctime fields inside the
332 # space reserved for the prefix field. The prefix field must be
333 # ignored in this case, otherwise it will mess up the name.
334 try:
335 self.tar.getmember("misc/regtype-xstar")
336 except KeyError:
337 self.fail("failed to find misc/regtype-xstar (mangled prefix?)")
338
Guido van Rossumd8faa362007-04-27 19:54:29 +0000339 def test_check_members(self):
340 for tarinfo in self.tar:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300341 self.assertEqual(int(tarinfo.mtime), 0o7606136617,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000342 "wrong mtime for %s" % tarinfo.name)
343 if not tarinfo.name.startswith("ustar/"):
344 continue
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300345 self.assertEqual(tarinfo.uname, "tarfile",
Guido van Rossumd8faa362007-04-27 19:54:29 +0000346 "wrong uname for %s" % tarinfo.name)
347
348 def test_find_members(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300349 self.assertEqual(self.tar.getmembers()[-1].name, "misc/eof",
Guido van Rossumd8faa362007-04-27 19:54:29 +0000350 "could not find all members")
351
Brian Curtin74e45612010-07-09 15:58:59 +0000352 @unittest.skipUnless(hasattr(os, "link"),
353 "Missing hardlink implementation")
Brian Curtin3b4499c2010-12-28 14:31:47 +0000354 @support.skip_unless_symlink
Guido van Rossumd8faa362007-04-27 19:54:29 +0000355 def test_extract_hardlink(self):
356 # Test hardlink extraction (e.g. bug #857297).
Serhiy Storchaka88339c42012-12-30 20:16:30 +0200357 with tarfile.open(tarname, errorlevel=1, encoding="iso8859-1") as tar:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000358 tar.extract("ustar/regtype", TEMPDIR)
Serhiy Storchaka88339c42012-12-30 20:16:30 +0200359 self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/regtype"))
Neal Norwitzf3396542005-10-28 05:52:22 +0000360
Serhiy Storchaka88339c42012-12-30 20:16:30 +0200361 tar.extract("ustar/lnktype", TEMPDIR)
362 self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/lnktype"))
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000363 with open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb") as f:
364 data = f.read()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000365 self.assertEqual(md5sum(data), md5_regtype)
Neal Norwitzf3396542005-10-28 05:52:22 +0000366
Serhiy Storchaka88339c42012-12-30 20:16:30 +0200367 tar.extract("ustar/symtype", TEMPDIR)
368 self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/symtype"))
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000369 with open(os.path.join(TEMPDIR, "ustar/symtype"), "rb") as f:
370 data = f.read()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000371 self.assertEqual(md5sum(data), md5_regtype)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000372
Christian Heimesfaf2f632008-01-06 16:59:19 +0000373 def test_extractall(self):
374 # Test if extractall() correctly restores directory permissions
375 # and times (see issue1735).
Christian Heimesfaf2f632008-01-06 16:59:19 +0000376 tar = tarfile.open(tarname, encoding="iso8859-1")
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000377 DIR = os.path.join(TEMPDIR, "extractall")
378 os.mkdir(DIR)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000379 try:
380 directories = [t for t in tar if t.isdir()]
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000381 tar.extractall(DIR, directories)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000382 for tarinfo in directories:
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000383 path = os.path.join(DIR, tarinfo.name)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000384 if sys.platform != "win32":
385 # Win32 has no support for fine grained permissions.
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300386 self.assertEqual(tarinfo.mode & 0o777,
387 os.stat(path).st_mode & 0o777)
Victor Stinner26bfb5a2010-10-29 10:59:08 +0000388 def format_mtime(mtime):
389 if isinstance(mtime, float):
390 return "{} ({})".format(mtime, mtime.hex())
391 else:
392 return "{!r} (int)".format(mtime)
Victor Stinner14d8fe72010-10-29 11:02:06 +0000393 file_mtime = os.path.getmtime(path)
Victor Stinner26bfb5a2010-10-29 10:59:08 +0000394 errmsg = "tar mtime {0} != file time {1} of path {2!a}".format(
395 format_mtime(tarinfo.mtime),
396 format_mtime(file_mtime),
397 path)
398 self.assertEqual(tarinfo.mtime, file_mtime, errmsg)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000399 finally:
400 tar.close()
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000401 shutil.rmtree(DIR)
Christian Heimesfaf2f632008-01-06 16:59:19 +0000402
Martin v. Löwis16f344d2010-11-01 21:39:13 +0000403 def test_extract_directory(self):
404 dirtype = "ustar/dirtype"
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000405 DIR = os.path.join(TEMPDIR, "extractdir")
406 os.mkdir(DIR)
407 try:
408 with tarfile.open(tarname, encoding="iso8859-1") as tar:
409 tarinfo = tar.getmember(dirtype)
410 tar.extract(tarinfo, path=DIR)
411 extracted = os.path.join(DIR, dirtype)
412 self.assertEqual(os.path.getmtime(extracted), tarinfo.mtime)
413 if sys.platform != "win32":
414 self.assertEqual(os.stat(extracted).st_mode & 0o777, 0o755)
415 finally:
416 shutil.rmtree(DIR)
Martin v. Löwis16f344d2010-11-01 21:39:13 +0000417
Lars Gustäbelb7f09232009-11-23 15:48:33 +0000418 def test_init_close_fobj(self):
419 # Issue #7341: Close the internal file object in the TarFile
420 # constructor in case of an error. For the test we rely on
421 # the fact that opening an empty file raises a ReadError.
422 empty = os.path.join(TEMPDIR, "empty")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000423 with open(empty, "wb") as fobj:
424 fobj.write(b"")
Lars Gustäbelb7f09232009-11-23 15:48:33 +0000425
426 try:
427 tar = object.__new__(tarfile.TarFile)
428 try:
429 tar.__init__(empty)
430 except tarfile.ReadError:
431 self.assertTrue(tar.fileobj.closed)
432 else:
433 self.fail("ReadError not raised")
434 finally:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000435 support.unlink(empty)
Lars Gustäbelb7f09232009-11-23 15:48:33 +0000436
Serhiy Storchaka263fab92013-05-09 14:22:26 +0300437 def test_parallel_iteration(self):
438 # Issue #16601: Restarting iteration over tarfile continued
439 # from where it left off.
440 with tarfile.open(self.tarname) as tar:
441 for m1, m2 in zip(tar, tar):
442 self.assertEqual(m1.offset, m2.offset)
443 self.assertEqual(m1.get_info(), m2.get_info())
444
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300445class MiscReadTest(MiscReadTestBase, unittest.TestCase):
446 test_fail_comp = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000447
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300448class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase):
449 def test_non_existent_targz_file(self):
450 # Test for issue11513: prevent non-existent gzipped tarfiles raising
451 # multiple exceptions.
452 with self.assertRaisesRegex(FileNotFoundError, "xxx"):
453 tarfile.open("xxx", self.mode)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000454
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300455class Bz2MiscReadTest(Bz2Test, MiscReadTestBase, unittest.TestCase):
456 def test_no_name_argument(self):
457 self.skipTest("BZ2File have no name attribute")
458
459class LzmaMiscReadTest(LzmaTest, MiscReadTestBase, unittest.TestCase):
460 def test_no_name_argument(self):
461 self.skipTest("LZMAFile have no name attribute")
462
463
464class StreamReadTest(CommonReadTest, unittest.TestCase):
465
466 prefix="r|"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000467
Lars Gustäbeldd071042011-02-23 11:42:22 +0000468 def test_read_through(self):
469 # Issue #11224: A poorly designed _FileInFile.read() method
470 # caused seeking errors with stream tar files.
471 for tarinfo in self.tar:
472 if not tarinfo.isreg():
473 continue
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200474 with self.tar.extractfile(tarinfo) as fobj:
475 while True:
476 try:
477 buf = fobj.read(512)
478 except tarfile.StreamError:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300479 self.fail("simple read-through using "
480 "TarFile.extractfile() failed")
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200481 if not buf:
482 break
Lars Gustäbeldd071042011-02-23 11:42:22 +0000483
Guido van Rossumd8faa362007-04-27 19:54:29 +0000484 def test_fileobj_regular_file(self):
485 tarinfo = self.tar.next() # get "regtype" (can't use getmember)
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200486 with self.tar.extractfile(tarinfo) as fobj:
487 data = fobj.read()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300488 self.assertEqual(len(data), tarinfo.size,
489 "regular file extraction failed")
490 self.assertEqual(md5sum(data), md5_regtype,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000491 "regular file extraction failed")
492
493 def test_provoke_stream_error(self):
494 tarinfos = self.tar.getmembers()
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200495 with self.tar.extractfile(tarinfos[0]) as f: # read the first member
496 self.assertRaises(tarfile.StreamError, f.read)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000497
Guido van Rossumd8faa362007-04-27 19:54:29 +0000498 def test_compare_members(self):
499 tar1 = tarfile.open(tarname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000500 try:
501 tar2 = self.tar
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000502
Antoine Pitrou95f55602010-09-23 18:36:46 +0000503 while True:
504 t1 = tar1.next()
505 t2 = tar2.next()
506 if t1 is None:
507 break
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300508 self.assertIsNotNone(t2, "stream.next() failed.")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000509
Antoine Pitrou95f55602010-09-23 18:36:46 +0000510 if t2.islnk() or t2.issym():
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300511 with self.assertRaises(tarfile.StreamError):
512 tar2.extractfile(t2)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000513 continue
Guido van Rossumd8faa362007-04-27 19:54:29 +0000514
Antoine Pitrou95f55602010-09-23 18:36:46 +0000515 v1 = tar1.extractfile(t1)
516 v2 = tar2.extractfile(t2)
517 if v1 is None:
518 continue
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300519 self.assertIsNotNone(v2, "stream.extractfile() failed")
520 self.assertEqual(v1.read(), v2.read(),
521 "stream extraction failed")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000522 finally:
523 tar1.close()
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000524
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300525class GzipStreamReadTest(GzipTest, StreamReadTest):
526 pass
Thomas Wouters89f507f2006-12-13 04:49:30 +0000527
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300528class Bz2StreamReadTest(Bz2Test, StreamReadTest):
529 pass
Thomas Wouterscf297e42007-02-23 15:07:44 +0000530
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300531class LzmaStreamReadTest(LzmaTest, StreamReadTest):
532 pass
533
534
535class DetectReadTest(TarTest, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000536 def _testfunc_file(self, name, mode):
537 try:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000538 tar = tarfile.open(name, mode)
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000539 except tarfile.ReadError as e:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000540 self.fail()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000541 else:
542 tar.close()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000543
Guido van Rossumd8faa362007-04-27 19:54:29 +0000544 def _testfunc_fileobj(self, name, mode):
545 try:
Antoine Pitrou605c2932010-09-23 20:15:14 +0000546 with open(name, "rb") as f:
547 tar = tarfile.open(name, mode, fileobj=f)
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000548 except tarfile.ReadError as e:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000549 self.fail()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000550 else:
551 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000552
553 def _test_modes(self, testfunc):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300554 if self.suffix:
555 with self.assertRaises(tarfile.ReadError):
556 tarfile.open(tarname, mode="r:" + self.suffix)
557 with self.assertRaises(tarfile.ReadError):
558 tarfile.open(tarname, mode="r|" + self.suffix)
559 with self.assertRaises(tarfile.ReadError):
560 tarfile.open(self.tarname, mode="r:")
561 with self.assertRaises(tarfile.ReadError):
562 tarfile.open(self.tarname, mode="r|")
563 testfunc(self.tarname, "r")
564 testfunc(self.tarname, "r:" + self.suffix)
565 testfunc(self.tarname, "r:*")
566 testfunc(self.tarname, "r|" + self.suffix)
567 testfunc(self.tarname, "r|*")
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100568
Guido van Rossumd8faa362007-04-27 19:54:29 +0000569 def test_detect_file(self):
570 self._test_modes(self._testfunc_file)
571
572 def test_detect_fileobj(self):
573 self._test_modes(self._testfunc_fileobj)
574
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300575class GzipDetectReadTest(GzipTest, DetectReadTest):
576 pass
577
578class Bz2DetectReadTest(Bz2Test, DetectReadTest):
Lars Gustäbeled1ac582011-12-06 12:56:38 +0100579 def test_detect_stream_bz2(self):
580 # Originally, tarfile's stream detection looked for the string
581 # "BZh91" at the start of the file. This is incorrect because
582 # the '9' represents the blocksize (900kB). If the file was
583 # compressed using another blocksize autodetection fails.
Lars Gustäbeled1ac582011-12-06 12:56:38 +0100584 with open(tarname, "rb") as fobj:
585 data = fobj.read()
586
587 # Compress with blocksize 100kB, the file starts with "BZh11".
588 with bz2.BZ2File(tmpname, "wb", compresslevel=1) as fobj:
589 fobj.write(data)
590
591 self._testfunc_file(tmpname, "r|*")
592
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300593class LzmaDetectReadTest(LzmaTest, DetectReadTest):
594 pass
Guido van Rossumd8faa362007-04-27 19:54:29 +0000595
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300596
597class MemberReadTest(ReadTest, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000598
599 def _test_member(self, tarinfo, chksum=None, **kwargs):
600 if chksum is not None:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300601 with self.tar.extractfile(tarinfo) as f:
602 self.assertEqual(md5sum(f.read()), chksum,
603 "wrong md5sum for %s" % tarinfo.name)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000604
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000605 kwargs["mtime"] = 0o7606136617
Guido van Rossumd8faa362007-04-27 19:54:29 +0000606 kwargs["uid"] = 1000
607 kwargs["gid"] = 100
608 if "old-v7" not in tarinfo.name:
609 # V7 tar can't handle alphabetic owners.
610 kwargs["uname"] = "tarfile"
611 kwargs["gname"] = "tarfile"
612 for k, v in kwargs.items():
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300613 self.assertEqual(getattr(tarinfo, k), v,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000614 "wrong value in %s field of %s" % (k, tarinfo.name))
615
616 def test_find_regtype(self):
617 tarinfo = self.tar.getmember("ustar/regtype")
618 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
619
620 def test_find_conttype(self):
621 tarinfo = self.tar.getmember("ustar/conttype")
622 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
623
624 def test_find_dirtype(self):
625 tarinfo = self.tar.getmember("ustar/dirtype")
626 self._test_member(tarinfo, size=0)
627
628 def test_find_dirtype_with_size(self):
629 tarinfo = self.tar.getmember("ustar/dirtype-with-size")
630 self._test_member(tarinfo, size=255)
631
632 def test_find_lnktype(self):
633 tarinfo = self.tar.getmember("ustar/lnktype")
634 self._test_member(tarinfo, size=0, linkname="ustar/regtype")
635
636 def test_find_symtype(self):
637 tarinfo = self.tar.getmember("ustar/symtype")
638 self._test_member(tarinfo, size=0, linkname="regtype")
639
640 def test_find_blktype(self):
641 tarinfo = self.tar.getmember("ustar/blktype")
642 self._test_member(tarinfo, size=0, devmajor=3, devminor=0)
643
644 def test_find_chrtype(self):
645 tarinfo = self.tar.getmember("ustar/chrtype")
646 self._test_member(tarinfo, size=0, devmajor=1, devminor=3)
647
648 def test_find_fifotype(self):
649 tarinfo = self.tar.getmember("ustar/fifotype")
650 self._test_member(tarinfo, size=0)
651
652 def test_find_sparse(self):
653 tarinfo = self.tar.getmember("ustar/sparse")
654 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
655
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000656 def test_find_gnusparse(self):
657 tarinfo = self.tar.getmember("gnu/sparse")
658 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
659
660 def test_find_gnusparse_00(self):
661 tarinfo = self.tar.getmember("gnu/sparse-0.0")
662 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
663
664 def test_find_gnusparse_01(self):
665 tarinfo = self.tar.getmember("gnu/sparse-0.1")
666 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
667
668 def test_find_gnusparse_10(self):
669 tarinfo = self.tar.getmember("gnu/sparse-1.0")
670 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
671
Guido van Rossumd8faa362007-04-27 19:54:29 +0000672 def test_find_umlauts(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300673 tarinfo = self.tar.getmember("ustar/umlauts-"
674 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000675 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
676
677 def test_find_ustar_longname(self):
678 name = "ustar/" + "12345/" * 39 + "1234567/longname"
Benjamin Peterson577473f2010-01-19 00:09:57 +0000679 self.assertIn(name, self.tar.getnames())
Guido van Rossumd8faa362007-04-27 19:54:29 +0000680
681 def test_find_regtype_oldv7(self):
682 tarinfo = self.tar.getmember("misc/regtype-old-v7")
683 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
684
685 def test_find_pax_umlauts(self):
Antoine Pitrouab58b5f2010-09-23 19:39:35 +0000686 self.tar.close()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300687 self.tar = tarfile.open(self.tarname, mode=self.mode,
688 encoding="iso8859-1")
689 tarinfo = self.tar.getmember("pax/umlauts-"
690 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000691 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
692
693
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300694class LongnameTest:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000695
696 def test_read_longname(self):
697 # Test reading of longname (bug #1471427).
Guido van Rossume7ba4952007-06-06 23:52:48 +0000698 longname = self.subdir + "/" + "123/" * 125 + "longname"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000699 try:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000700 tarinfo = self.tar.getmember(longname)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000701 except KeyError:
702 self.fail("longname not found")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300703 self.assertNotEqual(tarinfo.type, tarfile.DIRTYPE,
704 "read longname as dirtype")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000705
706 def test_read_longlink(self):
707 longname = self.subdir + "/" + "123/" * 125 + "longname"
708 longlink = self.subdir + "/" + "123/" * 125 + "longlink"
709 try:
710 tarinfo = self.tar.getmember(longlink)
711 except KeyError:
712 self.fail("longlink not found")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300713 self.assertEqual(tarinfo.linkname, longname, "linkname wrong")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000714
715 def test_truncated_longname(self):
716 longname = self.subdir + "/" + "123/" * 125 + "longname"
717 tarinfo = self.tar.getmember(longname)
718 offset = tarinfo.offset
719 self.tar.fileobj.seek(offset)
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000720 fobj = io.BytesIO(self.tar.fileobj.read(3 * 512))
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300721 with self.assertRaises(tarfile.ReadError):
722 tarfile.open(name="foo.tar", fileobj=fobj)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000723
Guido van Rossume7ba4952007-06-06 23:52:48 +0000724 def test_header_offset(self):
725 # Test if the start offset of the TarInfo object includes
726 # the preceding extended header.
727 longname = self.subdir + "/" + "123/" * 125 + "longname"
728 offset = self.tar.getmember(longname).offset
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000729 with open(tarname, "rb") as fobj:
730 fobj.seek(offset)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300731 tarinfo = tarfile.TarInfo.frombuf(fobj.read(512),
732 "iso8859-1", "strict")
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000733 self.assertEqual(tarinfo.type, self.longnametype)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000734
Guido van Rossumd8faa362007-04-27 19:54:29 +0000735
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300736class GNUReadTest(LongnameTest, ReadTest, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000737
738 subdir = "gnu"
Guido van Rossume7ba4952007-06-06 23:52:48 +0000739 longnametype = tarfile.GNUTYPE_LONGNAME
Guido van Rossumd8faa362007-04-27 19:54:29 +0000740
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000741 # Since 3.2 tarfile is supposed to accurately restore sparse members and
742 # produce files with holes. This is what we actually want to test here.
743 # Unfortunately, not all platforms/filesystems support sparse files, and
744 # even on platforms that do it is non-trivial to make reliable assertions
745 # about holes in files. Therefore, we first do one basic test which works
746 # an all platforms, and after that a test that will work only on
747 # platforms/filesystems that prove to support sparse files.
748 def _test_sparse_file(self, name):
749 self.tar.extract(name, TEMPDIR)
750 filename = os.path.join(TEMPDIR, name)
751 with open(filename, "rb") as fobj:
752 data = fobj.read()
753 self.assertEqual(md5sum(data), md5_sparse,
754 "wrong md5sum for %s" % name)
755
756 if self._fs_supports_holes():
757 s = os.stat(filename)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300758 self.assertLess(s.st_blocks * 512, s.st_size)
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000759
760 def test_sparse_file_old(self):
761 self._test_sparse_file("gnu/sparse")
762
763 def test_sparse_file_00(self):
764 self._test_sparse_file("gnu/sparse-0.0")
765
766 def test_sparse_file_01(self):
767 self._test_sparse_file("gnu/sparse-0.1")
768
769 def test_sparse_file_10(self):
770 self._test_sparse_file("gnu/sparse-1.0")
771
772 @staticmethod
773 def _fs_supports_holes():
774 # Return True if the platform knows the st_blocks stat attribute and
775 # uses st_blocks units of 512 bytes, and if the filesystem is able to
776 # store holes in files.
Victor Stinner9c3de4a2011-08-17 20:49:41 +0200777 if sys.platform.startswith("linux"):
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000778 # Linux evidentially has 512 byte st_blocks units.
779 name = os.path.join(TEMPDIR, "sparse-test")
780 with open(name, "wb") as fobj:
781 fobj.seek(4096)
782 fobj.truncate()
783 s = os.stat(name)
784 os.remove(name)
785 return s.st_blocks == 0
786 else:
787 return False
Guido van Rossumd8faa362007-04-27 19:54:29 +0000788
789
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300790class PaxReadTest(LongnameTest, ReadTest, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000791
792 subdir = "pax"
Guido van Rossume7ba4952007-06-06 23:52:48 +0000793 longnametype = tarfile.XHDTYPE
Guido van Rossumd8faa362007-04-27 19:54:29 +0000794
Guido van Rossume7ba4952007-06-06 23:52:48 +0000795 def test_pax_global_headers(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000796 tar = tarfile.open(tarname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000797 try:
798 tarinfo = tar.getmember("pax/regtype1")
799 self.assertEqual(tarinfo.uname, "foo")
800 self.assertEqual(tarinfo.gname, "bar")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300801 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"),
802 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Guido van Rossume7ba4952007-06-06 23:52:48 +0000803
Antoine Pitrou95f55602010-09-23 18:36:46 +0000804 tarinfo = tar.getmember("pax/regtype2")
805 self.assertEqual(tarinfo.uname, "")
806 self.assertEqual(tarinfo.gname, "bar")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300807 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"),
808 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000809
Antoine Pitrou95f55602010-09-23 18:36:46 +0000810 tarinfo = tar.getmember("pax/regtype3")
811 self.assertEqual(tarinfo.uname, "tarfile")
812 self.assertEqual(tarinfo.gname, "tarfile")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300813 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"),
814 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000815 finally:
816 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000817
818 def test_pax_number_fields(self):
819 # All following number fields are read from the pax header.
820 tar = tarfile.open(tarname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000821 try:
822 tarinfo = tar.getmember("pax/regtype4")
823 self.assertEqual(tarinfo.size, 7011)
824 self.assertEqual(tarinfo.uid, 123)
825 self.assertEqual(tarinfo.gid, 123)
826 self.assertEqual(tarinfo.mtime, 1041808783.0)
827 self.assertEqual(type(tarinfo.mtime), float)
828 self.assertEqual(float(tarinfo.pax_headers["atime"]), 1041808783.0)
829 self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0)
830 finally:
831 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000832
833
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300834class WriteTestBase(TarTest):
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000835 # Put all write tests in here that are supposed to be tested
836 # in all possible mode combinations.
837
838 def test_fileobj_no_close(self):
839 fobj = io.BytesIO()
840 tar = tarfile.open(fileobj=fobj, mode=self.mode)
841 tar.addfile(tarfile.TarInfo("foo"))
842 tar.close()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300843 self.assertFalse(fobj.closed, "external fileobjs must never closed")
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000844
845
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300846class WriteTest(WriteTestBase, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000847
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300848 prefix = "w:"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000849
850 def test_100_char_name(self):
851 # The name field in a tar header stores strings of at most 100 chars.
852 # If a string is shorter than 100 chars it has to be padded with '\0',
853 # which implies that a string of exactly 100 chars is stored without
854 # a trailing '\0'.
855 name = "0123456789" * 10
856 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000857 try:
858 t = tarfile.TarInfo(name)
859 tar.addfile(t)
860 finally:
861 tar.close()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000862
Guido van Rossumd8faa362007-04-27 19:54:29 +0000863 tar = tarfile.open(tmpname)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000864 try:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300865 self.assertEqual(tar.getnames()[0], name,
Antoine Pitrou95f55602010-09-23 18:36:46 +0000866 "failed to store 100 char filename")
867 finally:
868 tar.close()
Thomas Wouters89f507f2006-12-13 04:49:30 +0000869
Guido van Rossumd8faa362007-04-27 19:54:29 +0000870 def test_tar_size(self):
871 # Test for bug #1013882.
872 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000873 try:
874 path = os.path.join(TEMPDIR, "file")
875 with open(path, "wb") as fobj:
876 fobj.write(b"aaa")
877 tar.add(path)
878 finally:
879 tar.close()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300880 self.assertGreater(os.path.getsize(tmpname), 0,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000881 "tarfile is empty")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000882
Guido van Rossumd8faa362007-04-27 19:54:29 +0000883 # The test_*_size tests test for bug #1167128.
884 def test_file_size(self):
885 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000886 try:
887 path = os.path.join(TEMPDIR, "file")
888 with open(path, "wb"):
889 pass
890 tarinfo = tar.gettarinfo(path)
891 self.assertEqual(tarinfo.size, 0)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000892
Antoine Pitrou95f55602010-09-23 18:36:46 +0000893 with open(path, "wb") as fobj:
894 fobj.write(b"aaa")
895 tarinfo = tar.gettarinfo(path)
896 self.assertEqual(tarinfo.size, 3)
897 finally:
898 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000899
900 def test_directory_size(self):
901 path = os.path.join(TEMPDIR, "directory")
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000902 os.mkdir(path)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000903 try:
904 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000905 try:
906 tarinfo = tar.gettarinfo(path)
907 self.assertEqual(tarinfo.size, 0)
908 finally:
909 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000910 finally:
911 os.rmdir(path)
912
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300913 @unittest.skipUnless(hasattr(os, "link"),
914 "Missing hardlink implementation")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000915 def test_link_size(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300916 link = os.path.join(TEMPDIR, "link")
917 target = os.path.join(TEMPDIR, "link_target")
918 with open(target, "wb") as fobj:
919 fobj.write(b"aaa")
920 os.link(target, link)
921 try:
922 tar = tarfile.open(tmpname, self.mode)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000923 try:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300924 # Record the link target in the inodes list.
925 tar.gettarinfo(target)
926 tarinfo = tar.gettarinfo(link)
927 self.assertEqual(tarinfo.size, 0)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000928 finally:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300929 tar.close()
930 finally:
931 os.remove(target)
932 os.remove(link)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000933
Brian Curtin3b4499c2010-12-28 14:31:47 +0000934 @support.skip_unless_symlink
Guido van Rossumd8faa362007-04-27 19:54:29 +0000935 def test_symlink_size(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000936 path = os.path.join(TEMPDIR, "symlink")
937 os.symlink("link_target", path)
938 try:
939 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000940 try:
941 tarinfo = tar.gettarinfo(path)
942 self.assertEqual(tarinfo.size, 0)
943 finally:
944 tar.close()
Brian Curtind40e6f72010-07-08 21:39:08 +0000945 finally:
946 os.remove(path)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000947
948 def test_add_self(self):
949 # Test for #1257255.
950 dstname = os.path.abspath(tmpname)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000951 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000952 try:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300953 self.assertEqual(tar.name, dstname,
954 "archive name must be absolute")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000955 tar.add(dstname)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300956 self.assertEqual(tar.getnames(), [],
957 "added the archive to itself")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000958
Antoine Pitrou95f55602010-09-23 18:36:46 +0000959 cwd = os.getcwd()
960 os.chdir(TEMPDIR)
961 tar.add(dstname)
962 os.chdir(cwd)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300963 self.assertEqual(tar.getnames(), [],
964 "added the archive to itself")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000965 finally:
966 tar.close()
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000967
Guido van Rossum486364b2007-06-30 05:01:58 +0000968 def test_exclude(self):
969 tempdir = os.path.join(TEMPDIR, "exclude")
970 os.mkdir(tempdir)
971 try:
972 for name in ("foo", "bar", "baz"):
973 name = os.path.join(tempdir, name)
Victor Stinnerbf816222011-06-30 23:25:47 +0200974 support.create_empty_file(name)
Guido van Rossum486364b2007-06-30 05:01:58 +0000975
Benjamin Peterson886af962010-03-21 23:13:07 +0000976 exclude = os.path.isfile
Guido van Rossum486364b2007-06-30 05:01:58 +0000977
978 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000979 try:
980 with support.check_warnings(("use the filter argument",
981 DeprecationWarning)):
982 tar.add(tempdir, arcname="empty_dir", exclude=exclude)
983 finally:
984 tar.close()
Guido van Rossum486364b2007-06-30 05:01:58 +0000985
986 tar = tarfile.open(tmpname, "r")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000987 try:
988 self.assertEqual(len(tar.getmembers()), 1)
989 self.assertEqual(tar.getnames()[0], "empty_dir")
990 finally:
991 tar.close()
Guido van Rossum486364b2007-06-30 05:01:58 +0000992 finally:
993 shutil.rmtree(tempdir)
994
Lars Gustäbel049d2aa2009-09-12 10:44:00 +0000995 def test_filter(self):
996 tempdir = os.path.join(TEMPDIR, "filter")
997 os.mkdir(tempdir)
998 try:
999 for name in ("foo", "bar", "baz"):
1000 name = os.path.join(tempdir, name)
Victor Stinnerbf816222011-06-30 23:25:47 +02001001 support.create_empty_file(name)
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001002
1003 def filter(tarinfo):
1004 if os.path.basename(tarinfo.name) == "bar":
1005 return
1006 tarinfo.uid = 123
1007 tarinfo.uname = "foo"
1008 return tarinfo
1009
1010 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001011 try:
1012 tar.add(tempdir, arcname="empty_dir", filter=filter)
1013 finally:
1014 tar.close()
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001015
Raymond Hettingera63a3122011-01-26 20:34:14 +00001016 # Verify that filter is a keyword-only argument
1017 with self.assertRaises(TypeError):
1018 tar.add(tempdir, "empty_dir", True, None, filter)
1019
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001020 tar = tarfile.open(tmpname, "r")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001021 try:
1022 for tarinfo in tar:
1023 self.assertEqual(tarinfo.uid, 123)
1024 self.assertEqual(tarinfo.uname, "foo")
1025 self.assertEqual(len(tar.getmembers()), 3)
1026 finally:
1027 tar.close()
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001028 finally:
1029 shutil.rmtree(tempdir)
1030
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001031 # Guarantee that stored pathnames are not modified. Don't
1032 # remove ./ or ../ or double slashes. Still make absolute
1033 # pathnames relative.
1034 # For details see bug #6054.
1035 def _test_pathname(self, path, cmp_path=None, dir=False):
1036 # Create a tarfile with an empty member named path
1037 # and compare the stored name with the original.
1038 foo = os.path.join(TEMPDIR, "foo")
1039 if not dir:
Victor Stinnerbf816222011-06-30 23:25:47 +02001040 support.create_empty_file(foo)
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001041 else:
1042 os.mkdir(foo)
1043
1044 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001045 try:
1046 tar.add(foo, arcname=path)
1047 finally:
1048 tar.close()
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001049
1050 tar = tarfile.open(tmpname, "r")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001051 try:
1052 t = tar.next()
1053 finally:
1054 tar.close()
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001055
1056 if not dir:
1057 os.remove(foo)
1058 else:
1059 os.rmdir(foo)
1060
1061 self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
1062
Senthil Kumaranbe5dbeb2011-04-30 06:09:51 +08001063
1064 @support.skip_unless_symlink
Senthil Kumaran123932f2011-04-28 15:38:12 +08001065 def test_extractall_symlinks(self):
1066 # Test if extractall works properly when tarfile contains symlinks
1067 tempdir = os.path.join(TEMPDIR, "testsymlinks")
1068 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
1069 os.mkdir(tempdir)
1070 try:
1071 source_file = os.path.join(tempdir,'source')
1072 target_file = os.path.join(tempdir,'symlink')
1073 with open(source_file,'w') as f:
1074 f.write('something\n')
1075 os.symlink(source_file, target_file)
1076 tar = tarfile.open(temparchive,'w')
1077 tar.add(source_file)
1078 tar.add(target_file)
1079 tar.close()
1080 # Let's extract it to the location which contains the symlink
1081 tar = tarfile.open(temparchive,'r')
1082 # this should not raise OSError: [Errno 17] File exists
1083 try:
1084 tar.extractall(path=tempdir)
1085 except OSError:
1086 self.fail("extractall failed with symlinked files")
1087 finally:
1088 tar.close()
1089 finally:
1090 os.unlink(temparchive)
1091 shutil.rmtree(tempdir)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +00001092
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001093 def test_pathnames(self):
1094 self._test_pathname("foo")
1095 self._test_pathname(os.path.join("foo", ".", "bar"))
1096 self._test_pathname(os.path.join("foo", "..", "bar"))
1097 self._test_pathname(os.path.join(".", "foo"))
1098 self._test_pathname(os.path.join(".", "foo", "."))
1099 self._test_pathname(os.path.join(".", "foo", ".", "bar"))
1100 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
1101 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
1102 self._test_pathname(os.path.join("..", "foo"))
1103 self._test_pathname(os.path.join("..", "foo", ".."))
1104 self._test_pathname(os.path.join("..", "foo", ".", "bar"))
1105 self._test_pathname(os.path.join("..", "foo", "..", "bar"))
1106
1107 self._test_pathname("foo" + os.sep + os.sep + "bar")
1108 self._test_pathname("foo" + os.sep + os.sep, "foo", dir=True)
1109
1110 def test_abs_pathnames(self):
1111 if sys.platform == "win32":
1112 self._test_pathname("C:\\foo", "foo")
1113 else:
1114 self._test_pathname("/foo", "foo")
1115 self._test_pathname("///foo", "foo")
1116
1117 def test_cwd(self):
1118 # Test adding the current working directory.
1119 cwd = os.getcwd()
1120 os.chdir(TEMPDIR)
1121 try:
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001122 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001123 try:
1124 tar.add(".")
1125 finally:
1126 tar.close()
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001127
1128 tar = tarfile.open(tmpname, "r")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001129 try:
1130 for t in tar:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001131 if t.name != ".":
1132 self.assertTrue(t.name.startswith("./"), t.name)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001133 finally:
1134 tar.close()
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001135 finally:
1136 os.chdir(cwd)
1137
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001138class GzipWriteTest(GzipTest, WriteTest):
1139 pass
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001140
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001141class Bz2WriteTest(Bz2Test, WriteTest):
1142 pass
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001143
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001144class LzmaWriteTest(LzmaTest, WriteTest):
1145 pass
1146
1147
1148class StreamWriteTest(WriteTestBase, unittest.TestCase):
1149
1150 prefix = "w|"
1151 decompressor = None
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001152
Guido van Rossumd8faa362007-04-27 19:54:29 +00001153 def test_stream_padding(self):
1154 # Test for bug #1543303.
1155 tar = tarfile.open(tmpname, self.mode)
1156 tar.close()
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001157 if self.decompressor:
1158 dec = self.decompressor()
Antoine Pitrou95f55602010-09-23 18:36:46 +00001159 with open(tmpname, "rb") as fobj:
1160 data = fobj.read()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001161 data = dec.decompress(data)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001162 self.assertFalse(dec.unused_data, "found trailing data")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001163 else:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001164 with self.open(tmpname) as fobj:
Antoine Pitrou95f55602010-09-23 18:36:46 +00001165 data = fobj.read()
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001166 self.assertEqual(data.count(b"\0"), tarfile.RECORDSIZE,
1167 "incorrect zero padding")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001168
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001169 @unittest.skipUnless(sys.platform != "win32" and hasattr(os, "umask"),
1170 "Missing umask implementation")
Lars Gustäbeld6eb70b2010-04-29 15:37:02 +00001171 def test_file_mode(self):
1172 # Test for issue #8464: Create files with correct
1173 # permissions.
Lars Gustäbeld6eb70b2010-04-29 15:37:02 +00001174 if os.path.exists(tmpname):
1175 os.remove(tmpname)
1176
1177 original_umask = os.umask(0o022)
1178 try:
1179 tar = tarfile.open(tmpname, self.mode)
1180 tar.close()
1181 mode = os.stat(tmpname).st_mode & 0o777
1182 self.assertEqual(mode, 0o644, "wrong file permissions")
1183 finally:
1184 os.umask(original_umask)
1185
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001186class GzipStreamWriteTest(GzipTest, StreamWriteTest):
1187 pass
1188
1189class Bz2StreamWriteTest(Bz2Test, StreamWriteTest):
1190 decompressor = bz2.BZ2Decompressor if bz2 else None
1191
1192class LzmaStreamWriteTest(LzmaTest, StreamWriteTest):
1193 decompressor = lzma.LZMADecompressor if lzma else None
1194
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001195
Guido van Rossumd8faa362007-04-27 19:54:29 +00001196class GNUWriteTest(unittest.TestCase):
1197 # This testcase checks for correct creation of GNU Longname
1198 # and Longlink extended headers (cp. bug #812325).
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001199
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001200 def _length(self, s):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001201 blocks = len(s) // 512 + 1
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001202 return blocks * 512
1203
1204 def _calc_size(self, name, link=None):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001205 # Initial tar header
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001206 count = 512
1207
1208 if len(name) > tarfile.LENGTH_NAME:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001209 # GNU longname extended header + longname
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001210 count += 512
1211 count += self._length(name)
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001212 if link is not None and len(link) > tarfile.LENGTH_LINK:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001213 # GNU longlink extended header + longlink
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001214 count += 512
1215 count += self._length(link)
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001216 return count
1217
1218 def _test(self, name, link=None):
1219 tarinfo = tarfile.TarInfo(name)
1220 if link:
1221 tarinfo.linkname = link
1222 tarinfo.type = tarfile.LNKTYPE
1223
Guido van Rossumd8faa362007-04-27 19:54:29 +00001224 tar = tarfile.open(tmpname, "w")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001225 try:
1226 tar.format = tarfile.GNU_FORMAT
1227 tar.addfile(tarinfo)
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001228
Antoine Pitrou95f55602010-09-23 18:36:46 +00001229 v1 = self._calc_size(name, link)
1230 v2 = tar.offset
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001231 self.assertEqual(v1, v2, "GNU longname/longlink creation failed")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001232 finally:
1233 tar.close()
Thomas Wouters89f507f2006-12-13 04:49:30 +00001234
Guido van Rossumd8faa362007-04-27 19:54:29 +00001235 tar = tarfile.open(tmpname)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001236 try:
1237 member = tar.next()
1238 self.assertIsNotNone(member,
1239 "unable to read longname member")
1240 self.assertEqual(tarinfo.name, member.name,
1241 "unable to read longname member")
1242 self.assertEqual(tarinfo.linkname, member.linkname,
1243 "unable to read longname member")
1244 finally:
1245 tar.close()
Thomas Wouters89f507f2006-12-13 04:49:30 +00001246
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001247 def test_longname_1023(self):
1248 self._test(("longnam/" * 127) + "longnam")
1249
1250 def test_longname_1024(self):
1251 self._test(("longnam/" * 127) + "longname")
1252
1253 def test_longname_1025(self):
1254 self._test(("longnam/" * 127) + "longname_")
1255
1256 def test_longlink_1023(self):
1257 self._test("name", ("longlnk/" * 127) + "longlnk")
1258
1259 def test_longlink_1024(self):
1260 self._test("name", ("longlnk/" * 127) + "longlink")
1261
1262 def test_longlink_1025(self):
1263 self._test("name", ("longlnk/" * 127) + "longlink_")
1264
1265 def test_longnamelink_1023(self):
1266 self._test(("longnam/" * 127) + "longnam",
1267 ("longlnk/" * 127) + "longlnk")
1268
1269 def test_longnamelink_1024(self):
1270 self._test(("longnam/" * 127) + "longname",
1271 ("longlnk/" * 127) + "longlink")
1272
1273 def test_longnamelink_1025(self):
1274 self._test(("longnam/" * 127) + "longname_",
1275 ("longlnk/" * 127) + "longlink_")
1276
Guido van Rossumd8faa362007-04-27 19:54:29 +00001277
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001278@unittest.skipUnless(hasattr(os, "link"), "Missing hardlink implementation")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001279class HardlinkTest(unittest.TestCase):
1280 # Test the creation of LNKTYPE (hardlink) members in an archive.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001281
1282 def setUp(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001283 self.foo = os.path.join(TEMPDIR, "foo")
1284 self.bar = os.path.join(TEMPDIR, "bar")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001285
Antoine Pitrou95f55602010-09-23 18:36:46 +00001286 with open(self.foo, "wb") as fobj:
1287 fobj.write(b"foo")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001288
Guido van Rossumd8faa362007-04-27 19:54:29 +00001289 os.link(self.foo, self.bar)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001290
Guido van Rossumd8faa362007-04-27 19:54:29 +00001291 self.tar = tarfile.open(tmpname, "w")
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001292 self.tar.add(self.foo)
1293
Guido van Rossumd8faa362007-04-27 19:54:29 +00001294 def tearDown(self):
Hirokazu Yamamotoaf079d42008-09-21 11:50:03 +00001295 self.tar.close()
Antoine Pitrou95f55602010-09-23 18:36:46 +00001296 support.unlink(self.foo)
1297 support.unlink(self.bar)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001298
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001299 def test_add_twice(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001300 # The same name will be added as a REGTYPE every
1301 # time regardless of st_nlink.
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001302 tarinfo = self.tar.gettarinfo(self.foo)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001303 self.assertEqual(tarinfo.type, tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001304 "add file as regular failed")
1305
1306 def test_add_hardlink(self):
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001307 tarinfo = self.tar.gettarinfo(self.bar)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001308 self.assertEqual(tarinfo.type, tarfile.LNKTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001309 "add file as hardlink failed")
1310
1311 def test_dereference_hardlink(self):
1312 self.tar.dereference = True
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001313 tarinfo = self.tar.gettarinfo(self.bar)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001314 self.assertEqual(tarinfo.type, tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001315 "dereferencing hardlink failed")
1316
Neal Norwitza4f651a2004-07-20 22:07:44 +00001317
Guido van Rossumd8faa362007-04-27 19:54:29 +00001318class PaxWriteTest(GNUWriteTest):
Martin v. Löwis78be7df2005-03-05 12:47:42 +00001319
Guido van Rossumd8faa362007-04-27 19:54:29 +00001320 def _test(self, name, link=None):
1321 # See GNUWriteTest.
1322 tarinfo = tarfile.TarInfo(name)
1323 if link:
1324 tarinfo.linkname = link
1325 tarinfo.type = tarfile.LNKTYPE
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +00001326
Guido van Rossumd8faa362007-04-27 19:54:29 +00001327 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001328 try:
1329 tar.addfile(tarinfo)
1330 finally:
1331 tar.close()
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +00001332
Guido van Rossumd8faa362007-04-27 19:54:29 +00001333 tar = tarfile.open(tmpname)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001334 try:
1335 if link:
1336 l = tar.getmembers()[0].linkname
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001337 self.assertEqual(link, l, "PAX longlink creation failed")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001338 else:
1339 n = tar.getmembers()[0].name
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001340 self.assertEqual(name, n, "PAX longname creation failed")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001341 finally:
1342 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001343
Guido van Rossume7ba4952007-06-06 23:52:48 +00001344 def test_pax_global_header(self):
1345 pax_headers = {
Guido van Rossum9cbfffd2007-06-07 00:54:15 +00001346 "foo": "bar",
1347 "uid": "0",
1348 "mtime": "1.23",
Guido van Rossuma0557702007-08-07 23:19:53 +00001349 "test": "\xe4\xf6\xfc",
1350 "\xe4\xf6\xfc": "test"}
Guido van Rossume7ba4952007-06-06 23:52:48 +00001351
Benjamin Peterson886af962010-03-21 23:13:07 +00001352 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001353 pax_headers=pax_headers)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001354 try:
1355 tar.addfile(tarfile.TarInfo("test"))
1356 finally:
1357 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001358
1359 # Test if the global header was written correctly.
1360 tar = tarfile.open(tmpname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001361 try:
1362 self.assertEqual(tar.pax_headers, pax_headers)
1363 self.assertEqual(tar.getmembers()[0].pax_headers, pax_headers)
1364 # Test if all the fields are strings.
1365 for key, val in tar.pax_headers.items():
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001366 self.assertIsNot(type(key), bytes)
1367 self.assertIsNot(type(val), bytes)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001368 if key in tarfile.PAX_NUMBER_FIELDS:
1369 try:
1370 tarfile.PAX_NUMBER_FIELDS[key](val)
1371 except (TypeError, ValueError):
1372 self.fail("unable to convert pax header field")
1373 finally:
1374 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001375
1376 def test_pax_extended_header(self):
1377 # The fields from the pax header have priority over the
1378 # TarInfo.
Guido van Rossum9cbfffd2007-06-07 00:54:15 +00001379 pax_headers = {"path": "foo", "uid": "123"}
Guido van Rossume7ba4952007-06-06 23:52:48 +00001380
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001381 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT,
1382 encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001383 try:
1384 t = tarfile.TarInfo()
1385 t.name = "\xe4\xf6\xfc" # non-ASCII
1386 t.uid = 8**8 # too large
1387 t.pax_headers = pax_headers
1388 tar.addfile(t)
1389 finally:
1390 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001391
1392 tar = tarfile.open(tmpname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001393 try:
1394 t = tar.getmembers()[0]
1395 self.assertEqual(t.pax_headers, pax_headers)
1396 self.assertEqual(t.name, "foo")
1397 self.assertEqual(t.uid, 123)
1398 finally:
1399 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001400
1401
1402class UstarUnicodeTest(unittest.TestCase):
Guido van Rossume7ba4952007-06-06 23:52:48 +00001403
1404 format = tarfile.USTAR_FORMAT
1405
1406 def test_iso8859_1_filename(self):
1407 self._test_unicode_filename("iso8859-1")
1408
1409 def test_utf7_filename(self):
1410 self._test_unicode_filename("utf7")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001411
1412 def test_utf8_filename(self):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00001413 self._test_unicode_filename("utf-8")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001414
Guido van Rossumd8faa362007-04-27 19:54:29 +00001415 def _test_unicode_filename(self, encoding):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001416 tar = tarfile.open(tmpname, "w", format=self.format,
1417 encoding=encoding, errors="strict")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001418 try:
1419 name = "\xe4\xf6\xfc"
1420 tar.addfile(tarfile.TarInfo(name))
1421 finally:
1422 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001423
1424 tar = tarfile.open(tmpname, encoding=encoding)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001425 try:
1426 self.assertEqual(tar.getmembers()[0].name, name)
1427 finally:
1428 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001429
1430 def test_unicode_filename_error(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001431 tar = tarfile.open(tmpname, "w", format=self.format,
1432 encoding="ascii", errors="strict")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001433 try:
1434 tarinfo = tarfile.TarInfo()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001435
Antoine Pitrou95f55602010-09-23 18:36:46 +00001436 tarinfo.name = "\xe4\xf6\xfc"
1437 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001438
Antoine Pitrou95f55602010-09-23 18:36:46 +00001439 tarinfo.name = "foo"
1440 tarinfo.uname = "\xe4\xf6\xfc"
1441 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
1442 finally:
1443 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001444
1445 def test_unicode_argument(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001446 tar = tarfile.open(tarname, "r",
1447 encoding="iso8859-1", errors="strict")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001448 try:
1449 for t in tar:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001450 self.assertIs(type(t.name), str)
1451 self.assertIs(type(t.linkname), str)
1452 self.assertIs(type(t.uname), str)
1453 self.assertIs(type(t.gname), str)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001454 finally:
1455 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001456
Guido van Rossume7ba4952007-06-06 23:52:48 +00001457 def test_uname_unicode(self):
Lars Gustäbel3741eff2007-08-21 12:17:05 +00001458 t = tarfile.TarInfo("foo")
1459 t.uname = "\xe4\xf6\xfc"
1460 t.gname = "\xe4\xf6\xfc"
Guido van Rossumd8faa362007-04-27 19:54:29 +00001461
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001462 tar = tarfile.open(tmpname, mode="w", format=self.format,
1463 encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001464 try:
1465 tar.addfile(t)
1466 finally:
1467 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001468
Lars Gustäbel3741eff2007-08-21 12:17:05 +00001469 tar = tarfile.open(tmpname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001470 try:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001471 t = tar.getmember("foo")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001472 self.assertEqual(t.uname, "\xe4\xf6\xfc")
1473 self.assertEqual(t.gname, "\xe4\xf6\xfc")
1474
1475 if self.format != tarfile.PAX_FORMAT:
Antoine Pitrouab58b5f2010-09-23 19:39:35 +00001476 tar.close()
Antoine Pitrou95f55602010-09-23 18:36:46 +00001477 tar = tarfile.open(tmpname, encoding="ascii")
1478 t = tar.getmember("foo")
1479 self.assertEqual(t.uname, "\udce4\udcf6\udcfc")
1480 self.assertEqual(t.gname, "\udce4\udcf6\udcfc")
1481 finally:
1482 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001483
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001484
Guido van Rossume7ba4952007-06-06 23:52:48 +00001485class GNUUnicodeTest(UstarUnicodeTest):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001486
Guido van Rossume7ba4952007-06-06 23:52:48 +00001487 format = tarfile.GNU_FORMAT
Guido van Rossumd8faa362007-04-27 19:54:29 +00001488
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001489 def test_bad_pax_header(self):
1490 # Test for issue #8633. GNU tar <= 1.23 creates raw binary fields
1491 # without a hdrcharset=BINARY header.
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001492 for encoding, name in (
1493 ("utf-8", "pax/bad-pax-\udce4\udcf6\udcfc"),
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001494 ("iso8859-1", "pax/bad-pax-\xe4\xf6\xfc"),):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001495 with tarfile.open(tarname, encoding=encoding,
1496 errors="surrogateescape") as tar:
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001497 try:
1498 t = tar.getmember(name)
1499 except KeyError:
1500 self.fail("unable to read bad GNU tar pax header")
1501
Guido van Rossumd8faa362007-04-27 19:54:29 +00001502
Lars Gustäbel3741eff2007-08-21 12:17:05 +00001503class PAXUnicodeTest(UstarUnicodeTest):
1504
1505 format = tarfile.PAX_FORMAT
1506
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001507 # PAX_FORMAT ignores encoding in write mode.
1508 test_unicode_filename_error = None
1509
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001510 def test_binary_header(self):
1511 # Test a POSIX.1-2008 compatible header with a hdrcharset=BINARY field.
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001512 for encoding, name in (
1513 ("utf-8", "pax/hdrcharset-\udce4\udcf6\udcfc"),
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001514 ("iso8859-1", "pax/hdrcharset-\xe4\xf6\xfc"),):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001515 with tarfile.open(tarname, encoding=encoding,
1516 errors="surrogateescape") as tar:
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001517 try:
1518 t = tar.getmember(name)
1519 except KeyError:
1520 self.fail("unable to read POSIX.1-2008 binary header")
1521
Lars Gustäbel3741eff2007-08-21 12:17:05 +00001522
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001523class AppendTestBase:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001524 # Test append mode (cp. patch #1652681).
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001525
Guido van Rossumd8faa362007-04-27 19:54:29 +00001526 def setUp(self):
1527 self.tarname = tmpname
1528 if os.path.exists(self.tarname):
1529 os.remove(self.tarname)
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001530
Guido van Rossumd8faa362007-04-27 19:54:29 +00001531 def _create_testtar(self, mode="w:"):
Antoine Pitrou95f55602010-09-23 18:36:46 +00001532 with tarfile.open(tarname, encoding="iso8859-1") as src:
1533 t = src.getmember("ustar/regtype")
1534 t.name = "foo"
Lars Gustäbel7a919e92012-05-05 18:15:03 +02001535 with src.extractfile(t) as f:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +00001536 with tarfile.open(self.tarname, mode) as tar:
1537 tar.addfile(t, f)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001538
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001539 def test_append_compressed(self):
1540 self._create_testtar("w:" + self.suffix)
1541 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
1542
1543class AppendTest(AppendTestBase, unittest.TestCase):
1544 test_append_compressed = None
1545
1546 def _add_testfile(self, fileobj=None):
1547 with tarfile.open(self.tarname, "a", fileobj=fileobj) as tar:
1548 tar.addfile(tarfile.TarInfo("bar"))
1549
Guido van Rossumd8faa362007-04-27 19:54:29 +00001550 def _test(self, names=["bar"], fileobj=None):
Antoine Pitrou95f55602010-09-23 18:36:46 +00001551 with tarfile.open(self.tarname, fileobj=fileobj) as tar:
1552 self.assertEqual(tar.getnames(), names)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001553
1554 def test_non_existing(self):
1555 self._add_testfile()
1556 self._test()
1557
1558 def test_empty(self):
Lars Gustäbel9520a432009-11-22 18:48:49 +00001559 tarfile.open(self.tarname, "w:").close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001560 self._add_testfile()
1561 self._test()
1562
1563 def test_empty_fileobj(self):
Lars Gustäbel9520a432009-11-22 18:48:49 +00001564 fobj = io.BytesIO(b"\0" * 1024)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001565 self._add_testfile(fobj)
1566 fobj.seek(0)
1567 self._test(fileobj=fobj)
1568
1569 def test_fileobj(self):
1570 self._create_testtar()
Antoine Pitrou95f55602010-09-23 18:36:46 +00001571 with open(self.tarname, "rb") as fobj:
1572 data = fobj.read()
Guido van Rossum34d19282007-08-09 01:03:29 +00001573 fobj = io.BytesIO(data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001574 self._add_testfile(fobj)
1575 fobj.seek(0)
1576 self._test(names=["foo", "bar"], fileobj=fobj)
1577
1578 def test_existing(self):
1579 self._create_testtar()
1580 self._add_testfile()
1581 self._test(names=["foo", "bar"])
1582
Lars Gustäbel9520a432009-11-22 18:48:49 +00001583 # Append mode is supposed to fail if the tarfile to append to
1584 # does not end with a zero block.
1585 def _test_error(self, data):
Antoine Pitrou95f55602010-09-23 18:36:46 +00001586 with open(self.tarname, "wb") as fobj:
1587 fobj.write(data)
Lars Gustäbel9520a432009-11-22 18:48:49 +00001588 self.assertRaises(tarfile.ReadError, self._add_testfile)
1589
1590 def test_null(self):
1591 self._test_error(b"")
1592
1593 def test_incomplete(self):
1594 self._test_error(b"\0" * 13)
1595
1596 def test_premature_eof(self):
1597 data = tarfile.TarInfo("foo").tobuf()
1598 self._test_error(data)
1599
1600 def test_trailing_garbage(self):
1601 data = tarfile.TarInfo("foo").tobuf()
1602 self._test_error(data + b"\0" * 13)
1603
1604 def test_invalid(self):
1605 self._test_error(b"a" * 512)
1606
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001607class GzipAppendTest(GzipTest, AppendTestBase, unittest.TestCase):
1608 pass
1609
1610class Bz2AppendTest(Bz2Test, AppendTestBase, unittest.TestCase):
1611 pass
1612
1613class LzmaAppendTest(LzmaTest, AppendTestBase, unittest.TestCase):
1614 pass
1615
Guido van Rossumd8faa362007-04-27 19:54:29 +00001616
1617class LimitsTest(unittest.TestCase):
1618
1619 def test_ustar_limits(self):
1620 # 100 char name
1621 tarinfo = tarfile.TarInfo("0123456789" * 10)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001622 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001623
1624 # 101 char name that cannot be stored
1625 tarinfo = tarfile.TarInfo("0123456789" * 10 + "0")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001626 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001627
1628 # 256 char name with a slash at pos 156
1629 tarinfo = tarfile.TarInfo("123/" * 62 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001630 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001631
1632 # 256 char name that cannot be stored
1633 tarinfo = tarfile.TarInfo("1234567/" * 31 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001634 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001635
1636 # 512 char name
1637 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001638 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001639
1640 # 512 char linkname
1641 tarinfo = tarfile.TarInfo("longlink")
1642 tarinfo.linkname = "123/" * 126 + "longname"
Guido van Rossume7ba4952007-06-06 23:52:48 +00001643 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001644
1645 # uid > 8 digits
1646 tarinfo = tarfile.TarInfo("name")
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001647 tarinfo.uid = 0o10000000
Guido van Rossume7ba4952007-06-06 23:52:48 +00001648 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001649
1650 def test_gnu_limits(self):
1651 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001652 tarinfo.tobuf(tarfile.GNU_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001653
1654 tarinfo = tarfile.TarInfo("longlink")
1655 tarinfo.linkname = "123/" * 126 + "longname"
Guido van Rossume7ba4952007-06-06 23:52:48 +00001656 tarinfo.tobuf(tarfile.GNU_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001657
1658 # uid >= 256 ** 7
1659 tarinfo = tarfile.TarInfo("name")
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001660 tarinfo.uid = 0o4000000000000000000
Guido van Rossume7ba4952007-06-06 23:52:48 +00001661 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.GNU_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001662
1663 def test_pax_limits(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001664 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001665 tarinfo.tobuf(tarfile.PAX_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001666
1667 tarinfo = tarfile.TarInfo("longlink")
1668 tarinfo.linkname = "123/" * 126 + "longname"
Guido van Rossume7ba4952007-06-06 23:52:48 +00001669 tarinfo.tobuf(tarfile.PAX_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001670
1671 tarinfo = tarfile.TarInfo("name")
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001672 tarinfo.uid = 0o4000000000000000000
Guido van Rossume7ba4952007-06-06 23:52:48 +00001673 tarinfo.tobuf(tarfile.PAX_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001674
1675
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001676class MiscTest(unittest.TestCase):
1677
1678 def test_char_fields(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001679 self.assertEqual(tarfile.stn("foo", 8, "ascii", "strict"),
1680 b"foo\0\0\0\0\0")
1681 self.assertEqual(tarfile.stn("foobar", 3, "ascii", "strict"),
1682 b"foo")
1683 self.assertEqual(tarfile.nts(b"foo\0\0\0\0\0", "ascii", "strict"),
1684 "foo")
1685 self.assertEqual(tarfile.nts(b"foo\0bar\0", "ascii", "strict"),
1686 "foo")
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001687
Lars Gustäbelac3d1372011-10-14 12:46:40 +02001688 def test_read_number_fields(self):
1689 # Issue 13158: Test if GNU tar specific base-256 number fields
1690 # are decoded correctly.
1691 self.assertEqual(tarfile.nti(b"0000001\x00"), 1)
1692 self.assertEqual(tarfile.nti(b"7777777\x00"), 0o7777777)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001693 self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\x00\x20\x00\x00"),
1694 0o10000000)
1695 self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\xff\xff\xff\xff"),
1696 0xffffffff)
1697 self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\xff"),
1698 -1)
1699 self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\x9c"),
1700 -100)
1701 self.assertEqual(tarfile.nti(b"\xff\x00\x00\x00\x00\x00\x00\x00"),
1702 -0x100000000000000)
Lars Gustäbelac3d1372011-10-14 12:46:40 +02001703
1704 def test_write_number_fields(self):
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001705 self.assertEqual(tarfile.itn(1), b"0000001\x00")
Lars Gustäbelac3d1372011-10-14 12:46:40 +02001706 self.assertEqual(tarfile.itn(0o7777777), b"7777777\x00")
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001707 self.assertEqual(tarfile.itn(0o10000000),
1708 b"\x80\x00\x00\x00\x00\x20\x00\x00")
1709 self.assertEqual(tarfile.itn(0xffffffff),
1710 b"\x80\x00\x00\x00\xff\xff\xff\xff")
1711 self.assertEqual(tarfile.itn(-1),
1712 b"\xff\xff\xff\xff\xff\xff\xff\xff")
1713 self.assertEqual(tarfile.itn(-100),
1714 b"\xff\xff\xff\xff\xff\xff\xff\x9c")
1715 self.assertEqual(tarfile.itn(-0x100000000000000),
1716 b"\xff\x00\x00\x00\x00\x00\x00\x00")
Lars Gustäbelac3d1372011-10-14 12:46:40 +02001717
1718 def test_number_field_limits(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001719 with self.assertRaises(ValueError):
1720 tarfile.itn(-1, 8, tarfile.USTAR_FORMAT)
1721 with self.assertRaises(ValueError):
1722 tarfile.itn(0o10000000, 8, tarfile.USTAR_FORMAT)
1723 with self.assertRaises(ValueError):
1724 tarfile.itn(-0x10000000001, 6, tarfile.GNU_FORMAT)
1725 with self.assertRaises(ValueError):
1726 tarfile.itn(0x10000000000, 6, tarfile.GNU_FORMAT)
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001727
1728
Serhiy Storchakad27b4552013-11-24 01:53:29 +02001729class CommandLineTest(unittest.TestCase):
1730
1731 def tarfilecmd(self, *args):
1732 rc, out, err = script_helper.assert_python_ok('-m', 'tarfile', *args)
Antoine Pitrou3b7b1e52013-11-24 01:55:05 +01001733 return out.replace(os.linesep.encode(), b'\n')
Serhiy Storchakad27b4552013-11-24 01:53:29 +02001734
1735 def tarfilecmd_failure(self, *args):
1736 return script_helper.assert_python_failure('-m', 'tarfile', *args)
1737
1738 def make_simple_tarfile(self, tar_name):
1739 files = [support.findfile('tokenize_tests.txt'),
1740 support.findfile('tokenize_tests-no-coding-cookie-'
1741 'and-utf8-bom-sig-only.txt')]
1742 self.addCleanup(support.unlink, tar_name)
1743 with tarfile.open(tar_name, 'w') as tf:
1744 for tardata in files:
1745 tf.add(tardata, arcname=os.path.basename(tardata))
1746
1747 def test_test_command(self):
Serhiy Storchaka5e8c8092013-11-24 02:30:59 +02001748 for tar_name in testtarnames:
Serhiy Storchakad27b4552013-11-24 01:53:29 +02001749 for opt in '-t', '--test':
1750 out = self.tarfilecmd(opt, tar_name)
1751 self.assertEqual(out, b'')
1752
1753 def test_test_command_verbose(self):
Serhiy Storchaka5e8c8092013-11-24 02:30:59 +02001754 for tar_name in testtarnames:
Serhiy Storchakad27b4552013-11-24 01:53:29 +02001755 for opt in '-v', '--verbose':
1756 out = self.tarfilecmd(opt, '-t', tar_name)
1757 self.assertIn(b'is a tar archive.\n', out)
1758
1759 def test_test_command_invalid_file(self):
1760 zipname = support.findfile('zipdir.zip')
1761 rc, out, err = self.tarfilecmd_failure('-t', zipname)
1762 self.assertIn(b' is not a tar archive.', err)
1763 self.assertEqual(out, b'')
1764 self.assertEqual(rc, 1)
1765
Serhiy Storchaka5e8c8092013-11-24 02:30:59 +02001766 for tar_name in testtarnames:
Serhiy Storchakad27b4552013-11-24 01:53:29 +02001767 with self.subTest(tar_name=tar_name):
1768 with open(tar_name, 'rb') as f:
1769 data = f.read()
1770 try:
1771 with open(tmpname, 'wb') as f:
1772 f.write(data[:511])
1773 rc, out, err = self.tarfilecmd_failure('-t', tmpname)
1774 self.assertEqual(out, b'')
1775 self.assertEqual(rc, 1)
1776 finally:
1777 support.unlink(tmpname)
1778
1779 def test_list_command(self):
1780 self.make_simple_tarfile(tmpname)
1781 with support.captured_stdout() as t:
1782 with tarfile.open(tmpname, 'r') as tf:
1783 tf.list(verbose=False)
1784 expected = t.getvalue().encode(sys.getfilesystemencoding())
1785 for opt in '-l', '--list':
1786 out = self.tarfilecmd(opt, tmpname)
1787 self.assertEqual(out, expected)
1788
1789 def test_list_command_verbose(self):
1790 self.make_simple_tarfile(tmpname)
1791 with support.captured_stdout() as t:
1792 with tarfile.open(tmpname, 'r') as tf:
1793 tf.list(verbose=True)
1794 expected = t.getvalue().encode(sys.getfilesystemencoding())
1795 for opt in '-v', '--verbose':
1796 out = self.tarfilecmd(opt, '-l', tmpname)
1797 self.assertEqual(out, expected)
1798
1799 def test_list_command_invalid_file(self):
1800 zipname = support.findfile('zipdir.zip')
1801 rc, out, err = self.tarfilecmd_failure('-l', zipname)
1802 self.assertIn(b' is not a tar archive.', err)
1803 self.assertEqual(out, b'')
1804 self.assertEqual(rc, 1)
1805
1806 def test_create_command(self):
1807 files = [support.findfile('tokenize_tests.txt'),
1808 support.findfile('tokenize_tests-no-coding-cookie-'
1809 'and-utf8-bom-sig-only.txt')]
1810 for opt in '-c', '--create':
1811 try:
1812 out = self.tarfilecmd(opt, tmpname, *files)
1813 self.assertEqual(out, b'')
1814 with tarfile.open(tmpname) as tar:
1815 tar.getmembers()
1816 finally:
1817 support.unlink(tmpname)
1818
1819 def test_create_command_verbose(self):
1820 files = [support.findfile('tokenize_tests.txt'),
1821 support.findfile('tokenize_tests-no-coding-cookie-'
1822 'and-utf8-bom-sig-only.txt')]
1823 for opt in '-v', '--verbose':
1824 try:
1825 out = self.tarfilecmd(opt, '-c', tmpname, *files)
1826 self.assertIn(b' file created.', out)
1827 with tarfile.open(tmpname) as tar:
1828 tar.getmembers()
1829 finally:
1830 support.unlink(tmpname)
1831
1832 def test_create_command_dotless_filename(self):
1833 files = [support.findfile('tokenize_tests.txt')]
1834 try:
1835 out = self.tarfilecmd('-c', dotlessname, *files)
1836 self.assertEqual(out, b'')
1837 with tarfile.open(dotlessname) as tar:
1838 tar.getmembers()
1839 finally:
1840 support.unlink(dotlessname)
1841
1842 def test_create_command_dot_started_filename(self):
1843 tar_name = os.path.join(TEMPDIR, ".testtar")
1844 files = [support.findfile('tokenize_tests.txt')]
1845 try:
1846 out = self.tarfilecmd('-c', tar_name, *files)
1847 self.assertEqual(out, b'')
1848 with tarfile.open(tar_name) as tar:
1849 tar.getmembers()
1850 finally:
1851 support.unlink(tar_name)
1852
1853 def test_extract_command(self):
1854 self.make_simple_tarfile(tmpname)
1855 for opt in '-e', '--extract':
1856 try:
1857 with support.temp_cwd(tarextdir):
1858 out = self.tarfilecmd(opt, tmpname)
1859 self.assertEqual(out, b'')
1860 finally:
1861 support.rmtree(tarextdir)
1862
1863 def test_extract_command_verbose(self):
1864 self.make_simple_tarfile(tmpname)
1865 for opt in '-v', '--verbose':
1866 try:
1867 with support.temp_cwd(tarextdir):
1868 out = self.tarfilecmd(opt, '-e', tmpname)
1869 self.assertIn(b' file is extracted.', out)
1870 finally:
1871 support.rmtree(tarextdir)
1872
1873 def test_extract_command_different_directory(self):
1874 self.make_simple_tarfile(tmpname)
1875 try:
1876 with support.temp_cwd(tarextdir):
1877 out = self.tarfilecmd('-e', tmpname, 'spamdir')
1878 self.assertEqual(out, b'')
1879 finally:
1880 support.rmtree(tarextdir)
1881
1882 def test_extract_command_invalid_file(self):
1883 zipname = support.findfile('zipdir.zip')
1884 with support.temp_cwd(tarextdir):
1885 rc, out, err = self.tarfilecmd_failure('-e', zipname)
1886 self.assertIn(b' is not a tar archive.', err)
1887 self.assertEqual(out, b'')
1888 self.assertEqual(rc, 1)
1889
1890
Lars Gustäbel01385812010-03-03 12:08:54 +00001891class ContextManagerTest(unittest.TestCase):
1892
1893 def test_basic(self):
1894 with tarfile.open(tarname) as tar:
1895 self.assertFalse(tar.closed, "closed inside runtime context")
1896 self.assertTrue(tar.closed, "context manager failed")
1897
1898 def test_closed(self):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001899 # The __enter__() method is supposed to raise OSError
Lars Gustäbel01385812010-03-03 12:08:54 +00001900 # if the TarFile object is already closed.
1901 tar = tarfile.open(tarname)
1902 tar.close()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001903 with self.assertRaises(OSError):
Lars Gustäbel01385812010-03-03 12:08:54 +00001904 with tar:
1905 pass
1906
1907 def test_exception(self):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001908 # Test if the OSError exception is passed through properly.
Lars Gustäbel01385812010-03-03 12:08:54 +00001909 with self.assertRaises(Exception) as exc:
1910 with tarfile.open(tarname) as tar:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001911 raise OSError
1912 self.assertIsInstance(exc.exception, OSError,
Lars Gustäbel01385812010-03-03 12:08:54 +00001913 "wrong exception raised in context manager")
1914 self.assertTrue(tar.closed, "context manager failed")
1915
1916 def test_no_eof(self):
1917 # __exit__() must not write end-of-archive blocks if an
1918 # exception was raised.
1919 try:
1920 with tarfile.open(tmpname, "w") as tar:
1921 raise Exception
1922 except:
1923 pass
1924 self.assertEqual(os.path.getsize(tmpname), 0,
1925 "context manager wrote an end-of-archive block")
1926 self.assertTrue(tar.closed, "context manager failed")
1927
1928 def test_eof(self):
1929 # __exit__() must write end-of-archive blocks, i.e. call
1930 # TarFile.close() if there was no error.
1931 with tarfile.open(tmpname, "w"):
1932 pass
1933 self.assertNotEqual(os.path.getsize(tmpname), 0,
1934 "context manager wrote no end-of-archive block")
1935
1936 def test_fileobj(self):
1937 # Test that __exit__() did not close the external file
1938 # object.
Antoine Pitrou95f55602010-09-23 18:36:46 +00001939 with open(tmpname, "wb") as fobj:
1940 try:
1941 with tarfile.open(fileobj=fobj, mode="w") as tar:
1942 raise Exception
1943 except:
1944 pass
1945 self.assertFalse(fobj.closed, "external file object was closed")
1946 self.assertTrue(tar.closed, "context manager failed")
Lars Gustäbel01385812010-03-03 12:08:54 +00001947
1948
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001949@unittest.skipIf(hasattr(os, "link"), "requires os.link to be missing")
1950class LinkEmulationTest(ReadTest, unittest.TestCase):
Lars Gustäbel1b512722010-06-03 12:45:16 +00001951
1952 # Test for issue #8741 regression. On platforms that do not support
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001953 # symbolic or hard links tarfile tries to extract these types of members
1954 # as the regular files they point to.
Lars Gustäbel1b512722010-06-03 12:45:16 +00001955 def _test_link_extraction(self, name):
1956 self.tar.extract(name, TEMPDIR)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001957 with open(os.path.join(TEMPDIR, name), "rb") as f:
1958 data = f.read()
Lars Gustäbel1b512722010-06-03 12:45:16 +00001959 self.assertEqual(md5sum(data), md5_regtype)
1960
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001961 # See issues #1578269, #8879, and #17689 for some history on these skips
Brian Curtind40e6f72010-07-08 21:39:08 +00001962 @unittest.skipIf(hasattr(os.path, "islink"),
1963 "Skip emulation - has os.path.islink but not os.link")
Lars Gustäbel1b512722010-06-03 12:45:16 +00001964 def test_hardlink_extraction1(self):
1965 self._test_link_extraction("ustar/lnktype")
1966
Brian Curtind40e6f72010-07-08 21:39:08 +00001967 @unittest.skipIf(hasattr(os.path, "islink"),
1968 "Skip emulation - has os.path.islink but not os.link")
Lars Gustäbel1b512722010-06-03 12:45:16 +00001969 def test_hardlink_extraction2(self):
1970 self._test_link_extraction("./ustar/linktest2/lnktype")
1971
Brian Curtin74e45612010-07-09 15:58:59 +00001972 @unittest.skipIf(hasattr(os, "symlink"),
1973 "Skip emulation if symlink exists")
Lars Gustäbel1b512722010-06-03 12:45:16 +00001974 def test_symlink_extraction1(self):
1975 self._test_link_extraction("ustar/symtype")
1976
Brian Curtin74e45612010-07-09 15:58:59 +00001977 @unittest.skipIf(hasattr(os, "symlink"),
1978 "Skip emulation if symlink exists")
Lars Gustäbel1b512722010-06-03 12:45:16 +00001979 def test_symlink_extraction2(self):
1980 self._test_link_extraction("./ustar/linktest2/symtype")
1981
1982
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001983class Bz2PartialReadTest(Bz2Test, unittest.TestCase):
Lars Gustäbel42e00912009-03-22 20:34:29 +00001984 # Issue5068: The _BZ2Proxy.read() method loops forever
1985 # on an empty or partial bzipped file.
1986
1987 def _test_partial_input(self, mode):
1988 class MyBytesIO(io.BytesIO):
1989 hit_eof = False
1990 def read(self, n):
1991 if self.hit_eof:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001992 raise AssertionError("infinite loop detected in "
1993 "tarfile.open()")
Lars Gustäbel42e00912009-03-22 20:34:29 +00001994 self.hit_eof = self.tell() == len(self.getvalue())
1995 return super(MyBytesIO, self).read(n)
Lars Gustäbel9520a432009-11-22 18:48:49 +00001996 def seek(self, *args):
1997 self.hit_eof = False
1998 return super(MyBytesIO, self).seek(*args)
Lars Gustäbel42e00912009-03-22 20:34:29 +00001999
2000 data = bz2.compress(tarfile.TarInfo("foo").tobuf())
2001 for x in range(len(data) + 1):
Lars Gustäbel9520a432009-11-22 18:48:49 +00002002 try:
2003 tarfile.open(fileobj=MyBytesIO(data[:x]), mode=mode)
2004 except tarfile.ReadError:
2005 pass # we have no interest in ReadErrors
Lars Gustäbel42e00912009-03-22 20:34:29 +00002006
2007 def test_partial_input(self):
2008 self._test_partial_input("r")
2009
2010 def test_partial_input_bz2(self):
2011 self._test_partial_input("r:bz2")
2012
2013
Serhiy Storchaka8b562922013-06-17 15:38:50 +03002014def setUpModule():
Antoine Pitrou95f55602010-09-23 18:36:46 +00002015 support.unlink(TEMPDIR)
Antoine Pitrou941ee882009-11-11 20:59:38 +00002016 os.makedirs(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002017
Serhiy Storchaka5e8c8092013-11-24 02:30:59 +02002018 global testtarnames
2019 testtarnames = [tarname]
Antoine Pitrou95f55602010-09-23 18:36:46 +00002020 with open(tarname, "rb") as fobj:
2021 data = fobj.read()
Neal Norwitza4f651a2004-07-20 22:07:44 +00002022
Serhiy Storchaka8b562922013-06-17 15:38:50 +03002023 # Create compressed tarfiles.
2024 for c in GzipTest, Bz2Test, LzmaTest:
2025 if c.open:
2026 support.unlink(c.tarname)
Serhiy Storchaka5e8c8092013-11-24 02:30:59 +02002027 testtarnames.append(c.tarname)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03002028 with c.open(c.tarname, "wb") as tar:
2029 tar.write(data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002030
Serhiy Storchaka8b562922013-06-17 15:38:50 +03002031def tearDownModule():
2032 if os.path.exists(TEMPDIR):
2033 shutil.rmtree(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00002034
Neal Norwitz996acf12003-02-17 14:51:41 +00002035if __name__ == "__main__":
Serhiy Storchaka8b562922013-06-17 15:38:50 +03002036 unittest.main()