annotate Lib/test/test_tarfile.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents aff14bea5596
children a617a50d2766
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1 # -*- coding: iso-8859-15 -*-
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
2
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
3 import sys
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
4 import os
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
5 import shutil
38146
f61c892cadac Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents: 35804
diff changeset
6 import StringIO
41917
0e29bb80e227 Have md5 raise a DeprecationWarning as per PEP 4.
Brett Cannon <bcannon@gmail.com>
parents: 41897
diff changeset
7 from hashlib import md5
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
8 import errno
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
9
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
10 import unittest
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
11 import tarfile
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
12
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
13 from test import test_support
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
14
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
15 # Check for our compression modules.
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
16 try:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
17 import gzip
28598
3cf26b9f96f7 Get test working if gzip support is not available
Neal Norwitz <nnorwitz@gmail.com>
parents: 28216
diff changeset
18 gzip.GzipFile
3cf26b9f96f7 Get test working if gzip support is not available
Neal Norwitz <nnorwitz@gmail.com>
parents: 28216
diff changeset
19 except (ImportError, AttributeError):
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
20 gzip = None
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
21 try:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
22 import bz2
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
23 except ImportError:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
24 bz2 = None
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
25
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
26 def md5sum(data):
41917
0e29bb80e227 Have md5 raise a DeprecationWarning as per PEP 4.
Brett Cannon <bcannon@gmail.com>
parents: 41897
diff changeset
27 return md5(data).hexdigest()
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
28
57193
43f32db1bf46 Merged revisions 76217 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52268
diff changeset
29 TEMPDIR = os.path.abspath(test_support.TESTFN)
43f32db1bf46 Merged revisions 76217 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52268
diff changeset
30 tarname = test_support.findfile("testtar.tar")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
31 gzipname = os.path.join(TEMPDIR, "testtar.tar.gz")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
32 bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
33 tmpname = os.path.join(TEMPDIR, "tmp.tar")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
34
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
35 md5_regtype = "65f477c818ad9e15f7feab0c6d37742f"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
36 md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
37
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
38
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
39 class ReadTest(unittest.TestCase):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
40
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
41 tarname = tarname
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
42 mode = "r:"
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
43
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
44 def setUp(self):
41156
a3419c6c0cfe Quick fix for tests that fail on systems with an encoding other
Lars Gustäbel <lars@gustaebel.de>
parents: 41153
diff changeset
45 self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
46
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
47 def tearDown(self):
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
48 self.tar.close()
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
49
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
50
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
51 class UstarReadTest(ReadTest):
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
52
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
53 def test_fileobj_regular_file(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
54 tarinfo = self.tar.getmember("ustar/regtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
55 fobj = self.tar.extractfile(tarinfo)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
56 data = fobj.read()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
57 self.assertTrue((len(data), md5sum(data)) == (tarinfo.size, md5_regtype),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
58 "regular file extraction failed")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
59
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
60 def test_fileobj_readlines(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
61 self.tar.extract("ustar/regtype", TEMPDIR)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
62 tarinfo = self.tar.getmember("ustar/regtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
63 fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "rU")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
64 fobj2 = self.tar.extractfile(tarinfo)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
65
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
66 lines1 = fobj1.readlines()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
67 lines2 = fobj2.readlines()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
68 self.assertTrue(lines1 == lines2,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
69 "fileobj.readlines() failed")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
70 self.assertTrue(len(lines2) == 114,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
71 "fileobj.readlines() failed")
59777
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
72 self.assertTrue(lines2[83] ==
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
73 "I will gladly admit that Python is not the fastest running scripting language.\n",
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
74 "fileobj.readlines() failed")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
75
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
76 def test_fileobj_iter(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
77 self.tar.extract("ustar/regtype", TEMPDIR)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
78 tarinfo = self.tar.getmember("ustar/regtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
79 fobj1 = open(os.path.join(TEMPDIR, "ustar/regtype"), "rU")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
80 fobj2 = self.tar.extractfile(tarinfo)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
81 lines1 = fobj1.readlines()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
82 lines2 = [line for line in fobj2]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
83 self.assertTrue(lines1 == lines2,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
84 "fileobj.__iter__() failed")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
85
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
86 def test_fileobj_seek(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
87 self.tar.extract("ustar/regtype", TEMPDIR)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
88 fobj = open(os.path.join(TEMPDIR, "ustar/regtype"), "rb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
89 data = fobj.read()
35804
76a6e908624d Patch #1338314, Bug #1336623: fix tarfile so it can extract
Neal Norwitz <nnorwitz@gmail.com>
parents: 35735
diff changeset
90 fobj.close()
34649
13a950a3624b Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents: 33825
diff changeset
91
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
92 tarinfo = self.tar.getmember("ustar/regtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
93 fobj = self.tar.extractfile(tarinfo)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
94
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
95 text = fobj.read()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
96 fobj.seek(0)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
97 self.assertTrue(0 == fobj.tell(),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
98 "seek() to file's start failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
99 fobj.seek(2048, 0)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
100 self.assertTrue(2048 == fobj.tell(),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
101 "seek() to absolute position failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
102 fobj.seek(-1024, 1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
103 self.assertTrue(1024 == fobj.tell(),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
104 "seek() to negative relative position failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
105 fobj.seek(1024, 1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
106 self.assertTrue(2048 == fobj.tell(),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
107 "seek() to positive relative position failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
108 s = fobj.read(10)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
109 self.assertTrue(s == data[2048:2058],
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
110 "read() after seek failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
111 fobj.seek(0, 2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
112 self.assertTrue(tarinfo.size == fobj.tell(),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
113 "seek() to file's end failed")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
114 self.assertTrue(fobj.read() == "",
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
115 "read() at file's end did not return empty string")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
116 fobj.seek(-tarinfo.size, 2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
117 self.assertTrue(0 == fobj.tell(),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
118 "relative seek() to file's start failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
119 fobj.seek(512)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
120 s1 = fobj.readlines()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
121 fobj.seek(512)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
122 s2 = fobj.readlines()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
123 self.assertTrue(s1 == s2,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
124 "readlines() after seek failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
125 fobj.seek(0)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
126 self.assertTrue(len(fobj.readline()) == fobj.tell(),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
127 "tell() after readline() failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
128 fobj.seek(512)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
129 self.assertTrue(len(fobj.readline()) + 512 == fobj.tell(),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
130 "tell() after seek() and readline() failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
131 fobj.seek(0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
132 line = fobj.readline()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
133 self.assertTrue(fobj.read() == data[len(line):],
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
134 "read() after readline() failed")
35804
76a6e908624d Patch #1338314, Bug #1336623: fix tarfile so it can extract
Neal Norwitz <nnorwitz@gmail.com>
parents: 35735
diff changeset
135 fobj.close()
76a6e908624d Patch #1338314, Bug #1336623: fix tarfile so it can extract
Neal Norwitz <nnorwitz@gmail.com>
parents: 35735
diff changeset
136
62044
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
137 # Test if symbolic and hard links are resolved by extractfile(). The
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
138 # test link members each point to a regular member whose data is
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
139 # supposed to be exported.
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
140 def _test_fileobj_link(self, lnktype, regtype):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
141 a = self.tar.extractfile(lnktype)
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
142 b = self.tar.extractfile(regtype)
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
143 self.assertEqual(a.name, b.name)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
144
62044
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
145 def test_fileobj_link1(self):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
146 self._test_fileobj_link("ustar/lnktype", "ustar/regtype")
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
147
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
148 def test_fileobj_link2(self):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
149 self._test_fileobj_link("./ustar/linktest2/lnktype", "ustar/linktest1/regtype")
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
150
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
151 def test_fileobj_symlink1(self):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
152 self._test_fileobj_link("ustar/symtype", "ustar/regtype")
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
153
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
154 def test_fileobj_symlink2(self):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
155 self._test_fileobj_link("./ustar/linktest2/symtype", "ustar/linktest1/regtype")
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
156
76540
aff14bea5596 Issue #14160: TarFile.extractfile() failed to resolve symbolic links when
Lars Gustäbel <lars@gustaebel.de>
parents: 74121
diff changeset
157 def test_issue14160(self):
aff14bea5596 Issue #14160: TarFile.extractfile() failed to resolve symbolic links when
Lars Gustäbel <lars@gustaebel.de>
parents: 74121
diff changeset
158 self._test_fileobj_link("symtype2", "ustar/regtype")
aff14bea5596 Issue #14160: TarFile.extractfile() failed to resolve symbolic links when
Lars Gustäbel <lars@gustaebel.de>
parents: 74121
diff changeset
159
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
160
57393
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
161 class CommonReadTest(ReadTest):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
162
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
163 def test_empty_tarfile(self):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
164 # Test for issue6123: Allow opening empty archives.
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
165 # This test checks if tarfile.open() is able to open an empty tar
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
166 # archive successfully. Note that an empty tar archive is not the
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
167 # same as an empty file!
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
168 tarfile.open(tmpname, self.mode.replace("r", "w")).close()
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
169 try:
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
170 tar = tarfile.open(tmpname, self.mode)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
171 tar.getnames()
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
172 except tarfile.ReadError:
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
173 self.fail("tarfile.open() failed on empty archive")
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
174 self.assertListEqual(tar.getmembers(), [])
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
175
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
176 def test_null_tarfile(self):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
177 # Test for issue6123: Allow opening empty archives.
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
178 # This test guarantees that tarfile.open() does not treat an empty
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
179 # file as an empty tar archive.
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
180 open(tmpname, "wb").close()
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
181 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, self.mode)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
182 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
183
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
184 def test_ignore_zeros(self):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
185 # Test TarFile's ignore_zeros option.
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
186 if self.mode.endswith(":gz"):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
187 _open = gzip.GzipFile
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
188 elif self.mode.endswith(":bz2"):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
189 _open = bz2.BZ2File
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
190 else:
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
191 _open = open
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
192
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
193 for char in ('\0', 'a'):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
194 # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
195 # are ignored correctly.
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
196 fobj = _open(tmpname, "wb")
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
197 fobj.write(char * 1024)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
198 fobj.write(tarfile.TarInfo("foo").tobuf())
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
199 fobj.close()
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
200
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
201 tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
202 self.assertListEqual(tar.getnames(), ["foo"],
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
203 "ignore_zeros=True should have skipped the %r-blocks" % char)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
204 tar.close()
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
205
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
206
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
207 class MiscReadTest(CommonReadTest):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
208
42949
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
209 def test_no_name_argument(self):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
210 fobj = open(self.tarname, "rb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
211 tar = tarfile.open(fileobj=fobj, mode=self.mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
212 self.assertEqual(tar.name, os.path.abspath(fobj.name))
35804
76a6e908624d Patch #1338314, Bug #1336623: fix tarfile so it can extract
Neal Norwitz <nnorwitz@gmail.com>
parents: 35735
diff changeset
213
42949
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
214 def test_no_name_attribute(self):
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
215 data = open(self.tarname, "rb").read()
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
216 fobj = StringIO.StringIO(data)
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
217 self.assertRaises(AttributeError, getattr, fobj, "name")
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
218 tar = tarfile.open(fileobj=fobj, mode=self.mode)
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
219 self.assertEqual(tar.name, None)
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
220
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
221 def test_empty_name_attribute(self):
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
222 data = open(self.tarname, "rb").read()
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
223 fobj = StringIO.StringIO(data)
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
224 fobj.name = ""
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
225 tar = tarfile.open(fileobj=fobj, mode=self.mode)
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
226 self.assertEqual(tar.name, None)
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
227
44091
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
228 def test_fileobj_with_offset(self):
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
229 # Skip the first member and store values from the second member
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
230 # of the testtar.
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
231 tar = tarfile.open(self.tarname, mode=self.mode)
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
232 tar.next()
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
233 t = tar.next()
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
234 name = t.name
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
235 offset = t.offset
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
236 data = tar.extractfile(t).read()
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
237 tar.close()
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
238
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
239 # Open the testtar and seek to the offset of the second member.
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
240 if self.mode.endswith(":gz"):
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
241 _open = gzip.GzipFile
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
242 elif self.mode.endswith(":bz2"):
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
243 _open = bz2.BZ2File
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
244 else:
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
245 _open = open
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
246 fobj = _open(self.tarname, "rb")
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
247 fobj.seek(offset)
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
248
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
249 # Test if the tarfile starts with the second member.
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
250 tar = tar.open(self.tarname, mode="r:", fileobj=fobj)
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
251 t = tar.next()
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
252 self.assertEqual(t.name, name)
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
253 # Read to the end of fileobj and test if seeking back to the
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
254 # beginning works.
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
255 tar.getmembers()
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
256 self.assertEqual(tar.extractfile(t).read(), data,
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
257 "seek back did not work")
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
258 tar.close()
35804
76a6e908624d Patch #1338314, Bug #1336623: fix tarfile so it can extract
Neal Norwitz <nnorwitz@gmail.com>
parents: 35735
diff changeset
259
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
260 def test_fail_comp(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
261 # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
262 if self.mode == "r:":
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
263 return
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
264 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
265 fobj = open(tarname, "rb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
266 self.assertRaises(tarfile.ReadError, tarfile.open, fileobj=fobj, mode=self.mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
267
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
268 def test_v7_dirtype(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
269 # Test old style dirtype member (bug #1336623):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
270 # Old V7 tars create directory members using an AREGTYPE
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
271 # header with a "/" appended to the filename field.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
272 tarinfo = self.tar.getmember("misc/dirtype-old-v7")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
273 self.assertTrue(tarinfo.type == tarfile.DIRTYPE,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
274 "v7 dirtype failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
275
45285
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
276 def test_xstar_type(self):
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
277 # The xstar format stores extra atime and ctime fields inside the
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
278 # space reserved for the prefix field. The prefix field must be
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
279 # ignored in this case, otherwise it will mess up the name.
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
280 try:
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
281 self.tar.getmember("misc/regtype-xstar")
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
282 except KeyError:
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
283 self.fail("failed to find misc/regtype-xstar (mangled prefix?)")
35804
76a6e908624d Patch #1338314, Bug #1336623: fix tarfile so it can extract
Neal Norwitz <nnorwitz@gmail.com>
parents: 35735
diff changeset
284
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
285 def test_check_members(self):
41491
17e4363ef2b5 Fix directory names to have only one trailing slash.
Lars Gustäbel <lars@gustaebel.de>
parents: 40524
diff changeset
286 for tarinfo in self.tar:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
287 self.assertTrue(int(tarinfo.mtime) == 07606136617,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
288 "wrong mtime for %s" % tarinfo.name)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
289 if not tarinfo.name.startswith("ustar/"):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
290 continue
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
291 self.assertTrue(tarinfo.uname == "tarfile",
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
292 "wrong uname for %s" % tarinfo.name)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
293
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
294 def test_find_members(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
295 self.assertTrue(self.tar.getmembers()[-1].name == "misc/eof",
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
296 "could not find all members")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
297
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
298 def test_extract_hardlink(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
299 # Test hardlink extraction (e.g. bug #857297).
41156
a3419c6c0cfe Quick fix for tests that fail on systems with an encoding other
Lars Gustäbel <lars@gustaebel.de>
parents: 41153
diff changeset
300 tar = tarfile.open(tarname, errorlevel=1, encoding="iso8859-1")
35804
76a6e908624d Patch #1338314, Bug #1336623: fix tarfile so it can extract
Neal Norwitz <nnorwitz@gmail.com>
parents: 35735
diff changeset
301
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
302 tar.extract("ustar/regtype", TEMPDIR)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
303 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
304 tar.extract("ustar/lnktype", TEMPDIR)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
305 except EnvironmentError, e:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
306 if e.errno == errno.ENOENT:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
307 self.fail("hardlink not extracted properly")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
308
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
309 data = open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb").read()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
310 self.assertEqual(md5sum(data), md5_regtype)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
311
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
312 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
313 tar.extract("ustar/symtype", TEMPDIR)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
314 except EnvironmentError, e:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
315 if e.errno == errno.ENOENT:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
316 self.fail("symlink not extracted properly")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
317
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
318 data = open(os.path.join(TEMPDIR, "ustar/symtype"), "rb").read()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
319 self.assertEqual(md5sum(data), md5_regtype)
41491
17e4363ef2b5 Fix directory names to have only one trailing slash.
Lars Gustäbel <lars@gustaebel.de>
parents: 40524
diff changeset
320
44471
292c5e51a0e5 Issue #1735: TarFile.extractall() now correctly sets directory
Lars Gustäbel <lars@gustaebel.de>
parents: 44092
diff changeset
321 def test_extractall(self):
292c5e51a0e5 Issue #1735: TarFile.extractall() now correctly sets directory
Lars Gustäbel <lars@gustaebel.de>
parents: 44092
diff changeset
322 # Test if extractall() correctly restores directory permissions
292c5e51a0e5 Issue #1735: TarFile.extractall() now correctly sets directory
Lars Gustäbel <lars@gustaebel.de>
parents: 44092
diff changeset
323 # and times (see issue1735).
44470
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
324 tar = tarfile.open(tarname, encoding="iso8859-1")
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
325 directories = [t for t in tar if t.isdir()]
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
326 tar.extractall(TEMPDIR, directories)
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
327 for tarinfo in directories:
44471
292c5e51a0e5 Issue #1735: TarFile.extractall() now correctly sets directory
Lars Gustäbel <lars@gustaebel.de>
parents: 44092
diff changeset
328 path = os.path.join(TEMPDIR, tarinfo.name)
50130
f9b95294ac85 Merged revisions 67717 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 49255
diff changeset
329 if sys.platform != "win32":
f9b95294ac85 Merged revisions 67717 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 49255
diff changeset
330 # Win32 has no support for fine grained permissions.
f9b95294ac85 Merged revisions 67717 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 49255
diff changeset
331 self.assertEqual(tarinfo.mode & 0777, os.stat(path).st_mode & 0777)
44471
292c5e51a0e5 Issue #1735: TarFile.extractall() now correctly sets directory
Lars Gustäbel <lars@gustaebel.de>
parents: 44092
diff changeset
332 self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
292c5e51a0e5 Issue #1735: TarFile.extractall() now correctly sets directory
Lars Gustäbel <lars@gustaebel.de>
parents: 44092
diff changeset
333 tar.close()
292c5e51a0e5 Issue #1735: TarFile.extractall() now correctly sets directory
Lars Gustäbel <lars@gustaebel.de>
parents: 44092
diff changeset
334
57401
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
335 def test_init_close_fobj(self):
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
336 # Issue #7341: Close the internal file object in the TarFile
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
337 # constructor in case of an error. For the test we rely on
57399
de07880a53ff Add a testcase that checks if the TarFile constructor successfully
Lars Gustäbel <lars@gustaebel.de>
parents: 57393
diff changeset
338 # the fact that opening an empty file raises a ReadError.
de07880a53ff Add a testcase that checks if the TarFile constructor successfully
Lars Gustäbel <lars@gustaebel.de>
parents: 57393
diff changeset
339 empty = os.path.join(TEMPDIR, "empty")
de07880a53ff Add a testcase that checks if the TarFile constructor successfully
Lars Gustäbel <lars@gustaebel.de>
parents: 57393
diff changeset
340 open(empty, "wb").write("")
57401
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
341
45282
790554ae2b04 Backport the nts() function from the trunk. This fixes problems with
Lars Gustäbel <lars@gustaebel.de>
parents: 44471
diff changeset
342 try:
57401
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
343 tar = object.__new__(tarfile.TarFile)
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
344 try:
57399
de07880a53ff Add a testcase that checks if the TarFile constructor successfully
Lars Gustäbel <lars@gustaebel.de>
parents: 57393
diff changeset
345 tar.__init__(empty)
57401
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
346 except tarfile.ReadError:
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
347 self.assertTrue(tar.fileobj.closed)
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
348 else:
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
349 self.fail("ReadError not raised")
56f19081d7bc Merged revisions 76452 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57193
diff changeset
350 finally:
57399
de07880a53ff Add a testcase that checks if the TarFile constructor successfully
Lars Gustäbel <lars@gustaebel.de>
parents: 57393
diff changeset
351 os.remove(empty)
45282
790554ae2b04 Backport the nts() function from the trunk. This fixes problems with
Lars Gustäbel <lars@gustaebel.de>
parents: 44471
diff changeset
352
41491
17e4363ef2b5 Fix directory names to have only one trailing slash.
Lars Gustäbel <lars@gustaebel.de>
parents: 40524
diff changeset
353
57393
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
354 class StreamReadTest(CommonReadTest):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
355
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
356 mode="r|"
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
357
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
358 def test_fileobj_regular_file(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
359 tarinfo = self.tar.next() # get "regtype" (can't use getmember)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
360 fobj = self.tar.extractfile(tarinfo)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
361 data = fobj.read()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
362 self.assertTrue((len(data), md5sum(data)) == (tarinfo.size, md5_regtype),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
363 "regular file extraction failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
364
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
365 def test_provoke_stream_error(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
366 tarinfos = self.tar.getmembers()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
367 f = self.tar.extractfile(tarinfos[0]) # read the first member
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
368 self.assertRaises(tarfile.StreamError, f.read)
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
369
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
370 def test_compare_members(self):
41156
a3419c6c0cfe Quick fix for tests that fail on systems with an encoding other
Lars Gustäbel <lars@gustaebel.de>
parents: 41153
diff changeset
371 tar1 = tarfile.open(tarname, encoding="iso8859-1")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
372 tar2 = self.tar
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
373
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
374 while True:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
375 t1 = tar1.next()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
376 t2 = tar2.next()
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
377 if t1 is None:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
378 break
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
379 self.assertTrue(t2 is not None, "stream.next() failed.")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
380
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
381 if t2.islnk() or t2.issym():
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
382 self.assertRaises(tarfile.StreamError, tar2.extractfile, t2)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
383 continue
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
384
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
385 v1 = tar1.extractfile(t1)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
386 v2 = tar2.extractfile(t2)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
387 if v1 is None:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
388 continue
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
389 self.assertTrue(v2 is not None, "stream.extractfile() failed")
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
390 self.assertTrue(v1.read() == v2.read(), "stream extraction failed")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
391
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
392 tar1.close()
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
393
38185
ad8b76e99261 [ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents: 38146
diff changeset
394
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
395 class DetectReadTest(unittest.TestCase):
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
396
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
397 def _testfunc_file(self, name, mode):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
398 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
399 tarfile.open(name, mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
400 except tarfile.ReadError:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
401 self.fail()
34697
79302e50910e Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents: 34649
diff changeset
402
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
403 def _testfunc_fileobj(self, name, mode):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
404 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
405 tarfile.open(name, mode, fileobj=open(name, "rb"))
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
406 except tarfile.ReadError:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
407 self.fail()
38185
ad8b76e99261 [ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents: 38146
diff changeset
408
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
409 def _test_modes(self, testfunc):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
410 testfunc(tarname, "r")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
411 testfunc(tarname, "r:")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
412 testfunc(tarname, "r:*")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
413 testfunc(tarname, "r|")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
414 testfunc(tarname, "r|*")
38185
ad8b76e99261 [ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents: 38146
diff changeset
415
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
416 if gzip:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
417 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:gz")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
418 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|gz")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
419 self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r:")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
420 self.assertRaises(tarfile.ReadError, tarfile.open, gzipname, mode="r|")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
421
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
422 testfunc(gzipname, "r")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
423 testfunc(gzipname, "r:*")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
424 testfunc(gzipname, "r:gz")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
425 testfunc(gzipname, "r|*")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
426 testfunc(gzipname, "r|gz")
38185
ad8b76e99261 [ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents: 38146
diff changeset
427
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
428 if bz2:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
429 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r:bz2")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
430 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, mode="r|bz2")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
431 self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r:")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
432 self.assertRaises(tarfile.ReadError, tarfile.open, bz2name, mode="r|")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
433
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
434 testfunc(bz2name, "r")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
435 testfunc(bz2name, "r:*")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
436 testfunc(bz2name, "r:bz2")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
437 testfunc(bz2name, "r|*")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
438 testfunc(bz2name, "r|bz2")
40519
6763e45c7f7d Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents: 40517
diff changeset
439
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
440 def test_detect_file(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
441 self._test_modes(self._testfunc_file)
40519
6763e45c7f7d Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents: 40517
diff changeset
442
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
443 def test_detect_fileobj(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
444 self._test_modes(self._testfunc_fileobj)
40519
6763e45c7f7d Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents: 40517
diff changeset
445
73870
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
446 def test_detect_stream_bz2(self):
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
447 # Originally, tarfile's stream detection looked for the string
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
448 # "BZh91" at the start of the file. This is incorrect because
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
449 # the '9' represents the blocksize (900kB). If the file was
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
450 # compressed using another blocksize autodetection fails.
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
451 if not bz2:
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
452 return
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
453
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
454 with open(tarname, "rb") as fobj:
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
455 data = fobj.read()
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
456
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
457 # Compress with blocksize 100kB, the file starts with "BZh11".
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
458 with bz2.BZ2File(tmpname, "wb", compresslevel=1) as fobj:
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
459 fobj.write(data)
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
460
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
461 self._testfunc_file(tmpname, "r|*")
6d1a91e9f506 Correctly detect bzip2 compressed streams with blocksizes other than 900k.
Lars Gustäbel <lars@gustaebel.de>
parents: 70166
diff changeset
462
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
463
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
464 class MemberReadTest(ReadTest):
38185
ad8b76e99261 [ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents: 38146
diff changeset
465
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
466 def _test_member(self, tarinfo, chksum=None, **kwargs):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
467 if chksum is not None:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
468 self.assertTrue(md5sum(self.tar.extractfile(tarinfo).read()) == chksum,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
469 "wrong md5sum for %s" % tarinfo.name)
34697
79302e50910e Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents: 34649
diff changeset
470
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
471 kwargs["mtime"] = 07606136617
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
472 kwargs["uid"] = 1000
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
473 kwargs["gid"] = 100
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
474 if "old-v7" not in tarinfo.name:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
475 # V7 tar can't handle alphabetic owners.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
476 kwargs["uname"] = "tarfile"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
477 kwargs["gname"] = "tarfile"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
478 for k, v in kwargs.iteritems():
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
479 self.assertTrue(getattr(tarinfo, k) == v,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
480 "wrong value in %s field of %s" % (k, tarinfo.name))
34697
79302e50910e Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents: 34649
diff changeset
481
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
482 def test_find_regtype(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
483 tarinfo = self.tar.getmember("ustar/regtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
484 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
485
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
486 def test_find_conttype(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
487 tarinfo = self.tar.getmember("ustar/conttype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
488 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
34697
79302e50910e Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents: 34649
diff changeset
489
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
490 def test_find_dirtype(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
491 tarinfo = self.tar.getmember("ustar/dirtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
492 self._test_member(tarinfo, size=0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
493
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
494 def test_find_dirtype_with_size(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
495 tarinfo = self.tar.getmember("ustar/dirtype-with-size")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
496 self._test_member(tarinfo, size=255)
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
497
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
498 def test_find_lnktype(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
499 tarinfo = self.tar.getmember("ustar/lnktype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
500 self._test_member(tarinfo, size=0, linkname="ustar/regtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
501
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
502 def test_find_symtype(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
503 tarinfo = self.tar.getmember("ustar/symtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
504 self._test_member(tarinfo, size=0, linkname="regtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
505
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
506 def test_find_blktype(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
507 tarinfo = self.tar.getmember("ustar/blktype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
508 self._test_member(tarinfo, size=0, devmajor=3, devminor=0)
34697
79302e50910e Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents: 34649
diff changeset
509
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
510 def test_find_chrtype(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
511 tarinfo = self.tar.getmember("ustar/chrtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
512 self._test_member(tarinfo, size=0, devmajor=1, devminor=3)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
513
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
514 def test_find_fifotype(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
515 tarinfo = self.tar.getmember("ustar/fifotype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
516 self._test_member(tarinfo, size=0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
517
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
518 def test_find_sparse(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
519 tarinfo = self.tar.getmember("ustar/sparse")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
520 self._test_member(tarinfo, size=86016, chksum=md5_sparse)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
521
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
522 def test_find_umlauts(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
523 tarinfo = self.tar.getmember("ustar/umlauts-ÄÖÜäöüß")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
524 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
525
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
526 def test_find_ustar_longname(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
527 name = "ustar/" + "12345/" * 39 + "1234567/longname"
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
528 self.assertIn(name, self.tar.getnames())
44092
b21b1c4f0439 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42950
diff changeset
529
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
530 def test_find_regtype_oldv7(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
531 tarinfo = self.tar.getmember("misc/regtype-old-v7")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
532 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
533
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
534 def test_find_pax_umlauts(self):
41156
a3419c6c0cfe Quick fix for tests that fail on systems with an encoding other
Lars Gustäbel <lars@gustaebel.de>
parents: 41153
diff changeset
535 self.tar = tarfile.open(self.tarname, mode=self.mode, encoding="iso8859-1")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
536 tarinfo = self.tar.getmember("pax/umlauts-ÄÖÜäöüß")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
537 self._test_member(tarinfo, size=7011, chksum=md5_regtype)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
538
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
539
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
540 class LongnameTest(ReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
541
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
542 def test_read_longname(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
543 # Test reading of longname (bug #1471427).
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
544 longname = self.subdir + "/" + "123/" * 125 + "longname"
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
545 try:
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
546 tarinfo = self.tar.getmember(longname)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
547 except KeyError:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
548 self.fail("longname not found")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
549 self.assertTrue(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype")
44092
b21b1c4f0439 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42950
diff changeset
550
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
551 def test_read_longlink(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
552 longname = self.subdir + "/" + "123/" * 125 + "longname"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
553 longlink = self.subdir + "/" + "123/" * 125 + "longlink"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
554 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
555 tarinfo = self.tar.getmember(longlink)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
556 except KeyError:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
557 self.fail("longlink not found")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
558 self.assertTrue(tarinfo.linkname == longname, "linkname wrong")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
559
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
560 def test_truncated_longname(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
561 longname = self.subdir + "/" + "123/" * 125 + "longname"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
562 tarinfo = self.tar.getmember(longname)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
563 offset = tarinfo.offset
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
564 self.tar.fileobj.seek(offset)
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
565 fobj = StringIO.StringIO(self.tar.fileobj.read(3 * 512))
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
566 self.assertRaises(tarfile.ReadError, tarfile.open, name="foo.tar", fileobj=fobj)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
567
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
568 def test_header_offset(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
569 # Test if the start offset of the TarInfo object includes
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
570 # the preceding extended header.
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
571 longname = self.subdir + "/" + "123/" * 125 + "longname"
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
572 offset = self.tar.getmember(longname).offset
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
573 fobj = open(tarname)
44092
b21b1c4f0439 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42950
diff changeset
574 fobj.seek(offset)
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
575 tarinfo = tarfile.TarInfo.frombuf(fobj.read(512))
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
576 self.assertEqual(tarinfo.type, self.longnametype)
44092
b21b1c4f0439 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42950
diff changeset
577
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
578
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
579 class GNUReadTest(LongnameTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
580
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
581 subdir = "gnu"
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
582 longnametype = tarfile.GNUTYPE_LONGNAME
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
583
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
584 def test_sparse_file(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
585 tarinfo1 = self.tar.getmember("ustar/sparse")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
586 fobj1 = self.tar.extractfile(tarinfo1)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
587 tarinfo2 = self.tar.getmember("gnu/sparse")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
588 fobj2 = self.tar.extractfile(tarinfo2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
589 self.assertTrue(fobj1.read() == fobj2.read(),
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
590 "sparse file extraction failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
591
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
592
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
593 class PaxReadTest(LongnameTest):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
594
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
595 subdir = "pax"
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
596 longnametype = tarfile.XHDTYPE
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
597
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
598 def test_pax_global_headers(self):
41156
a3419c6c0cfe Quick fix for tests that fail on systems with an encoding other
Lars Gustäbel <lars@gustaebel.de>
parents: 41153
diff changeset
599 tar = tarfile.open(tarname, encoding="iso8859-1")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
600
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
601 tarinfo = tar.getmember("pax/regtype1")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
602 self.assertEqual(tarinfo.uname, "foo")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
603 self.assertEqual(tarinfo.gname, "bar")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
604 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
44092
b21b1c4f0439 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42950
diff changeset
605
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
606 tarinfo = tar.getmember("pax/regtype2")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
607 self.assertEqual(tarinfo.uname, "")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
608 self.assertEqual(tarinfo.gname, "bar")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
609 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
610
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
611 tarinfo = tar.getmember("pax/regtype3")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
612 self.assertEqual(tarinfo.uname, "tarfile")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
613 self.assertEqual(tarinfo.gname, "tarfile")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
614 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
615
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
616 def test_pax_number_fields(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
617 # All following number fields are read from the pax header.
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
618 tar = tarfile.open(tarname, encoding="iso8859-1")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
619 tarinfo = tar.getmember("pax/regtype4")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
620 self.assertEqual(tarinfo.size, 7011)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
621 self.assertEqual(tarinfo.uid, 123)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
622 self.assertEqual(tarinfo.gid, 123)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
623 self.assertEqual(tarinfo.mtime, 1041808783.0)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
624 self.assertEqual(type(tarinfo.mtime), float)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
625 self.assertEqual(float(tarinfo.pax_headers["atime"]), 1041808783.0)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
626 self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
627
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
628
47352
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
629 class WriteTestBase(unittest.TestCase):
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
630 # Put all write tests in here that are supposed to be tested
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
631 # in all possible mode combinations.
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
632
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
633 def test_fileobj_no_close(self):
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
634 fobj = StringIO.StringIO()
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
635 tar = tarfile.open(fileobj=fobj, mode=self.mode)
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
636 tar.addfile(tarfile.TarInfo("foo"))
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
637 tar.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
638 self.assertTrue(fobj.closed is False, "external fileobjs must never closed")
47352
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
639
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
640
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
641 class WriteTest(WriteTestBase):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
642
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
643 mode = "w:"
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
644
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
645 def test_100_char_name(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
646 # The name field in a tar header stores strings of at most 100 chars.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
647 # If a string is shorter than 100 chars it has to be padded with '\0',
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
648 # which implies that a string of exactly 100 chars is stored without
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
649 # a trailing '\0'.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
650 name = "0123456789" * 10
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
651 tar = tarfile.open(tmpname, self.mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
652 t = tarfile.TarInfo(name)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
653 tar.addfile(t)
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
654 tar.close()
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
655
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
656 tar = tarfile.open(tmpname)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
657 self.assertTrue(tar.getnames()[0] == name,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
658 "failed to store 100 char filename")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
659 tar.close()
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
660
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
661 def test_tar_size(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
662 # Test for bug #1013882.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
663 tar = tarfile.open(tmpname, self.mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
664 path = os.path.join(TEMPDIR, "file")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
665 fobj = open(path, "wb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
666 fobj.write("aaa")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
667 fobj.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
668 tar.add(path)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
669 tar.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
670 self.assertTrue(os.path.getsize(tmpname) > 0,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
671 "tarfile is empty")
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
672
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
673 # The test_*_size tests test for bug #1167128.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
674 def test_file_size(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
675 tar = tarfile.open(tmpname, self.mode)
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
676
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
677 path = os.path.join(TEMPDIR, "file")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
678 fobj = open(path, "wb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
679 fobj.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
680 tarinfo = tar.gettarinfo(path)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
681 self.assertEqual(tarinfo.size, 0)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
682
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
683 fobj = open(path, "wb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
684 fobj.write("aaa")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
685 fobj.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
686 tarinfo = tar.gettarinfo(path)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
687 self.assertEqual(tarinfo.size, 3)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
688
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
689 tar.close()
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
690
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
691 def test_directory_size(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
692 path = os.path.join(TEMPDIR, "directory")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
693 os.mkdir(path)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
694 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
695 tar = tarfile.open(tmpname, self.mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
696 tarinfo = tar.gettarinfo(path)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
697 self.assertEqual(tarinfo.size, 0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
698 finally:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
699 os.rmdir(path)
33234
ab34980ffa16 Flush bz2 data even if nothing had been written so far. Fixes #1013882.
Martin v. Löwis <martin@v.loewis.de>
parents: 32769
diff changeset
700
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
701 def test_link_size(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
702 if hasattr(os, "link"):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
703 link = os.path.join(TEMPDIR, "link")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
704 target = os.path.join(TEMPDIR, "link_target")
62041
ed983b116a7d Merged revisions 81663 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 61090
diff changeset
705 fobj = open(target, "wb")
ed983b116a7d Merged revisions 81663 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 61090
diff changeset
706 fobj.write("aaa")
ed983b116a7d Merged revisions 81663 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 61090
diff changeset
707 fobj.close()
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
708 os.link(target, link)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
709 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
710 tar = tarfile.open(tmpname, self.mode)
62041
ed983b116a7d Merged revisions 81663 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 61090
diff changeset
711 # Record the link target in the inodes list.
ed983b116a7d Merged revisions 81663 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 61090
diff changeset
712 tar.gettarinfo(target)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
713 tarinfo = tar.gettarinfo(link)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
714 self.assertEqual(tarinfo.size, 0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
715 finally:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
716 os.remove(target)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
717 os.remove(link)
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
718
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
719 def test_symlink_size(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
720 if hasattr(os, "symlink"):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
721 path = os.path.join(TEMPDIR, "symlink")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
722 os.symlink("link_target", path)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
723 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
724 tar = tarfile.open(tmpname, self.mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
725 tarinfo = tar.gettarinfo(path)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
726 self.assertEqual(tarinfo.size, 0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
727 finally:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
728 os.remove(path)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
729
40520
ef4b8fcd14d7 Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents: 40518
diff changeset
730 def test_add_self(self):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
731 # Test for #1257255.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
732 dstname = os.path.abspath(tmpname)
40520
ef4b8fcd14d7 Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents: 40518
diff changeset
733
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
734 tar = tarfile.open(tmpname, self.mode)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
735 self.assertTrue(tar.name == dstname, "archive name must be absolute")
40520
ef4b8fcd14d7 Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents: 40518
diff changeset
736
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
737 tar.add(dstname)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
738 self.assertTrue(tar.getnames() == [], "added the archive to itself")
40520
ef4b8fcd14d7 Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents: 40518
diff changeset
739
ef4b8fcd14d7 Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents: 40518
diff changeset
740 cwd = os.getcwd()
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
741 os.chdir(TEMPDIR)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
742 tar.add(dstname)
40520
ef4b8fcd14d7 Patch #1262036: Prevent TarFiles from being added to themselves under
Lars Gustäbel <lars@gustaebel.de>
parents: 40518
diff changeset
743 os.chdir(cwd)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
744 self.assertTrue(tar.getnames() == [], "added the archive to itself")
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
745
42109
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
746 def test_exclude(self):
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
747 tempdir = os.path.join(TEMPDIR, "exclude")
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
748 os.mkdir(tempdir)
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
749 try:
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
750 for name in ("foo", "bar", "baz"):
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
751 name = os.path.join(tempdir, name)
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
752 open(name, "wb").close()
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
753
59777
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
754 exclude = os.path.isfile
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
755
42109
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
756 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
59777
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
757 with test_support.check_warnings(("use the filter argument",
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
758 DeprecationWarning)):
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
759 tar.add(tempdir, arcname="empty_dir", exclude=exclude)
42109
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
760 tar.close()
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
761
42109
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
762 tar = tarfile.open(tmpname, "r")
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
763 self.assertEqual(len(tar.getmembers()), 1)
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
764 self.assertEqual(tar.getnames()[0], "empty_dir")
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
765 finally:
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
766 shutil.rmtree(tempdir)
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
767
55916
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
768 def test_filter(self):
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
769 tempdir = os.path.join(TEMPDIR, "filter")
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
770 os.mkdir(tempdir)
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
771 try:
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
772 for name in ("foo", "bar", "baz"):
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
773 name = os.path.join(tempdir, name)
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
774 open(name, "wb").close()
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
775
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
776 def filter(tarinfo):
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
777 if os.path.basename(tarinfo.name) == "bar":
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
778 return
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
779 tarinfo.uid = 123
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
780 tarinfo.uname = "foo"
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
781 return tarinfo
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
782
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
783 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
784 tar.add(tempdir, arcname="empty_dir", filter=filter)
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
785 tar.close()
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
786
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
787 tar = tarfile.open(tmpname, "r")
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
788 for tarinfo in tar:
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
789 self.assertEqual(tarinfo.uid, 123)
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
790 self.assertEqual(tarinfo.uname, "foo")
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
791 self.assertEqual(len(tar.getmembers()), 3)
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
792 tar.close()
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
793 finally:
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
794 shutil.rmtree(tempdir)
ecd22850d201 Issue #6856: Add a filter keyword argument to TarFile.add().
Lars Gustäbel <lars@gustaebel.de>
parents: 55746
diff changeset
795
55746
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
796 # Guarantee that stored pathnames are not modified. Don't
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
797 # remove ./ or ../ or double slashes. Still make absolute
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
798 # pathnames relative.
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
799 # For details see bug #6054.
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
800 def _test_pathname(self, path, cmp_path=None, dir=False):
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
801 # Create a tarfile with an empty member named path
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
802 # and compare the stored name with the original.
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
803 foo = os.path.join(TEMPDIR, "foo")
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
804 if not dir:
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
805 open(foo, "w").close()
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
806 else:
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
807 os.mkdir(foo)
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
808
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
809 tar = tarfile.open(tmpname, self.mode)
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
810 tar.add(foo, arcname=path)
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
811 tar.close()
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
812
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
813 tar = tarfile.open(tmpname, "r")
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
814 t = tar.next()
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
815 tar.close()
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
816
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
817 if not dir:
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
818 os.remove(foo)
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
819 else:
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
820 os.rmdir(foo)
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
821
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
822 self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
823
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
824 def test_pathnames(self):
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
825 self._test_pathname("foo")
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
826 self._test_pathname(os.path.join("foo", ".", "bar"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
827 self._test_pathname(os.path.join("foo", "..", "bar"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
828 self._test_pathname(os.path.join(".", "foo"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
829 self._test_pathname(os.path.join(".", "foo", "."))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
830 self._test_pathname(os.path.join(".", "foo", ".", "bar"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
831 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
832 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
833 self._test_pathname(os.path.join("..", "foo"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
834 self._test_pathname(os.path.join("..", "foo", ".."))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
835 self._test_pathname(os.path.join("..", "foo", ".", "bar"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
836 self._test_pathname(os.path.join("..", "foo", "..", "bar"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
837
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
838 self._test_pathname("foo" + os.sep + os.sep + "bar")
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
839 self._test_pathname("foo" + os.sep + os.sep, "foo", dir=True)
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
840
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
841 def test_abs_pathnames(self):
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
842 if sys.platform == "win32":
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
843 self._test_pathname("C:\\foo", "foo")
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
844 else:
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
845 self._test_pathname("/foo", "foo")
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
846 self._test_pathname("///foo", "foo")
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
847
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
848 def test_cwd(self):
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
849 # Test adding the current working directory.
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
850 cwd = os.getcwd()
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
851 os.chdir(TEMPDIR)
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
852 try:
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
853 open("foo", "w").close()
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
854
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
855 tar = tarfile.open(tmpname, self.mode)
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
856 tar.add(".")
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
857 tar.close()
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
858
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
859 tar = tarfile.open(tmpname, "r")
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
860 for t in tar:
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
861 self.assert_(t.name == "." or t.name.startswith("./"))
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
862 tar.close()
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
863 finally:
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
864 os.chdir(cwd)
5f9e13066d36 Issue #6054: Do not normalize stored pathnames.
Lars Gustäbel <lars@gustaebel.de>
parents: 55002
diff changeset
865
69658
e19d2e3a3a58 skip the extractall test on platforms where os.symlink is not available.
Senthil Kumaran <senthil@uthcode.com>
parents: 69653
diff changeset
866 @unittest.skipUnless(hasattr(os, 'symlink'), "needs os.symlink")
69653
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
867 def test_extractall_symlinks(self):
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
868 # Test if extractall works properly when tarfile contains symlinks
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
869 tempdir = os.path.join(TEMPDIR, "testsymlinks")
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
870 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
871 os.mkdir(tempdir)
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
872 try:
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
873 source_file = os.path.join(tempdir,'source')
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
874 target_file = os.path.join(tempdir,'symlink')
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
875 with open(source_file,'w') as f:
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
876 f.write('something\n')
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
877 os.symlink(source_file, target_file)
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
878 tar = tarfile.open(temparchive,'w')
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
879 tar.add(source_file, arcname=os.path.basename(source_file))
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
880 tar.add(target_file, arcname=os.path.basename(target_file))
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
881 tar.close()
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
882 # Let's extract it to the location which contains the symlink
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
883 tar = tarfile.open(temparchive,'r')
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
884 # this should not raise OSError: [Errno 17] File exists
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
885 try:
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
886 tar.extractall(path=tempdir)
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
887 except OSError:
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
888 self.fail("extractall failed with symlinked files")
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
889 finally:
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
890 tar.close()
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
891 finally:
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
892 os.unlink(temparchive)
0c8bc3a0130a Fix closes issue10761: tarfile.extractall failure when symlinked files are present.
Senthil Kumaran <orsenthil@gmail.com>
parents: 68256
diff changeset
893 shutil.rmtree(tempdir)
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
894
70166
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
895 @unittest.skipUnless(hasattr(os, 'symlink'), "needs os.symlink")
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
896 def test_extractall_broken_symlinks(self):
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
897 # Test if extractall works properly when tarfile contains broken
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
898 # symlinks
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
899 tempdir = os.path.join(TEMPDIR, "testsymlinks")
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
900 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
901 os.mkdir(tempdir)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
902 try:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
903 source_file = os.path.join(tempdir,'source')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
904 target_file = os.path.join(tempdir,'symlink')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
905 with open(source_file,'w') as f:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
906 f.write('something\n')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
907 os.symlink(source_file, target_file)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
908 tar = tarfile.open(temparchive,'w')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
909 tar.add(target_file, arcname=os.path.basename(target_file))
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
910 tar.close()
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
911 # remove the real file
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
912 os.unlink(source_file)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
913 # Let's extract it to the location which contains the symlink
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
914 tar = tarfile.open(temparchive,'r')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
915 # this should not raise OSError: [Errno 17] File exists
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
916 try:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
917 tar.extractall(path=tempdir)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
918 except OSError:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
919 self.fail("extractall failed with broken symlinked files")
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
920 finally:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
921 tar.close()
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
922 finally:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
923 os.unlink(temparchive)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
924 shutil.rmtree(tempdir)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
925
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
926 @unittest.skipUnless(hasattr(os, 'link'), "needs os.link")
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
927 def test_extractall_hardlinks(self):
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
928 # Test if extractall works properly when tarfile contains symlinks
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
929 tempdir = os.path.join(TEMPDIR, "testsymlinks")
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
930 temparchive = os.path.join(TEMPDIR, "testsymlinks.tar")
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
931 os.mkdir(tempdir)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
932 try:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
933 source_file = os.path.join(tempdir,'source')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
934 target_file = os.path.join(tempdir,'symlink')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
935 with open(source_file,'w') as f:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
936 f.write('something\n')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
937 os.link(source_file, target_file)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
938 tar = tarfile.open(temparchive,'w')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
939 tar.add(source_file, arcname=os.path.basename(source_file))
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
940 tar.add(target_file, arcname=os.path.basename(target_file))
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
941 tar.close()
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
942 # Let's extract it to the location which contains the symlink
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
943 tar = tarfile.open(temparchive,'r')
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
944 # this should not raise OSError: [Errno 17] File exists
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
945 try:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
946 tar.extractall(path=tempdir)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
947 except OSError:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
948 self.fail("extractall failed with linked files")
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
949 finally:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
950 tar.close()
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
951 finally:
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
952 os.unlink(temparchive)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
953 shutil.rmtree(tempdir)
b29f0b6fcb76 Fix closes issue #12088 - fixes the tarfile.extractall issue when the
Senthil Kumaran <senthil@uthcode.com>
parents: 69658
diff changeset
954
47352
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
955 class StreamWriteTest(WriteTestBase):
35466
85960ca4fc08 Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents: 34697
diff changeset
956
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
957 mode = "w|"
35466
85960ca4fc08 Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents: 34697
diff changeset
958
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
959 def test_stream_padding(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
960 # Test for bug #1543303.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
961 tar = tarfile.open(tmpname, self.mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
962 tar.close()
35466
85960ca4fc08 Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents: 34697
diff changeset
963
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
964 if self.mode.endswith("gz"):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
965 fobj = gzip.GzipFile(tmpname)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
966 data = fobj.read()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
967 fobj.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
968 elif self.mode.endswith("bz2"):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
969 dec = bz2.BZ2Decompressor()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
970 data = open(tmpname, "rb").read()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
971 data = dec.decompress(data)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
972 self.assertTrue(len(dec.unused_data) == 0,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
973 "found trailing data")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
974 else:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
975 fobj = open(tmpname, "rb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
976 data = fobj.read()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
977 fobj.close()
35466
85960ca4fc08 Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents: 34697
diff changeset
978
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
979 self.assertTrue(data.count("\0") == tarfile.RECORDSIZE,
39604
8dd57c53ccf6 Fix bug #1543303, tarfile adds padding that breaks gunzip.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38437
diff changeset
980 "incorrect zero padding")
35466
85960ca4fc08 Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents: 34697
diff changeset
981
61090
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
982 def test_file_mode(self):
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
983 # Test for issue #8464: Create files with correct
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
984 # permissions.
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
985 if sys.platform == "win32" or not hasattr(os, "umask"):
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
986 return
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
987
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
988 if os.path.exists(tmpname):
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
989 os.remove(tmpname)
35466
85960ca4fc08 Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents: 34697
diff changeset
990
61090
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
991 original_umask = os.umask(0022)
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
992 try:
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
993 tar = tarfile.open(tmpname, self.mode)
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
994 tar.close()
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
995 mode = os.stat(tmpname).st_mode & 0777
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
996 self.assertEqual(mode, 0644, "wrong file permissions")
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
997 finally:
6cda601f4c58 Merged revisions 80616 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 57401
diff changeset
998 os.umask(original_umask)
35466
85960ca4fc08 Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents: 34697
diff changeset
999
74121
a60a3610a97b Issue #13639: Accept unicode filenames in tarfile.open(mode="w|gz").
Lars Gustäbel <lars@gustaebel.de>
parents: 73870
diff changeset
1000 def test_issue13639(self):
a60a3610a97b Issue #13639: Accept unicode filenames in tarfile.open(mode="w|gz").
Lars Gustäbel <lars@gustaebel.de>
parents: 73870
diff changeset
1001 try:
a60a3610a97b Issue #13639: Accept unicode filenames in tarfile.open(mode="w|gz").
Lars Gustäbel <lars@gustaebel.de>
parents: 73870
diff changeset
1002 with tarfile.open(unicode(tmpname, sys.getfilesystemencoding()), self.mode):
a60a3610a97b Issue #13639: Accept unicode filenames in tarfile.open(mode="w|gz").
Lars Gustäbel <lars@gustaebel.de>
parents: 73870
diff changeset
1003 pass
a60a3610a97b Issue #13639: Accept unicode filenames in tarfile.open(mode="w|gz").
Lars Gustäbel <lars@gustaebel.de>
parents: 73870
diff changeset
1004 except UnicodeDecodeError:
a60a3610a97b Issue #13639: Accept unicode filenames in tarfile.open(mode="w|gz").
Lars Gustäbel <lars@gustaebel.de>
parents: 73870
diff changeset
1005 self.fail("_Stream failed to write unicode filename")
a60a3610a97b Issue #13639: Accept unicode filenames in tarfile.open(mode="w|gz").
Lars Gustäbel <lars@gustaebel.de>
parents: 73870
diff changeset
1006
35466
85960ca4fc08 Patch #1168594: set sizes of non-regular files to zero. Fixes #1167128.
Martin v. Löwis <martin@v.loewis.de>
parents: 34697
diff changeset
1007
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1008 class GNUWriteTest(unittest.TestCase):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1009 # This testcase checks for correct creation of GNU Longname
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1010 # and Longlink extended headers (cp. bug #812325).
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1011
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1012 def _length(self, s):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1013 blocks, remainder = divmod(len(s) + 1, 512)
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1014 if remainder:
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1015 blocks += 1
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1016 return blocks * 512
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1017
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1018 def _calc_size(self, name, link=None):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1019 # Initial tar header
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1020 count = 512
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1021
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1022 if len(name) > tarfile.LENGTH_NAME:
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1023 # GNU longname extended header + longname
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1024 count += 512
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1025 count += self._length(name)
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1026 if link is not None and len(link) > tarfile.LENGTH_LINK:
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1027 # GNU longlink extended header + longlink
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1028 count += 512
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1029 count += self._length(link)
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1030 return count
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1031
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1032 def _test(self, name, link=None):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1033 tarinfo = tarfile.TarInfo(name)
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1034 if link:
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1035 tarinfo.linkname = link
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1036 tarinfo.type = tarfile.LNKTYPE
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1037
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1038 tar = tarfile.open(tmpname, "w")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1039 tar.format = tarfile.GNU_FORMAT
40424
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
1040 tar.addfile(tarinfo)
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1041
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1042 v1 = self._calc_size(name, link)
40424
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
1043 v2 = tar.offset
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1044 self.assertTrue(v1 == v2, "GNU longname/longlink creation failed")
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1045
40424
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
1046 tar.close()
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
1047
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1048 tar = tarfile.open(tmpname)
40424
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
1049 member = tar.next()
59777
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
1050 self.assertIsNotNone(member,
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
1051 "unable to read longname member")
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
1052 self.assertEqual(tarinfo.name, member.name,
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
1053 "unable to read longname member")
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
1054 self.assertEqual(tarinfo.linkname, member.linkname,
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
1055 "unable to read longname member")
40424
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
1056
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1057 def test_longname_1023(self):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1058 self._test(("longnam/" * 127) + "longnam")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1059
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1060 def test_longname_1024(self):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1061 self._test(("longnam/" * 127) + "longname")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1062
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1063 def test_longname_1025(self):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1064 self._test(("longnam/" * 127) + "longname_")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1065
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1066 def test_longlink_1023(self):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1067 self._test("name", ("longlnk/" * 127) + "longlnk")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1068
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1069 def test_longlink_1024(self):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1070 self._test("name", ("longlnk/" * 127) + "longlink")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1071
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1072 def test_longlink_1025(self):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1073 self._test("name", ("longlnk/" * 127) + "longlink_")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1074
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1075 def test_longnamelink_1023(self):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1076 self._test(("longnam/" * 127) + "longnam",
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1077 ("longlnk/" * 127) + "longlnk")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1078
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1079 def test_longnamelink_1024(self):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1080 self._test(("longnam/" * 127) + "longname",
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1081 ("longlnk/" * 127) + "longlink")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1082
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1083 def test_longnamelink_1025(self):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1084 self._test(("longnam/" * 127) + "longname_",
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1085 ("longlnk/" * 127) + "longlink_")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
1086
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1087
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1088 class HardlinkTest(unittest.TestCase):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1089 # Test the creation of LNKTYPE (hardlink) members in an archive.
38146
f61c892cadac Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents: 35804
diff changeset
1090
f61c892cadac Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents: 35804
diff changeset
1091 def setUp(self):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1092 self.foo = os.path.join(TEMPDIR, "foo")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1093 self.bar = os.path.join(TEMPDIR, "bar")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1094
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1095 fobj = open(self.foo, "wb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1096 fobj.write("foo")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1097 fobj.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1098
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1099 os.link(self.foo, self.bar)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1100
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1101 self.tar = tarfile.open(tmpname, "w")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1102 self.tar.add(self.foo)
38146
f61c892cadac Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents: 35804
diff changeset
1103
f61c892cadac Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents: 35804
diff changeset
1104 def tearDown(self):
f61c892cadac Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents: 35804
diff changeset
1105 self.tar.close()
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1106 os.remove(self.foo)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1107 os.remove(self.bar)
35735
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1108
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1109 def test_add_twice(self):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1110 # The same name will be added as a REGTYPE every
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1111 # time regardless of st_nlink.
35735
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1112 tarinfo = self.tar.gettarinfo(self.foo)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1113 self.assertTrue(tarinfo.type == tarfile.REGTYPE,
35735
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1114 "add file as regular failed")
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1115
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1116 def test_add_hardlink(self):
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1117 tarinfo = self.tar.gettarinfo(self.bar)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1118 self.assertTrue(tarinfo.type == tarfile.LNKTYPE,
35735
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1119 "add file as hardlink failed")
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1120
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1121 def test_dereference_hardlink(self):
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1122 self.tar.dereference = True
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1123 tarinfo = self.tar.gettarinfo(self.bar)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1124 self.assertTrue(tarinfo.type == tarfile.REGTYPE,
35735
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1125 "dereferencing hardlink failed")
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
1126
32769
983a91b50f6e SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents: 32768
diff changeset
1127
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1128 class PaxWriteTest(GNUWriteTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1129
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1130 def _test(self, name, link=None):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1131 # See GNUWriteTest.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1132 tarinfo = tarfile.TarInfo(name)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1133 if link:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1134 tarinfo.linkname = link
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1135 tarinfo.type = tarfile.LNKTYPE
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1136
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1137 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1138 tar.addfile(tarinfo)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1139 tar.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1140
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1141 tar = tarfile.open(tmpname)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1142 if link:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1143 l = tar.getmembers()[0].linkname
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1144 self.assertTrue(link == l, "PAX longlink creation failed")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1145 else:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1146 n = tar.getmembers()[0].name
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1147 self.assertTrue(name == n, "PAX longname creation failed")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1148
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1149 def test_pax_global_header(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1150 pax_headers = {
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1151 u"foo": u"bar",
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1152 u"uid": u"0",
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1153 u"mtime": u"1.23",
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1154 u"test": u"äöü",
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1155 u"äöü": u"test"}
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1156
59777
0e0632c543e4 Cleanup test_tarfile, and use check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59301
diff changeset
1157 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT,
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1158 pax_headers=pax_headers)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1159 tar.addfile(tarfile.TarInfo("test"))
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1160 tar.close()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1161
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1162 # Test if the global header was written correctly.
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1163 tar = tarfile.open(tmpname, encoding="iso8859-1")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1164 self.assertEqual(tar.pax_headers, pax_headers)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1165 self.assertEqual(tar.getmembers()[0].pax_headers, pax_headers)
34697
79302e50910e Patch #918101: Add tarfile open mode r|* for auto-detection of the
Martin v. Löwis <martin@v.loewis.de>
parents: 34649
diff changeset
1166
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1167 # Test if all the fields are unicode.
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1168 for key, val in tar.pax_headers.iteritems():
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1169 self.assertTrue(type(key) is unicode)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1170 self.assertTrue(type(val) is unicode)
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1171 if key in tarfile.PAX_NUMBER_FIELDS:
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1172 try:
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1173 tarfile.PAX_NUMBER_FIELDS[key](val)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1174 except (TypeError, ValueError):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1175 self.fail("unable to convert pax header field")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1176
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1177 def test_pax_extended_header(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1178 # The fields from the pax header have priority over the
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1179 # TarInfo.
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1180 pax_headers = {u"path": u"foo", u"uid": u"123"}
33809
d443505e7eec Add test case for bug #1017553
Andrew M. Kuchling <amk@amk.ca>
parents: 33234
diff changeset
1181
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1182 tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1183 t = tarfile.TarInfo()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1184 t.name = u"äöü" # non-ASCII
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1185 t.uid = 8**8 # too large
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1186 t.pax_headers = pax_headers
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1187 tar.addfile(t)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1188 tar.close()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1189
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1190 tar = tarfile.open(tmpname, encoding="iso8859-1")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1191 t = tar.getmembers()[0]
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1192 self.assertEqual(t.pax_headers, pax_headers)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1193 self.assertEqual(t.name, "foo")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1194 self.assertEqual(t.uid, 123)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1195
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1196
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1197 class UstarUnicodeTest(unittest.TestCase):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1198 # All *UnicodeTests FIXME
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1199
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1200 format = tarfile.USTAR_FORMAT
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1201
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1202 def test_iso8859_1_filename(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1203 self._test_unicode_filename("iso8859-1")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1204
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1205 def test_utf7_filename(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1206 self._test_unicode_filename("utf7")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1207
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1208 def test_utf8_filename(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1209 self._test_unicode_filename("utf8")
33809
d443505e7eec Add test case for bug #1017553
Andrew M. Kuchling <amk@amk.ca>
parents: 33234
diff changeset
1210
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1211 def _test_unicode_filename(self, encoding):
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1212 tar = tarfile.open(tmpname, "w", format=self.format, encoding=encoding, errors="strict")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1213 name = u"äöü"
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1214 tar.addfile(tarfile.TarInfo(name))
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1215 tar.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1216
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1217 tar = tarfile.open(tmpname, encoding=encoding)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1218 self.assertTrue(type(tar.getnames()[0]) is not unicode)
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1219 self.assertEqual(tar.getmembers()[0].name, name.encode(encoding))
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1220 tar.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1221
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1222 def test_unicode_filename_error(self):
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1223 tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1224 tarinfo = tarfile.TarInfo()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1225
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1226 tarinfo.name = "äöü"
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1227 if self.format == tarfile.PAX_FORMAT:
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1228 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1229 else:
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1230 tar.addfile(tarinfo)
40524
2676d8287a11 Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Lars Gustäbel <lars@gustaebel.de>
parents: 40520
diff changeset
1231
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1232 tarinfo.name = u"äöü"
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1233 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1234
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1235 tarinfo.name = "foo"
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1236 tarinfo.uname = u"äöü"
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1237 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1238
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1239 def test_unicode_argument(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1240 tar = tarfile.open(tarname, "r", encoding="iso8859-1", errors="strict")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1241 for t in tar:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1242 self.assertTrue(type(t.name) is str)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1243 self.assertTrue(type(t.linkname) is str)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1244 self.assertTrue(type(t.uname) is str)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52268
diff changeset
1245 self.assertTrue(type(t.gname) is str)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1246 tar.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1247
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1248 def test_uname_unicode(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1249 for name in (u"äöü", "äöü"):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1250 t = tarfile.TarInfo("foo")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1251 t.uname = name
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1252 t.gname = name
33825
aed2526da1fc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33809
diff changeset
1253
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1254 fobj = StringIO.StringIO()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1255 tar = tarfile.open("foo.tar", mode="w", fileobj=fobj, format=self.format, encoding="iso8859-1")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1256 tar.addfile(t)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1257 tar.close()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1258 fobj.seek(0)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1259
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1260 tar = tarfile.open("foo.tar", fileobj=fobj, encoding="iso8859-1")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1261 t = tar.getmember("foo")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1262 self.assertEqual(t.uname, "äöü")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1263 self.assertEqual(t.gname, "äöü")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1264
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1265
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1266 class GNUUnicodeTest(UstarUnicodeTest):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1267
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1268 format = tarfile.GNU_FORMAT
33809
d443505e7eec Add test case for bug #1017553
Andrew M. Kuchling <amk@amk.ca>
parents: 33234
diff changeset
1269
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1270
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1271 class PaxUnicodeTest(UstarUnicodeTest):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1272
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1273 format = tarfile.PAX_FORMAT
42950
66e36ce5a56c TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 41492
diff changeset
1274
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1275 def _create_unicode_name(self, name):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1276 tar = tarfile.open(tmpname, "w", format=self.format)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1277 t = tarfile.TarInfo()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1278 t.pax_headers["path"] = name
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1279 tar.addfile(t)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1280 tar.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1281
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1282 def test_error_handlers(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1283 # Test if the unicode error handlers work correctly for characters
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1284 # that cannot be expressed in a given encoding.
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1285 self._create_unicode_name(u"äöü")
42950
66e36ce5a56c TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 41492
diff changeset
1286
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1287 for handler, name in (("utf-8", u"äöü".encode("utf8")),
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1288 ("replace", "???"), ("ignore", "")):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1289 tar = tarfile.open(tmpname, format=self.format, encoding="ascii",
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1290 errors=handler)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1291 self.assertEqual(tar.getnames()[0], name)
40491
1250fe42ce5c Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents: 40424
diff changeset
1292
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1293 self.assertRaises(UnicodeError, tarfile.open, tmpname,
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1294 encoding="ascii", errors="strict")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1295
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1296 def test_error_handler_utf8(self):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1297 # Create a pathname that has one component representable using
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1298 # iso8859-1 and the other only in iso8859-15.
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1299 self._create_unicode_name(u"äöü/¤")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1300
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1301 tar = tarfile.open(tmpname, format=self.format, encoding="iso8859-1",
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1302 errors="utf-8")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1303 self.assertEqual(tar.getnames()[0], "äöü/" + u"¤".encode("utf8"))
42950
66e36ce5a56c TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 41492
diff changeset
1304
41492
01e6d84e3892 Patch #1695229: Fix a regression with tarfile.open() and a missing name
Lars Gustäbel <lars@gustaebel.de>
parents: 41491
diff changeset
1305
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1306 class AppendTest(unittest.TestCase):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1307 # Test append mode (cp. patch #1652681).
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1308
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1309 def setUp(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1310 self.tarname = tmpname
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1311 if os.path.exists(self.tarname):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1312 os.remove(self.tarname)
40491
1250fe42ce5c Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents: 40424
diff changeset
1313
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1314 def _add_testfile(self, fileobj=None):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1315 tar = tarfile.open(self.tarname, "a", fileobj=fileobj)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1316 tar.addfile(tarfile.TarInfo("bar"))
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1317 tar.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1318
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1319 def _create_testtar(self, mode="w:"):
41156
a3419c6c0cfe Quick fix for tests that fail on systems with an encoding other
Lars Gustäbel <lars@gustaebel.de>
parents: 41153
diff changeset
1320 src = tarfile.open(tarname, encoding="iso8859-1")
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1321 t = src.getmember("ustar/regtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1322 t.name = "foo"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1323 f = src.extractfile(t)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1324 tar = tarfile.open(self.tarname, mode)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1325 tar.addfile(t, f)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1326 tar.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1327
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1328 def _test(self, names=["bar"], fileobj=None):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1329 tar = tarfile.open(self.tarname, fileobj=fileobj)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1330 self.assertEqual(tar.getnames(), names)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1331
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1332 def test_non_existing(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1333 self._add_testfile()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1334 self._test()
40491
1250fe42ce5c Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents: 40424
diff changeset
1335
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1336 def test_empty(self):
57393
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1337 tarfile.open(self.tarname, "w:").close()
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1338 self._add_testfile()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1339 self._test()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1340
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1341 def test_empty_fileobj(self):
57393
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1342 fobj = StringIO.StringIO("\0" * 1024)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1343 self._add_testfile(fobj)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1344 fobj.seek(0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1345 self._test(fileobj=fobj)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1346
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1347 def test_fileobj(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1348 self._create_testtar()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1349 data = open(self.tarname).read()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1350 fobj = StringIO.StringIO(data)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1351 self._add_testfile(fobj)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1352 fobj.seek(0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1353 self._test(names=["foo", "bar"], fileobj=fobj)
40491
1250fe42ce5c Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents: 40424
diff changeset
1354
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1355 def test_existing(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1356 self._create_testtar()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1357 self._add_testfile()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1358 self._test(names=["foo", "bar"])
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1359
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1360 def test_append_gz(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1361 if gzip is None:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1362 return
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1363 self._create_testtar("w:gz")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1364 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
33825
aed2526da1fc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33809
diff changeset
1365
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1366 def test_append_bz2(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1367 if bz2 is None:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1368 return
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1369 self._create_testtar("w:bz2")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1370 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1371
57393
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1372 # Append mode is supposed to fail if the tarfile to append to
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1373 # does not end with a zero block.
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1374 def _test_error(self, data):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1375 open(self.tarname, "wb").write(data)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1376 self.assertRaises(tarfile.ReadError, self._add_testfile)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1377
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1378 def test_null(self):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1379 self._test_error("")
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1380
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1381 def test_incomplete(self):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1382 self._test_error("\0" * 13)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1383
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1384 def test_premature_eof(self):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1385 data = tarfile.TarInfo("foo").tobuf()
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1386 self._test_error(data)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1387
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1388 def test_trailing_garbage(self):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1389 data = tarfile.TarInfo("foo").tobuf()
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1390 self._test_error(data + "\0" * 13)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1391
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1392 def test_invalid(self):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1393 self._test_error("a" * 512)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1394
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1395
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1396 class LimitsTest(unittest.TestCase):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1397
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1398 def test_ustar_limits(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1399 # 100 char name
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1400 tarinfo = tarfile.TarInfo("0123456789" * 10)
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1401 tarinfo.tobuf(tarfile.USTAR_FORMAT)
40523
e85ca3fd4bd6 Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Lars Gustäbel <lars@gustaebel.de>
parents: 40519
diff changeset
1402
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1403 # 101 char name that cannot be stored
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1404 tarinfo = tarfile.TarInfo("0123456789" * 10 + "0")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1405 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1406
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1407 # 256 char name with a slash at pos 156
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1408 tarinfo = tarfile.TarInfo("123/" * 62 + "longname")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1409 tarinfo.tobuf(tarfile.USTAR_FORMAT)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1410
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1411 # 256 char name that cannot be stored
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1412 tarinfo = tarfile.TarInfo("1234567/" * 31 + "longname")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1413 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
52271
dd9fddb0720d Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
Lars Gustäbel <lars@gustaebel.de>
parents: 45282
diff changeset
1414
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1415 # 512 char name
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1416 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1417 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1418
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1419 # 512 char linkname
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1420 tarinfo = tarfile.TarInfo("longlink")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1421 tarinfo.linkname = "123/" * 126 + "longname"
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1422 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1423
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1424 # uid > 8 digits
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1425 tarinfo = tarfile.TarInfo("name")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1426 tarinfo.uid = 010000000
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1427 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1428
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1429 def test_gnu_limits(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1430 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1431 tarinfo.tobuf(tarfile.GNU_FORMAT)
52271
dd9fddb0720d Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
Lars Gustäbel <lars@gustaebel.de>
parents: 45282
diff changeset
1432
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1433 tarinfo = tarfile.TarInfo("longlink")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1434 tarinfo.linkname = "123/" * 126 + "longname"
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1435 tarinfo.tobuf(tarfile.GNU_FORMAT)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1436
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1437 # uid >= 256 ** 7
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1438 tarinfo = tarfile.TarInfo("name")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1439 tarinfo.uid = 04000000000000000000L
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1440 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.GNU_FORMAT)
40523
e85ca3fd4bd6 Patch #1504073: Fix tarfile.open() for mode "r" with a fileobj argument.
Lars Gustäbel <lars@gustaebel.de>
parents: 40519
diff changeset
1441
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1442 def test_pax_limits(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1443 tarinfo = tarfile.TarInfo("123/" * 126 + "longname")
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1444 tarinfo.tobuf(tarfile.PAX_FORMAT)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1445
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1446 tarinfo = tarfile.TarInfo("longlink")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1447 tarinfo.linkname = "123/" * 126 + "longname"
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1448 tarinfo.tobuf(tarfile.PAX_FORMAT)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1449
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1450 tarinfo = tarfile.TarInfo("name")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1451 tarinfo.uid = 04000000000000000000L
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1452 tarinfo.tobuf(tarfile.PAX_FORMAT)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1453
52271
dd9fddb0720d Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
Lars Gustäbel <lars@gustaebel.de>
parents: 45282
diff changeset
1454
59301
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1455 class ContextManagerTest(unittest.TestCase):
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1456
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1457 def test_basic(self):
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1458 with tarfile.open(tarname) as tar:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1459 self.assertFalse(tar.closed, "closed inside runtime context")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1460 self.assertTrue(tar.closed, "context manager failed")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1461
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1462 def test_closed(self):
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1463 # The __enter__() method is supposed to raise IOError
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1464 # if the TarFile object is already closed.
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1465 tar = tarfile.open(tarname)
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1466 tar.close()
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1467 with self.assertRaises(IOError):
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1468 with tar:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1469 pass
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1470
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1471 def test_exception(self):
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1472 # Test if the IOError exception is passed through properly.
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1473 with self.assertRaises(Exception) as exc:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1474 with tarfile.open(tarname) as tar:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1475 raise IOError
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1476 self.assertIsInstance(exc.exception, IOError,
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1477 "wrong exception raised in context manager")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1478 self.assertTrue(tar.closed, "context manager failed")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1479
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1480 def test_no_eof(self):
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1481 # __exit__() must not write end-of-archive blocks if an
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1482 # exception was raised.
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1483 try:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1484 with tarfile.open(tmpname, "w") as tar:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1485 raise Exception
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1486 except:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1487 pass
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1488 self.assertEqual(os.path.getsize(tmpname), 0,
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1489 "context manager wrote an end-of-archive block")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1490 self.assertTrue(tar.closed, "context manager failed")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1491
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1492 def test_eof(self):
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1493 # __exit__() must write end-of-archive blocks, i.e. call
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1494 # TarFile.close() if there was no error.
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1495 with tarfile.open(tmpname, "w"):
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1496 pass
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1497 self.assertNotEqual(os.path.getsize(tmpname), 0,
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1498 "context manager wrote no end-of-archive block")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1499
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1500 def test_fileobj(self):
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1501 # Test that __exit__() did not close the external file
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1502 # object.
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1503 fobj = open(tmpname, "wb")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1504 try:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1505 with tarfile.open(fileobj=fobj, mode="w") as tar:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1506 raise Exception
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1507 except:
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1508 pass
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1509 self.assertFalse(fobj.closed, "external file object was closed")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1510 self.assertTrue(tar.closed, "context manager failed")
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1511 fobj.close()
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1512
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1513
62044
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1514 class LinkEmulationTest(ReadTest):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1515
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1516 # Test for issue #8741 regression. On platforms that do not support
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1517 # symbolic or hard links tarfile tries to extract these types of members as
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1518 # the regular files they point to.
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1519 def _test_link_extraction(self, name):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1520 self.tar.extract(name, TEMPDIR)
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1521 data = open(os.path.join(TEMPDIR, name), "rb").read()
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1522 self.assertEqual(md5sum(data), md5_regtype)
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1523
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1524 def test_hardlink_extraction1(self):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1525 self._test_link_extraction("ustar/lnktype")
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1526
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1527 def test_hardlink_extraction2(self):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1528 self._test_link_extraction("./ustar/linktest2/lnktype")
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1529
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1530 def test_symlink_extraction1(self):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1531 self._test_link_extraction("ustar/symtype")
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1532
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1533 def test_symlink_extraction2(self):
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1534 self._test_link_extraction("./ustar/linktest2/symtype")
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1535
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1536
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1537 class GzipMiscReadTest(MiscReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1538 tarname = gzipname
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1539 mode = "r:gz"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1540 class GzipUstarReadTest(UstarReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1541 tarname = gzipname
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1542 mode = "r:gz"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1543 class GzipStreamReadTest(StreamReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1544 tarname = gzipname
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1545 mode = "r|gz"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1546 class GzipWriteTest(WriteTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1547 mode = "w:gz"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1548 class GzipStreamWriteTest(StreamWriteTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1549 mode = "w|gz"
52271
dd9fddb0720d Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
Lars Gustäbel <lars@gustaebel.de>
parents: 45282
diff changeset
1550
dd9fddb0720d Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
Lars Gustäbel <lars@gustaebel.de>
parents: 45282
diff changeset
1551
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1552 class Bz2MiscReadTest(MiscReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1553 tarname = bz2name
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1554 mode = "r:bz2"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1555 class Bz2UstarReadTest(UstarReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1556 tarname = bz2name
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1557 mode = "r:bz2"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1558 class Bz2StreamReadTest(StreamReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1559 tarname = bz2name
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1560 mode = "r|bz2"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1561 class Bz2WriteTest(WriteTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1562 mode = "w:bz2"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1563 class Bz2StreamWriteTest(StreamWriteTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1564 mode = "w|bz2"
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1565
52268
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1566 class Bz2PartialReadTest(unittest.TestCase):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1567 # Issue5068: The _BZ2Proxy.read() method loops forever
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1568 # on an empty or partial bzipped file.
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1569
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1570 def _test_partial_input(self, mode):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1571 class MyStringIO(StringIO.StringIO):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1572 hit_eof = False
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1573 def read(self, n):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1574 if self.hit_eof:
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1575 raise AssertionError("infinite loop detected in tarfile.open()")
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1576 self.hit_eof = self.pos == self.len
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1577 return StringIO.StringIO.read(self, n)
57393
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1578 def seek(self, *args):
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1579 self.hit_eof = False
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1580 return StringIO.StringIO.seek(self, *args)
52268
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1581
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1582 data = bz2.compress(tarfile.TarInfo("foo").tobuf())
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1583 for x in range(len(data) + 1):
57393
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1584 try:
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1585 tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode)
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1586 except tarfile.ReadError:
4e270eab452d Issue #6123: Fix opening empty archives and files.
Lars Gustäbel <lars@gustaebel.de>
parents: 57192
diff changeset
1587 pass # we have no interest in ReadErrors
52268
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1588
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1589 def test_partial_input(self):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1590 self._test_partial_input("r")
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1591
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1592 def test_partial_input_bz2(self):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1593 self._test_partial_input("r:bz2")
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1594
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1595
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1596 def test_main():
57193
43f32db1bf46 Merged revisions 76217 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52268
diff changeset
1597 os.makedirs(TEMPDIR)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1598
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1599 tests = [
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1600 UstarReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1601 MiscReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1602 StreamReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1603 DetectReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1604 MemberReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1605 GNUReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1606 PaxReadTest,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1607 WriteTest,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1608 StreamWriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1609 GNUWriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1610 PaxWriteTest,
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1611 UstarUnicodeTest,
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1612 GNUUnicodeTest,
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1613 PaxUnicodeTest,
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
1614 AppendTest,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1615 LimitsTest,
59301
8ace2ead5523 Issue #7232: Add support for the context manager protocol
Lars Gustäbel <lars@gustaebel.de>
parents: 58817
diff changeset
1616 ContextManagerTest,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1617 ]
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1618
32769
983a91b50f6e SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents: 32768
diff changeset
1619 if hasattr(os, "link"):
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1620 tests.append(HardlinkTest)
62044
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1621 else:
9af600a66115 Issue #8741: Fixed the TarFile.makelink() method that is responsible
Lars Gustäbel <lars@gustaebel.de>
parents: 62040
diff changeset
1622 tests.append(LinkEmulationTest)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1623
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1624 fobj = open(tarname, "rb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1625 data = fobj.read()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1626 fobj.close()
32769
983a91b50f6e SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents: 32768
diff changeset
1627
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1628 if gzip:
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1629 # Create testtar.tar.gz and add gzip-specific tests.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1630 tar = gzip.open(gzipname, "wb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1631 tar.write(data)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1632 tar.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1633
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1634 tests += [
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1635 GzipMiscReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1636 GzipUstarReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1637 GzipStreamReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1638 GzipWriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1639 GzipStreamWriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1640 ]
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1641
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1642 if bz2:
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1643 # Create testtar.tar.bz2 and add bz2-specific tests.
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1644 tar = bz2.BZ2File(bz2name, "wb")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1645 tar.write(data)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1646 tar.close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1647
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1648 tests += [
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1649 Bz2MiscReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1650 Bz2UstarReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1651 Bz2StreamReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1652 Bz2WriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1653 Bz2StreamWriteTest,
52268
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1654 Bz2PartialReadTest,
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1655 ]
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1656
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1657 try:
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1658 test_support.run_unittest(*tests)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1659 finally:
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1660 if os.path.exists(TEMPDIR):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1661 shutil.rmtree(TEMPDIR)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1662
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1663 if __name__ == "__main__":
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1664 test_main()