blob: ff3265fc0ee324e21102090b71b86288a6c6fef0 [file] [log] [blame]
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001# -*- coding: iso-8859-15 -*-
Lars Gustäbelc64e4022007-03-13 10:47:19 +00002
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00003import sys
4import os
5import shutil
Georg Brandl38c6a222006-05-10 16:26:03 +00006import StringIO
Brett Cannon7eec2172007-05-30 22:24:28 +00007from hashlib import md5
Lars Gustäbelc64e4022007-03-13 10:47:19 +00008import errno
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00009
10import unittest
11import tarfile
12
13from test import test_support
14
15# Check for our compression modules.
16try:
17 import gzip
Neal Norwitzae323192003-04-14 01:18:32 +000018 gzip.GzipFile
19except (ImportError, AttributeError):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000020 gzip = None
21try:
22 import bz2
23except ImportError:
24 bz2 = None
25
Lars Gustäbelc64e4022007-03-13 10:47:19 +000026def md5sum(data):
Brett Cannon7eec2172007-05-30 22:24:28 +000027 return md5(data).hexdigest()
Lars Gustäbelc64e4022007-03-13 10:47:19 +000028
Antoine Pitrou310c9fe2009-11-11 20:55:07 +000029TEMPDIR = os.path.abspath(test_support.TESTFN)
30tarname = test_support.findfile("testtar.tar")
Lars Gustäbelc64e4022007-03-13 10:47:19 +000031gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
32bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
33tmpname = os.path.join(TEMPDIR, "tmp.tar")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000034
Lars Gustäbelc64e4022007-03-13 10:47:19 +000035md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
36md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000037
38
Lars Gustäbelc64e4022007-03-13 10:47:19 +000039class ReadTest(unittest.TestCase):
40
41 tarname = tarname
42 mode = "r:"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000043
44 def setUp(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +000045 self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000046
47 def tearDown(self):
48 self.tar.close()
49
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000050
Lars Gustäbelc64e4022007-03-13 10:47:19 +000051class UstarReadTest(ReadTest):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000052
Lars Gustäbelc64e4022007-03-13 10:47:19 +000053 def test_fileobj_regular_file(self):
54 tarinfo = self.tar.getmember("ustar/regtype")
55 fobj = self.tar.extractfile(tarinfo)
56 data = fobj.read()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000057 self.assertTrue((len(data), md5sum(data)) == (tarinfo.size, md5_regtype),
Lars Gustäbelc64e4022007-03-13 10:47:19 +000058 "regular file extraction failed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000059
Lars Gustäbelc64e4022007-03-13 10:47:19 +000060 def test_fileobj_readlines(self):
61 self.tar.extract("ustar/regtype", TEMPDIR)
62 tarinfo = self.tar.getmember("ustar/regtype")
63 fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "rU")
64 fobj2 = self.tar.extractfile(tarinfo)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000065
Lars Gustäbelc64e4022007-03-13 10:47:19 +000066 lines1 = fobj1.readlines()
67 lines2 = fobj2.readlines()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000068 self.assertTrue(lines1 == lines2,
Lars Gustäbelc64e4022007-03-13 10:47:19 +000069 "fileobj.readlines() failed")
Benjamin Peterson5c8da862009-06-30 22:57:08 +000070 self.assertTrue(len(lines2) == 114,
Lars Gustäbelc64e4022007-03-13 10:47:19 +000071 "fileobj.readlines() failed")
Florent Xiclunafc5f6a72010-03-20 22:26:42 +000072 self.assertTrue(lines2[83] ==
Lars Gustäbelc64e4022007-03-13 10:47:19 +000073 "I will gladly admit that Python is not the fastest running scripting language.\n",
74 "fileobj.readlines() failed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +000075
Lars Gustäbelc64e4022007-03-13 10:47:19 +000076 def test_fileobj_iter(self):
77 self.tar.extract("ustar/regtype", TEMPDIR)
78 tarinfo = self.tar.getmember("ustar/regtype")
79 fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "rU")
80 fobj2 = self.tar.extractfile(tarinfo)
81 lines1 = fobj1.readlines()
82 lines2 = [line for line in fobj2]
Benjamin Peterson5c8da862009-06-30 22:57:08 +000083 self.assertTrue(lines1 == lines2,
Lars Gustäbelc64e4022007-03-13 10:47:19 +000084 "fileobj.__iter__() failed")
Martin v. Löwisdf241532005-03-03 08:17:42 +000085
Lars Gustäbelc64e4022007-03-13 10:47:19 +000086 def test_fileobj_seek(self):
87 self.tar.extract("ustar/regtype", TEMPDIR)
88 fobj = open(os.path.join(TEMPDIR, "ustar/regtype"), "rb")
89 data = fobj.read()
Neal Norwitzf3396542005-10-28 05:52:22 +000090 fobj.close()
91
Lars Gustäbelc64e4022007-03-13 10:47:19 +000092 tarinfo = self.tar.getmember("ustar/regtype")
93 fobj = self.tar.extractfile(tarinfo)
94
95 text = fobj.read()
96 fobj.seek(0)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000097 self.assertTrue(0 == fobj.tell(),
Lars Gustäbelc64e4022007-03-13 10:47:19 +000098 "seek() to file's start failed")
99 fobj.seek(2048, 0)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000100 self.assertTrue(2048 == fobj.tell(),
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000101 "seek() to absolute position failed")
102 fobj.seek(-1024, 1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000103 self.assertTrue(1024 == fobj.tell(),
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000104 "seek() to negative relative position failed")
105 fobj.seek(1024, 1)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000106 self.assertTrue(2048 == fobj.tell(),
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000107 "seek() to positive relative position failed")
108 s = fobj.read(10)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000109 self.assertTrue(s == data[2048:2058],
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000110 "read() after seek failed")
111 fobj.seek(0, 2)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000112 self.assertTrue(tarinfo.size == fobj.tell(),
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000113 "seek() to file's end failed")
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000114 self.assertTrue(fobj.read() == "",
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000115 "read() at file's end did not return empty string")
116 fobj.seek(-tarinfo.size, 2)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000117 self.assertTrue(0 == fobj.tell(),
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000118 "relative seek() to file's start failed")
119 fobj.seek(512)
120 s1 = fobj.readlines()
121 fobj.seek(512)
122 s2 = fobj.readlines()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000123 self.assertTrue(s1 == s2,
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000124 "readlines() after seek failed")
125 fobj.seek(0)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000126 self.assertTrue(len(fobj.readline()) == fobj.tell(),
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000127 "tell() after readline() failed")
128 fobj.seek(512)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000129 self.assertTrue(len(fobj.readline()) + 512 == fobj.tell(),
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000130 "tell() after seek() and readline() failed")
131 fobj.seek(0)
132 line = fobj.readline()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000133 self.assertTrue(fobj.read() == data[len(line):],
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000134 "read() after readline() failed")
135 fobj.close()
136
Lars Gustäbel4da7d412010-06-03 12:34:14 +0000137 # Test if symbolic and hard links are resolved by extractfile(). The
138 # test link members each point to a regular member whose data is
139 # supposed to be exported.
140 def _test_fileobj_link(self, lnktype, regtype):
141 a = self.tar.extractfile(lnktype)
142 b = self.tar.extractfile(regtype)
143 self.assertEqual(a.name, b.name)
144
145 def test_fileobj_link1(self):
146 self._test_fileobj_link("ustar/lnktype", "ustar/regtype")
147
148 def test_fileobj_link2(self):
149 self._test_fileobj_link("./ustar/linktest2/lnktype", "ustar/linktest1/regtype")
150
151 def test_fileobj_symlink1(self):
152 self._test_fileobj_link("ustar/symtype", "ustar/regtype")
153
154 def test_fileobj_symlink2(self):
155 self._test_fileobj_link("./ustar/linktest2/symtype", "ustar/linktest1/regtype")
156
Lars Gustäbel231d4742012-04-24 22:42:08 +0200157 def test_issue14160(self):
158 self._test_fileobj_link("symtype2", "ustar/regtype")
159
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000160
Serhiy Storchakacfc2c7b2014-02-05 20:55:13 +0200161class ListTest(ReadTest, unittest.TestCase):
162
163 # Override setUp to use default encoding (UTF-8)
164 def setUp(self):
165 self.tar = tarfile.open(self.tarname, mode=self.mode)
166
167 def test_list(self):
168 with test_support.captured_stdout() as t:
169 self.tar.list(verbose=False)
170 out = t.getvalue()
171 self.assertIn('ustar/conttype', out)
172 self.assertIn('ustar/regtype', out)
173 self.assertIn('ustar/lnktype', out)
174 self.assertIn('ustar' + ('/12345' * 40) + '67/longname', out)
175 self.assertIn('./ustar/linktest2/symtype', out)
176 self.assertIn('./ustar/linktest2/lnktype', out)
177 # Make sure it puts trailing slash for directory
178 self.assertIn('ustar/dirtype/', out)
179 self.assertIn('ustar/dirtype-with-size/', out)
180 # Make sure it is able to print non-ASCII characters
181 self.assertIn('ustar/umlauts-'
182 '\xc4\xd6\xdc\xe4\xf6\xfc\xdf', out)
183 self.assertIn('misc/regtype-hpux-signed-chksum-'
184 '\xc4\xd6\xdc\xe4\xf6\xfc\xdf', out)
185 self.assertIn('misc/regtype-old-v7-signed-chksum-'
186 '\xc4\xd6\xdc\xe4\xf6\xfc\xdf', out)
187 # Make sure it prints files separated by one newline without any
188 # 'ls -l'-like accessories if verbose flag is not being used
189 # ...
190 # ustar/conttype
191 # ustar/regtype
192 # ...
193 self.assertRegexpMatches(out, r'ustar/conttype ?\r?\n'
194 r'ustar/regtype ?\r?\n')
195 # Make sure it does not print the source of link without verbose flag
196 self.assertNotIn('link to', out)
197 self.assertNotIn('->', out)
198
199 def test_list_verbose(self):
200 with test_support.captured_stdout() as t:
201 self.tar.list(verbose=True)
202 out = t.getvalue()
203 # Make sure it prints files separated by one newline with 'ls -l'-like
204 # accessories if verbose flag is being used
205 # ...
206 # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/conttype
207 # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype
208 # ...
209 self.assertRegexpMatches(out, (r'-rw-r--r-- tarfile/tarfile\s+7011 '
210 r'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
211 r'ustar/\w+type ?\r?\n') * 2)
212 # Make sure it prints the source of link with verbose flag
213 self.assertIn('ustar/symtype -> regtype', out)
214 self.assertIn('./ustar/linktest2/symtype -> ../linktest1/regtype', out)
215 self.assertIn('./ustar/linktest2/lnktype link to '
216 './ustar/linktest1/regtype', out)
217 self.assertIn('gnu' + ('/123' * 125) + '/longlink link to gnu' +
218 ('/123' * 125) + '/longname', out)
219 self.assertIn('pax' + ('/123' * 125) + '/longlink link to pax' +
220 ('/123' * 125) + '/longname', out)
221
222
223class GzipListTest(ListTest):
224 tarname = gzipname
225 mode = "r:gz"
226 taropen = tarfile.TarFile.gzopen
227
228
229class Bz2ListTest(ListTest):
230 tarname = bz2name
231 mode = "r:bz2"
232 taropen = tarfile.TarFile.bz2open
233
234
Lars Gustäbeldd866d52009-11-22 18:30:53 +0000235class CommonReadTest(ReadTest):
236
237 def test_empty_tarfile(self):
238 # Test for issue6123: Allow opening empty archives.
239 # This test checks if tarfile.open() is able to open an empty tar
240 # archive successfully. Note that an empty tar archive is not the
241 # same as an empty file!
242 tarfile.open(tmpname, self.mode.replace("r", "w")).close()
243 try:
244 tar = tarfile.open(tmpname, self.mode)
245 tar.getnames()
246 except tarfile.ReadError:
247 self.fail("tarfile.open() failed on empty archive")
248 self.assertListEqual(tar.getmembers(), [])
249
250 def test_null_tarfile(self):
251 # Test for issue6123: Allow opening empty archives.
252 # This test guarantees that tarfile.open() does not treat an empty
253 # file as an empty tar archive.
254 open(tmpname, "wb").close()
255 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, self.mode)
256 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname)
257
Serhiy Storchakad804f532014-01-13 19:08:51 +0200258 def test_non_existent_tarfile(self):
259 # Test for issue11513: prevent non-existent gzipped tarfiles raising
260 # multiple exceptions.
261 exctype = OSError if '|' in self.mode else IOError
262 with self.assertRaisesRegexp(exctype, "xxx") as ex:
263 tarfile.open("xxx", self.mode)
264 self.assertEqual(ex.exception.errno, errno.ENOENT)
265
Lars Gustäbeldd866d52009-11-22 18:30:53 +0000266 def test_ignore_zeros(self):
267 # Test TarFile's ignore_zeros option.
268 if self.mode.endswith(":gz"):
269 _open = gzip.GzipFile
270 elif self.mode.endswith(":bz2"):
271 _open = bz2.BZ2File
272 else:
273 _open = open
274
275 for char in ('\0', 'a'):
276 # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
277 # are ignored correctly.
278 fobj = _open(tmpname, "wb")
279 fobj.write(char * 1024)
280 fobj.write(tarfile.TarInfo("foo").tobuf())
281 fobj.close()
282
283 tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
284 self.assertListEqual(tar.getnames(), ["foo"],
285 "ignore_zeros=True should have skipped the %r-blocks" % char)
286 tar.close()
287
288
289class MiscReadTest(CommonReadTest):
Serhiy Storchaka75ba21a2014-01-18 15:35:19 +0200290 taropen = tarfile.TarFile.taropen
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000291
Lars Gustäbel0f4a14b2007-08-28 12:31:09 +0000292 def test_no_name_argument(self):
Serhiy Storchaka7cc3b0a2014-07-22 10:39:59 +0300293 fobj = open(self.tarname, "rb")
294 tar = tarfile.open(fileobj=fobj, mode=self.mode)
295 self.assertEqual(tar.name, os.path.abspath(fobj.name))
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000296
Lars Gustäbel0f4a14b2007-08-28 12:31:09 +0000297 def test_no_name_attribute(self):
298 data = open(self.tarname, "rb").read()
299 fobj = StringIO.StringIO(data)
300 self.assertRaises(AttributeError, getattr, fobj, "name")
301 tar = tarfile.open(fileobj=fobj, mode=self.mode)
Serhiy Storchaka7cc3b0a2014-07-22 10:39:59 +0300302 self.assertEqual(tar.name, None)
Lars Gustäbel0f4a14b2007-08-28 12:31:09 +0000303
304 def test_empty_name_attribute(self):
305 data = open(self.tarname, "rb").read()
306 fobj = StringIO.StringIO(data)
307 fobj.name = ""
308 tar = tarfile.open(fileobj=fobj, mode=self.mode)
Serhiy Storchaka7cc3b0a2014-07-22 10:39:59 +0300309 self.assertEqual(tar.name, None)
Lars Gustäbel0f4a14b2007-08-28 12:31:09 +0000310
Serhiy Storchaka75ba21a2014-01-18 15:35:19 +0200311 def test_illegal_mode_arg(self):
312 with open(tmpname, 'wb'):
313 pass
314 self.addCleanup(os.unlink, tmpname)
315 with self.assertRaisesRegexp(ValueError, 'mode must be '):
316 tar = self.taropen(tmpname, 'q')
317 with self.assertRaisesRegexp(ValueError, 'mode must be '):
318 tar = self.taropen(tmpname, 'rw')
319 with self.assertRaisesRegexp(ValueError, 'mode must be '):
320 tar = self.taropen(tmpname, '')
321
Lars Gustäbel77b2d632007-12-01 21:02:12 +0000322 def test_fileobj_with_offset(self):
323 # Skip the first member and store values from the second member
324 # of the testtar.
325 tar = tarfile.open(self.tarname, mode=self.mode)
326 tar.next()
327 t = tar.next()
328 name = t.name
329 offset = t.offset
330 data = tar.extractfile(t).read()
331 tar.close()
332
333 # Open the testtar and seek to the offset of the second member.
334 if self.mode.endswith(":gz"):
335 _open = gzip.GzipFile
336 elif self.mode.endswith(":bz2"):
337 _open = bz2.BZ2File
338 else:
339 _open = open
340 fobj = _open(self.tarname, "rb")
341 fobj.seek(offset)
342
343 # Test if the tarfile starts with the second member.
344 tar = tar.open(self.tarname, mode="r:", fileobj=fobj)
345 t = tar.next()
346 self.assertEqual(t.name, name)
347 # Read to the end of fileobj and test if seeking back to the
348 # beginning works.
349 tar.getmembers()
350 self.assertEqual(tar.extractfile(t).read(), data,
351 "seek back did not work")
352 tar.close()
353
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000354 def test_fail_comp(self):
355 # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
356 if self.mode == "r:":
Zachary Ware1f702212013-12-10 14:09:20 -0600357 self.skipTest('needs a gz or bz2 mode')
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000358 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode)
359 fobj = open(tarname, "rb")
360 self.assertRaises(tarfile.ReadError, tarfile.open, fileobj=fobj, mode=self.mode)
361
362 def test_v7_dirtype(self):
363 # Test old style dirtype member (bug #1336623):
364 # Old V7 tars create directory members using an AREGTYPE
365 # header with a "/" appended to the filename field.
366 tarinfo = self.tar.getmember("misc/dirtype-old-v7")
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000367 self.assertTrue(tarinfo.type == tarfile.DIRTYPE,
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000368 "v7 dirtype failed")
369
Lars Gustäbel6bf51da2008-02-11 19:17:10 +0000370 def test_xstar_type(self):
371 # The xstar format stores extra atime and ctime fields inside the
372 # space reserved for the prefix field. The prefix field must be
373 # ignored in this case, otherwise it will mess up the name.
374 try:
375 self.tar.getmember("misc/regtype-xstar")
376 except KeyError:
377 self.fail("failed to find misc/regtype-xstar (mangled prefix?)")
378
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000379 def test_check_members(self):
380 for tarinfo in self.tar:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000381 self.assertTrue(int(tarinfo.mtime) == 07606136617,
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000382 "wrong mtime for %s" % tarinfo.name)
383 if not tarinfo.name.startswith("ustar/"):
384 continue
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000385 self.assertTrue(tarinfo.uname == "tarfile",
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000386 "wrong uname for %s" % tarinfo.name)
387
388 def test_find_members(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000389 self.assertTrue(self.tar.getmembers()[-1].name == "misc/eof",
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000390 "could not find all members")
391
392 def test_extract_hardlink(self):
393 # Test hardlink extraction (e.g. bug #857297).
Serhiy Storchaka421489f2012-12-30 20:15:10 +0200394 with tarfile.open(tarname, errorlevel=1, encoding="iso8859-1") as tar:
395 tar.extract("ustar/regtype", TEMPDIR)
396 self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/regtype"))
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000397
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000398 tar.extract("ustar/lnktype", TEMPDIR)
Serhiy Storchaka421489f2012-12-30 20:15:10 +0200399 self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/lnktype"))
400 with open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb") as f:
401 data = f.read()
402 self.assertEqual(md5sum(data), md5_regtype)
Neal Norwitzf3396542005-10-28 05:52:22 +0000403
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000404 tar.extract("ustar/symtype", TEMPDIR)
Serhiy Storchaka421489f2012-12-30 20:15:10 +0200405 self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/symtype"))
406 with open(os.path.join(TEMPDIR, "ustar/symtype"), "rb") as f:
407 data = f.read()
408 self.assertEqual(md5sum(data), md5_regtype)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000409
Lars Gustäbel2ee1c762008-01-04 14:00:33 +0000410 def test_extractall(self):
411 # Test if extractall() correctly restores directory permissions
412 # and times (see issue1735).
Lars Gustäbel2ee1c762008-01-04 14:00:33 +0000413 tar = tarfile.open(tarname, encoding="iso8859-1")
414 directories = [t for t in tar if t.isdir()]
415 tar.extractall(TEMPDIR, directories)
416 for tarinfo in directories:
417 path = os.path.join(TEMPDIR, tarinfo.name)
Lars Gustäbel3b027422008-12-12 13:58:03 +0000418 if sys.platform != "win32":
419 # Win32 has no support for fine grained permissions.
420 self.assertEqual(tarinfo.mode & 0777, os.stat(path).st_mode & 0777)
Lars Gustäbel2ee1c762008-01-04 14:00:33 +0000421 self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
422 tar.close()
423
Lars Gustäbel12adc652009-11-23 15:46:19 +0000424 def test_init_close_fobj(self):
425 # Issue #7341: Close the internal file object in the TarFile
426 # constructor in case of an error. For the test we rely on
427 # the fact that opening an empty file raises a ReadError.
428 empty = os.path.join(TEMPDIR, "empty")
429 open(empty, "wb").write("")
430
431 try:
432 tar = object.__new__(tarfile.TarFile)
433 try:
434 tar.__init__(empty)
435 except tarfile.ReadError:
436 self.assertTrue(tar.fileobj.closed)
437 else:
438 self.fail("ReadError not raised")
439 finally:
440 os.remove(empty)
441
Serhiy Storchakace34ba62013-05-09 14:22:05 +0300442 def test_parallel_iteration(self):
443 # Issue #16601: Restarting iteration over tarfile continued
444 # from where it left off.
445 with tarfile.open(self.tarname) as tar:
446 for m1, m2 in zip(tar, tar):
447 self.assertEqual(m1.offset, m2.offset)
448 self.assertEqual(m1.name, m2.name)
449
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000450
Lars Gustäbeldd866d52009-11-22 18:30:53 +0000451class StreamReadTest(CommonReadTest):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000452
453 mode="r|"
454
455 def test_fileobj_regular_file(self):
456 tarinfo = self.tar.next() # get "regtype" (can't use getmember)
457 fobj = self.tar.extractfile(tarinfo)
458 data = fobj.read()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000459 self.assertTrue((len(data), md5sum(data)) == (tarinfo.size, md5_regtype),
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000460 "regular file extraction failed")
461
462 def test_provoke_stream_error(self):
463 tarinfos = self.tar.getmembers()
464 f = self.tar.extractfile(tarinfos[0]) # read the first member
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000465 self.assertRaises(tarfile.StreamError, f.read)
466
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000467 def test_compare_members(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000468 tar1 = tarfile.open(tarname, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000469 tar2 = self.tar
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000470
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000471 while True:
472 t1 = tar1.next()
473 t2 = tar2.next()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000474 if t1 is None:
475 break
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000476 self.assertTrue(t2 is not None, "stream.next() failed.")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000477
478 if t2.islnk() or t2.issym():
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000479 self.assertRaises(tarfile.StreamError, tar2.extractfile, t2)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000480 continue
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000481
482 v1 = tar1.extractfile(t1)
483 v2 = tar2.extractfile(t2)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000484 if v1 is None:
485 continue
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000486 self.assertTrue(v2 is not None, "stream.extractfile() failed")
487 self.assertTrue(v1.read() == v2.read(), "stream extraction failed")
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +0000488
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000489 tar1.close()
Lars Gustäbela4b23812006-12-23 17:57:23 +0000490
Georg Brandla32e0a02006-10-24 16:54:16 +0000491
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000492class DetectReadTest(unittest.TestCase):
Lars Gustäbel3f8aca12007-02-06 18:38:13 +0000493
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000494 def _testfunc_file(self, name, mode):
495 try:
496 tarfile.open(name, mode)
497 except tarfile.ReadError:
498 self.fail()
Lars Gustäbel3f8aca12007-02-06 18:38:13 +0000499
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000500 def _testfunc_fileobj(self, name, mode):
501 try:
502 tarfile.open(name, mode, fileobj=open(name, "rb"))
503 except tarfile.ReadError:
504 self.fail()
505
506 def _test_modes(self, testfunc):
507 testfunc(tarname, "r")
508 testfunc(tarname, "r:")
509 testfunc(tarname, "r:*")
510 testfunc(tarname, "r|")
511 testfunc(tarname, "r|*")
512
513 if gzip:
514 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:gz")
515 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|gz")
516 self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r:")
517 self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r|")
518
519 testfunc(gzipname, "r")
520 testfunc(gzipname, "r:*")
521 testfunc(gzipname, "r:gz")
522 testfunc(gzipname, "r|*")
523 testfunc(gzipname, "r|gz")
524
525 if bz2:
526 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:bz2")
527 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|bz2")
528 self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r:")
529 self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r|")
530
531 testfunc(bz2name, "r")
532 testfunc(bz2name, "r:*")
533 testfunc(bz2name, "r:bz2")
534 testfunc(bz2name, "r|*")
535 testfunc(bz2name, "r|bz2")
536
537 def test_detect_file(self):
538 self._test_modes(self._testfunc_file)
539
540 def test_detect_fileobj(self):
541 self._test_modes(self._testfunc_fileobj)
542
Zachary Ware1f702212013-12-10 14:09:20 -0600543 @unittest.skipUnless(bz2, 'requires bz2')
Lars Gustäbel9a388632011-12-06 13:07:09 +0100544 def test_detect_stream_bz2(self):
545 # Originally, tarfile's stream detection looked for the string
546 # "BZh91" at the start of the file. This is incorrect because
547 # the '9' represents the blocksize (900kB). If the file was
548 # compressed using another blocksize autodetection fails.
Lars Gustäbel9a388632011-12-06 13:07:09 +0100549 with open(tarname, "rb") as fobj:
550 data = fobj.read()
551
552 # Compress with blocksize 100kB, the file starts with "BZh11".
553 with bz2.BZ2File(tmpname, "wb", compresslevel=1) as fobj:
554 fobj.write(data)
555
556 self._testfunc_file(tmpname, "r|*")
557
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000558
559class MemberReadTest(ReadTest):
560
561 def _test_member(self, tarinfo, chksum=None, **kwargs):
562 if chksum is not None:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000563 self.assertTrue(md5sum(self.tar.extractfile(tarinfo).read()) == chksum,
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000564 "wrong md5sum for %s" % tarinfo.name)
565
566 kwargs["mtime"] = 07606136617
567 kwargs["uid"] = 1000
568 kwargs["gid"] = 100
569 if "old-v7" not in tarinfo.name:
570 # V7 tar can't handle alphabetic owners.
571 kwargs["uname"] = "tarfile"
572 kwargs["gname"] = "tarfile"
573 for k, v in kwargs.iteritems():
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000574 self.assertTrue(getattr(tarinfo, k) == v,
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000575 "wrong value in %s field of %s" % (k, tarinfo.name))
576
577 def test_find_regtype(self):
578 tarinfo = self.tar.getmember("ustar/regtype")
579 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
580
581 def test_find_conttype(self):
582 tarinfo = self.tar.getmember("ustar/conttype")
583 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
584
585 def test_find_dirtype(self):
586 tarinfo = self.tar.getmember("ustar/dirtype")
587 self._test_member(tarinfo, size=0)
588
589 def test_find_dirtype_with_size(self):
590 tarinfo = self.tar.getmember("ustar/dirtype-with-size")
591 self._test_member(tarinfo, size=255)
592
593 def test_find_lnktype(self):
594 tarinfo = self.tar.getmember("ustar/lnktype")
595 self._test_member(tarinfo, size=0, linkname="ustar/regtype")
596
597 def test_find_symtype(self):
598 tarinfo = self.tar.getmember("ustar/symtype")
599 self._test_member(tarinfo, size=0, linkname="regtype")
600
601 def test_find_blktype(self):
602 tarinfo = self.tar.getmember("ustar/blktype")
603 self._test_member(tarinfo, size=0, devmajor=3, devminor=0)
604
605 def test_find_chrtype(self):
606 tarinfo = self.tar.getmember("ustar/chrtype")
607 self._test_member(tarinfo, size=0, devmajor=1, devminor=3)
608
609 def test_find_fifotype(self):
610 tarinfo = self.tar.getmember("ustar/fifotype")
611 self._test_member(tarinfo, size=0)
612
613 def test_find_sparse(self):
614 tarinfo = self.tar.getmember("ustar/sparse")
615 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
616
617 def test_find_umlauts(self):
618 tarinfo = self.tar.getmember("ustar/umlauts-ÄÖÜäöüß")
619 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
620
621 def test_find_ustar_longname(self):
622 name = "ustar/" + "12345/" * 39 + "1234567/longname"
Ezio Melottiaa980582010-01-23 23:04:36 +0000623 self.assertIn(name, self.tar.getnames())
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000624
625 def test_find_regtype_oldv7(self):
626 tarinfo = self.tar.getmember("misc/regtype-old-v7")
627 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
628
629 def test_find_pax_umlauts(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000630 self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000631 tarinfo = self.tar.getmember("pax/umlauts-ÄÖÜäöüß")
632 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
633
634
635class LongnameTest(ReadTest):
636
637 def test_read_longname(self):
638 # Test reading of longname (bug #1471427).
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000639 longname = self.subdir + "/" + "123/" * 125 + "longname"
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000640 try:
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000641 tarinfo = self.tar.getmember(longname)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000642 except KeyError:
643 self.fail("longname not found")
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000644 self.assertTrue(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000645
646 def test_read_longlink(self):
647 longname = self.subdir + "/" + "123/" * 125 + "longname"
648 longlink = self.subdir + "/" + "123/" * 125 + "longlink"
649 try:
650 tarinfo = self.tar.getmember(longlink)
651 except KeyError:
652 self.fail("longlink not found")
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000653 self.assertTrue(tarinfo.linkname == longname, "linkname wrong")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000654
655 def test_truncated_longname(self):
656 longname = self.subdir + "/" + "123/" * 125 + "longname"
657 tarinfo = self.tar.getmember(longname)
658 offset = tarinfo.offset
659 self.tar.fileobj.seek(offset)
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000660 fobj = StringIO.StringIO(self.tar.fileobj.read(3 * 512))
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000661 self.assertRaises(tarfile.ReadError, tarfile.open, name="foo.tar", fileobj=fobj)
662
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000663 def test_header_offset(self):
664 # Test if the start offset of the TarInfo object includes
665 # the preceding extended header.
666 longname = self.subdir + "/" + "123/" * 125 + "longname"
667 offset = self.tar.getmember(longname).offset
668 fobj = open(tarname)
669 fobj.seek(offset)
670 tarinfo = tarfile.TarInfo.frombuf(fobj.read(512))
671 self.assertEqual(tarinfo.type, self.longnametype)
672
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000673
674class GNUReadTest(LongnameTest):
675
676 subdir = "gnu"
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000677 longnametype = tarfile.GNUTYPE_LONGNAME
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000678
679 def test_sparse_file(self):
680 tarinfo1 = self.tar.getmember("ustar/sparse")
681 fobj1 = self.tar.extractfile(tarinfo1)
682 tarinfo2 = self.tar.getmember("gnu/sparse")
683 fobj2 = self.tar.extractfile(tarinfo2)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000684 self.assertTrue(fobj1.read() == fobj2.read(),
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000685 "sparse file extraction failed")
686
687
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000688class PaxReadTest(LongnameTest):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000689
690 subdir = "pax"
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000691 longnametype = tarfile.XHDTYPE
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000692
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000693 def test_pax_global_headers(self):
Lars Gustäbela36cde42007-03-13 15:47:07 +0000694 tar = tarfile.open(tarname, encoding="iso8859-1")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000695
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000696 tarinfo = tar.getmember("pax/regtype1")
697 self.assertEqual(tarinfo.uname, "foo")
698 self.assertEqual(tarinfo.gname, "bar")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000699 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000700
701 tarinfo = tar.getmember("pax/regtype2")
702 self.assertEqual(tarinfo.uname, "")
703 self.assertEqual(tarinfo.gname, "bar")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000704 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000705
706 tarinfo = tar.getmember("pax/regtype3")
707 self.assertEqual(tarinfo.uname, "tarfile")
708 self.assertEqual(tarinfo.gname, "tarfile")
Lars Gustäbela0fcb932007-05-27 19:49:30 +0000709 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
710
711 def test_pax_number_fields(self):
712 # All following number fields are read from the pax header.
713 tar = tarfile.open(tarname, encoding="iso8859-1")
714 tarinfo = tar.getmember("pax/regtype4")
715 self.assertEqual(tarinfo.size, 7011)
716 self.assertEqual(tarinfo.uid, 123)
717 self.assertEqual(tarinfo.gid, 123)
718 self.assertEqual(tarinfo.mtime, 1041808783.0)
719 self.assertEqual(type(tarinfo.mtime), float)
720 self.assertEqual(float(tarinfo.pax_headers["atime"]), 1041808783.0)
721 self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000722
723
Lars Gustäbelb1a54a32008-05-27 12:39:23 +0000724class WriteTestBase(unittest.TestCase):
725 # Put all write tests in here that are supposed to be tested
726 # in all possible mode combinations.
727
728 def test_fileobj_no_close(self):
729 fobj = StringIO.StringIO()
730 tar = tarfile.open(fileobj=fobj, mode=self.mode)
731 tar.addfile(tarfile.TarInfo("foo"))
732 tar.close()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000733 self.assertTrue(fobj.closed is False, "external fileobjs must never closed")
Serhiy Storchakacdf1ebd2014-01-18 15:54:32 +0200734 # Issue #20238: Incomplete gzip output with mode="w:gz"
735 data = fobj.getvalue()
736 del tar
737 test_support.gc_collect()
738 self.assertFalse(fobj.closed)
739 self.assertEqual(data, fobj.getvalue())
Lars Gustäbelb1a54a32008-05-27 12:39:23 +0000740
741
742class WriteTest(WriteTestBase):
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000743
744 mode = "w:"
745
746 def test_100_char_name(self):
747 # The name field in a tar header stores strings of at most 100 chars.
748 # If a string is shorter than 100 chars it has to be padded with '\0',
749 # which implies that a string of exactly 100 chars is stored without
750 # a trailing '\0'.
751 name = "0123456789" * 10
752 tar = tarfile.open(tmpname, self.mode)
753 t = tarfile.TarInfo(name)
754 tar.addfile(t)
Lars Gustäbel3f8aca12007-02-06 18:38:13 +0000755 tar.close()
756
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000757 tar = tarfile.open(tmpname)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000758 self.assertTrue(tar.getnames()[0] == name,
Georg Brandla32e0a02006-10-24 16:54:16 +0000759 "failed to store 100 char filename")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000760 tar.close()
Georg Brandla32e0a02006-10-24 16:54:16 +0000761
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000762 def test_tar_size(self):
763 # Test for bug #1013882.
764 tar = tarfile.open(tmpname, self.mode)
765 path = os.path.join(TEMPDIR, "file")
766 fobj = open(path, "wb")
767 fobj.write("aaa")
768 fobj.close()
769 tar.add(path)
770 tar.close()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000771 self.assertTrue(os.path.getsize(tmpname) > 0,
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000772 "tarfile is empty")
Georg Brandla32e0a02006-10-24 16:54:16 +0000773
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000774 # The test_*_size tests test for bug #1167128.
775 def test_file_size(self):
776 tar = tarfile.open(tmpname, self.mode)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000777
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000778 path = os.path.join(TEMPDIR, "file")
779 fobj = open(path, "wb")
780 fobj.close()
781 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000782 self.assertEqual(tarinfo.size, 0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000783
784 fobj = open(path, "wb")
785 fobj.write("aaa")
786 fobj.close()
787 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000788 self.assertEqual(tarinfo.size, 3)
789
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000790 tar.close()
791
792 def test_directory_size(self):
793 path = os.path.join(TEMPDIR, "directory")
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000794 os.mkdir(path)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000795 try:
796 tar = tarfile.open(tmpname, self.mode)
797 tarinfo = tar.gettarinfo(path)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000798 self.assertEqual(tarinfo.size, 0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000799 finally:
800 os.rmdir(path)
801
802 def test_link_size(self):
803 if hasattr(os, "link"):
804 link = os.path.join(TEMPDIR, "link")
805 target = os.path.join(TEMPDIR, "link_target")
Lars Gustäbel2ee9c6f2010-06-03 09:56:22 +0000806 fobj = open(target, "wb")
807 fobj.write("aaa")
808 fobj.close()
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000809 os.link(target, link)
810 try:
811 tar = tarfile.open(tmpname, self.mode)
Lars Gustäbel2ee9c6f2010-06-03 09:56:22 +0000812 # Record the link target in the inodes list.
813 tar.gettarinfo(target)
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000814 tarinfo = tar.gettarinfo(link)
815 self.assertEqual(tarinfo.size, 0)
816 finally:
817 os.remove(target)
818 os.remove(link)
819
820 def test_symlink_size(self):
821 if hasattr(os, "symlink"):
822 path = os.path.join(TEMPDIR, "symlink")
823 os.symlink("link_target", path)
824 try:
825 tar = tarfile.open(tmpname, self.mode)
826 tarinfo = tar.gettarinfo(path)
827 self.assertEqual(tarinfo.size, 0)
828 finally:
829 os.remove(path)
830
831 def test_add_self(self):
832 # Test for #1257255.
833 dstname = os.path.abspath(tmpname)
834
835 tar = tarfile.open(tmpname, self.mode)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000836 self.assertTrue(tar.name == dstname, "archive name must be absolute")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000837
838 tar.add(dstname)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000839 self.assertTrue(tar.getnames() == [], "added the archive to itself")
Lars Gustäbelc64e4022007-03-13 10:47:19 +0000840
841 cwd = os.getcwd()
842 os.chdir(TEMPDIR)
843 tar.add(dstname)
844 os.chdir(cwd)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000845 self.assertTrue(tar.getnames() == [], "added the archive to itself")
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000846
Lars Gustäbel104490e2007-06-18 11:42:11 +0000847 def test_exclude(self):
848 tempdir = os.path.join(TEMPDIR, "exclude")
849 os.mkdir(tempdir)
850 try:
851 for name in ("foo", "bar", "baz"):
852 name = os.path.join(tempdir, name)
853 open(name, "wb").close()
854
Florent Xiclunafc5f6a72010-03-20 22:26:42 +0000855 exclude = os.path.isfile
Lars Gustäbel104490e2007-06-18 11:42:11 +0000856
857 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
Florent Xiclunafc5f6a72010-03-20 22:26:42 +0000858 with test_support.check_warnings(("use the filter argument",
859 DeprecationWarning)):
860 tar.add(tempdir, arcname="empty_dir", exclude=exclude)
Lars Gustäbel104490e2007-06-18 11:42:11 +0000861 tar.close()
862
863 tar = tarfile.open(tmpname, "r")
864 self.assertEqual(len(tar.getmembers()), 1)
865 self.assertEqual(tar.getnames()[0], "empty_dir")
866 finally:
867 shutil.rmtree(tempdir)
868
Lars Gustäbel21121e62009-09-12 10:28:15 +0000869 def test_filter(self):
870 tempdir = os.path.join(TEMPDIR, "filter")
871 os.mkdir(tempdir)
872 try:
873 for name in ("foo", "bar", "baz"):
874 name = os.path.join(tempdir, name)
875 open(name, "wb").close()
876
877 def filter(tarinfo):
878 if os.path.basename(tarinfo.name) == "bar":
879 return
880 tarinfo.uid = 123
881 tarinfo.uname = "foo"
882 return tarinfo
883
884 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
885 tar.add(tempdir, arcname="empty_dir", filter=filter)
886 tar.close()
887
888 tar = tarfile.open(tmpname, "r")
889 for tarinfo in tar:
890 self.assertEqual(tarinfo.uid, 123)
891 self.assertEqual(tarinfo.uname, "foo")
892 self.assertEqual(len(tar.getmembers()), 3)
893 tar.close()
894 finally:
895 shutil.rmtree(tempdir)
896
Lars Gustäbelf7cda522009-08-28 19:23:44 +0000897 # Guarantee that stored pathnames are not modified. Don't
898 # remove ./ or ../ or double slashes. Still make absolute
899 # pathnames relative.
900 # For details see bug #6054.
901 def _test_pathname(self, path, cmp_path=None, dir=False):
902 # Create a tarfile with an empty member named path
903 # and compare the stored name with the original.
904 foo = os.path.join(TEMPDIR, "foo")
905 if not dir:
906 open(foo, "w").close()
907 else:
908 os.mkdir(foo)
909
910 tar = tarfile.open(tmpname, self.mode)
911 tar.add(foo, arcname=path)
912 tar.close()
913
914 tar = tarfile.open(tmpname, "r")
915 t = tar.next()
916 tar.close()
917
918 if not dir:
919 os.remove(foo)
920 else:
921 os.rmdir(foo)
922
923 self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
924
925 def test_pathnames(self):
926 self._test_pathname("foo")
927 self._test_pathname(os.path.join("foo", ".", "bar"))
928 self._test_pathname(os.path.join("foo", "..", "bar"))
929 self._test_pathname(os.path.join(".", "foo"))
930 self._test_pathname(os.path.join(".", "foo", "."))
931 self._test_pathname(os.path.join(".", "foo", ".", "bar"))
932 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
933 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
934 self._test_pathname(os.path.join("..", "foo"))
935 self._test_pathname(os.path.join("..", "foo", ".."))
936 self._test_pathname(os.path.join("..", "foo", ".", "bar"))
937 self._test_pathname(os.path.join("..", "foo", "..", "bar"))
938
939 self._test_pathname("foo" + os.sep + os.sep + "bar")
940 self._test_pathname("foo" + os.sep + os.sep, "foo", dir=True)
941
942 def test_abs_pathnames(self):
943 if sys.platform == "win32":
944 self._test_pathname("C:\\foo", "foo")
945 else:
946 self._test_pathname("/foo", "foo")
947 self._test_pathname("///foo", "foo")
948
949 def test_cwd(self):
950 # Test adding the current working directory.
951 cwd = os.getcwd()
952 os.chdir(TEMPDIR)
953 try:
954 open("foo", "w").close()
955
956 tar = tarfile.open(tmpname, self.mode)
957 tar.add(".")
958 tar.close()
959
960 tar = tarfile.open(tmpname, "r")
961 for t in tar:
Serhiy Storchaka88761452012-12-28 00:32:19 +0200962 self.assertTrue(t.name == "." or t.name.startswith("./"))
Lars Gustäbelf7cda522009-08-28 19:23:44 +0000963 tar.close()
964 finally:
965 os.chdir(cwd)
966
Senthil Kumaranf3eb7d32011-04-28 17:00:19 +0800967 @unittest.skipUnless(hasattr(os, 'symlink'), "needs os.symlink")
Senthil Kumaran011525e2011-04-28 15:30:31 +0800968 def test_extractall_symlinks(self):
969 # Test if extractall works properly when tarfile contains symlinks
970 tempdir = os.path.join(TEMPDIR, "testsymlinks")
971 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
972 os.mkdir(tempdir)
973 try:
974 source_file = os.path.join(tempdir,'source')
975 target_file = os.path.join(tempdir,'symlink')
976 with open(source_file,'w') as f:
977 f.write('something\n')
978 os.symlink(source_file, target_file)
979 tar = tarfile.open(temparchive,'w')
980 tar.add(source_file, arcname=os.path.basename(source_file))
981 tar.add(target_file, arcname=os.path.basename(target_file))
982 tar.close()
983 # Let's extract it to the location which contains the symlink
984 tar = tarfile.open(temparchive,'r')
985 # this should not raise OSError: [Errno 17] File exists
986 try:
987 tar.extractall(path=tempdir)
988 except OSError:
989 self.fail("extractall failed with symlinked files")
990 finally:
991 tar.close()
992 finally:
993 os.unlink(temparchive)
994 shutil.rmtree(tempdir)
Martin v. Löwis5dbdc592005-08-27 10:07:56 +0000995
Senthil Kumaran4dd89ce2011-05-17 10:12:18 +0800996 @unittest.skipUnless(hasattr(os, 'symlink'), "needs os.symlink")
997 def test_extractall_broken_symlinks(self):
998 # Test if extractall works properly when tarfile contains broken
999 # symlinks
1000 tempdir = os.path.join(TEMPDIR, "testsymlinks")
1001 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
1002 os.mkdir(tempdir)
1003 try:
1004 source_file = os.path.join(tempdir,'source')
1005 target_file = os.path.join(tempdir,'symlink')
1006 with open(source_file,'w') as f:
1007 f.write('something\n')
1008 os.symlink(source_file, target_file)
1009 tar = tarfile.open(temparchive,'w')
1010 tar.add(target_file, arcname=os.path.basename(target_file))
1011 tar.close()
1012 # remove the real file
1013 os.unlink(source_file)
1014 # Let's extract it to the location which contains the symlink
1015 tar = tarfile.open(temparchive,'r')
1016 # this should not raise OSError: [Errno 17] File exists
1017 try:
1018 tar.extractall(path=tempdir)
1019 except OSError:
1020 self.fail("extractall failed with broken symlinked files")
1021 finally:
1022 tar.close()
1023 finally:
1024 os.unlink(temparchive)
1025 shutil.rmtree(tempdir)
1026
1027 @unittest.skipUnless(hasattr(os, 'link'), "needs os.link")
1028 def test_extractall_hardlinks(self):
1029 # Test if extractall works properly when tarfile contains symlinks
1030 tempdir = os.path.join(TEMPDIR, "testsymlinks")
1031 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
1032 os.mkdir(tempdir)
1033 try:
1034 source_file = os.path.join(tempdir,'source')
1035 target_file = os.path.join(tempdir,'symlink')
1036 with open(source_file,'w') as f:
1037 f.write('something\n')
1038 os.link(source_file, target_file)
1039 tar = tarfile.open(temparchive,'w')
1040 tar.add(source_file, arcname=os.path.basename(source_file))
1041 tar.add(target_file, arcname=os.path.basename(target_file))
1042 tar.close()
1043 # Let's extract it to the location which contains the symlink
1044 tar = tarfile.open(temparchive,'r')
1045 # this should not raise OSError: [Errno 17] File exists
1046 try:
1047 tar.extractall(path=tempdir)
1048 except OSError:
1049 self.fail("extractall failed with linked files")
1050 finally:
1051 tar.close()
1052 finally:
1053 os.unlink(temparchive)
1054 shutil.rmtree(tempdir)
1055
Serhiy Storchaka7a278da2014-01-18 16:14:00 +02001056 def test_open_nonwritable_fileobj(self):
1057 for exctype in IOError, EOFError, RuntimeError:
1058 class BadFile(StringIO.StringIO):
1059 first = True
1060 def write(self, data):
1061 if self.first:
1062 self.first = False
1063 raise exctype
1064
1065 f = BadFile()
1066 with self.assertRaises(exctype):
1067 tar = tarfile.open(tmpname, self.mode, fileobj=f,
1068 format=tarfile.PAX_FORMAT,
1069 pax_headers={'non': 'empty'})
1070 self.assertFalse(f.closed)
1071
Lars Gustäbelb1a54a32008-05-27 12:39:23 +00001072class StreamWriteTest(WriteTestBase):
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001073
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001074 mode = "w|"
Neal Norwitz8a519392006-08-21 17:59:46 +00001075
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001076 def test_stream_padding(self):
1077 # Test for bug #1543303.
1078 tar = tarfile.open(tmpname, self.mode)
1079 tar.close()
1080
1081 if self.mode.endswith("gz"):
1082 fobj = gzip.GzipFile(tmpname)
1083 data = fobj.read()
1084 fobj.close()
1085 elif self.mode.endswith("bz2"):
1086 dec = bz2.BZ2Decompressor()
1087 data = open(tmpname, "rb").read()
1088 data = dec.decompress(data)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001089 self.assertTrue(len(dec.unused_data) == 0,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001090 "found trailing data")
Neal Norwitz8a519392006-08-21 17:59:46 +00001091 else:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001092 fobj = open(tmpname, "rb")
1093 data = fobj.read()
1094 fobj.close()
Neal Norwitz8a519392006-08-21 17:59:46 +00001095
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001096 self.assertTrue(data.count("\0") == tarfile.RECORDSIZE,
Neal Norwitz8a519392006-08-21 17:59:46 +00001097 "incorrect zero padding")
1098
Zachary Ware1f702212013-12-10 14:09:20 -06001099 @unittest.skipIf(sys.platform == 'win32', 'not appropriate for Windows')
1100 @unittest.skipUnless(hasattr(os, 'umask'), 'requires os.umask')
Lars Gustäbel5c4c4612010-04-29 15:23:38 +00001101 def test_file_mode(self):
1102 # Test for issue #8464: Create files with correct
1103 # permissions.
Lars Gustäbel5c4c4612010-04-29 15:23:38 +00001104 if os.path.exists(tmpname):
1105 os.remove(tmpname)
1106
1107 original_umask = os.umask(0022)
1108 try:
1109 tar = tarfile.open(tmpname, self.mode)
1110 tar.close()
1111 mode = os.stat(tmpname).st_mode & 0777
1112 self.assertEqual(mode, 0644, "wrong file permissions")
1113 finally:
1114 os.umask(original_umask)
1115
Lars Gustäbel7d4d0742011-12-21 19:27:50 +01001116 def test_issue13639(self):
1117 try:
1118 with tarfile.open(unicode(tmpname, sys.getfilesystemencoding()), self.mode):
1119 pass
1120 except UnicodeDecodeError:
1121 self.fail("_Stream failed to write unicode filename")
1122
Neal Norwitz8a519392006-08-21 17:59:46 +00001123
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001124class GNUWriteTest(unittest.TestCase):
1125 # This testcase checks for correct creation of GNU Longname
1126 # and Longlink extended headers (cp. bug #812325).
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001127
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001128 def _length(self, s):
1129 blocks, remainder = divmod(len(s) + 1, 512)
1130 if remainder:
1131 blocks += 1
1132 return blocks * 512
1133
1134 def _calc_size(self, name, link=None):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001135 # Initial tar header
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001136 count = 512
1137
1138 if len(name) > tarfile.LENGTH_NAME:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001139 # GNU longname extended header + longname
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001140 count += 512
1141 count += self._length(name)
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001142 if link is not None and len(link) > tarfile.LENGTH_LINK:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001143 # GNU longlink extended header + longlink
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001144 count += 512
1145 count += self._length(link)
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001146 return count
1147
1148 def _test(self, name, link=None):
1149 tarinfo = tarfile.TarInfo(name)
1150 if link:
1151 tarinfo.linkname = link
1152 tarinfo.type = tarfile.LNKTYPE
1153
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001154 tar = tarfile.open(tmpname, "w")
1155 tar.format = tarfile.GNU_FORMAT
Georg Brandl87fa5592006-12-06 22:21:18 +00001156 tar.addfile(tarinfo)
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001157
1158 v1 = self._calc_size(name, link)
Georg Brandl87fa5592006-12-06 22:21:18 +00001159 v2 = tar.offset
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001160 self.assertTrue(v1 == v2, "GNU longname/longlink creation failed")
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001161
Georg Brandl87fa5592006-12-06 22:21:18 +00001162 tar.close()
1163
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001164 tar = tarfile.open(tmpname)
Georg Brandl87fa5592006-12-06 22:21:18 +00001165 member = tar.next()
Florent Xiclunafc5f6a72010-03-20 22:26:42 +00001166 self.assertIsNotNone(member,
1167 "unable to read longname member")
1168 self.assertEqual(tarinfo.name, member.name,
1169 "unable to read longname member")
1170 self.assertEqual(tarinfo.linkname, member.linkname,
1171 "unable to read longname member")
Georg Brandl87fa5592006-12-06 22:21:18 +00001172
Neal Norwitz0662f8a2004-07-20 21:54:18 +00001173 def test_longname_1023(self):
1174 self._test(("longnam/" * 127) + "longnam")
1175
1176 def test_longname_1024(self):
1177 self._test(("longnam/" * 127) + "longname")
1178
1179 def test_longname_1025(self):
1180 self._test(("longnam/" * 127) + "longname_")
1181
1182 def test_longlink_1023(self):
1183 self._test("name", ("longlnk/" * 127) + "longlnk")
1184
1185 def test_longlink_1024(self):
1186 self._test("name", ("longlnk/" * 127) + "longlink")
1187
1188 def test_longlink_1025(self):
1189 self._test("name", ("longlnk/" * 127) + "longlink_")
1190
1191 def test_longnamelink_1023(self):
1192 self._test(("longnam/" * 127) + "longnam",
1193 ("longlnk/" * 127) + "longlnk")
1194
1195 def test_longnamelink_1024(self):
1196 self._test(("longnam/" * 127) + "longname",
1197 ("longlnk/" * 127) + "longlink")
1198
1199 def test_longnamelink_1025(self):
1200 self._test(("longnam/" * 127) + "longname_",
1201 ("longlnk/" * 127) + "longlink_")
1202
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001203
1204class HardlinkTest(unittest.TestCase):
1205 # Test the creation of LNKTYPE (hardlink) members in an archive.
Georg Brandl38c6a222006-05-10 16:26:03 +00001206
1207 def setUp(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001208 self.foo = os.path.join(TEMPDIR, "foo")
1209 self.bar = os.path.join(TEMPDIR, "bar")
Georg Brandl38c6a222006-05-10 16:26:03 +00001210
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001211 fobj = open(self.foo, "wb")
1212 fobj.write("foo")
1213 fobj.close()
Georg Brandl38c6a222006-05-10 16:26:03 +00001214
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001215 os.link(self.foo, self.bar)
Georg Brandl38c6a222006-05-10 16:26:03 +00001216
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001217 self.tar = tarfile.open(tmpname, "w")
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001218 self.tar.add(self.foo)
1219
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001220 def tearDown(self):
Hirokazu Yamamoto56d380d2008-09-21 11:44:23 +00001221 self.tar.close()
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001222 os.remove(self.foo)
1223 os.remove(self.bar)
1224
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001225 def test_add_twice(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001226 # The same name will be added as a REGTYPE every
1227 # time regardless of st_nlink.
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001228 tarinfo = self.tar.gettarinfo(self.foo)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001229 self.assertTrue(tarinfo.type == tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001230 "add file as regular failed")
1231
1232 def test_add_hardlink(self):
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001233 tarinfo = self.tar.gettarinfo(self.bar)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001234 self.assertTrue(tarinfo.type == tarfile.LNKTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001235 "add file as hardlink failed")
1236
1237 def test_dereference_hardlink(self):
1238 self.tar.dereference = True
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001239 tarinfo = self.tar.gettarinfo(self.bar)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001240 self.assertTrue(tarinfo.type == tarfile.REGTYPE,
Neal Norwitzb0e32e22005-10-20 04:50:13 +00001241 "dereferencing hardlink failed")
1242
Neal Norwitza4f651a2004-07-20 22:07:44 +00001243
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001244class PaxWriteTest(GNUWriteTest):
Martin v. Löwis78be7df2005-03-05 12:47:42 +00001245
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001246 def _test(self, name, link=None):
1247 # See GNUWriteTest.
1248 tarinfo = tarfile.TarInfo(name)
1249 if link:
1250 tarinfo.linkname = link
1251 tarinfo.type = tarfile.LNKTYPE
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +00001252
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001253 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT)
1254 tar.addfile(tarinfo)
1255 tar.close()
Andrew M. Kuchlingd4f25522004-10-20 11:47:01 +00001256
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001257 tar = tarfile.open(tmpname)
1258 if link:
1259 l = tar.getmembers()[0].linkname
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001260 self.assertTrue(link == l, "PAX longlink creation failed")
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001261 else:
1262 n = tar.getmembers()[0].name
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001263 self.assertTrue(name == n, "PAX longname creation failed")
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001264
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001265 def test_pax_global_header(self):
1266 pax_headers = {
1267 u"foo": u"bar",
1268 u"uid": u"0",
1269 u"mtime": u"1.23",
1270 u"test": u"äöü",
1271 u"äöü": u"test"}
1272
Florent Xiclunafc5f6a72010-03-20 22:26:42 +00001273 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT,
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001274 pax_headers=pax_headers)
1275 tar.addfile(tarfile.TarInfo("test"))
1276 tar.close()
1277
1278 # Test if the global header was written correctly.
1279 tar = tarfile.open(tmpname, encoding="iso8859-1")
1280 self.assertEqual(tar.pax_headers, pax_headers)
1281 self.assertEqual(tar.getmembers()[0].pax_headers, pax_headers)
1282
1283 # Test if all the fields are unicode.
1284 for key, val in tar.pax_headers.iteritems():
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001285 self.assertTrue(type(key) is unicode)
1286 self.assertTrue(type(val) is unicode)
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001287 if key in tarfile.PAX_NUMBER_FIELDS:
1288 try:
1289 tarfile.PAX_NUMBER_FIELDS[key](val)
1290 except (TypeError, ValueError):
1291 self.fail("unable to convert pax header field")
1292
1293 def test_pax_extended_header(self):
1294 # The fields from the pax header have priority over the
1295 # TarInfo.
1296 pax_headers = {u"path": u"foo", u"uid": u"123"}
1297
1298 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1")
1299 t = tarfile.TarInfo()
1300 t.name = u"äöü" # non-ASCII
1301 t.uid = 8**8 # too large
1302 t.pax_headers = pax_headers
1303 tar.addfile(t)
1304 tar.close()
1305
1306 tar = tarfile.open(tmpname, encoding="iso8859-1")
1307 t = tar.getmembers()[0]
1308 self.assertEqual(t.pax_headers, pax_headers)
1309 self.assertEqual(t.name, "foo")
1310 self.assertEqual(t.uid, 123)
1311
1312
1313class UstarUnicodeTest(unittest.TestCase):
1314 # All *UnicodeTests FIXME
1315
1316 format = tarfile.USTAR_FORMAT
1317
1318 def test_iso8859_1_filename(self):
1319 self._test_unicode_filename("iso8859-1")
1320
1321 def test_utf7_filename(self):
1322 self._test_unicode_filename("utf7")
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001323
1324 def test_utf8_filename(self):
1325 self._test_unicode_filename("utf8")
1326
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001327 def _test_unicode_filename(self, encoding):
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001328 tar = tarfile.open(tmpname, "w", format=self.format, encoding=encoding, errors="strict")
1329 name = u"äöü"
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001330 tar.addfile(tarfile.TarInfo(name))
1331 tar.close()
1332
1333 tar = tarfile.open(tmpname, encoding=encoding)
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001334 self.assertTrue(type(tar.getnames()[0]) is not unicode)
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001335 self.assertEqual(tar.getmembers()[0].name, name.encode(encoding))
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001336 tar.close()
1337
1338 def test_unicode_filename_error(self):
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001339 tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict")
1340 tarinfo = tarfile.TarInfo()
1341
1342 tarinfo.name = "äöü"
1343 if self.format == tarfile.PAX_FORMAT:
1344 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
1345 else:
1346 tar.addfile(tarinfo)
1347
1348 tarinfo.name = u"äöü"
1349 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
1350
1351 tarinfo.name = "foo"
1352 tarinfo.uname = u"äöü"
1353 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
1354
1355 def test_unicode_argument(self):
1356 tar = tarfile.open(tarname, "r", encoding="iso8859-1", errors="strict")
1357 for t in tar:
Benjamin Peterson5c8da862009-06-30 22:57:08 +00001358 self.assertTrue(type(t.name) is str)
1359 self.assertTrue(type(t.linkname) is str)
1360 self.assertTrue(type(t.uname) is str)
1361 self.assertTrue(type(t.gname) is str)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001362 tar.close()
1363
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001364 def test_uname_unicode(self):
1365 for name in (u"äöü", "äöü"):
1366 t = tarfile.TarInfo("foo")
1367 t.uname = name
1368 t.gname = name
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001369
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001370 fobj = StringIO.StringIO()
1371 tar = tarfile.open("foo.tar", mode="w", fileobj=fobj, format=self.format, encoding="iso8859-1")
1372 tar.addfile(t)
1373 tar.close()
1374 fobj.seek(0)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001375
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001376 tar = tarfile.open("foo.tar", fileobj=fobj, encoding="iso8859-1")
1377 t = tar.getmember("foo")
1378 self.assertEqual(t.uname, "äöü")
1379 self.assertEqual(t.gname, "äöü")
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001380
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001381
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001382class GNUUnicodeTest(UstarUnicodeTest):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001383
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001384 format = tarfile.GNU_FORMAT
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001385
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001386
1387class PaxUnicodeTest(UstarUnicodeTest):
1388
1389 format = tarfile.PAX_FORMAT
1390
1391 def _create_unicode_name(self, name):
1392 tar = tarfile.open(tmpname, "w", format=self.format)
1393 t = tarfile.TarInfo()
1394 t.pax_headers["path"] = name
1395 tar.addfile(t)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001396 tar.close()
1397
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001398 def test_error_handlers(self):
1399 # Test if the unicode error handlers work correctly for characters
1400 # that cannot be expressed in a given encoding.
1401 self._create_unicode_name(u"äöü")
Georg Brandlded1c4d2006-12-20 11:55:16 +00001402
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001403 for handler, name in (("utf-8", u"äöü".encode("utf8")),
1404 ("replace", "???"), ("ignore", "")):
1405 tar = tarfile.open(tmpname, format=self.format, encoding="ascii",
1406 errors=handler)
1407 self.assertEqual(tar.getnames()[0], name)
Georg Brandlded1c4d2006-12-20 11:55:16 +00001408
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001409 self.assertRaises(UnicodeError, tarfile.open, tmpname,
1410 encoding="ascii", errors="strict")
1411
1412 def test_error_handler_utf8(self):
1413 # Create a pathname that has one component representable using
1414 # iso8859-1 and the other only in iso8859-15.
1415 self._create_unicode_name(u"äöü/¤")
1416
1417 tar = tarfile.open(tmpname, format=self.format, encoding="iso8859-1",
1418 errors="utf-8")
1419 self.assertEqual(tar.getnames()[0], "äöü/" + u"¤".encode("utf8"))
Georg Brandlded1c4d2006-12-20 11:55:16 +00001420
Georg Brandlded1c4d2006-12-20 11:55:16 +00001421
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001422class AppendTest(unittest.TestCase):
1423 # Test append mode (cp. patch #1652681).
Tim Peters8ceefc52004-10-25 03:19:41 +00001424
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001425 def setUp(self):
1426 self.tarname = tmpname
1427 if os.path.exists(self.tarname):
1428 os.remove(self.tarname)
Lars Gustäbela7ba6fc2006-12-27 10:30:46 +00001429
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001430 def _add_testfile(self, fileobj=None):
1431 tar = tarfile.open(self.tarname, "a", fileobj=fileobj)
1432 tar.addfile(tarfile.TarInfo("bar"))
1433 tar.close()
Lars Gustäbela7ba6fc2006-12-27 10:30:46 +00001434
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001435 def _create_testtar(self, mode="w:"):
Lars Gustäbela36cde42007-03-13 15:47:07 +00001436 src = tarfile.open(tarname, encoding="iso8859-1")
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001437 t = src.getmember("ustar/regtype")
1438 t.name = "foo"
1439 f = src.extractfile(t)
1440 tar = tarfile.open(self.tarname, mode)
1441 tar.addfile(t, f)
1442 tar.close()
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001443
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001444 def _test(self, names=["bar"], fileobj=None):
1445 tar = tarfile.open(self.tarname, fileobj=fileobj)
1446 self.assertEqual(tar.getnames(), names)
1447
1448 def test_non_existing(self):
1449 self._add_testfile()
1450 self._test()
1451
1452 def test_empty(self):
Lars Gustäbeldd866d52009-11-22 18:30:53 +00001453 tarfile.open(self.tarname, "w:").close()
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001454 self._add_testfile()
1455 self._test()
1456
1457 def test_empty_fileobj(self):
Lars Gustäbeldd866d52009-11-22 18:30:53 +00001458 fobj = StringIO.StringIO("\0" * 1024)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001459 self._add_testfile(fobj)
1460 fobj.seek(0)
1461 self._test(fileobj=fobj)
1462
1463 def test_fileobj(self):
1464 self._create_testtar()
1465 data = open(self.tarname).read()
1466 fobj = StringIO.StringIO(data)
1467 self._add_testfile(fobj)
1468 fobj.seek(0)
1469 self._test(names=["foo", "bar"], fileobj=fobj)
1470
1471 def test_existing(self):
1472 self._create_testtar()
1473 self._add_testfile()
1474 self._test(names=["foo", "bar"])
1475
Zachary Ware1f702212013-12-10 14:09:20 -06001476 @unittest.skipUnless(gzip, 'requires gzip')
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001477 def test_append_gz(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001478 self._create_testtar("w:gz")
1479 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
1480
Zachary Ware1f702212013-12-10 14:09:20 -06001481 @unittest.skipUnless(bz2, 'requires bz2')
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001482 def test_append_bz2(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001483 self._create_testtar("w:bz2")
1484 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
1485
Lars Gustäbeldd866d52009-11-22 18:30:53 +00001486 # Append mode is supposed to fail if the tarfile to append to
1487 # does not end with a zero block.
1488 def _test_error(self, data):
1489 open(self.tarname, "wb").write(data)
1490 self.assertRaises(tarfile.ReadError, self._add_testfile)
1491
1492 def test_null(self):
1493 self._test_error("")
1494
1495 def test_incomplete(self):
1496 self._test_error("\0" * 13)
1497
1498 def test_premature_eof(self):
1499 data = tarfile.TarInfo("foo").tobuf()
1500 self._test_error(data)
1501
1502 def test_trailing_garbage(self):
1503 data = tarfile.TarInfo("foo").tobuf()
1504 self._test_error(data + "\0" * 13)
1505
1506 def test_invalid(self):
1507 self._test_error("a" * 512)
1508
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001509
1510class LimitsTest(unittest.TestCase):
1511
1512 def test_ustar_limits(self):
1513 # 100 char name
1514 tarinfo = tarfile.TarInfo("0123456789" * 10)
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001515 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001516
1517 # 101 char name that cannot be stored
1518 tarinfo = tarfile.TarInfo("0123456789" * 10 + "0")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001519 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001520
1521 # 256 char name with a slash at pos 156
1522 tarinfo = tarfile.TarInfo("123/" * 62 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001523 tarinfo.tobuf(tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001524
1525 # 256 char name that cannot be stored
1526 tarinfo = tarfile.TarInfo("1234567/" * 31 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001527 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001528
1529 # 512 char name
1530 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001531 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001532
1533 # 512 char linkname
1534 tarinfo = tarfile.TarInfo("longlink")
1535 tarinfo.linkname = "123/" * 126 + "longname"
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001536 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001537
1538 # uid > 8 digits
1539 tarinfo = tarfile.TarInfo("name")
1540 tarinfo.uid = 010000000
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001541 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001542
1543 def test_gnu_limits(self):
1544 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001545 tarinfo.tobuf(tarfile.GNU_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001546
1547 tarinfo = tarfile.TarInfo("longlink")
1548 tarinfo.linkname = "123/" * 126 + "longname"
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001549 tarinfo.tobuf(tarfile.GNU_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001550
1551 # uid >= 256 ** 7
1552 tarinfo = tarfile.TarInfo("name")
1553 tarinfo.uid = 04000000000000000000L
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001554 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.GNU_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001555
1556 def test_pax_limits(self):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001557 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001558 tarinfo.tobuf(tarfile.PAX_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001559
1560 tarinfo = tarfile.TarInfo("longlink")
1561 tarinfo.linkname = "123/" * 126 + "longname"
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001562 tarinfo.tobuf(tarfile.PAX_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001563
1564 tarinfo = tarfile.TarInfo("name")
1565 tarinfo.uid = 04000000000000000000L
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001566 tarinfo.tobuf(tarfile.PAX_FORMAT)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001567
1568
Lars Gustäbel64581042010-03-03 11:55:48 +00001569class ContextManagerTest(unittest.TestCase):
1570
1571 def test_basic(self):
1572 with tarfile.open(tarname) as tar:
1573 self.assertFalse(tar.closed, "closed inside runtime context")
1574 self.assertTrue(tar.closed, "context manager failed")
1575
1576 def test_closed(self):
1577 # The __enter__() method is supposed to raise IOError
1578 # if the TarFile object is already closed.
1579 tar = tarfile.open(tarname)
1580 tar.close()
1581 with self.assertRaises(IOError):
1582 with tar:
1583 pass
1584
1585 def test_exception(self):
1586 # Test if the IOError exception is passed through properly.
1587 with self.assertRaises(Exception) as exc:
1588 with tarfile.open(tarname) as tar:
1589 raise IOError
1590 self.assertIsInstance(exc.exception, IOError,
1591 "wrong exception raised in context manager")
1592 self.assertTrue(tar.closed, "context manager failed")
1593
1594 def test_no_eof(self):
1595 # __exit__() must not write end-of-archive blocks if an
1596 # exception was raised.
1597 try:
1598 with tarfile.open(tmpname, "w") as tar:
1599 raise Exception
1600 except:
1601 pass
1602 self.assertEqual(os.path.getsize(tmpname), 0,
1603 "context manager wrote an end-of-archive block")
1604 self.assertTrue(tar.closed, "context manager failed")
1605
1606 def test_eof(self):
1607 # __exit__() must write end-of-archive blocks, i.e. call
1608 # TarFile.close() if there was no error.
1609 with tarfile.open(tmpname, "w"):
1610 pass
1611 self.assertNotEqual(os.path.getsize(tmpname), 0,
1612 "context manager wrote no end-of-archive block")
1613
1614 def test_fileobj(self):
1615 # Test that __exit__() did not close the external file
1616 # object.
1617 fobj = open(tmpname, "wb")
1618 try:
1619 with tarfile.open(fileobj=fobj, mode="w") as tar:
1620 raise Exception
1621 except:
1622 pass
1623 self.assertFalse(fobj.closed, "external file object was closed")
1624 self.assertTrue(tar.closed, "context manager failed")
1625 fobj.close()
1626
1627
Lars Gustäbel4da7d412010-06-03 12:34:14 +00001628class LinkEmulationTest(ReadTest):
1629
1630 # Test for issue #8741 regression. On platforms that do not support
1631 # symbolic or hard links tarfile tries to extract these types of members as
1632 # the regular files they point to.
1633 def _test_link_extraction(self, name):
1634 self.tar.extract(name, TEMPDIR)
1635 data = open(os.path.join(TEMPDIR, name), "rb").read()
1636 self.assertEqual(md5sum(data), md5_regtype)
1637
1638 def test_hardlink_extraction1(self):
1639 self._test_link_extraction("ustar/lnktype")
1640
1641 def test_hardlink_extraction2(self):
1642 self._test_link_extraction("./ustar/linktest2/lnktype")
1643
1644 def test_symlink_extraction1(self):
1645 self._test_link_extraction("ustar/symtype")
1646
1647 def test_symlink_extraction2(self):
1648 self._test_link_extraction("./ustar/linktest2/symtype")
1649
1650
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001651class GzipMiscReadTest(MiscReadTest):
1652 tarname = gzipname
1653 mode = "r:gz"
Serhiy Storchaka75ba21a2014-01-18 15:35:19 +02001654 taropen = tarfile.TarFile.gzopen
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001655class GzipUstarReadTest(UstarReadTest):
1656 tarname = gzipname
1657 mode = "r:gz"
1658class GzipStreamReadTest(StreamReadTest):
1659 tarname = gzipname
1660 mode = "r|gz"
1661class GzipWriteTest(WriteTest):
1662 mode = "w:gz"
1663class GzipStreamWriteTest(StreamWriteTest):
1664 mode = "w|gz"
1665
1666
1667class Bz2MiscReadTest(MiscReadTest):
1668 tarname = bz2name
1669 mode = "r:bz2"
Serhiy Storchaka75ba21a2014-01-18 15:35:19 +02001670 taropen = tarfile.TarFile.bz2open
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001671class Bz2UstarReadTest(UstarReadTest):
1672 tarname = bz2name
1673 mode = "r:bz2"
1674class Bz2StreamReadTest(StreamReadTest):
1675 tarname = bz2name
1676 mode = "r|bz2"
1677class Bz2WriteTest(WriteTest):
1678 mode = "w:bz2"
1679class Bz2StreamWriteTest(StreamWriteTest):
1680 mode = "w|bz2"
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001681
Lars Gustäbel2020a592009-03-22 20:09:33 +00001682class Bz2PartialReadTest(unittest.TestCase):
1683 # Issue5068: The _BZ2Proxy.read() method loops forever
1684 # on an empty or partial bzipped file.
1685
1686 def _test_partial_input(self, mode):
1687 class MyStringIO(StringIO.StringIO):
1688 hit_eof = False
1689 def read(self, n):
1690 if self.hit_eof:
1691 raise AssertionError("infinite loop detected in tarfile.open()")
1692 self.hit_eof = self.pos == self.len
1693 return StringIO.StringIO.read(self, n)
Lars Gustäbeldd866d52009-11-22 18:30:53 +00001694 def seek(self, *args):
1695 self.hit_eof = False
1696 return StringIO.StringIO.seek(self, *args)
Lars Gustäbel2020a592009-03-22 20:09:33 +00001697
1698 data = bz2.compress(tarfile.TarInfo("foo").tobuf())
1699 for x in range(len(data) + 1):
Lars Gustäbeldd866d52009-11-22 18:30:53 +00001700 try:
1701 tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode)
1702 except tarfile.ReadError:
1703 pass # we have no interest in ReadErrors
Lars Gustäbel2020a592009-03-22 20:09:33 +00001704
1705 def test_partial_input(self):
1706 self._test_partial_input("r")
1707
1708 def test_partial_input_bz2(self):
1709 self._test_partial_input("r:bz2")
1710
1711
Neal Norwitz996acf12003-02-17 14:51:41 +00001712def test_main():
Antoine Pitrou310c9fe2009-11-11 20:55:07 +00001713 os.makedirs(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001714
Walter Dörwald21d3a322003-05-01 17:45:56 +00001715 tests = [
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001716 UstarReadTest,
1717 MiscReadTest,
1718 StreamReadTest,
1719 DetectReadTest,
1720 MemberReadTest,
1721 GNUReadTest,
1722 PaxReadTest,
Serhiy Storchakacfc2c7b2014-02-05 20:55:13 +02001723 ListTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001724 WriteTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001725 StreamWriteTest,
1726 GNUWriteTest,
1727 PaxWriteTest,
Lars Gustäbela0fcb932007-05-27 19:49:30 +00001728 UstarUnicodeTest,
1729 GNUUnicodeTest,
1730 PaxUnicodeTest,
Lars Gustäbel3f8aca12007-02-06 18:38:13 +00001731 AppendTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001732 LimitsTest,
Lars Gustäbel64581042010-03-03 11:55:48 +00001733 ContextManagerTest,
Walter Dörwald21d3a322003-05-01 17:45:56 +00001734 ]
1735
Neal Norwitza4f651a2004-07-20 22:07:44 +00001736 if hasattr(os, "link"):
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001737 tests.append(HardlinkTest)
Lars Gustäbel4da7d412010-06-03 12:34:14 +00001738 else:
1739 tests.append(LinkEmulationTest)
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001740
1741 fobj = open(tarname, "rb")
1742 data = fobj.read()
1743 fobj.close()
Neal Norwitza4f651a2004-07-20 22:07:44 +00001744
Walter Dörwald21d3a322003-05-01 17:45:56 +00001745 if gzip:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001746 # Create testtar.tar.gz and add gzip-specific tests.
1747 tar = gzip.open(gzipname, "wb")
1748 tar.write(data)
1749 tar.close()
1750
1751 tests += [
1752 GzipMiscReadTest,
1753 GzipUstarReadTest,
1754 GzipStreamReadTest,
Serhiy Storchakacfc2c7b2014-02-05 20:55:13 +02001755 GzipListTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001756 GzipWriteTest,
1757 GzipStreamWriteTest,
1758 ]
Walter Dörwald21d3a322003-05-01 17:45:56 +00001759
1760 if bz2:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001761 # Create testtar.tar.bz2 and add bz2-specific tests.
1762 tar = bz2.BZ2File(bz2name, "wb")
1763 tar.write(data)
1764 tar.close()
1765
1766 tests += [
1767 Bz2MiscReadTest,
1768 Bz2UstarReadTest,
1769 Bz2StreamReadTest,
Serhiy Storchakacfc2c7b2014-02-05 20:55:13 +02001770 Bz2ListTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001771 Bz2WriteTest,
1772 Bz2StreamWriteTest,
Lars Gustäbel2020a592009-03-22 20:09:33 +00001773 Bz2PartialReadTest,
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001774 ]
1775
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001776 try:
Walter Dörwald21d3a322003-05-01 17:45:56 +00001777 test_support.run_unittest(*tests)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001778 finally:
Lars Gustäbelc64e4022007-03-13 10:47:19 +00001779 if os.path.exists(TEMPDIR):
1780 shutil.rmtree(TEMPDIR)
Neal Norwitzb9ef4ae2003-01-05 23:19:43 +00001781
Neal Norwitz996acf12003-02-17 14:51:41 +00001782if __name__ == "__main__":
1783 test_main()