blob: 68fe60801de2fcd45358d40a46f737071adbfd8c [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +000010from test import support
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000011
12# Check for our compression modules.
13try:
14 import gzip
Serhiy Storchaka8b562922013-06-17 15:38:50 +030015except ImportError:
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000016 gzip = None
17try:
18 import bz2
19except ImportError:
20 bz2 = None
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +010021try:
22 import lzma
23except ImportError:
24 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"
Antoine Pitrou941ee882009-11-11 20:59:38 +000030tarname = support.findfile("testtar.tar")
Guido van Rossumd8faa362007-04-27 19:54:29 +000031gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
32bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +010033xzname = os.path.join(TEMPDIR, "testtar.tar.xz")
Guido van Rossumd8faa362007-04-27 19:54:29 +000034tmpname = os.path.join(TEMPDIR, "tmp.tar")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000035
Guido van Rossumd8faa362007-04-27 19:54:29 +000036md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
37md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000038
39
Serhiy Storchaka8b562922013-06-17 15:38:50 +030040class TarTest:
Guido van Rossumd8faa362007-04-27 19:54:29 +000041 tarname = tarname
Serhiy Storchaka8b562922013-06-17 15:38:50 +030042 suffix = ''
43 open = io.FileIO
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +020044 taropen = tarfile.TarFile.taropen
Serhiy Storchaka8b562922013-06-17 15:38:50 +030045
46 @property
47 def mode(self):
48 return self.prefix + self.suffix
49
50@support.requires_gzip
51class GzipTest:
52 tarname = gzipname
53 suffix = 'gz'
54 open = gzip.GzipFile if gzip else None
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +020055 taropen = tarfile.TarFile.gzopen
Serhiy Storchaka8b562922013-06-17 15:38:50 +030056
57@support.requires_bz2
58class Bz2Test:
59 tarname = bz2name
60 suffix = 'bz2'
61 open = bz2.BZ2File if bz2 else None
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +020062 taropen = tarfile.TarFile.bz2open
Serhiy Storchaka8b562922013-06-17 15:38:50 +030063
64@support.requires_lzma
65class LzmaTest:
66 tarname = xzname
67 suffix = 'xz'
68 open = lzma.LZMAFile if lzma else None
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +020069 taropen = tarfile.TarFile.xzopen
Serhiy Storchaka8b562922013-06-17 15:38:50 +030070
71
72class ReadTest(TarTest):
73
74 prefix = "r:"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000075
76 def setUp(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +030077 self.tar = tarfile.open(self.tarname, mode=self.mode,
78 encoding="iso8859-1")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000079
80 def tearDown(self):
81 self.tar.close()
82
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000083
Serhiy Storchaka8b562922013-06-17 15:38:50 +030084class UstarReadTest(ReadTest, unittest.TestCase):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000085
Guido van Rossumd8faa362007-04-27 19:54:29 +000086 def test_fileobj_regular_file(self):
87 tarinfo = self.tar.getmember("ustar/regtype")
Lars Gustäbel7a919e92012-05-05 18:15:03 +020088 with self.tar.extractfile(tarinfo) as fobj:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +000089 data = fobj.read()
Serhiy Storchaka8b562922013-06-17 15:38:50 +030090 self.assertEqual(len(data), tarinfo.size,
91 "regular file extraction failed")
92 self.assertEqual(md5sum(data), md5_regtype,
Antoine Pitroue1eca4e2010-10-29 23:49:49 +000093 "regular file extraction failed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000094
Guido van Rossumd8faa362007-04-27 19:54:29 +000095 def test_fileobj_readlines(self):
96 self.tar.extract("ustar/regtype", TEMPDIR)
97 tarinfo = self.tar.getmember("ustar/regtype")
Antoine Pitrou95f55602010-09-23 18:36:46 +000098 with open(os.path.join(TEMPDIR, "ustar/regtype"), "r") as fobj1:
99 lines1 = fobj1.readlines()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000100
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200101 with self.tar.extractfile(tarinfo) as fobj:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000102 fobj2 = io.TextIOWrapper(fobj)
103 lines2 = fobj2.readlines()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300104 self.assertEqual(lines1, lines2,
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000105 "fileobj.readlines() failed")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300106 self.assertEqual(len(lines2), 114,
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000107 "fileobj.readlines() failed")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300108 self.assertEqual(lines2[83],
109 "I will gladly admit that Python is not the fastest "
110 "running scripting language.\n",
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000111 "fileobj.readlines() failed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000112
Guido van Rossumd8faa362007-04-27 19:54:29 +0000113 def test_fileobj_iter(self):
114 self.tar.extract("ustar/regtype", TEMPDIR)
115 tarinfo = self.tar.getmember("ustar/regtype")
Victor Stinner4e86d5b2011-05-04 13:55:36 +0200116 with open(os.path.join(TEMPDIR, "ustar/regtype"), "r") as fobj1:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000117 lines1 = fobj1.readlines()
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200118 with self.tar.extractfile(tarinfo) as fobj2:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000119 lines2 = list(io.TextIOWrapper(fobj2))
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300120 self.assertEqual(lines1, lines2,
121 "fileobj.__iter__() failed")
Martin v. Löwisdf241532005-03-03 08:17:42 +0000122
Guido van Rossumd8faa362007-04-27 19:54:29 +0000123 def test_fileobj_seek(self):
124 self.tar.extract("ustar/regtype", TEMPDIR)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000125 with open(os.path.join(TEMPDIR, "ustar/regtype"), "rb") as fobj:
126 data = fobj.read()
Neal Norwitzf3396542005-10-28 05:52:22 +0000127
Guido van Rossumd8faa362007-04-27 19:54:29 +0000128 tarinfo = self.tar.getmember("ustar/regtype")
129 fobj = self.tar.extractfile(tarinfo)
130
131 text = fobj.read()
132 fobj.seek(0)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000133 self.assertEqual(0, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000134 "seek() to file's start failed")
135 fobj.seek(2048, 0)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000136 self.assertEqual(2048, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000137 "seek() to absolute position failed")
138 fobj.seek(-1024, 1)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000139 self.assertEqual(1024, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000140 "seek() to negative relative position failed")
141 fobj.seek(1024, 1)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000142 self.assertEqual(2048, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000143 "seek() to positive relative position failed")
144 s = fobj.read(10)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300145 self.assertEqual(s, data[2048:2058],
Guido van Rossumd8faa362007-04-27 19:54:29 +0000146 "read() after seek failed")
147 fobj.seek(0, 2)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000148 self.assertEqual(tarinfo.size, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000149 "seek() to file's end failed")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300150 self.assertEqual(fobj.read(), b"",
Guido van Rossumd8faa362007-04-27 19:54:29 +0000151 "read() at file's end did not return empty string")
152 fobj.seek(-tarinfo.size, 2)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000153 self.assertEqual(0, fobj.tell(),
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000154 "relative seek() to file's end failed")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000155 fobj.seek(512)
156 s1 = fobj.readlines()
157 fobj.seek(512)
158 s2 = fobj.readlines()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300159 self.assertEqual(s1, s2,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000160 "readlines() after seek failed")
161 fobj.seek(0)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000162 self.assertEqual(len(fobj.readline()), fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000163 "tell() after readline() failed")
164 fobj.seek(512)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300165 self.assertEqual(len(fobj.readline()) + 512, fobj.tell(),
Guido van Rossumd8faa362007-04-27 19:54:29 +0000166 "tell() after seek() and readline() failed")
167 fobj.seek(0)
168 line = fobj.readline()
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000169 self.assertEqual(fobj.read(), data[len(line):],
Guido van Rossumd8faa362007-04-27 19:54:29 +0000170 "read() after readline() failed")
171 fobj.close()
172
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200173 def test_fileobj_text(self):
174 with self.tar.extractfile("ustar/regtype") as fobj:
175 fobj = io.TextIOWrapper(fobj)
176 data = fobj.read().encode("iso8859-1")
177 self.assertEqual(md5sum(data), md5_regtype)
178 try:
179 fobj.seek(100)
180 except AttributeError:
181 # Issue #13815: seek() complained about a missing
182 # flush() method.
183 self.fail("seeking failed in text mode")
184
Lars Gustäbel1b512722010-06-03 12:45:16 +0000185 # Test if symbolic and hard links are resolved by extractfile(). The
186 # test link members each point to a regular member whose data is
187 # supposed to be exported.
188 def _test_fileobj_link(self, lnktype, regtype):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300189 with self.tar.extractfile(lnktype) as a, \
190 self.tar.extractfile(regtype) as b:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000191 self.assertEqual(a.name, b.name)
Lars Gustäbel1b512722010-06-03 12:45:16 +0000192
193 def test_fileobj_link1(self):
194 self._test_fileobj_link("ustar/lnktype", "ustar/regtype")
195
196 def test_fileobj_link2(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300197 self._test_fileobj_link("./ustar/linktest2/lnktype",
198 "ustar/linktest1/regtype")
Lars Gustäbel1b512722010-06-03 12:45:16 +0000199
200 def test_fileobj_symlink1(self):
201 self._test_fileobj_link("ustar/symtype", "ustar/regtype")
202
203 def test_fileobj_symlink2(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300204 self._test_fileobj_link("./ustar/linktest2/symtype",
205 "ustar/linktest1/regtype")
Lars Gustäbel1b512722010-06-03 12:45:16 +0000206
Lars Gustäbel1ef9eda2012-04-24 21:04:40 +0200207 def test_issue14160(self):
208 self._test_fileobj_link("symtype2", "ustar/regtype")
209
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300210class GzipUstarReadTest(GzipTest, UstarReadTest):
211 pass
212
213class Bz2UstarReadTest(Bz2Test, UstarReadTest):
214 pass
215
216class LzmaUstarReadTest(LzmaTest, UstarReadTest):
217 pass
218
Guido van Rossumd8faa362007-04-27 19:54:29 +0000219
Serhiy Storchaka3b4f1592014-02-05 20:53:36 +0200220class ListTest(ReadTest, unittest.TestCase):
221
222 # Override setUp to use default encoding (UTF-8)
223 def setUp(self):
224 self.tar = tarfile.open(self.tarname, mode=self.mode)
225
226 def test_list(self):
227 tio = io.TextIOWrapper(io.BytesIO(), 'ascii', newline='\n')
228 with support.swap_attr(sys, 'stdout', tio):
229 self.tar.list(verbose=False)
230 out = tio.detach().getvalue()
231 self.assertIn(b'ustar/conttype', out)
232 self.assertIn(b'ustar/regtype', out)
233 self.assertIn(b'ustar/lnktype', out)
234 self.assertIn(b'ustar' + (b'/12345' * 40) + b'67/longname', out)
235 self.assertIn(b'./ustar/linktest2/symtype', out)
236 self.assertIn(b'./ustar/linktest2/lnktype', out)
237 # Make sure it puts trailing slash for directory
238 self.assertIn(b'ustar/dirtype/', out)
239 self.assertIn(b'ustar/dirtype-with-size/', out)
240 # Make sure it is able to print unencodable characters
Serhiy Storchaka162c4772014-02-19 18:44:12 +0200241 def conv(b):
242 s = b.decode(self.tar.encoding, 'surrogateescape')
243 return s.encode('ascii', 'backslashreplace')
244 self.assertIn(conv(b'ustar/umlauts-\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out)
245 self.assertIn(conv(b'misc/regtype-hpux-signed-chksum-'
246 b'\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out)
247 self.assertIn(conv(b'misc/regtype-old-v7-signed-chksum-'
248 b'\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out)
249 self.assertIn(conv(b'pax/bad-pax-\xe4\xf6\xfc'), out)
250 self.assertIn(conv(b'pax/hdrcharset-\xe4\xf6\xfc'), out)
Serhiy Storchaka3b4f1592014-02-05 20:53:36 +0200251 # Make sure it prints files separated by one newline without any
252 # 'ls -l'-like accessories if verbose flag is not being used
253 # ...
254 # ustar/conttype
255 # ustar/regtype
256 # ...
257 self.assertRegex(out, br'ustar/conttype ?\r?\n'
258 br'ustar/regtype ?\r?\n')
259 # Make sure it does not print the source of link without verbose flag
260 self.assertNotIn(b'link to', out)
261 self.assertNotIn(b'->', out)
262
263 def test_list_verbose(self):
264 tio = io.TextIOWrapper(io.BytesIO(), 'ascii', newline='\n')
265 with support.swap_attr(sys, 'stdout', tio):
266 self.tar.list(verbose=True)
267 out = tio.detach().getvalue()
268 # Make sure it prints files separated by one newline with 'ls -l'-like
269 # accessories if verbose flag is being used
270 # ...
271 # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/conttype
272 # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype
273 # ...
274 self.assertRegex(out, (br'-rw-r--r-- tarfile/tarfile\s+7011 '
275 br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
276 br'ustar/\w+type ?\r?\n') * 2)
277 # Make sure it prints the source of link with verbose flag
278 self.assertIn(b'ustar/symtype -> regtype', out)
279 self.assertIn(b'./ustar/linktest2/symtype -> ../linktest1/regtype', out)
280 self.assertIn(b'./ustar/linktest2/lnktype link to '
281 b'./ustar/linktest1/regtype', out)
282 self.assertIn(b'gnu' + (b'/123' * 125) + b'/longlink link to gnu' +
283 (b'/123' * 125) + b'/longname', out)
284 self.assertIn(b'pax' + (b'/123' * 125) + b'/longlink link to pax' +
285 (b'/123' * 125) + b'/longname', out)
286
287
288class GzipListTest(GzipTest, ListTest):
289 pass
290
291
292class Bz2ListTest(Bz2Test, ListTest):
293 pass
294
295
296class LzmaListTest(LzmaTest, ListTest):
297 pass
298
299
Lars Gustäbel9520a432009-11-22 18:48:49 +0000300class CommonReadTest(ReadTest):
301
302 def test_empty_tarfile(self):
303 # Test for issue6123: Allow opening empty archives.
304 # This test checks if tarfile.open() is able to open an empty tar
305 # archive successfully. Note that an empty tar archive is not the
306 # same as an empty file!
Antoine Pitrou95f55602010-09-23 18:36:46 +0000307 with tarfile.open(tmpname, self.mode.replace("r", "w")):
308 pass
Lars Gustäbel9520a432009-11-22 18:48:49 +0000309 try:
310 tar = tarfile.open(tmpname, self.mode)
311 tar.getnames()
312 except tarfile.ReadError:
313 self.fail("tarfile.open() failed on empty archive")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000314 else:
315 self.assertListEqual(tar.getmembers(), [])
316 finally:
317 tar.close()
Lars Gustäbel9520a432009-11-22 18:48:49 +0000318
Serhiy Storchaka2a3d7d12014-01-13 19:07:33 +0200319 def test_non_existent_tarfile(self):
320 # Test for issue11513: prevent non-existent gzipped tarfiles raising
321 # multiple exceptions.
Serhiy Storchaka2d5a0922014-01-24 22:19:23 +0200322 test = 'xxx'
323 if sys.platform == 'win32' and '|' in self.mode:
324 # Issue #20384: On Windows os.open() error message doesn't
325 # contain file name.
Serhiy Storchakaa7184e62014-01-24 22:28:06 +0200326 test = ''
Serhiy Storchaka2d5a0922014-01-24 22:19:23 +0200327 with self.assertRaisesRegex(FileNotFoundError, test):
Serhiy Storchaka2a3d7d12014-01-13 19:07:33 +0200328 tarfile.open("xxx", self.mode)
329
Lars Gustäbel9520a432009-11-22 18:48:49 +0000330 def test_null_tarfile(self):
331 # Test for issue6123: Allow opening empty archives.
332 # This test guarantees that tarfile.open() does not treat an empty
333 # file as an empty tar archive.
Antoine Pitrou95f55602010-09-23 18:36:46 +0000334 with open(tmpname, "wb"):
335 pass
Lars Gustäbel9520a432009-11-22 18:48:49 +0000336 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, self.mode)
337 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname)
338
339 def test_ignore_zeros(self):
340 # Test TarFile's ignore_zeros option.
Lars Gustäbel9520a432009-11-22 18:48:49 +0000341 for char in (b'\0', b'a'):
342 # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
343 # are ignored correctly.
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300344 with self.open(tmpname, "w") as fobj:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000345 fobj.write(char * 1024)
346 fobj.write(tarfile.TarInfo("foo").tobuf())
Lars Gustäbel9520a432009-11-22 18:48:49 +0000347
348 tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000349 try:
350 self.assertListEqual(tar.getnames(), ["foo"],
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300351 "ignore_zeros=True should have skipped the %r-blocks" %
352 char)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000353 finally:
354 tar.close()
Lars Gustäbel9520a432009-11-22 18:48:49 +0000355
356
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300357class MiscReadTestBase(CommonReadTest):
Thomas Woutersed03b412007-08-28 21:37:11 +0000358 def test_no_name_argument(self):
Antoine Pitrou95f55602010-09-23 18:36:46 +0000359 with open(self.tarname, "rb") as fobj:
360 tar = tarfile.open(fileobj=fobj, mode=self.mode)
361 self.assertEqual(tar.name, os.path.abspath(fobj.name))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000362
Thomas Woutersed03b412007-08-28 21:37:11 +0000363 def test_no_name_attribute(self):
Antoine Pitrou95f55602010-09-23 18:36:46 +0000364 with open(self.tarname, "rb") as fobj:
365 data = fobj.read()
Thomas Woutersed03b412007-08-28 21:37:11 +0000366 fobj = io.BytesIO(data)
367 self.assertRaises(AttributeError, getattr, fobj, "name")
368 tar = tarfile.open(fileobj=fobj, mode=self.mode)
369 self.assertEqual(tar.name, None)
370
371 def test_empty_name_attribute(self):
Antoine Pitrou95f55602010-09-23 18:36:46 +0000372 with open(self.tarname, "rb") as fobj:
373 data = fobj.read()
Thomas Woutersed03b412007-08-28 21:37:11 +0000374 fobj = io.BytesIO(data)
375 fobj.name = ""
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000376 with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
377 self.assertEqual(tar.name, None)
Thomas Woutersed03b412007-08-28 21:37:11 +0000378
Serhiy Storchaka53ad0cd2014-01-18 15:35:37 +0200379 def test_illegal_mode_arg(self):
380 with open(tmpname, 'wb'):
381 pass
382 with self.assertRaisesRegex(ValueError, 'mode must be '):
383 tar = self.taropen(tmpname, 'q')
384 with self.assertRaisesRegex(ValueError, 'mode must be '):
385 tar = self.taropen(tmpname, 'rw')
386 with self.assertRaisesRegex(ValueError, 'mode must be '):
387 tar = self.taropen(tmpname, '')
388
Christian Heimesd8654cf2007-12-02 15:22:16 +0000389 def test_fileobj_with_offset(self):
390 # Skip the first member and store values from the second member
391 # of the testtar.
392 tar = tarfile.open(self.tarname, mode=self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000393 try:
394 tar.next()
395 t = tar.next()
396 name = t.name
397 offset = t.offset
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200398 with tar.extractfile(t) as f:
399 data = f.read()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000400 finally:
401 tar.close()
Christian Heimesd8654cf2007-12-02 15:22:16 +0000402
403 # Open the testtar and seek to the offset of the second member.
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300404 with self.open(self.tarname) as fobj:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000405 fobj.seek(offset)
Christian Heimesd8654cf2007-12-02 15:22:16 +0000406
Antoine Pitrou95f55602010-09-23 18:36:46 +0000407 # Test if the tarfile starts with the second member.
408 tar = tar.open(self.tarname, mode="r:", fileobj=fobj)
409 t = tar.next()
410 self.assertEqual(t.name, name)
411 # Read to the end of fileobj and test if seeking back to the
412 # beginning works.
413 tar.getmembers()
414 self.assertEqual(tar.extractfile(t).read(), data,
415 "seek back did not work")
416 tar.close()
Christian Heimesd8654cf2007-12-02 15:22:16 +0000417
Guido van Rossumd8faa362007-04-27 19:54:29 +0000418 def test_fail_comp(self):
419 # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000420 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000421 with open(tarname, "rb") as fobj:
422 self.assertRaises(tarfile.ReadError, tarfile.open,
423 fileobj=fobj, mode=self.mode)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000424
425 def test_v7_dirtype(self):
426 # Test old style dirtype member (bug #1336623):
427 # Old V7 tars create directory members using an AREGTYPE
428 # header with a "/" appended to the filename field.
429 tarinfo = self.tar.getmember("misc/dirtype-old-v7")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300430 self.assertEqual(tarinfo.type, tarfile.DIRTYPE,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000431 "v7 dirtype failed")
432
Christian Heimes126d29a2008-02-11 22:57:17 +0000433 def test_xstar_type(self):
434 # The xstar format stores extra atime and ctime fields inside the
435 # space reserved for the prefix field. The prefix field must be
436 # ignored in this case, otherwise it will mess up the name.
437 try:
438 self.tar.getmember("misc/regtype-xstar")
439 except KeyError:
440 self.fail("failed to find misc/regtype-xstar (mangled prefix?)")
441
Guido van Rossumd8faa362007-04-27 19:54:29 +0000442 def test_check_members(self):
443 for tarinfo in self.tar:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300444 self.assertEqual(int(tarinfo.mtime), 0o7606136617,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000445 "wrong mtime for %s" % tarinfo.name)
446 if not tarinfo.name.startswith("ustar/"):
447 continue
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300448 self.assertEqual(tarinfo.uname, "tarfile",
Guido van Rossumd8faa362007-04-27 19:54:29 +0000449 "wrong uname for %s" % tarinfo.name)
450
451 def test_find_members(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300452 self.assertEqual(self.tar.getmembers()[-1].name, "misc/eof",
Guido van Rossumd8faa362007-04-27 19:54:29 +0000453 "could not find all members")
454
Brian Curtin74e45612010-07-09 15:58:59 +0000455 @unittest.skipUnless(hasattr(os, "link"),
456 "Missing hardlink implementation")
Brian Curtin3b4499c2010-12-28 14:31:47 +0000457 @support.skip_unless_symlink
Guido van Rossumd8faa362007-04-27 19:54:29 +0000458 def test_extract_hardlink(self):
459 # Test hardlink extraction (e.g. bug #857297).
Serhiy Storchaka88339c42012-12-30 20:16:30 +0200460 with tarfile.open(tarname, errorlevel=1, encoding="iso8859-1") as tar:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000461 tar.extract("ustar/regtype", TEMPDIR)
Serhiy Storchaka88339c42012-12-30 20:16:30 +0200462 self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/regtype"))
Neal Norwitzf3396542005-10-28 05:52:22 +0000463
Serhiy Storchaka88339c42012-12-30 20:16:30 +0200464 tar.extract("ustar/lnktype", TEMPDIR)
465 self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/lnktype"))
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000466 with open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb") as f:
467 data = f.read()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000468 self.assertEqual(md5sum(data), md5_regtype)
Neal Norwitzf3396542005-10-28 05:52:22 +0000469
Serhiy Storchaka88339c42012-12-30 20:16:30 +0200470 tar.extract("ustar/symtype", TEMPDIR)
471 self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/symtype"))
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000472 with open(os.path.join(TEMPDIR, "ustar/symtype"), "rb") as f:
473 data = f.read()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000474 self.assertEqual(md5sum(data), md5_regtype)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000475
Christian Heimesfaf2f632008-01-06 16:59:19 +0000476 def test_extractall(self):
477 # Test if extractall() correctly restores directory permissions
478 # and times (see issue1735).
Christian Heimesfaf2f632008-01-06 16:59:19 +0000479 tar = tarfile.open(tarname, encoding="iso8859-1")
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000480 DIR = os.path.join(TEMPDIR, "extractall")
481 os.mkdir(DIR)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000482 try:
483 directories = [t for t in tar if t.isdir()]
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000484 tar.extractall(DIR, directories)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000485 for tarinfo in directories:
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000486 path = os.path.join(DIR, tarinfo.name)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000487 if sys.platform != "win32":
488 # Win32 has no support for fine grained permissions.
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300489 self.assertEqual(tarinfo.mode & 0o777,
490 os.stat(path).st_mode & 0o777)
Victor Stinner26bfb5a2010-10-29 10:59:08 +0000491 def format_mtime(mtime):
492 if isinstance(mtime, float):
493 return "{} ({})".format(mtime, mtime.hex())
494 else:
495 return "{!r} (int)".format(mtime)
Victor Stinner14d8fe72010-10-29 11:02:06 +0000496 file_mtime = os.path.getmtime(path)
Victor Stinner26bfb5a2010-10-29 10:59:08 +0000497 errmsg = "tar mtime {0} != file time {1} of path {2!a}".format(
498 format_mtime(tarinfo.mtime),
499 format_mtime(file_mtime),
500 path)
501 self.assertEqual(tarinfo.mtime, file_mtime, errmsg)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000502 finally:
503 tar.close()
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000504 shutil.rmtree(DIR)
Christian Heimesfaf2f632008-01-06 16:59:19 +0000505
Martin v. Löwis16f344d2010-11-01 21:39:13 +0000506 def test_extract_directory(self):
507 dirtype = "ustar/dirtype"
Martin v. Löwisbe647e22010-11-01 22:08:46 +0000508 DIR = os.path.join(TEMPDIR, "extractdir")
509 os.mkdir(DIR)
510 try:
511 with tarfile.open(tarname, encoding="iso8859-1") as tar:
512 tarinfo = tar.getmember(dirtype)
513 tar.extract(tarinfo, path=DIR)
514 extracted = os.path.join(DIR, dirtype)
515 self.assertEqual(os.path.getmtime(extracted), tarinfo.mtime)
516 if sys.platform != "win32":
517 self.assertEqual(os.stat(extracted).st_mode & 0o777, 0o755)
518 finally:
519 shutil.rmtree(DIR)
Martin v. Löwis16f344d2010-11-01 21:39:13 +0000520
Lars Gustäbelb7f09232009-11-23 15:48:33 +0000521 def test_init_close_fobj(self):
522 # Issue #7341: Close the internal file object in the TarFile
523 # constructor in case of an error. For the test we rely on
524 # the fact that opening an empty file raises a ReadError.
525 empty = os.path.join(TEMPDIR, "empty")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000526 with open(empty, "wb") as fobj:
527 fobj.write(b"")
Lars Gustäbelb7f09232009-11-23 15:48:33 +0000528
529 try:
530 tar = object.__new__(tarfile.TarFile)
531 try:
532 tar.__init__(empty)
533 except tarfile.ReadError:
534 self.assertTrue(tar.fileobj.closed)
535 else:
536 self.fail("ReadError not raised")
537 finally:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000538 support.unlink(empty)
Lars Gustäbelb7f09232009-11-23 15:48:33 +0000539
Serhiy Storchaka263fab92013-05-09 14:22:26 +0300540 def test_parallel_iteration(self):
541 # Issue #16601: Restarting iteration over tarfile continued
542 # from where it left off.
543 with tarfile.open(self.tarname) as tar:
544 for m1, m2 in zip(tar, tar):
545 self.assertEqual(m1.offset, m2.offset)
546 self.assertEqual(m1.get_info(), m2.get_info())
547
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300548class MiscReadTest(MiscReadTestBase, unittest.TestCase):
549 test_fail_comp = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000550
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300551class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase):
Serhiy Storchaka2a3d7d12014-01-13 19:07:33 +0200552 pass
Guido van Rossumd8faa362007-04-27 19:54:29 +0000553
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300554class Bz2MiscReadTest(Bz2Test, MiscReadTestBase, unittest.TestCase):
555 def test_no_name_argument(self):
556 self.skipTest("BZ2File have no name attribute")
557
558class LzmaMiscReadTest(LzmaTest, MiscReadTestBase, unittest.TestCase):
559 def test_no_name_argument(self):
560 self.skipTest("LZMAFile have no name attribute")
561
562
563class StreamReadTest(CommonReadTest, unittest.TestCase):
564
565 prefix="r|"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000566
Lars Gustäbeldd071042011-02-23 11:42:22 +0000567 def test_read_through(self):
568 # Issue #11224: A poorly designed _FileInFile.read() method
569 # caused seeking errors with stream tar files.
570 for tarinfo in self.tar:
571 if not tarinfo.isreg():
572 continue
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200573 with self.tar.extractfile(tarinfo) as fobj:
574 while True:
575 try:
576 buf = fobj.read(512)
577 except tarfile.StreamError:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300578 self.fail("simple read-through using "
579 "TarFile.extractfile() failed")
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200580 if not buf:
581 break
Lars Gustäbeldd071042011-02-23 11:42:22 +0000582
Guido van Rossumd8faa362007-04-27 19:54:29 +0000583 def test_fileobj_regular_file(self):
584 tarinfo = self.tar.next() # get "regtype" (can't use getmember)
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200585 with self.tar.extractfile(tarinfo) as fobj:
586 data = fobj.read()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300587 self.assertEqual(len(data), tarinfo.size,
588 "regular file extraction failed")
589 self.assertEqual(md5sum(data), md5_regtype,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000590 "regular file extraction failed")
591
592 def test_provoke_stream_error(self):
593 tarinfos = self.tar.getmembers()
Lars Gustäbel7a919e92012-05-05 18:15:03 +0200594 with self.tar.extractfile(tarinfos[0]) as f: # read the first member
595 self.assertRaises(tarfile.StreamError, f.read)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000596
Guido van Rossumd8faa362007-04-27 19:54:29 +0000597 def test_compare_members(self):
598 tar1 = tarfile.open(tarname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000599 try:
600 tar2 = self.tar
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000601
Antoine Pitrou95f55602010-09-23 18:36:46 +0000602 while True:
603 t1 = tar1.next()
604 t2 = tar2.next()
605 if t1 is None:
606 break
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300607 self.assertIsNotNone(t2, "stream.next() failed.")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000608
Antoine Pitrou95f55602010-09-23 18:36:46 +0000609 if t2.islnk() or t2.issym():
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300610 with self.assertRaises(tarfile.StreamError):
611 tar2.extractfile(t2)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000612 continue
Guido van Rossumd8faa362007-04-27 19:54:29 +0000613
Antoine Pitrou95f55602010-09-23 18:36:46 +0000614 v1 = tar1.extractfile(t1)
615 v2 = tar2.extractfile(t2)
616 if v1 is None:
617 continue
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300618 self.assertIsNotNone(v2, "stream.extractfile() failed")
619 self.assertEqual(v1.read(), v2.read(),
620 "stream extraction failed")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000621 finally:
622 tar1.close()
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000623
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300624class GzipStreamReadTest(GzipTest, StreamReadTest):
625 pass
Thomas Wouters89f507f2006-12-13 04:49:30 +0000626
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300627class Bz2StreamReadTest(Bz2Test, StreamReadTest):
628 pass
Thomas Wouterscf297e42007-02-23 15:07:44 +0000629
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300630class LzmaStreamReadTest(LzmaTest, StreamReadTest):
631 pass
632
633
634class DetectReadTest(TarTest, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000635 def _testfunc_file(self, name, mode):
636 try:
Antoine Pitrou95f55602010-09-23 18:36:46 +0000637 tar = tarfile.open(name, mode)
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000638 except tarfile.ReadError as e:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000639 self.fail()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000640 else:
641 tar.close()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000642
Guido van Rossumd8faa362007-04-27 19:54:29 +0000643 def _testfunc_fileobj(self, name, mode):
644 try:
Antoine Pitrou605c2932010-09-23 20:15:14 +0000645 with open(name, "rb") as f:
646 tar = tarfile.open(name, mode, fileobj=f)
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000647 except tarfile.ReadError as e:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000648 self.fail()
Antoine Pitrou95f55602010-09-23 18:36:46 +0000649 else:
650 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000651
652 def _test_modes(self, testfunc):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300653 if self.suffix:
654 with self.assertRaises(tarfile.ReadError):
655 tarfile.open(tarname, mode="r:" + self.suffix)
656 with self.assertRaises(tarfile.ReadError):
657 tarfile.open(tarname, mode="r|" + self.suffix)
658 with self.assertRaises(tarfile.ReadError):
659 tarfile.open(self.tarname, mode="r:")
660 with self.assertRaises(tarfile.ReadError):
661 tarfile.open(self.tarname, mode="r|")
662 testfunc(self.tarname, "r")
663 testfunc(self.tarname, "r:" + self.suffix)
664 testfunc(self.tarname, "r:*")
665 testfunc(self.tarname, "r|" + self.suffix)
666 testfunc(self.tarname, "r|*")
Lars Gustäbel0a9dd2f2011-12-10 20:38:14 +0100667
Guido van Rossumd8faa362007-04-27 19:54:29 +0000668 def test_detect_file(self):
669 self._test_modes(self._testfunc_file)
670
671 def test_detect_fileobj(self):
672 self._test_modes(self._testfunc_fileobj)
673
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300674class GzipDetectReadTest(GzipTest, DetectReadTest):
675 pass
676
677class Bz2DetectReadTest(Bz2Test, DetectReadTest):
Lars Gustäbeled1ac582011-12-06 12:56:38 +0100678 def test_detect_stream_bz2(self):
679 # Originally, tarfile's stream detection looked for the string
680 # "BZh91" at the start of the file. This is incorrect because
681 # the '9' represents the blocksize (900kB). If the file was
682 # compressed using another blocksize autodetection fails.
Lars Gustäbeled1ac582011-12-06 12:56:38 +0100683 with open(tarname, "rb") as fobj:
684 data = fobj.read()
685
686 # Compress with blocksize 100kB, the file starts with "BZh11".
687 with bz2.BZ2File(tmpname, "wb", compresslevel=1) as fobj:
688 fobj.write(data)
689
690 self._testfunc_file(tmpname, "r|*")
691
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300692class LzmaDetectReadTest(LzmaTest, DetectReadTest):
693 pass
Guido van Rossumd8faa362007-04-27 19:54:29 +0000694
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300695
696class MemberReadTest(ReadTest, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000697
698 def _test_member(self, tarinfo, chksum=None, **kwargs):
699 if chksum is not None:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300700 with self.tar.extractfile(tarinfo) as f:
701 self.assertEqual(md5sum(f.read()), chksum,
702 "wrong md5sum for %s" % tarinfo.name)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000703
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000704 kwargs["mtime"] = 0o7606136617
Guido van Rossumd8faa362007-04-27 19:54:29 +0000705 kwargs["uid"] = 1000
706 kwargs["gid"] = 100
707 if "old-v7" not in tarinfo.name:
708 # V7 tar can't handle alphabetic owners.
709 kwargs["uname"] = "tarfile"
710 kwargs["gname"] = "tarfile"
711 for k, v in kwargs.items():
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300712 self.assertEqual(getattr(tarinfo, k), v,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000713 "wrong value in %s field of %s" % (k, tarinfo.name))
714
715 def test_find_regtype(self):
716 tarinfo = self.tar.getmember("ustar/regtype")
717 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
718
719 def test_find_conttype(self):
720 tarinfo = self.tar.getmember("ustar/conttype")
721 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
722
723 def test_find_dirtype(self):
724 tarinfo = self.tar.getmember("ustar/dirtype")
725 self._test_member(tarinfo, size=0)
726
727 def test_find_dirtype_with_size(self):
728 tarinfo = self.tar.getmember("ustar/dirtype-with-size")
729 self._test_member(tarinfo, size=255)
730
731 def test_find_lnktype(self):
732 tarinfo = self.tar.getmember("ustar/lnktype")
733 self._test_member(tarinfo, size=0, linkname="ustar/regtype")
734
735 def test_find_symtype(self):
736 tarinfo = self.tar.getmember("ustar/symtype")
737 self._test_member(tarinfo, size=0, linkname="regtype")
738
739 def test_find_blktype(self):
740 tarinfo = self.tar.getmember("ustar/blktype")
741 self._test_member(tarinfo, size=0, devmajor=3, devminor=0)
742
743 def test_find_chrtype(self):
744 tarinfo = self.tar.getmember("ustar/chrtype")
745 self._test_member(tarinfo, size=0, devmajor=1, devminor=3)
746
747 def test_find_fifotype(self):
748 tarinfo = self.tar.getmember("ustar/fifotype")
749 self._test_member(tarinfo, size=0)
750
751 def test_find_sparse(self):
752 tarinfo = self.tar.getmember("ustar/sparse")
753 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
754
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000755 def test_find_gnusparse(self):
756 tarinfo = self.tar.getmember("gnu/sparse")
757 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
758
759 def test_find_gnusparse_00(self):
760 tarinfo = self.tar.getmember("gnu/sparse-0.0")
761 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
762
763 def test_find_gnusparse_01(self):
764 tarinfo = self.tar.getmember("gnu/sparse-0.1")
765 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
766
767 def test_find_gnusparse_10(self):
768 tarinfo = self.tar.getmember("gnu/sparse-1.0")
769 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
770
Guido van Rossumd8faa362007-04-27 19:54:29 +0000771 def test_find_umlauts(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300772 tarinfo = self.tar.getmember("ustar/umlauts-"
773 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000774 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
775
776 def test_find_ustar_longname(self):
777 name = "ustar/" + "12345/" * 39 + "1234567/longname"
Benjamin Peterson577473f2010-01-19 00:09:57 +0000778 self.assertIn(name, self.tar.getnames())
Guido van Rossumd8faa362007-04-27 19:54:29 +0000779
780 def test_find_regtype_oldv7(self):
781 tarinfo = self.tar.getmember("misc/regtype-old-v7")
782 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
783
784 def test_find_pax_umlauts(self):
Antoine Pitrouab58b5f2010-09-23 19:39:35 +0000785 self.tar.close()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300786 self.tar = tarfile.open(self.tarname, mode=self.mode,
787 encoding="iso8859-1")
788 tarinfo = self.tar.getmember("pax/umlauts-"
789 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000790 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
791
792
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300793class LongnameTest:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000794
795 def test_read_longname(self):
796 # Test reading of longname (bug #1471427).
Guido van Rossume7ba4952007-06-06 23:52:48 +0000797 longname = self.subdir + "/" + "123/" * 125 + "longname"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000798 try:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000799 tarinfo = self.tar.getmember(longname)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000800 except KeyError:
801 self.fail("longname not found")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300802 self.assertNotEqual(tarinfo.type, tarfile.DIRTYPE,
803 "read longname as dirtype")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000804
805 def test_read_longlink(self):
806 longname = self.subdir + "/" + "123/" * 125 + "longname"
807 longlink = self.subdir + "/" + "123/" * 125 + "longlink"
808 try:
809 tarinfo = self.tar.getmember(longlink)
810 except KeyError:
811 self.fail("longlink not found")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300812 self.assertEqual(tarinfo.linkname, longname, "linkname wrong")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000813
814 def test_truncated_longname(self):
815 longname = self.subdir + "/" + "123/" * 125 + "longname"
816 tarinfo = self.tar.getmember(longname)
817 offset = tarinfo.offset
818 self.tar.fileobj.seek(offset)
Lars Gustäbelb506dc32007-08-07 18:36:16 +0000819 fobj = io.BytesIO(self.tar.fileobj.read(3 * 512))
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300820 with self.assertRaises(tarfile.ReadError):
821 tarfile.open(name="foo.tar", fileobj=fobj)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000822
Guido van Rossume7ba4952007-06-06 23:52:48 +0000823 def test_header_offset(self):
824 # Test if the start offset of the TarInfo object includes
825 # the preceding extended header.
826 longname = self.subdir + "/" + "123/" * 125 + "longname"
827 offset = self.tar.getmember(longname).offset
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000828 with open(tarname, "rb") as fobj:
829 fobj.seek(offset)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300830 tarinfo = tarfile.TarInfo.frombuf(fobj.read(512),
831 "iso8859-1", "strict")
Antoine Pitroue1eca4e2010-10-29 23:49:49 +0000832 self.assertEqual(tarinfo.type, self.longnametype)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000833
Guido van Rossumd8faa362007-04-27 19:54:29 +0000834
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300835class GNUReadTest(LongnameTest, ReadTest, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000836
837 subdir = "gnu"
Guido van Rossume7ba4952007-06-06 23:52:48 +0000838 longnametype = tarfile.GNUTYPE_LONGNAME
Guido van Rossumd8faa362007-04-27 19:54:29 +0000839
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000840 # Since 3.2 tarfile is supposed to accurately restore sparse members and
841 # produce files with holes. This is what we actually want to test here.
842 # Unfortunately, not all platforms/filesystems support sparse files, and
843 # even on platforms that do it is non-trivial to make reliable assertions
844 # about holes in files. Therefore, we first do one basic test which works
845 # an all platforms, and after that a test that will work only on
846 # platforms/filesystems that prove to support sparse files.
847 def _test_sparse_file(self, name):
848 self.tar.extract(name, TEMPDIR)
849 filename = os.path.join(TEMPDIR, name)
850 with open(filename, "rb") as fobj:
851 data = fobj.read()
852 self.assertEqual(md5sum(data), md5_sparse,
853 "wrong md5sum for %s" % name)
854
855 if self._fs_supports_holes():
856 s = os.stat(filename)
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300857 self.assertLess(s.st_blocks * 512, s.st_size)
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000858
859 def test_sparse_file_old(self):
860 self._test_sparse_file("gnu/sparse")
861
862 def test_sparse_file_00(self):
863 self._test_sparse_file("gnu/sparse-0.0")
864
865 def test_sparse_file_01(self):
866 self._test_sparse_file("gnu/sparse-0.1")
867
868 def test_sparse_file_10(self):
869 self._test_sparse_file("gnu/sparse-1.0")
870
871 @staticmethod
872 def _fs_supports_holes():
873 # Return True if the platform knows the st_blocks stat attribute and
874 # uses st_blocks units of 512 bytes, and if the filesystem is able to
875 # store holes in files.
Victor Stinner9c3de4a2011-08-17 20:49:41 +0200876 if sys.platform.startswith("linux"):
Lars Gustäbel9cbdd752010-10-29 09:08:19 +0000877 # Linux evidentially has 512 byte st_blocks units.
878 name = os.path.join(TEMPDIR, "sparse-test")
879 with open(name, "wb") as fobj:
880 fobj.seek(4096)
881 fobj.truncate()
882 s = os.stat(name)
883 os.remove(name)
884 return s.st_blocks == 0
885 else:
886 return False
Guido van Rossumd8faa362007-04-27 19:54:29 +0000887
888
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300889class PaxReadTest(LongnameTest, ReadTest, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000890
891 subdir = "pax"
Guido van Rossume7ba4952007-06-06 23:52:48 +0000892 longnametype = tarfile.XHDTYPE
Guido van Rossumd8faa362007-04-27 19:54:29 +0000893
Guido van Rossume7ba4952007-06-06 23:52:48 +0000894 def test_pax_global_headers(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000895 tar = tarfile.open(tarname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000896 try:
897 tarinfo = tar.getmember("pax/regtype1")
898 self.assertEqual(tarinfo.uname, "foo")
899 self.assertEqual(tarinfo.gname, "bar")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300900 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"),
901 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Guido van Rossume7ba4952007-06-06 23:52:48 +0000902
Antoine Pitrou95f55602010-09-23 18:36:46 +0000903 tarinfo = tar.getmember("pax/regtype2")
904 self.assertEqual(tarinfo.uname, "")
905 self.assertEqual(tarinfo.gname, "bar")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300906 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"),
907 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000908
Antoine Pitrou95f55602010-09-23 18:36:46 +0000909 tarinfo = tar.getmember("pax/regtype3")
910 self.assertEqual(tarinfo.uname, "tarfile")
911 self.assertEqual(tarinfo.gname, "tarfile")
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300912 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"),
913 "\xc4\xd6\xdc\xe4\xf6\xfc\xdf")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000914 finally:
915 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +0000916
917 def test_pax_number_fields(self):
918 # All following number fields are read from the pax header.
919 tar = tarfile.open(tarname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +0000920 try:
921 tarinfo = tar.getmember("pax/regtype4")
922 self.assertEqual(tarinfo.size, 7011)
923 self.assertEqual(tarinfo.uid, 123)
924 self.assertEqual(tarinfo.gid, 123)
925 self.assertEqual(tarinfo.mtime, 1041808783.0)
926 self.assertEqual(type(tarinfo.mtime), float)
927 self.assertEqual(float(tarinfo.pax_headers["atime"]), 1041808783.0)
928 self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0)
929 finally:
930 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000931
932
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300933class WriteTestBase(TarTest):
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000934 # Put all write tests in here that are supposed to be tested
935 # in all possible mode combinations.
936
937 def test_fileobj_no_close(self):
938 fobj = io.BytesIO()
939 tar = tarfile.open(fileobj=fobj, mode=self.mode)
940 tar.addfile(tarfile.TarInfo("foo"))
941 tar.close()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300942 self.assertFalse(fobj.closed, "external fileobjs must never closed")
Serhiy Storchaka9fbec7a2014-01-18 15:53:05 +0200943 # Issue #20238: Incomplete gzip output with mode="w:gz"
944 data = fobj.getvalue()
945 del tar
946 support.gc_collect()
947 self.assertFalse(fobj.closed)
948 self.assertEqual(data, fobj.getvalue())
Georg Brandlf08a9dd2008-06-10 16:57:31 +0000949
950
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300951class WriteTest(WriteTestBase, unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000952
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300953 prefix = "w:"
Guido van Rossumd8faa362007-04-27 19:54:29 +0000954
955 def test_100_char_name(self):
956 # The name field in a tar header stores strings of at most 100 chars.
957 # If a string is shorter than 100 chars it has to be padded with '\0',
958 # which implies that a string of exactly 100 chars is stored without
959 # a trailing '\0'.
960 name = "0123456789" * 10
961 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000962 try:
963 t = tarfile.TarInfo(name)
964 tar.addfile(t)
965 finally:
966 tar.close()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000967
Guido van Rossumd8faa362007-04-27 19:54:29 +0000968 tar = tarfile.open(tmpname)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000969 try:
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300970 self.assertEqual(tar.getnames()[0], name,
Antoine Pitrou95f55602010-09-23 18:36:46 +0000971 "failed to store 100 char filename")
972 finally:
973 tar.close()
Thomas Wouters89f507f2006-12-13 04:49:30 +0000974
Guido van Rossumd8faa362007-04-27 19:54:29 +0000975 def test_tar_size(self):
976 # Test for bug #1013882.
977 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000978 try:
979 path = os.path.join(TEMPDIR, "file")
980 with open(path, "wb") as fobj:
981 fobj.write(b"aaa")
982 tar.add(path)
983 finally:
984 tar.close()
Serhiy Storchaka8b562922013-06-17 15:38:50 +0300985 self.assertGreater(os.path.getsize(tmpname), 0,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000986 "tarfile is empty")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000987
Guido van Rossumd8faa362007-04-27 19:54:29 +0000988 # The test_*_size tests test for bug #1167128.
989 def test_file_size(self):
990 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +0000991 try:
992 path = os.path.join(TEMPDIR, "file")
993 with open(path, "wb"):
994 pass
995 tarinfo = tar.gettarinfo(path)
996 self.assertEqual(tarinfo.size, 0)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000997
Antoine Pitrou95f55602010-09-23 18:36:46 +0000998 with open(path, "wb") as fobj:
999 fobj.write(b"aaa")
1000 tarinfo = tar.gettarinfo(path)
1001 self.assertEqual(tarinfo.size, 3)
1002 finally:
1003 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001004
1005 def test_directory_size(self):
1006 path = os.path.join(TEMPDIR, "directory")
Martin v. Löwis5dbdc592005-08-27 10:07:56 +00001007 os.mkdir(path)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001008 try:
1009 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001010 try:
1011 tarinfo = tar.gettarinfo(path)
1012 self.assertEqual(tarinfo.size, 0)
1013 finally:
1014 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001015 finally:
1016 os.rmdir(path)
1017
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001018 @unittest.skipUnless(hasattr(os, "link"),
1019 "Missing hardlink implementation")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001020 def test_link_size(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001021 link = os.path.join(TEMPDIR, "link")
1022 target = os.path.join(TEMPDIR, "link_target")
1023 with open(target, "wb") as fobj:
1024 fobj.write(b"aaa")
1025 os.link(target, link)
1026 try:
1027 tar = tarfile.open(tmpname, self.mode)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001028 try:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001029 # Record the link target in the inodes list.
1030 tar.gettarinfo(target)
1031 tarinfo = tar.gettarinfo(link)
1032 self.assertEqual(tarinfo.size, 0)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001033 finally:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001034 tar.close()
1035 finally:
1036 os.remove(target)
1037 os.remove(link)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001038
Brian Curtin3b4499c2010-12-28 14:31:47 +00001039 @support.skip_unless_symlink
Guido van Rossumd8faa362007-04-27 19:54:29 +00001040 def test_symlink_size(self):
Brian Curtind40e6f72010-07-08 21:39:08 +00001041 path = os.path.join(TEMPDIR, "symlink")
1042 os.symlink("link_target", path)
1043 try:
1044 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001045 try:
1046 tarinfo = tar.gettarinfo(path)
1047 self.assertEqual(tarinfo.size, 0)
1048 finally:
1049 tar.close()
Brian Curtind40e6f72010-07-08 21:39:08 +00001050 finally:
1051 os.remove(path)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001052
1053 def test_add_self(self):
1054 # Test for #1257255.
1055 dstname = os.path.abspath(tmpname)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001056 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001057 try:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001058 self.assertEqual(tar.name, dstname,
1059 "archive name must be absolute")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001060 tar.add(dstname)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001061 self.assertEqual(tar.getnames(), [],
1062 "added the archive to itself")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001063
Antoine Pitrou95f55602010-09-23 18:36:46 +00001064 cwd = os.getcwd()
1065 os.chdir(TEMPDIR)
1066 tar.add(dstname)
1067 os.chdir(cwd)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001068 self.assertEqual(tar.getnames(), [],
1069 "added the archive to itself")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001070 finally:
1071 tar.close()
Martin v. Löwis5dbdc592005-08-27 10:07:56 +00001072
Guido van Rossum486364b2007-06-30 05:01:58 +00001073 def test_exclude(self):
1074 tempdir = os.path.join(TEMPDIR, "exclude")
1075 os.mkdir(tempdir)
1076 try:
1077 for name in ("foo", "bar", "baz"):
1078 name = os.path.join(tempdir, name)
Victor Stinnerbf816222011-06-30 23:25:47 +02001079 support.create_empty_file(name)
Guido van Rossum486364b2007-06-30 05:01:58 +00001080
Benjamin Peterson886af962010-03-21 23:13:07 +00001081 exclude = os.path.isfile
Guido van Rossum486364b2007-06-30 05:01:58 +00001082
1083 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001084 try:
1085 with support.check_warnings(("use the filter argument",
1086 DeprecationWarning)):
1087 tar.add(tempdir, arcname="empty_dir", exclude=exclude)
1088 finally:
1089 tar.close()
Guido van Rossum486364b2007-06-30 05:01:58 +00001090
1091 tar = tarfile.open(tmpname, "r")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001092 try:
1093 self.assertEqual(len(tar.getmembers()), 1)
1094 self.assertEqual(tar.getnames()[0], "empty_dir")
1095 finally:
1096 tar.close()
Guido van Rossum486364b2007-06-30 05:01:58 +00001097 finally:
1098 shutil.rmtree(tempdir)
1099
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001100 def test_filter(self):
1101 tempdir = os.path.join(TEMPDIR, "filter")
1102 os.mkdir(tempdir)
1103 try:
1104 for name in ("foo", "bar", "baz"):
1105 name = os.path.join(tempdir, name)
Victor Stinnerbf816222011-06-30 23:25:47 +02001106 support.create_empty_file(name)
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001107
1108 def filter(tarinfo):
1109 if os.path.basename(tarinfo.name) == "bar":
1110 return
1111 tarinfo.uid = 123
1112 tarinfo.uname = "foo"
1113 return tarinfo
1114
1115 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001116 try:
1117 tar.add(tempdir, arcname="empty_dir", filter=filter)
1118 finally:
1119 tar.close()
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001120
Raymond Hettingera63a3122011-01-26 20:34:14 +00001121 # Verify that filter is a keyword-only argument
1122 with self.assertRaises(TypeError):
1123 tar.add(tempdir, "empty_dir", True, None, filter)
1124
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001125 tar = tarfile.open(tmpname, "r")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001126 try:
1127 for tarinfo in tar:
1128 self.assertEqual(tarinfo.uid, 123)
1129 self.assertEqual(tarinfo.uname, "foo")
1130 self.assertEqual(len(tar.getmembers()), 3)
1131 finally:
1132 tar.close()
Lars Gustäbel049d2aa2009-09-12 10:44:00 +00001133 finally:
1134 shutil.rmtree(tempdir)
1135
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001136 # Guarantee that stored pathnames are not modified. Don't
1137 # remove ./ or ../ or double slashes. Still make absolute
1138 # pathnames relative.
1139 # For details see bug #6054.
1140 def _test_pathname(self, path, cmp_path=None, dir=False):
1141 # Create a tarfile with an empty member named path
1142 # and compare the stored name with the original.
1143 foo = os.path.join(TEMPDIR, "foo")
1144 if not dir:
Victor Stinnerbf816222011-06-30 23:25:47 +02001145 support.create_empty_file(foo)
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001146 else:
1147 os.mkdir(foo)
1148
1149 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001150 try:
1151 tar.add(foo, arcname=path)
1152 finally:
1153 tar.close()
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001154
1155 tar = tarfile.open(tmpname, "r")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001156 try:
1157 t = tar.next()
1158 finally:
1159 tar.close()
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001160
1161 if not dir:
1162 os.remove(foo)
1163 else:
1164 os.rmdir(foo)
1165
1166 self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
1167
Senthil Kumaranbe5dbeb2011-04-30 06:09:51 +08001168
1169 @support.skip_unless_symlink
Senthil Kumaran123932f2011-04-28 15:38:12 +08001170 def test_extractall_symlinks(self):
1171 # Test if extractall works properly when tarfile contains symlinks
1172 tempdir = os.path.join(TEMPDIR, "testsymlinks")
1173 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
1174 os.mkdir(tempdir)
1175 try:
1176 source_file = os.path.join(tempdir,'source')
1177 target_file = os.path.join(tempdir,'symlink')
1178 with open(source_file,'w') as f:
1179 f.write('something\n')
1180 os.symlink(source_file, target_file)
1181 tar = tarfile.open(temparchive,'w')
1182 tar.add(source_file)
1183 tar.add(target_file)
1184 tar.close()
1185 # Let's extract it to the location which contains the symlink
1186 tar = tarfile.open(temparchive,'r')
1187 # this should not raise OSError: [Errno 17] File exists
1188 try:
1189 tar.extractall(path=tempdir)
1190 except OSError:
1191 self.fail("extractall failed with symlinked files")
1192 finally:
1193 tar.close()
1194 finally:
1195 os.unlink(temparchive)
1196 shutil.rmtree(tempdir)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +00001197
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001198 def test_pathnames(self):
1199 self._test_pathname("foo")
1200 self._test_pathname(os.path.join("foo", ".", "bar"))
1201 self._test_pathname(os.path.join("foo", "..", "bar"))
1202 self._test_pathname(os.path.join(".", "foo"))
1203 self._test_pathname(os.path.join(".", "foo", "."))
1204 self._test_pathname(os.path.join(".", "foo", ".", "bar"))
1205 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
1206 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
1207 self._test_pathname(os.path.join("..", "foo"))
1208 self._test_pathname(os.path.join("..", "foo", ".."))
1209 self._test_pathname(os.path.join("..", "foo", ".", "bar"))
1210 self._test_pathname(os.path.join("..", "foo", "..", "bar"))
1211
1212 self._test_pathname("foo" + os.sep + os.sep + "bar")
1213 self._test_pathname("foo" + os.sep + os.sep, "foo", dir=True)
1214
1215 def test_abs_pathnames(self):
1216 if sys.platform == "win32":
1217 self._test_pathname("C:\\foo", "foo")
1218 else:
1219 self._test_pathname("/foo", "foo")
1220 self._test_pathname("///foo", "foo")
1221
1222 def test_cwd(self):
1223 # Test adding the current working directory.
1224 cwd = os.getcwd()
1225 os.chdir(TEMPDIR)
1226 try:
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001227 tar = tarfile.open(tmpname, self.mode)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001228 try:
1229 tar.add(".")
1230 finally:
1231 tar.close()
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001232
1233 tar = tarfile.open(tmpname, "r")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001234 try:
1235 for t in tar:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001236 if t.name != ".":
1237 self.assertTrue(t.name.startswith("./"), t.name)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001238 finally:
1239 tar.close()
Lars Gustäbelbfdfdda2009-08-28 19:59:59 +00001240 finally:
1241 os.chdir(cwd)
1242
Serhiy Storchakac2d01422014-01-18 16:14:10 +02001243 def test_open_nonwritable_fileobj(self):
1244 for exctype in OSError, EOFError, RuntimeError:
1245 class BadFile(io.BytesIO):
1246 first = True
1247 def write(self, data):
1248 if self.first:
1249 self.first = False
1250 raise exctype
1251
1252 f = BadFile()
1253 with self.assertRaises(exctype):
1254 tar = tarfile.open(tmpname, self.mode, fileobj=f,
1255 format=tarfile.PAX_FORMAT,
1256 pax_headers={'non': 'empty'})
1257 self.assertFalse(f.closed)
1258
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001259class GzipWriteTest(GzipTest, WriteTest):
1260 pass
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001261
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001262class Bz2WriteTest(Bz2Test, WriteTest):
1263 pass
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001264
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001265class LzmaWriteTest(LzmaTest, WriteTest):
1266 pass
1267
1268
1269class StreamWriteTest(WriteTestBase, unittest.TestCase):
1270
1271 prefix = "w|"
1272 decompressor = None
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001273
Guido van Rossumd8faa362007-04-27 19:54:29 +00001274 def test_stream_padding(self):
1275 # Test for bug #1543303.
1276 tar = tarfile.open(tmpname, self.mode)
1277 tar.close()
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001278 if self.decompressor:
1279 dec = self.decompressor()
Antoine Pitrou95f55602010-09-23 18:36:46 +00001280 with open(tmpname, "rb") as fobj:
1281 data = fobj.read()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001282 data = dec.decompress(data)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001283 self.assertFalse(dec.unused_data, "found trailing data")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001284 else:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001285 with self.open(tmpname) as fobj:
Antoine Pitrou95f55602010-09-23 18:36:46 +00001286 data = fobj.read()
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001287 self.assertEqual(data.count(b"\0"), tarfile.RECORDSIZE,
1288 "incorrect zero padding")
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001289
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001290 @unittest.skipUnless(sys.platform != "win32" and hasattr(os, "umask"),
1291 "Missing umask implementation")
Lars Gustäbeld6eb70b2010-04-29 15:37:02 +00001292 def test_file_mode(self):
1293 # Test for issue #8464: Create files with correct
1294 # permissions.
Lars Gustäbeld6eb70b2010-04-29 15:37:02 +00001295 if os.path.exists(tmpname):
1296 os.remove(tmpname)
1297
1298 original_umask = os.umask(0o022)
1299 try:
1300 tar = tarfile.open(tmpname, self.mode)
1301 tar.close()
1302 mode = os.stat(tmpname).st_mode & 0o777
1303 self.assertEqual(mode, 0o644, "wrong file permissions")
1304 finally:
1305 os.umask(original_umask)
1306
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001307class GzipStreamWriteTest(GzipTest, StreamWriteTest):
1308 pass
1309
1310class Bz2StreamWriteTest(Bz2Test, StreamWriteTest):
1311 decompressor = bz2.BZ2Decompressor if bz2 else None
1312
1313class LzmaStreamWriteTest(LzmaTest, StreamWriteTest):
1314 decompressor = lzma.LZMADecompressor if lzma else None
1315
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001316
Guido van Rossumd8faa362007-04-27 19:54:29 +00001317class GNUWriteTest(unittest.TestCase):
1318 # This testcase checks for correct creation of GNU Longname
1319 # and Longlink extended headers (cp. bug #812325).
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001320
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001321 def _length(self, s):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001322 blocks = len(s) // 512 + 1
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001323 return blocks * 512
1324
1325 def _calc_size(self, name, link=None):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001326 # Initial tar header
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001327 count = 512
1328
1329 if len(name) > tarfile.LENGTH_NAME:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001330 # GNU longname extended header + longname
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001331 count += 512
1332 count += self._length(name)
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001333 if link is not None and len(link) > tarfile.LENGTH_LINK:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001334 # GNU longlink extended header + longlink
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001335 count += 512
1336 count += self._length(link)
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001337 return count
1338
1339 def _test(self, name, link=None):
1340 tarinfo = tarfile.TarInfo(name)
1341 if link:
1342 tarinfo.linkname = link
1343 tarinfo.type = tarfile.LNKTYPE
1344
Guido van Rossumd8faa362007-04-27 19:54:29 +00001345 tar = tarfile.open(tmpname, "w")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001346 try:
1347 tar.format = tarfile.GNU_FORMAT
1348 tar.addfile(tarinfo)
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001349
Antoine Pitrou95f55602010-09-23 18:36:46 +00001350 v1 = self._calc_size(name, link)
1351 v2 = tar.offset
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001352 self.assertEqual(v1, v2, "GNU longname/longlink creation failed")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001353 finally:
1354 tar.close()
Thomas Wouters89f507f2006-12-13 04:49:30 +00001355
Guido van Rossumd8faa362007-04-27 19:54:29 +00001356 tar = tarfile.open(tmpname)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001357 try:
1358 member = tar.next()
1359 self.assertIsNotNone(member,
1360 "unable to read longname member")
1361 self.assertEqual(tarinfo.name, member.name,
1362 "unable to read longname member")
1363 self.assertEqual(tarinfo.linkname, member.linkname,
1364 "unable to read longname member")
1365 finally:
1366 tar.close()
Thomas Wouters89f507f2006-12-13 04:49:30 +00001367
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001368 def test_longname_1023(self):
1369 self._test(("longnam/" * 127) + "longnam")
1370
1371 def test_longname_1024(self):
1372 self._test(("longnam/" * 127) + "longname")
1373
1374 def test_longname_1025(self):
1375 self._test(("longnam/" * 127) + "longname_")
1376
1377 def test_longlink_1023(self):
1378 self._test("name", ("longlnk/" * 127) + "longlnk")
1379
1380 def test_longlink_1024(self):
1381 self._test("name", ("longlnk/" * 127) + "longlink")
1382
1383 def test_longlink_1025(self):
1384 self._test("name", ("longlnk/" * 127) + "longlink_")
1385
1386 def test_longnamelink_1023(self):
1387 self._test(("longnam/" * 127) + "longnam",
1388 ("longlnk/" * 127) + "longlnk")
1389
1390 def test_longnamelink_1024(self):
1391 self._test(("longnam/" * 127) + "longname",
1392 ("longlnk/" * 127) + "longlink")
1393
1394 def test_longnamelink_1025(self):
1395 self._test(("longnam/" * 127) + "longname_",
1396 ("longlnk/" * 127) + "longlink_")
1397
Guido van Rossumd8faa362007-04-27 19:54:29 +00001398
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001399@unittest.skipUnless(hasattr(os, "link"), "Missing hardlink implementation")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001400class HardlinkTest(unittest.TestCase):
1401 # Test the creation of LNKTYPE (hardlink) members in an archive.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001402
1403 def setUp(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001404 self.foo = os.path.join(TEMPDIR, "foo")
1405 self.bar = os.path.join(TEMPDIR, "bar")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001406
Antoine Pitrou95f55602010-09-23 18:36:46 +00001407 with open(self.foo, "wb") as fobj:
1408 fobj.write(b"foo")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001409
Guido van Rossumd8faa362007-04-27 19:54:29 +00001410 os.link(self.foo, self.bar)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001411
Guido van Rossumd8faa362007-04-27 19:54:29 +00001412 self.tar = tarfile.open(tmpname, "w")
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001413 self.tar.add(self.foo)
1414
Guido van Rossumd8faa362007-04-27 19:54:29 +00001415 def tearDown(self):
Hirokazu Yamamotoaf079d42008-09-21 11:50:03 +00001416 self.tar.close()
Antoine Pitrou95f55602010-09-23 18:36:46 +00001417 support.unlink(self.foo)
1418 support.unlink(self.bar)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001419
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001420 def test_add_twice(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001421 # The same name will be added as a REGTYPE every
1422 # time regardless of st_nlink.
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001423 tarinfo = self.tar.gettarinfo(self.foo)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001424 self.assertEqual(tarinfo.type, tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001425 "add file as regular failed")
1426
1427 def test_add_hardlink(self):
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001428 tarinfo = self.tar.gettarinfo(self.bar)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001429 self.assertEqual(tarinfo.type, tarfile.LNKTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001430 "add file as hardlink failed")
1431
1432 def test_dereference_hardlink(self):
1433 self.tar.dereference = True
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001434 tarinfo = self.tar.gettarinfo(self.bar)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001435 self.assertEqual(tarinfo.type, tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001436 "dereferencing hardlink failed")
1437
Neal Norwitza4f651a2004-07-20 22:07:44 +00001438
Guido van Rossumd8faa362007-04-27 19:54:29 +00001439class PaxWriteTest(GNUWriteTest):
Martin v. Löwis78be7df2005-03-05 12:47:42 +00001440
Guido van Rossumd8faa362007-04-27 19:54:29 +00001441 def _test(self, name, link=None):
1442 # See GNUWriteTest.
1443 tarinfo = tarfile.TarInfo(name)
1444 if link:
1445 tarinfo.linkname = link
1446 tarinfo.type = tarfile.LNKTYPE
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +00001447
Guido van Rossumd8faa362007-04-27 19:54:29 +00001448 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001449 try:
1450 tar.addfile(tarinfo)
1451 finally:
1452 tar.close()
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +00001453
Guido van Rossumd8faa362007-04-27 19:54:29 +00001454 tar = tarfile.open(tmpname)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001455 try:
1456 if link:
1457 l = tar.getmembers()[0].linkname
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001458 self.assertEqual(link, l, "PAX longlink creation failed")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001459 else:
1460 n = tar.getmembers()[0].name
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001461 self.assertEqual(name, n, "PAX longname creation failed")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001462 finally:
1463 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001464
Guido van Rossume7ba4952007-06-06 23:52:48 +00001465 def test_pax_global_header(self):
1466 pax_headers = {
Guido van Rossum9cbfffd2007-06-07 00:54:15 +00001467 "foo": "bar",
1468 "uid": "0",
1469 "mtime": "1.23",
Guido van Rossuma0557702007-08-07 23:19:53 +00001470 "test": "\xe4\xf6\xfc",
1471 "\xe4\xf6\xfc": "test"}
Guido van Rossume7ba4952007-06-06 23:52:48 +00001472
Benjamin Peterson886af962010-03-21 23:13:07 +00001473 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT,
Guido van Rossume7ba4952007-06-06 23:52:48 +00001474 pax_headers=pax_headers)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001475 try:
1476 tar.addfile(tarfile.TarInfo("test"))
1477 finally:
1478 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001479
1480 # Test if the global header was written correctly.
1481 tar = tarfile.open(tmpname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001482 try:
1483 self.assertEqual(tar.pax_headers, pax_headers)
1484 self.assertEqual(tar.getmembers()[0].pax_headers, pax_headers)
1485 # Test if all the fields are strings.
1486 for key, val in tar.pax_headers.items():
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001487 self.assertIsNot(type(key), bytes)
1488 self.assertIsNot(type(val), bytes)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001489 if key in tarfile.PAX_NUMBER_FIELDS:
1490 try:
1491 tarfile.PAX_NUMBER_FIELDS[key](val)
1492 except (TypeError, ValueError):
1493 self.fail("unable to convert pax header field")
1494 finally:
1495 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001496
1497 def test_pax_extended_header(self):
1498 # The fields from the pax header have priority over the
1499 # TarInfo.
Guido van Rossum9cbfffd2007-06-07 00:54:15 +00001500 pax_headers = {"path": "foo", "uid": "123"}
Guido van Rossume7ba4952007-06-06 23:52:48 +00001501
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001502 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT,
1503 encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001504 try:
1505 t = tarfile.TarInfo()
1506 t.name = "\xe4\xf6\xfc" # non-ASCII
1507 t.uid = 8**8 # too large
1508 t.pax_headers = pax_headers
1509 tar.addfile(t)
1510 finally:
1511 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001512
1513 tar = tarfile.open(tmpname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001514 try:
1515 t = tar.getmembers()[0]
1516 self.assertEqual(t.pax_headers, pax_headers)
1517 self.assertEqual(t.name, "foo")
1518 self.assertEqual(t.uid, 123)
1519 finally:
1520 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001521
1522
1523class UstarUnicodeTest(unittest.TestCase):
Guido van Rossume7ba4952007-06-06 23:52:48 +00001524
1525 format = tarfile.USTAR_FORMAT
1526
1527 def test_iso8859_1_filename(self):
1528 self._test_unicode_filename("iso8859-1")
1529
1530 def test_utf7_filename(self):
1531 self._test_unicode_filename("utf7")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001532
1533 def test_utf8_filename(self):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +00001534 self._test_unicode_filename("utf-8")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001535
Guido van Rossumd8faa362007-04-27 19:54:29 +00001536 def _test_unicode_filename(self, encoding):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001537 tar = tarfile.open(tmpname, "w", format=self.format,
1538 encoding=encoding, errors="strict")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001539 try:
1540 name = "\xe4\xf6\xfc"
1541 tar.addfile(tarfile.TarInfo(name))
1542 finally:
1543 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001544
1545 tar = tarfile.open(tmpname, encoding=encoding)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001546 try:
1547 self.assertEqual(tar.getmembers()[0].name, name)
1548 finally:
1549 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001550
1551 def test_unicode_filename_error(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001552 tar = tarfile.open(tmpname, "w", format=self.format,
1553 encoding="ascii", errors="strict")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001554 try:
1555 tarinfo = tarfile.TarInfo()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001556
Antoine Pitrou95f55602010-09-23 18:36:46 +00001557 tarinfo.name = "\xe4\xf6\xfc"
1558 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001559
Antoine Pitrou95f55602010-09-23 18:36:46 +00001560 tarinfo.name = "foo"
1561 tarinfo.uname = "\xe4\xf6\xfc"
1562 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
1563 finally:
1564 tar.close()
Guido van Rossume7ba4952007-06-06 23:52:48 +00001565
1566 def test_unicode_argument(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001567 tar = tarfile.open(tarname, "r",
1568 encoding="iso8859-1", errors="strict")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001569 try:
1570 for t in tar:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001571 self.assertIs(type(t.name), str)
1572 self.assertIs(type(t.linkname), str)
1573 self.assertIs(type(t.uname), str)
1574 self.assertIs(type(t.gname), str)
Antoine Pitrou95f55602010-09-23 18:36:46 +00001575 finally:
1576 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001577
Guido van Rossume7ba4952007-06-06 23:52:48 +00001578 def test_uname_unicode(self):
Lars Gustäbel3741eff2007-08-21 12:17:05 +00001579 t = tarfile.TarInfo("foo")
1580 t.uname = "\xe4\xf6\xfc"
1581 t.gname = "\xe4\xf6\xfc"
Guido van Rossumd8faa362007-04-27 19:54:29 +00001582
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001583 tar = tarfile.open(tmpname, mode="w", format=self.format,
1584 encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001585 try:
1586 tar.addfile(t)
1587 finally:
1588 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001589
Lars Gustäbel3741eff2007-08-21 12:17:05 +00001590 tar = tarfile.open(tmpname, encoding="iso8859-1")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001591 try:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001592 t = tar.getmember("foo")
Antoine Pitrou95f55602010-09-23 18:36:46 +00001593 self.assertEqual(t.uname, "\xe4\xf6\xfc")
1594 self.assertEqual(t.gname, "\xe4\xf6\xfc")
1595
1596 if self.format != tarfile.PAX_FORMAT:
Antoine Pitrouab58b5f2010-09-23 19:39:35 +00001597 tar.close()
Antoine Pitrou95f55602010-09-23 18:36:46 +00001598 tar = tarfile.open(tmpname, encoding="ascii")
1599 t = tar.getmember("foo")
1600 self.assertEqual(t.uname, "\udce4\udcf6\udcfc")
1601 self.assertEqual(t.gname, "\udce4\udcf6\udcfc")
1602 finally:
1603 tar.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001604
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001605
Guido van Rossume7ba4952007-06-06 23:52:48 +00001606class GNUUnicodeTest(UstarUnicodeTest):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001607
Guido van Rossume7ba4952007-06-06 23:52:48 +00001608 format = tarfile.GNU_FORMAT
Guido van Rossumd8faa362007-04-27 19:54:29 +00001609
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001610 def test_bad_pax_header(self):
1611 # Test for issue #8633. GNU tar <= 1.23 creates raw binary fields
1612 # without a hdrcharset=BINARY header.
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001613 for encoding, name in (
1614 ("utf-8", "pax/bad-pax-\udce4\udcf6\udcfc"),
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001615 ("iso8859-1", "pax/bad-pax-\xe4\xf6\xfc"),):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001616 with tarfile.open(tarname, encoding=encoding,
1617 errors="surrogateescape") as tar:
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001618 try:
1619 t = tar.getmember(name)
1620 except KeyError:
1621 self.fail("unable to read bad GNU tar pax header")
1622
Guido van Rossumd8faa362007-04-27 19:54:29 +00001623
Lars Gustäbel3741eff2007-08-21 12:17:05 +00001624class PAXUnicodeTest(UstarUnicodeTest):
1625
1626 format = tarfile.PAX_FORMAT
1627
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001628 # PAX_FORMAT ignores encoding in write mode.
1629 test_unicode_filename_error = None
1630
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001631 def test_binary_header(self):
1632 # Test a POSIX.1-2008 compatible header with a hdrcharset=BINARY field.
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001633 for encoding, name in (
1634 ("utf-8", "pax/hdrcharset-\udce4\udcf6\udcfc"),
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001635 ("iso8859-1", "pax/hdrcharset-\xe4\xf6\xfc"),):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001636 with tarfile.open(tarname, encoding=encoding,
1637 errors="surrogateescape") as tar:
Lars Gustäbel1465cc22010-05-17 18:02:50 +00001638 try:
1639 t = tar.getmember(name)
1640 except KeyError:
1641 self.fail("unable to read POSIX.1-2008 binary header")
1642
Lars Gustäbel3741eff2007-08-21 12:17:05 +00001643
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001644class AppendTestBase:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001645 # Test append mode (cp. patch #1652681).
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001646
Guido van Rossumd8faa362007-04-27 19:54:29 +00001647 def setUp(self):
1648 self.tarname = tmpname
1649 if os.path.exists(self.tarname):
1650 os.remove(self.tarname)
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001651
Guido van Rossumd8faa362007-04-27 19:54:29 +00001652 def _create_testtar(self, mode="w:"):
Antoine Pitrou95f55602010-09-23 18:36:46 +00001653 with tarfile.open(tarname, encoding="iso8859-1") as src:
1654 t = src.getmember("ustar/regtype")
1655 t.name = "foo"
Lars Gustäbel7a919e92012-05-05 18:15:03 +02001656 with src.extractfile(t) as f:
Antoine Pitroue1eca4e2010-10-29 23:49:49 +00001657 with tarfile.open(self.tarname, mode) as tar:
1658 tar.addfile(t, f)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001659
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001660 def test_append_compressed(self):
1661 self._create_testtar("w:" + self.suffix)
1662 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
1663
1664class AppendTest(AppendTestBase, unittest.TestCase):
1665 test_append_compressed = None
1666
1667 def _add_testfile(self, fileobj=None):
1668 with tarfile.open(self.tarname, "a", fileobj=fileobj) as tar:
1669 tar.addfile(tarfile.TarInfo("bar"))
1670
Guido van Rossumd8faa362007-04-27 19:54:29 +00001671 def _test(self, names=["bar"], fileobj=None):
Antoine Pitrou95f55602010-09-23 18:36:46 +00001672 with tarfile.open(self.tarname, fileobj=fileobj) as tar:
1673 self.assertEqual(tar.getnames(), names)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001674
1675 def test_non_existing(self):
1676 self._add_testfile()
1677 self._test()
1678
1679 def test_empty(self):
Lars Gustäbel9520a432009-11-22 18:48:49 +00001680 tarfile.open(self.tarname, "w:").close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001681 self._add_testfile()
1682 self._test()
1683
1684 def test_empty_fileobj(self):
Lars Gustäbel9520a432009-11-22 18:48:49 +00001685 fobj = io.BytesIO(b"\0" * 1024)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001686 self._add_testfile(fobj)
1687 fobj.seek(0)
1688 self._test(fileobj=fobj)
1689
1690 def test_fileobj(self):
1691 self._create_testtar()
Antoine Pitrou95f55602010-09-23 18:36:46 +00001692 with open(self.tarname, "rb") as fobj:
1693 data = fobj.read()
Guido van Rossum34d19282007-08-09 01:03:29 +00001694 fobj = io.BytesIO(data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001695 self._add_testfile(fobj)
1696 fobj.seek(0)
1697 self._test(names=["foo", "bar"], fileobj=fobj)
1698
1699 def test_existing(self):
1700 self._create_testtar()
1701 self._add_testfile()
1702 self._test(names=["foo", "bar"])
1703
Lars Gustäbel9520a432009-11-22 18:48:49 +00001704 # Append mode is supposed to fail if the tarfile to append to
1705 # does not end with a zero block.
1706 def _test_error(self, data):
Antoine Pitrou95f55602010-09-23 18:36:46 +00001707 with open(self.tarname, "wb") as fobj:
1708 fobj.write(data)
Lars Gustäbel9520a432009-11-22 18:48:49 +00001709 self.assertRaises(tarfile.ReadError, self._add_testfile)
1710
1711 def test_null(self):
1712 self._test_error(b"")
1713
1714 def test_incomplete(self):
1715 self._test_error(b"\0" * 13)
1716
1717 def test_premature_eof(self):
1718 data = tarfile.TarInfo("foo").tobuf()
1719 self._test_error(data)
1720
1721 def test_trailing_garbage(self):
1722 data = tarfile.TarInfo("foo").tobuf()
1723 self._test_error(data + b"\0" * 13)
1724
1725 def test_invalid(self):
1726 self._test_error(b"a" * 512)
1727
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001728class GzipAppendTest(GzipTest, AppendTestBase, unittest.TestCase):
1729 pass
1730
1731class Bz2AppendTest(Bz2Test, AppendTestBase, unittest.TestCase):
1732 pass
1733
1734class LzmaAppendTest(LzmaTest, AppendTestBase, unittest.TestCase):
1735 pass
1736
Guido van Rossumd8faa362007-04-27 19:54:29 +00001737
1738class LimitsTest(unittest.TestCase):
1739
1740 def test_ustar_limits(self):
1741 # 100 char name
1742 tarinfo = tarfile.TarInfo("0123456789" * 10)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001743 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001744
1745 # 101 char name that cannot be stored
1746 tarinfo = tarfile.TarInfo("0123456789" * 10 + "0")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001747 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001748
1749 # 256 char name with a slash at pos 156
1750 tarinfo = tarfile.TarInfo("123/" * 62 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001751 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001752
1753 # 256 char name that cannot be stored
1754 tarinfo = tarfile.TarInfo("1234567/" * 31 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001755 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001756
1757 # 512 char name
1758 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001759 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001760
1761 # 512 char linkname
1762 tarinfo = tarfile.TarInfo("longlink")
1763 tarinfo.linkname = "123/" * 126 + "longname"
Guido van Rossume7ba4952007-06-06 23:52:48 +00001764 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001765
1766 # uid > 8 digits
1767 tarinfo = tarfile.TarInfo("name")
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001768 tarinfo.uid = 0o10000000
Guido van Rossume7ba4952007-06-06 23:52:48 +00001769 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001770
1771 def test_gnu_limits(self):
1772 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001773 tarinfo.tobuf(tarfile.GNU_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001774
1775 tarinfo = tarfile.TarInfo("longlink")
1776 tarinfo.linkname = "123/" * 126 + "longname"
Guido van Rossume7ba4952007-06-06 23:52:48 +00001777 tarinfo.tobuf(tarfile.GNU_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001778
1779 # uid >= 256 ** 7
1780 tarinfo = tarfile.TarInfo("name")
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001781 tarinfo.uid = 0o4000000000000000000
Guido van Rossume7ba4952007-06-06 23:52:48 +00001782 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.GNU_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001783
1784 def test_pax_limits(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001785 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Guido van Rossume7ba4952007-06-06 23:52:48 +00001786 tarinfo.tobuf(tarfile.PAX_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001787
1788 tarinfo = tarfile.TarInfo("longlink")
1789 tarinfo.linkname = "123/" * 126 + "longname"
Guido van Rossume7ba4952007-06-06 23:52:48 +00001790 tarinfo.tobuf(tarfile.PAX_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001791
1792 tarinfo = tarfile.TarInfo("name")
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001793 tarinfo.uid = 0o4000000000000000000
Guido van Rossume7ba4952007-06-06 23:52:48 +00001794 tarinfo.tobuf(tarfile.PAX_FORMAT)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001795
1796
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001797class MiscTest(unittest.TestCase):
1798
1799 def test_char_fields(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001800 self.assertEqual(tarfile.stn("foo", 8, "ascii", "strict"),
1801 b"foo\0\0\0\0\0")
1802 self.assertEqual(tarfile.stn("foobar", 3, "ascii", "strict"),
1803 b"foo")
1804 self.assertEqual(tarfile.nts(b"foo\0\0\0\0\0", "ascii", "strict"),
1805 "foo")
1806 self.assertEqual(tarfile.nts(b"foo\0bar\0", "ascii", "strict"),
1807 "foo")
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001808
Lars Gustäbelac3d1372011-10-14 12:46:40 +02001809 def test_read_number_fields(self):
1810 # Issue 13158: Test if GNU tar specific base-256 number fields
1811 # are decoded correctly.
1812 self.assertEqual(tarfile.nti(b"0000001\x00"), 1)
1813 self.assertEqual(tarfile.nti(b"7777777\x00"), 0o7777777)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001814 self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\x00\x20\x00\x00"),
1815 0o10000000)
1816 self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\xff\xff\xff\xff"),
1817 0xffffffff)
1818 self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\xff"),
1819 -1)
1820 self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\x9c"),
1821 -100)
1822 self.assertEqual(tarfile.nti(b"\xff\x00\x00\x00\x00\x00\x00\x00"),
1823 -0x100000000000000)
Lars Gustäbelac3d1372011-10-14 12:46:40 +02001824
1825 def test_write_number_fields(self):
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001826 self.assertEqual(tarfile.itn(1), b"0000001\x00")
Lars Gustäbelac3d1372011-10-14 12:46:40 +02001827 self.assertEqual(tarfile.itn(0o7777777), b"7777777\x00")
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001828 self.assertEqual(tarfile.itn(0o10000000),
1829 b"\x80\x00\x00\x00\x00\x20\x00\x00")
1830 self.assertEqual(tarfile.itn(0xffffffff),
1831 b"\x80\x00\x00\x00\xff\xff\xff\xff")
1832 self.assertEqual(tarfile.itn(-1),
1833 b"\xff\xff\xff\xff\xff\xff\xff\xff")
1834 self.assertEqual(tarfile.itn(-100),
1835 b"\xff\xff\xff\xff\xff\xff\xff\x9c")
1836 self.assertEqual(tarfile.itn(-0x100000000000000),
1837 b"\xff\x00\x00\x00\x00\x00\x00\x00")
Lars Gustäbelac3d1372011-10-14 12:46:40 +02001838
1839 def test_number_field_limits(self):
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001840 with self.assertRaises(ValueError):
1841 tarfile.itn(-1, 8, tarfile.USTAR_FORMAT)
1842 with self.assertRaises(ValueError):
1843 tarfile.itn(0o10000000, 8, tarfile.USTAR_FORMAT)
1844 with self.assertRaises(ValueError):
1845 tarfile.itn(-0x10000000001, 6, tarfile.GNU_FORMAT)
1846 with self.assertRaises(ValueError):
1847 tarfile.itn(0x10000000000, 6, tarfile.GNU_FORMAT)
Lars Gustäbelb506dc32007-08-07 18:36:16 +00001848
1849
Lars Gustäbel01385812010-03-03 12:08:54 +00001850class ContextManagerTest(unittest.TestCase):
1851
1852 def test_basic(self):
1853 with tarfile.open(tarname) as tar:
1854 self.assertFalse(tar.closed, "closed inside runtime context")
1855 self.assertTrue(tar.closed, "context manager failed")
1856
1857 def test_closed(self):
1858 # The __enter__() method is supposed to raise IOError
1859 # if the TarFile object is already closed.
1860 tar = tarfile.open(tarname)
1861 tar.close()
1862 with self.assertRaises(IOError):
1863 with tar:
1864 pass
1865
1866 def test_exception(self):
1867 # Test if the IOError exception is passed through properly.
1868 with self.assertRaises(Exception) as exc:
1869 with tarfile.open(tarname) as tar:
1870 raise IOError
1871 self.assertIsInstance(exc.exception, IOError,
1872 "wrong exception raised in context manager")
1873 self.assertTrue(tar.closed, "context manager failed")
1874
1875 def test_no_eof(self):
1876 # __exit__() must not write end-of-archive blocks if an
1877 # exception was raised.
1878 try:
1879 with tarfile.open(tmpname, "w") as tar:
1880 raise Exception
1881 except:
1882 pass
1883 self.assertEqual(os.path.getsize(tmpname), 0,
1884 "context manager wrote an end-of-archive block")
1885 self.assertTrue(tar.closed, "context manager failed")
1886
1887 def test_eof(self):
1888 # __exit__() must write end-of-archive blocks, i.e. call
1889 # TarFile.close() if there was no error.
1890 with tarfile.open(tmpname, "w"):
1891 pass
1892 self.assertNotEqual(os.path.getsize(tmpname), 0,
1893 "context manager wrote no end-of-archive block")
1894
1895 def test_fileobj(self):
1896 # Test that __exit__() did not close the external file
1897 # object.
Antoine Pitrou95f55602010-09-23 18:36:46 +00001898 with open(tmpname, "wb") as fobj:
1899 try:
1900 with tarfile.open(fileobj=fobj, mode="w") as tar:
1901 raise Exception
1902 except:
1903 pass
1904 self.assertFalse(fobj.closed, "external file object was closed")
1905 self.assertTrue(tar.closed, "context manager failed")
Lars Gustäbel01385812010-03-03 12:08:54 +00001906
1907
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001908@unittest.skipIf(hasattr(os, "link"), "requires os.link to be missing")
1909class LinkEmulationTest(ReadTest, unittest.TestCase):
Lars Gustäbel1b512722010-06-03 12:45:16 +00001910
1911 # Test for issue #8741 regression. On platforms that do not support
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001912 # symbolic or hard links tarfile tries to extract these types of members
1913 # as the regular files they point to.
Lars Gustäbel1b512722010-06-03 12:45:16 +00001914 def _test_link_extraction(self, name):
1915 self.tar.extract(name, TEMPDIR)
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001916 with open(os.path.join(TEMPDIR, name), "rb") as f:
1917 data = f.read()
Lars Gustäbel1b512722010-06-03 12:45:16 +00001918 self.assertEqual(md5sum(data), md5_regtype)
1919
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001920 # See issues #1578269, #8879, and #17689 for some history on these skips
Brian Curtind40e6f72010-07-08 21:39:08 +00001921 @unittest.skipIf(hasattr(os.path, "islink"),
1922 "Skip emulation - has os.path.islink but not os.link")
Lars Gustäbel1b512722010-06-03 12:45:16 +00001923 def test_hardlink_extraction1(self):
1924 self._test_link_extraction("ustar/lnktype")
1925
Brian Curtind40e6f72010-07-08 21:39:08 +00001926 @unittest.skipIf(hasattr(os.path, "islink"),
1927 "Skip emulation - has os.path.islink but not os.link")
Lars Gustäbel1b512722010-06-03 12:45:16 +00001928 def test_hardlink_extraction2(self):
1929 self._test_link_extraction("./ustar/linktest2/lnktype")
1930
Brian Curtin74e45612010-07-09 15:58:59 +00001931 @unittest.skipIf(hasattr(os, "symlink"),
1932 "Skip emulation if symlink exists")
Lars Gustäbel1b512722010-06-03 12:45:16 +00001933 def test_symlink_extraction1(self):
1934 self._test_link_extraction("ustar/symtype")
1935
Brian Curtin74e45612010-07-09 15:58:59 +00001936 @unittest.skipIf(hasattr(os, "symlink"),
1937 "Skip emulation if symlink exists")
Lars Gustäbel1b512722010-06-03 12:45:16 +00001938 def test_symlink_extraction2(self):
1939 self._test_link_extraction("./ustar/linktest2/symtype")
1940
1941
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001942class Bz2PartialReadTest(Bz2Test, unittest.TestCase):
Lars Gustäbel42e00912009-03-22 20:34:29 +00001943 # Issue5068: The _BZ2Proxy.read() method loops forever
1944 # on an empty or partial bzipped file.
1945
1946 def _test_partial_input(self, mode):
1947 class MyBytesIO(io.BytesIO):
1948 hit_eof = False
1949 def read(self, n):
1950 if self.hit_eof:
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001951 raise AssertionError("infinite loop detected in "
1952 "tarfile.open()")
Lars Gustäbel42e00912009-03-22 20:34:29 +00001953 self.hit_eof = self.tell() == len(self.getvalue())
1954 return super(MyBytesIO, self).read(n)
Lars Gustäbel9520a432009-11-22 18:48:49 +00001955 def seek(self, *args):
1956 self.hit_eof = False
1957 return super(MyBytesIO, self).seek(*args)
Lars Gustäbel42e00912009-03-22 20:34:29 +00001958
1959 data = bz2.compress(tarfile.TarInfo("foo").tobuf())
1960 for x in range(len(data) + 1):
Lars Gustäbel9520a432009-11-22 18:48:49 +00001961 try:
1962 tarfile.open(fileobj=MyBytesIO(data[:x]), mode=mode)
1963 except tarfile.ReadError:
1964 pass # we have no interest in ReadErrors
Lars Gustäbel42e00912009-03-22 20:34:29 +00001965
1966 def test_partial_input(self):
1967 self._test_partial_input("r")
1968
1969 def test_partial_input_bz2(self):
1970 self._test_partial_input("r:bz2")
1971
1972
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001973def setUpModule():
Antoine Pitrou95f55602010-09-23 18:36:46 +00001974 support.unlink(TEMPDIR)
Antoine Pitrou941ee882009-11-11 20:59:38 +00001975 os.makedirs(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001976
Antoine Pitrou95f55602010-09-23 18:36:46 +00001977 with open(tarname, "rb") as fobj:
1978 data = fobj.read()
Neal Norwitza4f651a2004-07-20 22:07:44 +00001979
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001980 # Create compressed tarfiles.
1981 for c in GzipTest, Bz2Test, LzmaTest:
1982 if c.open:
1983 support.unlink(c.tarname)
1984 with c.open(c.tarname, "wb") as tar:
1985 tar.write(data)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001986
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001987def tearDownModule():
1988 if os.path.exists(TEMPDIR):
1989 shutil.rmtree(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001990
Neal Norwitz996acf12003-02-17 14:51:41 +00001991if __name__ == "__main__":
Serhiy Storchaka8b562922013-06-17 15:38:50 +03001992 unittest.main()