annotate Lib/test/test_tarfile.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 856ed418768c
children 43f32db1bf46 6768b2350c57
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
29425
ffd02baab259 Make creation of temporary file and directory more portable. Also made cleanup
Brett Cannon <bcannon@gmail.com>
parents: 29066
diff changeset
6 import tempfile
38146
f61c892cadac Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents: 35804
diff changeset
7 import StringIO
41917
0e29bb80e227 Have md5 raise a DeprecationWarning as per PEP 4.
Brett Cannon <bcannon@gmail.com>
parents: 41897
diff changeset
8 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
9 import errno
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
10
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
11 import unittest
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
12 import tarfile
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
13
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
14 from test import test_support
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
15
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
16 # Check for our compression modules.
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
17 try:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
18 import gzip
28598
3cf26b9f96f7 Get test working if gzip support is not available
Neal Norwitz <nnorwitz@gmail.com>
parents: 28216
diff changeset
19 gzip.GzipFile
3cf26b9f96f7 Get test working if gzip support is not available
Neal Norwitz <nnorwitz@gmail.com>
parents: 28216
diff changeset
20 except (ImportError, AttributeError):
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
21 gzip = None
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
22 try:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
23 import bz2
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
24 except ImportError:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
25 bz2 = None
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
26
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
27 def md5sum(data):
41917
0e29bb80e227 Have md5 raise a DeprecationWarning as per PEP 4.
Brett Cannon <bcannon@gmail.com>
parents: 41897
diff changeset
28 return md5(data).hexdigest()
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
29
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
30 def path(path):
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
31 return test_support.findfile(path)
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
32
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
33 TEMPDIR = os.path.join(tempfile.gettempdir(), "test_tarfile_tmp")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
34 tarname = path("testtar.tar")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
35 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
36 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
37 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
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 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
40 md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6"
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
41
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
42
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
43 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
44
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
45 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
46 mode = "r:"
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
47
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
48 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
49 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
50
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
51 def tearDown(self):
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
52 self.tar.close()
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
53
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
54
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
55 class UstarReadTest(ReadTest):
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
56
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
57 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
58 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
59 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
60 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
61 self.assert_((len(data), md5sum(data)) == (tarinfo.size, 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
62 "regular file extraction failed")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
63
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
64 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
65 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
66 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
67 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
68 fobj2 = self.tar.extractfile(tarinfo)
34649
13a950a3624b Patch #1107973: tarfile.ExFileObject iterators.
Martin v. Löwis <martin@v.loewis.de>
parents: 33825
diff changeset
69
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
70 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
71 lines2 = fobj2.readlines()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
72 self.assert_(lines1 == lines2,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
73 "fileobj.readlines() failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
74 self.assert_(len(lines2) == 114,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
75 "fileobj.readlines() failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
76 self.assert_(lines2[83] == \
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
77 "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
78 "fileobj.readlines() failed")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
79
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
80 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
81 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
82 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
83 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
84 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
85 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
86 lines2 = [line for line in fobj2]
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.assert_(lines1 == lines2,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
88 "fileobj.__iter__() failed")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
89
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
90 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
91 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
92 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
93 data = fobj.read()
35804
76a6e908624d Patch #1338314, Bug #1336623: fix tarfile so it can extract
Neal Norwitz <nnorwitz@gmail.com>
parents: 35735
diff changeset
94 fobj.close()
76a6e908624d Patch #1338314, Bug #1336623: fix tarfile so it can extract
Neal Norwitz <nnorwitz@gmail.com>
parents: 35735
diff changeset
95
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
96 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
97 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
98
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
99 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
100 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
101 self.assert_(0 == fobj.tell(),
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
102 "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
103 fobj.seek(2048, 0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
104 self.assert_(2048 == fobj.tell(),
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
105 "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
106 fobj.seek(-1024, 1)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
107 self.assert_(1024 == fobj.tell(),
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
108 "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
109 fobj.seek(1024, 1)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
110 self.assert_(2048 == fobj.tell(),
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
111 "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
112 s = fobj.read(10)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
113 self.assert_(s == data[2048:2058],
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
114 "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
115 fobj.seek(0, 2)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
116 self.assert_(tarinfo.size == fobj.tell(),
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
117 "seek() to file's end failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
118 self.assert_(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
119 "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
120 fobj.seek(-tarinfo.size, 2)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
121 self.assert_(0 == fobj.tell(),
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
122 "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
123 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
124 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
125 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
126 s2 = 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
127 self.assert_(s1 == s2,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
128 "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
129 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
130 self.assert_(len(fobj.readline()) == fobj.tell(),
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
131 "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
132 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
133 self.assert_(len(fobj.readline()) + 512 == fobj.tell(),
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
134 "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
135 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
136 line = fobj.readline()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
137 self.assert_(fobj.read() == data[len(line):],
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
138 "read() 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
139 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
140
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
141
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
142 class MiscReadTest(ReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
143
42949
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
144 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
145 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
146 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
147 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
148
42949
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
149 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
150 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
151 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
152 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
153 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
154 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
155
3bf2cad8f22a TarFile.__init__() no longer fails if no name argument is passed and
Lars Gustäbel <lars@gustaebel.de>
parents: 42109
diff changeset
156 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
157 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
158 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
159 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
160 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
161 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
162
44091
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
163 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
164 # 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
165 # 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
166 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
167 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
168 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
169 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
170 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
171 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
172 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
173
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
174 # 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
175 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
176 _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
177 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
178 _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
179 else:
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
180 _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
181 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
182 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
183
e634511b4af5 Issue #1531: Read fileobj from the current offset, do not seek to
Lars Gustäbel <lars@gustaebel.de>
parents: 42949
diff changeset
184 # 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
185 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
186 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
187 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
188 # 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
189 # 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
190 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
191 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
192 "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
193 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
194
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
195 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
196 # 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
197 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
198 return
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
199 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
200 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
201 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
202
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
203 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
204 # 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
205 # 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
206 # 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
207 tarinfo = self.tar.getmember("misc/dirtype-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
208 self.assert_(tarinfo.type == tarfile.DIRTYPE,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
209 "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
210
45285
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
211 def test_xstar_type(self):
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
212 # 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
213 # 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
214 # 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
215 try:
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
216 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
217 except KeyError:
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
218 self.fail("failed to find misc/regtype-xstar (mangled prefix?)")
ca0853622f1a Make sure that xstar headers are read correctly.
Lars Gustäbel <lars@gustaebel.de>
parents: 44470
diff changeset
219
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
220 def test_check_members(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
221 for tarinfo in self.tar:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
222 self.assert_(int(tarinfo.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
223 "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
224 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
225 continue
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
226 self.assert_(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
227 "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
228
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
229 def test_find_members(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
230 self.assert_(self.tar.getmembers()[-1].name == "misc/eof",
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
231 "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
232
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
233 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
234 # 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
235 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
236
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
237 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
238 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
239 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
240 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
241 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
242 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
243
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
244 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
245 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
246
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
247 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
248 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
249 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
250 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
251 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
252
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
253 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
254 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
255
44470
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
256 def test_extractall(self):
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
257 # Test if extractall() correctly restores directory permissions
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
258 # and times (see issue1735).
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
259 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
260 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
261 tar.extractall(TEMPDIR, directories)
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
262 for tarinfo in directories:
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
263 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
264 if sys.platform != "win32":
f9b95294ac85 Merged revisions 67717 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 49255
diff changeset
265 # 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
266 self.assertEqual(tarinfo.mode & 0777, os.stat(path).st_mode & 0777)
44470
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
267 self.assertEqual(tarinfo.mtime, os.path.getmtime(path))
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
268 tar.close()
bd0258df330e Issue #1735: TarFile.extractall() now correctly sets
Lars Gustäbel <lars@gustaebel.de>
parents: 44091
diff changeset
269
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
270
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
271 class StreamReadTest(ReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
272
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
273 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
274
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
275 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
276 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
277 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
278 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
279 self.assert_((len(data), md5sum(data)) == (tarinfo.size, 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
280 "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
281
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
282 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
283 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
284 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
285 self.assertRaises(tarfile.StreamError, f.read)
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
286
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
287 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
288 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
289 tar2 = self.tar
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
290
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
291 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
292 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
293 t2 = tar2.next()
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
294 if t1 is None:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
295 break
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
296 self.assert_(t2 is not None, "stream.next() failed.")
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
297
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
298 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
299 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
300 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
301
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
302 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
303 v2 = tar2.extractfile(t2)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
304 if v1 is None:
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
305 continue
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
306 self.assert_(v2 is not None, "stream.extractfile() failed")
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
307 self.assert_(v1.read() == v2.read(), "stream extraction failed")
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
308
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
309 tar1.close()
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
310
38185
ad8b76e99261 [ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents: 38146
diff changeset
311
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
312 class DetectReadTest(unittest.TestCase):
38185
ad8b76e99261 [ 1488881 ] tarfile.py: support for file-objects and bz2 (cp. #1488634)
Georg Brandl <georg@python.org>
parents: 38146
diff changeset
313
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
314 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
315 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
316 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
317 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
318 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
319
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
320 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
321 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
322 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
323 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
324 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
325
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
326 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
327 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
328 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
329 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
330 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
331 testfunc(tarname, "r|*")
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
332
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
333 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
334 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
335 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
336 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
337 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
338
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
339 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
340 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
341 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
342 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
343 testfunc(gzipname, "r|gz")
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
344
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
345 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
346 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
347 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
348 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
349 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
350
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
351 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
352 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
353 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
354 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
355 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
356
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
357 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
358 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
359
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
360 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
361 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
362
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
363
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
364 class MemberReadTest(ReadTest):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
365
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
366 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
367 if chksum is not None:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
368 self.assert_(md5sum(self.tar.extractfile(tarinfo).read()) == chksum,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
369 "wrong md5sum 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
370
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
371 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
372 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
373 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
374 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
375 # 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
376 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
377 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
378 for k, v in kwargs.iteritems():
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
379 self.assert_(getattr(tarinfo, k) == v,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
380 "wrong value in %s field of %s" % (k, 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
381
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
382 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
383 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
384 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
385
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
386 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
387 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
388 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
389
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
390 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
391 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
392 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
393
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
394 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
395 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
396 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
397
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
398 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
399 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
400 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
401
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
402 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
403 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
404 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
405
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
406 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
407 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
408 self._test_member(tarinfo, size=0, devmajor=3, devminor=0)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
409
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
410 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
411 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
412 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
413
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
414 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
415 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
416 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
417
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
418 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
419 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
420 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
421
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
422 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
423 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
424 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
425
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
426 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
427 name = "ustar/" + "12345/" * 39 + "1234567/longname"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
428 self.assert_(name in self.tar.getnames())
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
429
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
430 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
431 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
432 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
433
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
434 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
435 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
436 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
437 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
438
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
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 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
441
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
442 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
443 # 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
444 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
445 try:
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
446 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
447 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
448 self.fail("longname not found")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
449 self.assert_(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
450
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
451 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
452 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
453 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
454 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
455 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
456 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
457 self.fail("longlink not found")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
458 self.assert_(tarinfo.linkname == longname, "linkname wrong")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
459
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
460 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
461 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
462 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
463 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
464 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
465 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
466 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
467
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
468 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
469 # 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
470 # 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
471 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
472 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
473 fobj = open(tarname)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
474 fobj.seek(offset)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
475 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
476 self.assertEqual(tarinfo.type, self.longnametype)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
477
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
478
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
479 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
480
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
481 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
482 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
483
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
484 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
485 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
486 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
487 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
488 fobj2 = self.tar.extractfile(tarinfo2)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
489 self.assert_(fobj1.read() == fobj2.read(),
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
490 "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
491
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
492
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
493 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
494
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
495 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
496 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
497
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
498 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
499 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
500
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
501 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
502 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
503 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
504 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
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 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
507 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
508 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
509 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
510
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 = 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
512 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
513 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
514 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), u"ÄÖÜäöüß")
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
515
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
516 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
517 # 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
518 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
519 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
520 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
521 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
522 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
523 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
524 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
525 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
526 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
527
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
528
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
529 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
530 # 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
531 # 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
532
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
533 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
534 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
535 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
536 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
537 tar.close()
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
538 self.assert_(fobj.closed is False, "external fileobjs must never closed")
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
539
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
540
3eaa7236713f Do not close external file objects passed to tarfile.open(mode='w:bz2')
Lars Gustäbel <lars@gustaebel.de>
parents: 45285
diff changeset
541 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
542
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
543 mode = "w:"
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
544
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
545 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
546 # 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
547 # 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
548 # 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
549 # 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
550 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
551 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
552 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
553 tar.addfile(t)
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
554 tar.close()
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
555
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
556 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
557 self.assert_(tar.getnames()[0] == name,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
558 "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
559 tar.close()
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
560
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
561 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
562 # 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
563 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
564 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
565 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
566 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
567 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
568 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
569 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
570 self.assert_(os.path.getsize(tmpname) > 0,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
571 "tarfile is empty")
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
572
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
573 # 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
574 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
575 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
576
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
577 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
578 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
579 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
580 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
581 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
582
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
583 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
584 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
585 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
586 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
587 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
588
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
589 tar.close()
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
590
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
591 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
592 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
593 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
594 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
595 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
596 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
597 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
598 finally:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
599 os.rmdir(path)
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
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 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
602 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
603 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
604 target = os.path.join(TEMPDIR, "link_target")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
605 open(target, "wb").close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
606 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
607 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
608 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
609 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
610 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
611 finally:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
612 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
613 os.remove(link)
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
614
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
615 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
616 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
617 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
618 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
619 try:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
620 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
621 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
622 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
623 finally:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
624 os.remove(path)
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
625
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
626 def test_add_self(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
627 # 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
628 dstname = os.path.abspath(tmpname)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
629
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
630 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
631 self.assert_(tar.name == dstname, "archive name must be absolute")
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
632
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
633 tar.add(dstname)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
634 self.assert_(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
635
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
636 cwd = os.getcwd()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
637 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
638 tar.add(dstname)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
639 os.chdir(cwd)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
640 self.assert_(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
641
42109
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
642 def test_exclude(self):
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
643 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
644 os.mkdir(tempdir)
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
645 try:
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
646 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
647 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
648 open(name, "wb").close()
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
649
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
650 def exclude(name):
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
651 return os.path.isfile(name)
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
652
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
653 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
654 tar.add(tempdir, arcname="empty_dir", exclude=exclude)
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
655 tar.close()
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
656
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
657 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
658 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
659 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
660 finally:
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
661 shutil.rmtree(tempdir)
b2e7336feb82 Added exclude keyword argument to the TarFile.add() method.
Lars Gustäbel <lars@gustaebel.de>
parents: 41917
diff changeset
662
40170
e82e696c03dc Patch [ 1583506 ] tarfile.py: 100-char filenames are truncated
Georg Brandl <georg@python.org>
parents: 39604
diff changeset
663
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
664 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
665
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
666 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
667
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
668 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
669 # 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
670 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
671 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
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 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
674 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
675 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
676 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
677 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
678 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
679 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
680 data = dec.decompress(data)
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.assert_(len(dec.unused_data) == 0,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
682 "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
683 else:
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 = 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
685 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
686 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
687
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
688 self.assert_(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
689 "incorrect zero padding")
8dd57c53ccf6 Fix bug #1543303, tarfile adds padding that breaks gunzip.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38437
diff changeset
690
8dd57c53ccf6 Fix bug #1543303, tarfile adds padding that breaks gunzip.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38437
diff changeset
691
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
692 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
693 # 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
694 # 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
695
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
696 def _length(self, s):
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
697 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
698 if remainder:
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
699 blocks += 1
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
700 return blocks * 512
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
701
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
702 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
703 # Initial tar header
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
704 count = 512
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
705
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
706 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
707 # 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
708 count += 512
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
709 count += self._length(name)
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
710 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
711 # 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
712 count += 512
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
713 count += self._length(link)
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
714 return count
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
715
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
716 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
717 tarinfo = tarfile.TarInfo(name)
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
718 if link:
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
719 tarinfo.linkname = link
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
720 tarinfo.type = tarfile.LNKTYPE
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
721
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
722 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
723 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
724 tar.addfile(tarinfo)
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
725
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
726 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
727 v2 = tar.offset
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
728 self.assert_(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
729
40424
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
730 tar.close()
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
731
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
732 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
733 member = tar.next()
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
734 self.failIf(member is None, "unable to read longname member")
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
735 self.assert_(tarinfo.name == member.name and \
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
736 tarinfo.linkname == member.linkname, \
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
737 "unable to read longname member")
eb30d30f22a1 Patch #1610437: fix a tarfile bug with long filename headers.
Georg Brandl <georg@python.org>
parents: 40170
diff changeset
738
32768
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
739 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
740 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
741
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
742 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
743 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
744
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
745 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
746 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
747
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
748 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
749 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
750
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
751 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
752 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
753
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
754 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
755 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
756
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
757 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
758 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
759 ("longlnk/" * 127) + "longlnk")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
760
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
761 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
762 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
763 ("longlnk/" * 127) + "longlink")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
764
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
765 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
766 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
767 ("longlnk/" * 127) + "longlink_")
ffa8ab0081f2 SF #846659, fix bufsize violation and GNU longname/longlink extensions
Neal Norwitz <nnorwitz@gmail.com>
parents: 29429
diff changeset
768
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
769
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
770 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
771 # 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
772
f61c892cadac Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents: 35804
diff changeset
773 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
774 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
775 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
776
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
777 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
778 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
779 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
780
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
781 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
782
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
783 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
784 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
785
f61c892cadac Patch #1484695: Update the tarfile module to version 0.8. This fixes
Georg Brandl <georg@python.org>
parents: 35804
diff changeset
786 def tearDown(self):
49255
55cb3e11bdf7 Issue #3838: TarFile object assigned to self.tar should be closed explicitly.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 47352
diff changeset
787 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
788 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
789 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
790
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
791 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
792 # 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
793 # 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
794 tarinfo = self.tar.gettarinfo(self.foo)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
795 self.assert_(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
796 "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
797
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
798 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
799 tarinfo = self.tar.gettarinfo(self.bar)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
800 self.assert_(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
801 "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
802
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
803 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
804 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
805 tarinfo = self.tar.gettarinfo(self.bar)
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
806 self.assert_(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
807 "dereferencing hardlink failed")
ea6808ba1637 Fix SF bug # 1330039, patch # 1331635 from Lars Gustaebel (tarfile maintainer)
Neal Norwitz <nnorwitz@gmail.com>
parents: 35466
diff changeset
808
32769
983a91b50f6e SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents: 32768
diff changeset
809
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
810 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
811
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
812 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
813 # 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
814 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
815 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
816 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
817 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
818
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
819 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
820 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
821 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
822
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
823 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
824 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
825 l = tar.getmembers()[0].linkname
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
826 self.assert_(link == l, "PAX longlink creation failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
827 else:
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
828 n = tar.getmembers()[0].name
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
829 self.assert_(name == n, "PAX longname creation failed")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
830
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
831 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
832 pax_headers = {
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
833 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
834 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
835 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
836 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
837 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
838
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
839 tar = tarfile.open(tmpname, "w", 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
840 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
841 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
842 tar.close()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
843
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
844 # 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
845 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
846 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
847 self.assertEqual(tar.getmembers()[0].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
848
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
849 # 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
850 for key, val in tar.pax_headers.iteritems():
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
851 self.assert_(type(key) is unicode)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
852 self.assert_(type(val) is unicode)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
853 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
854 try:
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
855 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
856 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
857 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
858
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
859 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
860 # 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
861 # TarInfo.
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
862 pax_headers = {u"path": u"foo", u"uid": u"123"}
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
863
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
864 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
865 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
866 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
867 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
868 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
869 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
870 tar.close()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
871
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
872 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
873 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
874 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
875 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
876 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
877
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
878
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
879 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
880 # 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
881
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
882 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
883
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
884 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
885 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
886
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
887 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
888 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
889
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
890 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
891 self._test_unicode_filename("utf8")
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
892
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
893 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
894 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
895 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
896 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
897 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
898
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
899 tar = tarfile.open(tmpname, encoding=encoding)
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
900 self.assert_(type(tar.getnames()[0]) is not unicode)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
901 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
902 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
903
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
904 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
905 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
906 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
907
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
908 tarinfo.name = "äöü"
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
909 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
910 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
911 else:
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
912 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
913
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
914 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
915 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
916
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
917 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
918 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
919 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
920
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
921 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
922 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
923 for t in tar:
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
924 self.assert_(type(t.name) is str)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
925 self.assert_(type(t.linkname) is str)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
926 self.assert_(type(t.uname) is str)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
927 self.assert_(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
928 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
929
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
930 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
931 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
932 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
933 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
934 t.gname = 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
935
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
936 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
937 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
938 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
939 tar.close()
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
940 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
941
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
942 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
943 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
944 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
945 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
946
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
947
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
948 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
949
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
950 format = tarfile.GNU_FORMAT
33809
d443505e7eec Add test case for bug #1017553
Andrew M. Kuchling <amk@amk.ca>
parents: 33234
diff changeset
951
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
952
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
953 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
954
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
955 format = 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
956
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
957 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
958 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
959 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
960 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
961 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
962 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
963
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
964 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
965 # 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
966 # 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
967 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
968
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
969 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
970 ("replace", "???"), ("ignore", "")):
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
971 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
972 errors=handler)
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
973 self.assertEqual(tar.getnames()[0], name)
40491
1250fe42ce5c Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents: 40424
diff changeset
974
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
975 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
976 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
977
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
978 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
979 # 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
980 # 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
981 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
982
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
983 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
984 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
985 self.assertEqual(tar.getnames()[0], "äöü/" + u"¤".encode("utf8"))
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
986
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
987
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
988 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
989 # 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
990
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
991 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
992 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
993 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
994 os.remove(self.tarname)
40491
1250fe42ce5c Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents: 40424
diff changeset
995
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
996 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
997 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
998 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
999 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
1000
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1001 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
1002 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
1003 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
1004 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
1005 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
1006 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
1007 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
1008 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
1009
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1010 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
1011 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
1012 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
1013
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1014 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
1015 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
1016 self._test()
40491
1250fe42ce5c Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents: 40424
diff changeset
1017
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1018 def test_empty(self):
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1019 open(self.tarname, "w").close()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1020 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
1021 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
1022
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1023 def test_empty_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
1024 fobj = StringIO.StringIO()
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1025 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
1026 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
1027 self._test(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
1028
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1029 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
1030 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
1031 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
1032 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
1033 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
1034 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
1035 self._test(names=["foo", "bar"], fileobj=fobj)
40491
1250fe42ce5c Testcase for patch #1484695.
Georg Brandl <georg@python.org>
parents: 40424
diff changeset
1036
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1037 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
1038 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
1039 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
1040 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
1041
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1042 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
1043 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
1044 return
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1045 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
1046 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
33825
aed2526da1fc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33809
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 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
1049 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
1050 return
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1051 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
1052 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
1053
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1054
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1055 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
1056
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1057 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
1058 # 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
1059 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
1060 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
1061
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1062 # 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
1063 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
1064 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
1065
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1066 # 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
1067 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
1068 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
1069
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1070 # 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
1071 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
1072 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
1073
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1074 # 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
1075 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
1076 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
1077
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1078 # 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
1079 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
1080 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
1081 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
1082
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1083 # 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
1084 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
1085 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
1086 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
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 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
1089 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
1090 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
1091
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1092 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
1093 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
1094 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
1095
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1096 # 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
1097 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
1098 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
1099 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
1100
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1101 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
1102 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
1103 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
1104
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1105 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
1106 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
1107 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
1108
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1109 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
1110 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
1111 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
1112
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1113
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1114 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
1115 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
1116 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
1117 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
1118 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
1119 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
1120 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
1121 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
1122 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
1123 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
1124 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
1125 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
1126 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
1127
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1128
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1129 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
1130 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
1131 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
1132 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
1133 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
1134 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
1135 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
1136 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
1137 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
1138 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
1139 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
1140 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
1141 mode = "w|bz2"
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1142
52268
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1143 class Bz2PartialReadTest(unittest.TestCase):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1144 # Issue5068: The _BZ2Proxy.read() method loops forever
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1145 # on an empty or partial bzipped file.
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1146
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1147 def _test_partial_input(self, mode):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1148 class MyStringIO(StringIO.StringIO):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1149 hit_eof = False
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1150 def read(self, n):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1151 if self.hit_eof:
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1152 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
1153 self.hit_eof = self.pos == self.len
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1154 return StringIO.StringIO.read(self, n)
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1155
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1156 data = bz2.compress(tarfile.TarInfo("foo").tobuf())
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1157 for x in range(len(data) + 1):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1158 tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode)
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1159
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1160 def test_partial_input(self):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1161 self._test_partial_input("r")
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1162
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1163 def test_partial_input_bz2(self):
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1164 self._test_partial_input("r:bz2")
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1165
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1166
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1167 def test_main():
41153
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1168 if not 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
1169 os.mkdir(TEMPDIR)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1170
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1171 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
1172 UstarReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1173 MiscReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1174 StreamReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1175 DetectReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1176 MemberReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1177 GNUReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1178 PaxReadTest,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1179 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
1180 StreamWriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1181 GNUWriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1182 PaxWriteTest,
41897
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1183 UstarUnicodeTest,
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1184 GNUUnicodeTest,
9de87569181c Added errors argument to TarFile class that allows the user to
Lars Gustäbel <lars@gustaebel.de>
parents: 41532
diff changeset
1185 PaxUnicodeTest,
40748
09dcacc8a0bc Patch #1652681: create nonexistent files in append mode and
Lars Gustäbel <lars@gustaebel.de>
parents: 40523
diff changeset
1186 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
1187 LimitsTest,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1188 ]
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1189
32769
983a91b50f6e SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents: 32768
diff changeset
1190 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
1191 tests.append(HardlinkTest)
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1192
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1193 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
1194 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
1195 fobj.close()
32769
983a91b50f6e SF #857297 and 916874, improve handling of hard links when extracting
Neal Norwitz <nnorwitz@gmail.com>
parents: 32768
diff changeset
1196
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1197 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
1198 # 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
1199 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
1200 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
1201 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
1202
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1203 tests += [
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1204 GzipMiscReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1205 GzipUstarReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1206 GzipStreamReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1207 GzipWriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1208 GzipStreamWriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1209 ]
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1210
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1211 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
1212 # 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
1213 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
1214 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
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 tests += [
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1218 Bz2MiscReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1219 Bz2UstarReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1220 Bz2StreamReadTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1221 Bz2WriteTest,
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1222 Bz2StreamWriteTest,
52268
856ed418768c Merged revisions 70523 via svnmerge from
Lars Gustäbel <lars@gustaebel.de>
parents: 50130
diff changeset
1223 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
1224 ]
5a3a1b02418a This is the implementation of POSIX.1-2001 (pax) format read/write
Lars Gustäbel <lars@gustaebel.de>
parents: 40846
diff changeset
1225
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1226 try:
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28598
diff changeset
1227 test_support.run_unittest(*tests)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1228 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
1229 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
1230 shutil.rmtree(TEMPDIR)
27098
f9a5ed092660 SF #651082, tarfile module implementation from Lars Gustäbel
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1231
27912
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1232 if __name__ == "__main__":
c5ebacbba0be Actually run these tests from regrtest.py.
Neal Norwitz <nnorwitz@gmail.com>
parents: 27098
diff changeset
1233 test_main()