Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import os |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 3 | import io |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 4 | import shutil |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 5 | from hashlib import md5 |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 6 | |
| 7 | import unittest |
| 8 | import tarfile |
| 9 | |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 10 | from test import support, script_helper |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 11 | |
| 12 | # Check for our compression modules. |
| 13 | try: |
| 14 | import gzip |
Brett Cannon | 260fbe8 | 2013-07-04 18:16:15 -0400 | [diff] [blame] | 15 | except ImportError: |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 16 | gzip = None |
| 17 | try: |
| 18 | import bz2 |
Brett Cannon | 260fbe8 | 2013-07-04 18:16:15 -0400 | [diff] [blame] | 19 | except ImportError: |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 20 | bz2 = None |
Lars Gustäbel | 0a9dd2f | 2011-12-10 20:38:14 +0100 | [diff] [blame] | 21 | try: |
| 22 | import lzma |
Brett Cannon | 260fbe8 | 2013-07-04 18:16:15 -0400 | [diff] [blame] | 23 | except ImportError: |
Lars Gustäbel | 0a9dd2f | 2011-12-10 20:38:14 +0100 | [diff] [blame] | 24 | lzma = None |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 26 | def md5sum(data): |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 27 | return md5(data).hexdigest() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 28 | |
Antoine Pitrou | ab58b5f | 2010-09-23 19:39:35 +0000 | [diff] [blame] | 29 | TEMPDIR = os.path.abspath(support.TESTFN) + "-tardir" |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 30 | tarextdir = TEMPDIR + '-extract-test' |
Antoine Pitrou | 941ee88 | 2009-11-11 20:59:38 +0000 | [diff] [blame] | 31 | tarname = support.findfile("testtar.tar") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 32 | gzipname = os.path.join(TEMPDIR, "testtar.tar.gz") |
| 33 | bz2name = os.path.join(TEMPDIR, "testtar.tar.bz2") |
Lars Gustäbel | 0a9dd2f | 2011-12-10 20:38:14 +0100 | [diff] [blame] | 34 | xzname = os.path.join(TEMPDIR, "testtar.tar.xz") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 35 | tmpname = os.path.join(TEMPDIR, "tmp.tar") |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 36 | dotlessname = os.path.join(TEMPDIR, "testtar") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 38 | md5_regtype = "65f477c818ad9e15f7feab0c6d37742f" |
| 39 | md5_sparse = "a54fbc4ca4f4399a90e1b27164012fc6" |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 40 | |
| 41 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 42 | class TarTest: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 43 | tarname = tarname |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 44 | suffix = '' |
| 45 | open = io.FileIO |
Serhiy Storchaka | 53ad0cd | 2014-01-18 15:35:37 +0200 | [diff] [blame] | 46 | taropen = tarfile.TarFile.taropen |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 47 | |
| 48 | @property |
| 49 | def mode(self): |
| 50 | return self.prefix + self.suffix |
| 51 | |
| 52 | @support.requires_gzip |
| 53 | class GzipTest: |
| 54 | tarname = gzipname |
| 55 | suffix = 'gz' |
| 56 | open = gzip.GzipFile if gzip else None |
Serhiy Storchaka | 53ad0cd | 2014-01-18 15:35:37 +0200 | [diff] [blame] | 57 | taropen = tarfile.TarFile.gzopen |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 58 | |
| 59 | @support.requires_bz2 |
| 60 | class Bz2Test: |
| 61 | tarname = bz2name |
| 62 | suffix = 'bz2' |
| 63 | open = bz2.BZ2File if bz2 else None |
Serhiy Storchaka | 53ad0cd | 2014-01-18 15:35:37 +0200 | [diff] [blame] | 64 | taropen = tarfile.TarFile.bz2open |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 65 | |
| 66 | @support.requires_lzma |
| 67 | class LzmaTest: |
| 68 | tarname = xzname |
| 69 | suffix = 'xz' |
| 70 | open = lzma.LZMAFile if lzma else None |
Serhiy Storchaka | 53ad0cd | 2014-01-18 15:35:37 +0200 | [diff] [blame] | 71 | taropen = tarfile.TarFile.xzopen |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 72 | |
| 73 | |
| 74 | class ReadTest(TarTest): |
| 75 | |
| 76 | prefix = "r:" |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 77 | |
| 78 | def setUp(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 79 | self.tar = tarfile.open(self.tarname, mode=self.mode, |
| 80 | encoding="iso8859-1") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 81 | |
| 82 | def tearDown(self): |
| 83 | self.tar.close() |
| 84 | |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 85 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 86 | class UstarReadTest(ReadTest, unittest.TestCase): |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 88 | def test_fileobj_regular_file(self): |
| 89 | tarinfo = self.tar.getmember("ustar/regtype") |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 90 | with self.tar.extractfile(tarinfo) as fobj: |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 91 | data = fobj.read() |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 92 | self.assertEqual(len(data), tarinfo.size, |
| 93 | "regular file extraction failed") |
| 94 | self.assertEqual(md5sum(data), md5_regtype, |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 95 | "regular file extraction failed") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 97 | def test_fileobj_readlines(self): |
| 98 | self.tar.extract("ustar/regtype", TEMPDIR) |
| 99 | tarinfo = self.tar.getmember("ustar/regtype") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 100 | with open(os.path.join(TEMPDIR, "ustar/regtype"), "r") as fobj1: |
| 101 | lines1 = fobj1.readlines() |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 102 | |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 103 | with self.tar.extractfile(tarinfo) as fobj: |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 104 | fobj2 = io.TextIOWrapper(fobj) |
| 105 | lines2 = fobj2.readlines() |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 106 | self.assertEqual(lines1, lines2, |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 107 | "fileobj.readlines() failed") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 108 | self.assertEqual(len(lines2), 114, |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 109 | "fileobj.readlines() failed") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 110 | self.assertEqual(lines2[83], |
| 111 | "I will gladly admit that Python is not the fastest " |
| 112 | "running scripting language.\n", |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 113 | "fileobj.readlines() failed") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 115 | def test_fileobj_iter(self): |
| 116 | self.tar.extract("ustar/regtype", TEMPDIR) |
| 117 | tarinfo = self.tar.getmember("ustar/regtype") |
Victor Stinner | 4e86d5b | 2011-05-04 13:55:36 +0200 | [diff] [blame] | 118 | with open(os.path.join(TEMPDIR, "ustar/regtype"), "r") as fobj1: |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 119 | lines1 = fobj1.readlines() |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 120 | with self.tar.extractfile(tarinfo) as fobj2: |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 121 | lines2 = list(io.TextIOWrapper(fobj2)) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 122 | self.assertEqual(lines1, lines2, |
| 123 | "fileobj.__iter__() failed") |
Martin v. Löwis | df24153 | 2005-03-03 08:17:42 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 125 | def test_fileobj_seek(self): |
| 126 | self.tar.extract("ustar/regtype", TEMPDIR) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 127 | with open(os.path.join(TEMPDIR, "ustar/regtype"), "rb") as fobj: |
| 128 | data = fobj.read() |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 130 | tarinfo = self.tar.getmember("ustar/regtype") |
| 131 | fobj = self.tar.extractfile(tarinfo) |
| 132 | |
| 133 | text = fobj.read() |
| 134 | fobj.seek(0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 135 | self.assertEqual(0, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 136 | "seek() to file's start failed") |
| 137 | fobj.seek(2048, 0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 138 | self.assertEqual(2048, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 139 | "seek() to absolute position failed") |
| 140 | fobj.seek(-1024, 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 141 | self.assertEqual(1024, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 142 | "seek() to negative relative position failed") |
| 143 | fobj.seek(1024, 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 144 | self.assertEqual(2048, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 145 | "seek() to positive relative position failed") |
| 146 | s = fobj.read(10) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 147 | self.assertEqual(s, data[2048:2058], |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 148 | "read() after seek failed") |
| 149 | fobj.seek(0, 2) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 150 | self.assertEqual(tarinfo.size, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 151 | "seek() to file's end failed") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 152 | self.assertEqual(fobj.read(), b"", |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 153 | "read() at file's end did not return empty string") |
| 154 | fobj.seek(-tarinfo.size, 2) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 155 | self.assertEqual(0, fobj.tell(), |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 156 | "relative seek() to file's end failed") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 157 | fobj.seek(512) |
| 158 | s1 = fobj.readlines() |
| 159 | fobj.seek(512) |
| 160 | s2 = fobj.readlines() |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 161 | self.assertEqual(s1, s2, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 162 | "readlines() after seek failed") |
| 163 | fobj.seek(0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 164 | self.assertEqual(len(fobj.readline()), fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 165 | "tell() after readline() failed") |
| 166 | fobj.seek(512) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 167 | self.assertEqual(len(fobj.readline()) + 512, fobj.tell(), |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 168 | "tell() after seek() and readline() failed") |
| 169 | fobj.seek(0) |
| 170 | line = fobj.readline() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 171 | self.assertEqual(fobj.read(), data[len(line):], |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 172 | "read() after readline() failed") |
| 173 | fobj.close() |
| 174 | |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 175 | def test_fileobj_text(self): |
| 176 | with self.tar.extractfile("ustar/regtype") as fobj: |
| 177 | fobj = io.TextIOWrapper(fobj) |
| 178 | data = fobj.read().encode("iso8859-1") |
| 179 | self.assertEqual(md5sum(data), md5_regtype) |
| 180 | try: |
| 181 | fobj.seek(100) |
| 182 | except AttributeError: |
| 183 | # Issue #13815: seek() complained about a missing |
| 184 | # flush() method. |
| 185 | self.fail("seeking failed in text mode") |
| 186 | |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 187 | # Test if symbolic and hard links are resolved by extractfile(). The |
| 188 | # test link members each point to a regular member whose data is |
| 189 | # supposed to be exported. |
| 190 | def _test_fileobj_link(self, lnktype, regtype): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 191 | with self.tar.extractfile(lnktype) as a, \ |
| 192 | self.tar.extractfile(regtype) as b: |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 193 | self.assertEqual(a.name, b.name) |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 194 | |
| 195 | def test_fileobj_link1(self): |
| 196 | self._test_fileobj_link("ustar/lnktype", "ustar/regtype") |
| 197 | |
| 198 | def test_fileobj_link2(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 199 | self._test_fileobj_link("./ustar/linktest2/lnktype", |
| 200 | "ustar/linktest1/regtype") |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 201 | |
| 202 | def test_fileobj_symlink1(self): |
| 203 | self._test_fileobj_link("ustar/symtype", "ustar/regtype") |
| 204 | |
| 205 | def test_fileobj_symlink2(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 206 | self._test_fileobj_link("./ustar/linktest2/symtype", |
| 207 | "ustar/linktest1/regtype") |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 208 | |
Lars Gustäbel | 1ef9eda | 2012-04-24 21:04:40 +0200 | [diff] [blame] | 209 | def test_issue14160(self): |
| 210 | self._test_fileobj_link("symtype2", "ustar/regtype") |
| 211 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 212 | class GzipUstarReadTest(GzipTest, UstarReadTest): |
| 213 | pass |
| 214 | |
| 215 | class Bz2UstarReadTest(Bz2Test, UstarReadTest): |
| 216 | pass |
| 217 | |
| 218 | class LzmaUstarReadTest(LzmaTest, UstarReadTest): |
| 219 | pass |
| 220 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 221 | |
Serhiy Storchaka | 3b4f159 | 2014-02-05 20:53:36 +0200 | [diff] [blame] | 222 | class ListTest(ReadTest, unittest.TestCase): |
| 223 | |
| 224 | # Override setUp to use default encoding (UTF-8) |
| 225 | def setUp(self): |
| 226 | self.tar = tarfile.open(self.tarname, mode=self.mode) |
| 227 | |
| 228 | def test_list(self): |
| 229 | tio = io.TextIOWrapper(io.BytesIO(), 'ascii', newline='\n') |
| 230 | with support.swap_attr(sys, 'stdout', tio): |
| 231 | self.tar.list(verbose=False) |
| 232 | out = tio.detach().getvalue() |
| 233 | self.assertIn(b'ustar/conttype', out) |
| 234 | self.assertIn(b'ustar/regtype', out) |
| 235 | self.assertIn(b'ustar/lnktype', out) |
| 236 | self.assertIn(b'ustar' + (b'/12345' * 40) + b'67/longname', out) |
| 237 | self.assertIn(b'./ustar/linktest2/symtype', out) |
| 238 | self.assertIn(b'./ustar/linktest2/lnktype', out) |
| 239 | # Make sure it puts trailing slash for directory |
| 240 | self.assertIn(b'ustar/dirtype/', out) |
| 241 | self.assertIn(b'ustar/dirtype-with-size/', out) |
| 242 | # Make sure it is able to print unencodable characters |
Serhiy Storchaka | 162c477 | 2014-02-19 18:44:12 +0200 | [diff] [blame] | 243 | def conv(b): |
| 244 | s = b.decode(self.tar.encoding, 'surrogateescape') |
| 245 | return s.encode('ascii', 'backslashreplace') |
| 246 | self.assertIn(conv(b'ustar/umlauts-\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out) |
| 247 | self.assertIn(conv(b'misc/regtype-hpux-signed-chksum-' |
| 248 | b'\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out) |
| 249 | self.assertIn(conv(b'misc/regtype-old-v7-signed-chksum-' |
| 250 | b'\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out) |
| 251 | self.assertIn(conv(b'pax/bad-pax-\xe4\xf6\xfc'), out) |
| 252 | self.assertIn(conv(b'pax/hdrcharset-\xe4\xf6\xfc'), out) |
Serhiy Storchaka | 3b4f159 | 2014-02-05 20:53:36 +0200 | [diff] [blame] | 253 | # Make sure it prints files separated by one newline without any |
| 254 | # 'ls -l'-like accessories if verbose flag is not being used |
| 255 | # ... |
| 256 | # ustar/conttype |
| 257 | # ustar/regtype |
| 258 | # ... |
| 259 | self.assertRegex(out, br'ustar/conttype ?\r?\n' |
| 260 | br'ustar/regtype ?\r?\n') |
| 261 | # Make sure it does not print the source of link without verbose flag |
| 262 | self.assertNotIn(b'link to', out) |
| 263 | self.assertNotIn(b'->', out) |
| 264 | |
| 265 | def test_list_verbose(self): |
| 266 | tio = io.TextIOWrapper(io.BytesIO(), 'ascii', newline='\n') |
| 267 | with support.swap_attr(sys, 'stdout', tio): |
| 268 | self.tar.list(verbose=True) |
| 269 | out = tio.detach().getvalue() |
| 270 | # Make sure it prints files separated by one newline with 'ls -l'-like |
| 271 | # accessories if verbose flag is being used |
| 272 | # ... |
| 273 | # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/conttype |
| 274 | # ?rw-r--r-- tarfile/tarfile 7011 2003-01-06 07:19:43 ustar/regtype |
| 275 | # ... |
Serhiy Storchaka | 255493c | 2014-02-05 20:54:43 +0200 | [diff] [blame] | 276 | self.assertRegex(out, (br'\?rw-r--r-- tarfile/tarfile\s+7011 ' |
Serhiy Storchaka | 3b4f159 | 2014-02-05 20:53:36 +0200 | [diff] [blame] | 277 | br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d ' |
| 278 | br'ustar/\w+type ?\r?\n') * 2) |
| 279 | # Make sure it prints the source of link with verbose flag |
| 280 | self.assertIn(b'ustar/symtype -> regtype', out) |
| 281 | self.assertIn(b'./ustar/linktest2/symtype -> ../linktest1/regtype', out) |
| 282 | self.assertIn(b'./ustar/linktest2/lnktype link to ' |
| 283 | b'./ustar/linktest1/regtype', out) |
| 284 | self.assertIn(b'gnu' + (b'/123' * 125) + b'/longlink link to gnu' + |
| 285 | (b'/123' * 125) + b'/longname', out) |
| 286 | self.assertIn(b'pax' + (b'/123' * 125) + b'/longlink link to pax' + |
| 287 | (b'/123' * 125) + b'/longname', out) |
| 288 | |
| 289 | |
| 290 | class GzipListTest(GzipTest, ListTest): |
| 291 | pass |
| 292 | |
| 293 | |
| 294 | class Bz2ListTest(Bz2Test, ListTest): |
| 295 | pass |
| 296 | |
| 297 | |
| 298 | class LzmaListTest(LzmaTest, ListTest): |
| 299 | pass |
| 300 | |
| 301 | |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 302 | class CommonReadTest(ReadTest): |
| 303 | |
| 304 | def test_empty_tarfile(self): |
| 305 | # Test for issue6123: Allow opening empty archives. |
| 306 | # This test checks if tarfile.open() is able to open an empty tar |
| 307 | # archive successfully. Note that an empty tar archive is not the |
| 308 | # same as an empty file! |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 309 | with tarfile.open(tmpname, self.mode.replace("r", "w")): |
| 310 | pass |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 311 | try: |
| 312 | tar = tarfile.open(tmpname, self.mode) |
| 313 | tar.getnames() |
| 314 | except tarfile.ReadError: |
| 315 | self.fail("tarfile.open() failed on empty archive") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 316 | else: |
| 317 | self.assertListEqual(tar.getmembers(), []) |
| 318 | finally: |
| 319 | tar.close() |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 320 | |
Serhiy Storchaka | f22fe0f | 2014-01-13 19:08:00 +0200 | [diff] [blame] | 321 | def test_non_existent_tarfile(self): |
| 322 | # Test for issue11513: prevent non-existent gzipped tarfiles raising |
| 323 | # multiple exceptions. |
| 324 | with self.assertRaisesRegex(FileNotFoundError, "xxx"): |
| 325 | tarfile.open("xxx", self.mode) |
| 326 | |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 327 | def test_null_tarfile(self): |
| 328 | # Test for issue6123: Allow opening empty archives. |
| 329 | # This test guarantees that tarfile.open() does not treat an empty |
| 330 | # file as an empty tar archive. |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 331 | with open(tmpname, "wb"): |
| 332 | pass |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 333 | self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, self.mode) |
| 334 | self.assertRaises(tarfile.ReadError, tarfile.open, tmpname) |
| 335 | |
| 336 | def test_ignore_zeros(self): |
| 337 | # Test TarFile's ignore_zeros option. |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 338 | for char in (b'\0', b'a'): |
| 339 | # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a') |
| 340 | # are ignored correctly. |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 341 | with self.open(tmpname, "w") as fobj: |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 342 | fobj.write(char * 1024) |
| 343 | fobj.write(tarfile.TarInfo("foo").tobuf()) |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 344 | |
| 345 | tar = tarfile.open(tmpname, mode="r", ignore_zeros=True) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 346 | try: |
| 347 | self.assertListEqual(tar.getnames(), ["foo"], |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 348 | "ignore_zeros=True should have skipped the %r-blocks" % |
| 349 | char) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 350 | finally: |
| 351 | tar.close() |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 352 | |
| 353 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 354 | class MiscReadTestBase(CommonReadTest): |
Serhiy Storchaka | 2c6a3ae | 2014-07-16 23:58:58 +0300 | [diff] [blame] | 355 | def requires_name_attribute(self): |
| 356 | pass |
| 357 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 358 | def test_no_name_argument(self): |
Serhiy Storchaka | 2c6a3ae | 2014-07-16 23:58:58 +0300 | [diff] [blame] | 359 | self.requires_name_attribute() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 360 | with open(self.tarname, "rb") as fobj: |
Serhiy Storchaka | 2c6a3ae | 2014-07-16 23:58:58 +0300 | [diff] [blame] | 361 | self.assertIsInstance(fobj.name, str) |
| 362 | with tarfile.open(fileobj=fobj, mode=self.mode) as tar: |
| 363 | self.assertIsInstance(tar.name, str) |
| 364 | self.assertEqual(tar.name, os.path.abspath(fobj.name)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 365 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 366 | def test_no_name_attribute(self): |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 367 | with open(self.tarname, "rb") as fobj: |
| 368 | data = fobj.read() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 369 | fobj = io.BytesIO(data) |
| 370 | self.assertRaises(AttributeError, getattr, fobj, "name") |
| 371 | tar = tarfile.open(fileobj=fobj, mode=self.mode) |
Serhiy Storchaka | 2c6a3ae | 2014-07-16 23:58:58 +0300 | [diff] [blame] | 372 | self.assertIsNone(tar.name) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 373 | |
| 374 | def test_empty_name_attribute(self): |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 375 | with open(self.tarname, "rb") as fobj: |
| 376 | data = fobj.read() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 377 | fobj = io.BytesIO(data) |
| 378 | fobj.name = "" |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 379 | with tarfile.open(fileobj=fobj, mode=self.mode) as tar: |
Serhiy Storchaka | 2c6a3ae | 2014-07-16 23:58:58 +0300 | [diff] [blame] | 380 | self.assertIsNone(tar.name) |
| 381 | |
| 382 | def test_int_name_attribute(self): |
| 383 | # Issue 21044: tarfile.open() should handle fileobj with an integer |
| 384 | # 'name' attribute. |
| 385 | fd = os.open(self.tarname, os.O_RDONLY) |
| 386 | with open(fd, 'rb') as fobj: |
| 387 | self.assertIsInstance(fobj.name, int) |
| 388 | with tarfile.open(fileobj=fobj, mode=self.mode) as tar: |
| 389 | self.assertIsNone(tar.name) |
| 390 | |
| 391 | def test_bytes_name_attribute(self): |
| 392 | self.requires_name_attribute() |
| 393 | tarname = os.fsencode(self.tarname) |
| 394 | with open(tarname, 'rb') as fobj: |
| 395 | self.assertIsInstance(fobj.name, bytes) |
| 396 | with tarfile.open(fileobj=fobj, mode=self.mode) as tar: |
| 397 | self.assertIsInstance(tar.name, bytes) |
| 398 | self.assertEqual(tar.name, os.path.abspath(fobj.name)) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 399 | |
Serhiy Storchaka | 53ad0cd | 2014-01-18 15:35:37 +0200 | [diff] [blame] | 400 | def test_illegal_mode_arg(self): |
| 401 | with open(tmpname, 'wb'): |
| 402 | pass |
| 403 | with self.assertRaisesRegex(ValueError, 'mode must be '): |
| 404 | tar = self.taropen(tmpname, 'q') |
| 405 | with self.assertRaisesRegex(ValueError, 'mode must be '): |
| 406 | tar = self.taropen(tmpname, 'rw') |
| 407 | with self.assertRaisesRegex(ValueError, 'mode must be '): |
| 408 | tar = self.taropen(tmpname, '') |
| 409 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 410 | def test_fileobj_with_offset(self): |
| 411 | # Skip the first member and store values from the second member |
| 412 | # of the testtar. |
| 413 | tar = tarfile.open(self.tarname, mode=self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 414 | try: |
| 415 | tar.next() |
| 416 | t = tar.next() |
| 417 | name = t.name |
| 418 | offset = t.offset |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 419 | with tar.extractfile(t) as f: |
| 420 | data = f.read() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 421 | finally: |
| 422 | tar.close() |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 423 | |
| 424 | # Open the testtar and seek to the offset of the second member. |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 425 | with self.open(self.tarname) as fobj: |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 426 | fobj.seek(offset) |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 427 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 428 | # Test if the tarfile starts with the second member. |
| 429 | tar = tar.open(self.tarname, mode="r:", fileobj=fobj) |
| 430 | t = tar.next() |
| 431 | self.assertEqual(t.name, name) |
| 432 | # Read to the end of fileobj and test if seeking back to the |
| 433 | # beginning works. |
| 434 | tar.getmembers() |
| 435 | self.assertEqual(tar.extractfile(t).read(), data, |
| 436 | "seek back did not work") |
| 437 | tar.close() |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 439 | def test_fail_comp(self): |
| 440 | # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 441 | self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 442 | with open(tarname, "rb") as fobj: |
| 443 | self.assertRaises(tarfile.ReadError, tarfile.open, |
| 444 | fileobj=fobj, mode=self.mode) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 445 | |
| 446 | def test_v7_dirtype(self): |
| 447 | # Test old style dirtype member (bug #1336623): |
| 448 | # Old V7 tars create directory members using an AREGTYPE |
| 449 | # header with a "/" appended to the filename field. |
| 450 | tarinfo = self.tar.getmember("misc/dirtype-old-v7") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 451 | self.assertEqual(tarinfo.type, tarfile.DIRTYPE, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 452 | "v7 dirtype failed") |
| 453 | |
Christian Heimes | 126d29a | 2008-02-11 22:57:17 +0000 | [diff] [blame] | 454 | def test_xstar_type(self): |
| 455 | # The xstar format stores extra atime and ctime fields inside the |
| 456 | # space reserved for the prefix field. The prefix field must be |
| 457 | # ignored in this case, otherwise it will mess up the name. |
| 458 | try: |
| 459 | self.tar.getmember("misc/regtype-xstar") |
| 460 | except KeyError: |
| 461 | self.fail("failed to find misc/regtype-xstar (mangled prefix?)") |
| 462 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 463 | def test_check_members(self): |
| 464 | for tarinfo in self.tar: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 465 | self.assertEqual(int(tarinfo.mtime), 0o7606136617, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 466 | "wrong mtime for %s" % tarinfo.name) |
| 467 | if not tarinfo.name.startswith("ustar/"): |
| 468 | continue |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 469 | self.assertEqual(tarinfo.uname, "tarfile", |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 470 | "wrong uname for %s" % tarinfo.name) |
| 471 | |
| 472 | def test_find_members(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 473 | self.assertEqual(self.tar.getmembers()[-1].name, "misc/eof", |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 474 | "could not find all members") |
| 475 | |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 476 | @unittest.skipUnless(hasattr(os, "link"), |
| 477 | "Missing hardlink implementation") |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 478 | @support.skip_unless_symlink |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 479 | def test_extract_hardlink(self): |
| 480 | # Test hardlink extraction (e.g. bug #857297). |
Serhiy Storchaka | 88339c4 | 2012-12-30 20:16:30 +0200 | [diff] [blame] | 481 | with tarfile.open(tarname, errorlevel=1, encoding="iso8859-1") as tar: |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 482 | tar.extract("ustar/regtype", TEMPDIR) |
Serhiy Storchaka | 88339c4 | 2012-12-30 20:16:30 +0200 | [diff] [blame] | 483 | self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/regtype")) |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 484 | |
Serhiy Storchaka | 88339c4 | 2012-12-30 20:16:30 +0200 | [diff] [blame] | 485 | tar.extract("ustar/lnktype", TEMPDIR) |
| 486 | self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/lnktype")) |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 487 | with open(os.path.join(TEMPDIR, "ustar/lnktype"), "rb") as f: |
| 488 | data = f.read() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 489 | self.assertEqual(md5sum(data), md5_regtype) |
Neal Norwitz | f339654 | 2005-10-28 05:52:22 +0000 | [diff] [blame] | 490 | |
Serhiy Storchaka | 88339c4 | 2012-12-30 20:16:30 +0200 | [diff] [blame] | 491 | tar.extract("ustar/symtype", TEMPDIR) |
| 492 | self.addCleanup(os.remove, os.path.join(TEMPDIR, "ustar/symtype")) |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 493 | with open(os.path.join(TEMPDIR, "ustar/symtype"), "rb") as f: |
| 494 | data = f.read() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 495 | self.assertEqual(md5sum(data), md5_regtype) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 496 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 497 | def test_extractall(self): |
| 498 | # Test if extractall() correctly restores directory permissions |
| 499 | # and times (see issue1735). |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 500 | tar = tarfile.open(tarname, encoding="iso8859-1") |
Martin v. Löwis | be647e2 | 2010-11-01 22:08:46 +0000 | [diff] [blame] | 501 | DIR = os.path.join(TEMPDIR, "extractall") |
| 502 | os.mkdir(DIR) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 503 | try: |
| 504 | directories = [t for t in tar if t.isdir()] |
Martin v. Löwis | be647e2 | 2010-11-01 22:08:46 +0000 | [diff] [blame] | 505 | tar.extractall(DIR, directories) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 506 | for tarinfo in directories: |
Martin v. Löwis | be647e2 | 2010-11-01 22:08:46 +0000 | [diff] [blame] | 507 | path = os.path.join(DIR, tarinfo.name) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 508 | if sys.platform != "win32": |
| 509 | # Win32 has no support for fine grained permissions. |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 510 | self.assertEqual(tarinfo.mode & 0o777, |
| 511 | os.stat(path).st_mode & 0o777) |
Victor Stinner | 26bfb5a | 2010-10-29 10:59:08 +0000 | [diff] [blame] | 512 | def format_mtime(mtime): |
| 513 | if isinstance(mtime, float): |
| 514 | return "{} ({})".format(mtime, mtime.hex()) |
| 515 | else: |
| 516 | return "{!r} (int)".format(mtime) |
Victor Stinner | 14d8fe7 | 2010-10-29 11:02:06 +0000 | [diff] [blame] | 517 | file_mtime = os.path.getmtime(path) |
Victor Stinner | 26bfb5a | 2010-10-29 10:59:08 +0000 | [diff] [blame] | 518 | errmsg = "tar mtime {0} != file time {1} of path {2!a}".format( |
| 519 | format_mtime(tarinfo.mtime), |
| 520 | format_mtime(file_mtime), |
| 521 | path) |
| 522 | self.assertEqual(tarinfo.mtime, file_mtime, errmsg) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 523 | finally: |
| 524 | tar.close() |
Martin v. Löwis | be647e2 | 2010-11-01 22:08:46 +0000 | [diff] [blame] | 525 | shutil.rmtree(DIR) |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 526 | |
Martin v. Löwis | 16f344d | 2010-11-01 21:39:13 +0000 | [diff] [blame] | 527 | def test_extract_directory(self): |
| 528 | dirtype = "ustar/dirtype" |
Martin v. Löwis | be647e2 | 2010-11-01 22:08:46 +0000 | [diff] [blame] | 529 | DIR = os.path.join(TEMPDIR, "extractdir") |
| 530 | os.mkdir(DIR) |
| 531 | try: |
| 532 | with tarfile.open(tarname, encoding="iso8859-1") as tar: |
| 533 | tarinfo = tar.getmember(dirtype) |
| 534 | tar.extract(tarinfo, path=DIR) |
| 535 | extracted = os.path.join(DIR, dirtype) |
| 536 | self.assertEqual(os.path.getmtime(extracted), tarinfo.mtime) |
| 537 | if sys.platform != "win32": |
| 538 | self.assertEqual(os.stat(extracted).st_mode & 0o777, 0o755) |
| 539 | finally: |
| 540 | shutil.rmtree(DIR) |
Martin v. Löwis | 16f344d | 2010-11-01 21:39:13 +0000 | [diff] [blame] | 541 | |
Lars Gustäbel | b7f0923 | 2009-11-23 15:48:33 +0000 | [diff] [blame] | 542 | def test_init_close_fobj(self): |
| 543 | # Issue #7341: Close the internal file object in the TarFile |
| 544 | # constructor in case of an error. For the test we rely on |
| 545 | # the fact that opening an empty file raises a ReadError. |
| 546 | empty = os.path.join(TEMPDIR, "empty") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 547 | with open(empty, "wb") as fobj: |
| 548 | fobj.write(b"") |
Lars Gustäbel | b7f0923 | 2009-11-23 15:48:33 +0000 | [diff] [blame] | 549 | |
| 550 | try: |
| 551 | tar = object.__new__(tarfile.TarFile) |
| 552 | try: |
| 553 | tar.__init__(empty) |
| 554 | except tarfile.ReadError: |
| 555 | self.assertTrue(tar.fileobj.closed) |
| 556 | else: |
| 557 | self.fail("ReadError not raised") |
| 558 | finally: |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 559 | support.unlink(empty) |
Lars Gustäbel | b7f0923 | 2009-11-23 15:48:33 +0000 | [diff] [blame] | 560 | |
Serhiy Storchaka | 263fab9 | 2013-05-09 14:22:26 +0300 | [diff] [blame] | 561 | def test_parallel_iteration(self): |
| 562 | # Issue #16601: Restarting iteration over tarfile continued |
| 563 | # from where it left off. |
| 564 | with tarfile.open(self.tarname) as tar: |
| 565 | for m1, m2 in zip(tar, tar): |
| 566 | self.assertEqual(m1.offset, m2.offset) |
| 567 | self.assertEqual(m1.get_info(), m2.get_info()) |
| 568 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 569 | class MiscReadTest(MiscReadTestBase, unittest.TestCase): |
| 570 | test_fail_comp = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 571 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 572 | class GzipMiscReadTest(GzipTest, MiscReadTestBase, unittest.TestCase): |
Serhiy Storchaka | f22fe0f | 2014-01-13 19:08:00 +0200 | [diff] [blame] | 573 | pass |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 574 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 575 | class Bz2MiscReadTest(Bz2Test, MiscReadTestBase, unittest.TestCase): |
Serhiy Storchaka | 2c6a3ae | 2014-07-16 23:58:58 +0300 | [diff] [blame] | 576 | def requires_name_attribute(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 577 | self.skipTest("BZ2File have no name attribute") |
| 578 | |
| 579 | class LzmaMiscReadTest(LzmaTest, MiscReadTestBase, unittest.TestCase): |
Serhiy Storchaka | 2c6a3ae | 2014-07-16 23:58:58 +0300 | [diff] [blame] | 580 | def requires_name_attribute(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 581 | self.skipTest("LZMAFile have no name attribute") |
| 582 | |
| 583 | |
| 584 | class StreamReadTest(CommonReadTest, unittest.TestCase): |
| 585 | |
| 586 | prefix="r|" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 587 | |
Lars Gustäbel | dd07104 | 2011-02-23 11:42:22 +0000 | [diff] [blame] | 588 | def test_read_through(self): |
| 589 | # Issue #11224: A poorly designed _FileInFile.read() method |
| 590 | # caused seeking errors with stream tar files. |
| 591 | for tarinfo in self.tar: |
| 592 | if not tarinfo.isreg(): |
| 593 | continue |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 594 | with self.tar.extractfile(tarinfo) as fobj: |
| 595 | while True: |
| 596 | try: |
| 597 | buf = fobj.read(512) |
| 598 | except tarfile.StreamError: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 599 | self.fail("simple read-through using " |
| 600 | "TarFile.extractfile() failed") |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 601 | if not buf: |
| 602 | break |
Lars Gustäbel | dd07104 | 2011-02-23 11:42:22 +0000 | [diff] [blame] | 603 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 604 | def test_fileobj_regular_file(self): |
| 605 | tarinfo = self.tar.next() # get "regtype" (can't use getmember) |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 606 | with self.tar.extractfile(tarinfo) as fobj: |
| 607 | data = fobj.read() |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 608 | self.assertEqual(len(data), tarinfo.size, |
| 609 | "regular file extraction failed") |
| 610 | self.assertEqual(md5sum(data), md5_regtype, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 611 | "regular file extraction failed") |
| 612 | |
| 613 | def test_provoke_stream_error(self): |
| 614 | tarinfos = self.tar.getmembers() |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 615 | with self.tar.extractfile(tarinfos[0]) as f: # read the first member |
| 616 | self.assertRaises(tarfile.StreamError, f.read) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 617 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 618 | def test_compare_members(self): |
| 619 | tar1 = tarfile.open(tarname, encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 620 | try: |
| 621 | tar2 = self.tar |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 622 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 623 | while True: |
| 624 | t1 = tar1.next() |
| 625 | t2 = tar2.next() |
| 626 | if t1 is None: |
| 627 | break |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 628 | self.assertIsNotNone(t2, "stream.next() failed.") |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 630 | if t2.islnk() or t2.issym(): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 631 | with self.assertRaises(tarfile.StreamError): |
| 632 | tar2.extractfile(t2) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 633 | continue |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 635 | v1 = tar1.extractfile(t1) |
| 636 | v2 = tar2.extractfile(t2) |
| 637 | if v1 is None: |
| 638 | continue |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 639 | self.assertIsNotNone(v2, "stream.extractfile() failed") |
| 640 | self.assertEqual(v1.read(), v2.read(), |
| 641 | "stream extraction failed") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 642 | finally: |
| 643 | tar1.close() |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 644 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 645 | class GzipStreamReadTest(GzipTest, StreamReadTest): |
| 646 | pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 647 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 648 | class Bz2StreamReadTest(Bz2Test, StreamReadTest): |
| 649 | pass |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 650 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 651 | class LzmaStreamReadTest(LzmaTest, StreamReadTest): |
| 652 | pass |
| 653 | |
| 654 | |
| 655 | class DetectReadTest(TarTest, unittest.TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 656 | def _testfunc_file(self, name, mode): |
| 657 | try: |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 658 | tar = tarfile.open(name, mode) |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 659 | except tarfile.ReadError as e: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 660 | self.fail() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 661 | else: |
| 662 | tar.close() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 663 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 664 | def _testfunc_fileobj(self, name, mode): |
| 665 | try: |
Antoine Pitrou | 605c293 | 2010-09-23 20:15:14 +0000 | [diff] [blame] | 666 | with open(name, "rb") as f: |
| 667 | tar = tarfile.open(name, mode, fileobj=f) |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 668 | except tarfile.ReadError as e: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 669 | self.fail() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 670 | else: |
| 671 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 672 | |
| 673 | def _test_modes(self, testfunc): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 674 | if self.suffix: |
| 675 | with self.assertRaises(tarfile.ReadError): |
| 676 | tarfile.open(tarname, mode="r:" + self.suffix) |
| 677 | with self.assertRaises(tarfile.ReadError): |
| 678 | tarfile.open(tarname, mode="r|" + self.suffix) |
| 679 | with self.assertRaises(tarfile.ReadError): |
| 680 | tarfile.open(self.tarname, mode="r:") |
| 681 | with self.assertRaises(tarfile.ReadError): |
| 682 | tarfile.open(self.tarname, mode="r|") |
| 683 | testfunc(self.tarname, "r") |
| 684 | testfunc(self.tarname, "r:" + self.suffix) |
| 685 | testfunc(self.tarname, "r:*") |
| 686 | testfunc(self.tarname, "r|" + self.suffix) |
| 687 | testfunc(self.tarname, "r|*") |
Lars Gustäbel | 0a9dd2f | 2011-12-10 20:38:14 +0100 | [diff] [blame] | 688 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 689 | def test_detect_file(self): |
| 690 | self._test_modes(self._testfunc_file) |
| 691 | |
| 692 | def test_detect_fileobj(self): |
| 693 | self._test_modes(self._testfunc_fileobj) |
| 694 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 695 | class GzipDetectReadTest(GzipTest, DetectReadTest): |
| 696 | pass |
| 697 | |
| 698 | class Bz2DetectReadTest(Bz2Test, DetectReadTest): |
Lars Gustäbel | ed1ac58 | 2011-12-06 12:56:38 +0100 | [diff] [blame] | 699 | def test_detect_stream_bz2(self): |
| 700 | # Originally, tarfile's stream detection looked for the string |
| 701 | # "BZh91" at the start of the file. This is incorrect because |
| 702 | # the '9' represents the blocksize (900kB). If the file was |
| 703 | # compressed using another blocksize autodetection fails. |
Lars Gustäbel | ed1ac58 | 2011-12-06 12:56:38 +0100 | [diff] [blame] | 704 | with open(tarname, "rb") as fobj: |
| 705 | data = fobj.read() |
| 706 | |
| 707 | # Compress with blocksize 100kB, the file starts with "BZh11". |
| 708 | with bz2.BZ2File(tmpname, "wb", compresslevel=1) as fobj: |
| 709 | fobj.write(data) |
| 710 | |
| 711 | self._testfunc_file(tmpname, "r|*") |
| 712 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 713 | class LzmaDetectReadTest(LzmaTest, DetectReadTest): |
| 714 | pass |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 715 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 716 | |
| 717 | class MemberReadTest(ReadTest, unittest.TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 718 | |
| 719 | def _test_member(self, tarinfo, chksum=None, **kwargs): |
| 720 | if chksum is not None: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 721 | with self.tar.extractfile(tarinfo) as f: |
| 722 | self.assertEqual(md5sum(f.read()), chksum, |
| 723 | "wrong md5sum for %s" % tarinfo.name) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 724 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 725 | kwargs["mtime"] = 0o7606136617 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 726 | kwargs["uid"] = 1000 |
| 727 | kwargs["gid"] = 100 |
| 728 | if "old-v7" not in tarinfo.name: |
| 729 | # V7 tar can't handle alphabetic owners. |
| 730 | kwargs["uname"] = "tarfile" |
| 731 | kwargs["gname"] = "tarfile" |
| 732 | for k, v in kwargs.items(): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 733 | self.assertEqual(getattr(tarinfo, k), v, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 734 | "wrong value in %s field of %s" % (k, tarinfo.name)) |
| 735 | |
| 736 | def test_find_regtype(self): |
| 737 | tarinfo = self.tar.getmember("ustar/regtype") |
| 738 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 739 | |
| 740 | def test_find_conttype(self): |
| 741 | tarinfo = self.tar.getmember("ustar/conttype") |
| 742 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 743 | |
| 744 | def test_find_dirtype(self): |
| 745 | tarinfo = self.tar.getmember("ustar/dirtype") |
| 746 | self._test_member(tarinfo, size=0) |
| 747 | |
| 748 | def test_find_dirtype_with_size(self): |
| 749 | tarinfo = self.tar.getmember("ustar/dirtype-with-size") |
| 750 | self._test_member(tarinfo, size=255) |
| 751 | |
| 752 | def test_find_lnktype(self): |
| 753 | tarinfo = self.tar.getmember("ustar/lnktype") |
| 754 | self._test_member(tarinfo, size=0, linkname="ustar/regtype") |
| 755 | |
| 756 | def test_find_symtype(self): |
| 757 | tarinfo = self.tar.getmember("ustar/symtype") |
| 758 | self._test_member(tarinfo, size=0, linkname="regtype") |
| 759 | |
| 760 | def test_find_blktype(self): |
| 761 | tarinfo = self.tar.getmember("ustar/blktype") |
| 762 | self._test_member(tarinfo, size=0, devmajor=3, devminor=0) |
| 763 | |
| 764 | def test_find_chrtype(self): |
| 765 | tarinfo = self.tar.getmember("ustar/chrtype") |
| 766 | self._test_member(tarinfo, size=0, devmajor=1, devminor=3) |
| 767 | |
| 768 | def test_find_fifotype(self): |
| 769 | tarinfo = self.tar.getmember("ustar/fifotype") |
| 770 | self._test_member(tarinfo, size=0) |
| 771 | |
| 772 | def test_find_sparse(self): |
| 773 | tarinfo = self.tar.getmember("ustar/sparse") |
| 774 | self._test_member(tarinfo, size=86016, chksum=md5_sparse) |
| 775 | |
Lars Gustäbel | 9cbdd75 | 2010-10-29 09:08:19 +0000 | [diff] [blame] | 776 | def test_find_gnusparse(self): |
| 777 | tarinfo = self.tar.getmember("gnu/sparse") |
| 778 | self._test_member(tarinfo, size=86016, chksum=md5_sparse) |
| 779 | |
| 780 | def test_find_gnusparse_00(self): |
| 781 | tarinfo = self.tar.getmember("gnu/sparse-0.0") |
| 782 | self._test_member(tarinfo, size=86016, chksum=md5_sparse) |
| 783 | |
| 784 | def test_find_gnusparse_01(self): |
| 785 | tarinfo = self.tar.getmember("gnu/sparse-0.1") |
| 786 | self._test_member(tarinfo, size=86016, chksum=md5_sparse) |
| 787 | |
| 788 | def test_find_gnusparse_10(self): |
| 789 | tarinfo = self.tar.getmember("gnu/sparse-1.0") |
| 790 | self._test_member(tarinfo, size=86016, chksum=md5_sparse) |
| 791 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 792 | def test_find_umlauts(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 793 | tarinfo = self.tar.getmember("ustar/umlauts-" |
| 794 | "\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 795 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 796 | |
| 797 | def test_find_ustar_longname(self): |
| 798 | name = "ustar/" + "12345/" * 39 + "1234567/longname" |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 799 | self.assertIn(name, self.tar.getnames()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 800 | |
| 801 | def test_find_regtype_oldv7(self): |
| 802 | tarinfo = self.tar.getmember("misc/regtype-old-v7") |
| 803 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 804 | |
| 805 | def test_find_pax_umlauts(self): |
Antoine Pitrou | ab58b5f | 2010-09-23 19:39:35 +0000 | [diff] [blame] | 806 | self.tar.close() |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 807 | self.tar = tarfile.open(self.tarname, mode=self.mode, |
| 808 | encoding="iso8859-1") |
| 809 | tarinfo = self.tar.getmember("pax/umlauts-" |
| 810 | "\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 811 | self._test_member(tarinfo, size=7011, chksum=md5_regtype) |
| 812 | |
| 813 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 814 | class LongnameTest: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 815 | |
| 816 | def test_read_longname(self): |
| 817 | # Test reading of longname (bug #1471427). |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 818 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 819 | try: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 820 | tarinfo = self.tar.getmember(longname) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 821 | except KeyError: |
| 822 | self.fail("longname not found") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 823 | self.assertNotEqual(tarinfo.type, tarfile.DIRTYPE, |
| 824 | "read longname as dirtype") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 825 | |
| 826 | def test_read_longlink(self): |
| 827 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
| 828 | longlink = self.subdir + "/" + "123/" * 125 + "longlink" |
| 829 | try: |
| 830 | tarinfo = self.tar.getmember(longlink) |
| 831 | except KeyError: |
| 832 | self.fail("longlink not found") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 833 | self.assertEqual(tarinfo.linkname, longname, "linkname wrong") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 834 | |
| 835 | def test_truncated_longname(self): |
| 836 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
| 837 | tarinfo = self.tar.getmember(longname) |
| 838 | offset = tarinfo.offset |
| 839 | self.tar.fileobj.seek(offset) |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 840 | fobj = io.BytesIO(self.tar.fileobj.read(3 * 512)) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 841 | with self.assertRaises(tarfile.ReadError): |
| 842 | tarfile.open(name="foo.tar", fileobj=fobj) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 843 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 844 | def test_header_offset(self): |
| 845 | # Test if the start offset of the TarInfo object includes |
| 846 | # the preceding extended header. |
| 847 | longname = self.subdir + "/" + "123/" * 125 + "longname" |
| 848 | offset = self.tar.getmember(longname).offset |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 849 | with open(tarname, "rb") as fobj: |
| 850 | fobj.seek(offset) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 851 | tarinfo = tarfile.TarInfo.frombuf(fobj.read(512), |
| 852 | "iso8859-1", "strict") |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 853 | self.assertEqual(tarinfo.type, self.longnametype) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 854 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 855 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 856 | class GNUReadTest(LongnameTest, ReadTest, unittest.TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 857 | |
| 858 | subdir = "gnu" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 859 | longnametype = tarfile.GNUTYPE_LONGNAME |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 860 | |
Lars Gustäbel | 9cbdd75 | 2010-10-29 09:08:19 +0000 | [diff] [blame] | 861 | # Since 3.2 tarfile is supposed to accurately restore sparse members and |
| 862 | # produce files with holes. This is what we actually want to test here. |
| 863 | # Unfortunately, not all platforms/filesystems support sparse files, and |
| 864 | # even on platforms that do it is non-trivial to make reliable assertions |
| 865 | # about holes in files. Therefore, we first do one basic test which works |
| 866 | # an all platforms, and after that a test that will work only on |
| 867 | # platforms/filesystems that prove to support sparse files. |
| 868 | def _test_sparse_file(self, name): |
| 869 | self.tar.extract(name, TEMPDIR) |
| 870 | filename = os.path.join(TEMPDIR, name) |
| 871 | with open(filename, "rb") as fobj: |
| 872 | data = fobj.read() |
| 873 | self.assertEqual(md5sum(data), md5_sparse, |
| 874 | "wrong md5sum for %s" % name) |
| 875 | |
| 876 | if self._fs_supports_holes(): |
| 877 | s = os.stat(filename) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 878 | self.assertLess(s.st_blocks * 512, s.st_size) |
Lars Gustäbel | 9cbdd75 | 2010-10-29 09:08:19 +0000 | [diff] [blame] | 879 | |
| 880 | def test_sparse_file_old(self): |
| 881 | self._test_sparse_file("gnu/sparse") |
| 882 | |
| 883 | def test_sparse_file_00(self): |
| 884 | self._test_sparse_file("gnu/sparse-0.0") |
| 885 | |
| 886 | def test_sparse_file_01(self): |
| 887 | self._test_sparse_file("gnu/sparse-0.1") |
| 888 | |
| 889 | def test_sparse_file_10(self): |
| 890 | self._test_sparse_file("gnu/sparse-1.0") |
| 891 | |
| 892 | @staticmethod |
| 893 | def _fs_supports_holes(): |
| 894 | # Return True if the platform knows the st_blocks stat attribute and |
| 895 | # uses st_blocks units of 512 bytes, and if the filesystem is able to |
| 896 | # store holes in files. |
Victor Stinner | 9c3de4a | 2011-08-17 20:49:41 +0200 | [diff] [blame] | 897 | if sys.platform.startswith("linux"): |
Lars Gustäbel | 9cbdd75 | 2010-10-29 09:08:19 +0000 | [diff] [blame] | 898 | # Linux evidentially has 512 byte st_blocks units. |
| 899 | name = os.path.join(TEMPDIR, "sparse-test") |
| 900 | with open(name, "wb") as fobj: |
| 901 | fobj.seek(4096) |
| 902 | fobj.truncate() |
| 903 | s = os.stat(name) |
| 904 | os.remove(name) |
| 905 | return s.st_blocks == 0 |
| 906 | else: |
| 907 | return False |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 908 | |
| 909 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 910 | class PaxReadTest(LongnameTest, ReadTest, unittest.TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 911 | |
| 912 | subdir = "pax" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 913 | longnametype = tarfile.XHDTYPE |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 914 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 915 | def test_pax_global_headers(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 916 | tar = tarfile.open(tarname, encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 917 | try: |
| 918 | tarinfo = tar.getmember("pax/regtype1") |
| 919 | self.assertEqual(tarinfo.uname, "foo") |
| 920 | self.assertEqual(tarinfo.gname, "bar") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 921 | self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), |
| 922 | "\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 924 | tarinfo = tar.getmember("pax/regtype2") |
| 925 | self.assertEqual(tarinfo.uname, "") |
| 926 | self.assertEqual(tarinfo.gname, "bar") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 927 | self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), |
| 928 | "\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 930 | tarinfo = tar.getmember("pax/regtype3") |
| 931 | self.assertEqual(tarinfo.uname, "tarfile") |
| 932 | self.assertEqual(tarinfo.gname, "tarfile") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 933 | self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"), |
| 934 | "\xc4\xd6\xdc\xe4\xf6\xfc\xdf") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 935 | finally: |
| 936 | tar.close() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 937 | |
| 938 | def test_pax_number_fields(self): |
| 939 | # All following number fields are read from the pax header. |
| 940 | tar = tarfile.open(tarname, encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 941 | try: |
| 942 | tarinfo = tar.getmember("pax/regtype4") |
| 943 | self.assertEqual(tarinfo.size, 7011) |
| 944 | self.assertEqual(tarinfo.uid, 123) |
| 945 | self.assertEqual(tarinfo.gid, 123) |
| 946 | self.assertEqual(tarinfo.mtime, 1041808783.0) |
| 947 | self.assertEqual(type(tarinfo.mtime), float) |
| 948 | self.assertEqual(float(tarinfo.pax_headers["atime"]), 1041808783.0) |
| 949 | self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0) |
| 950 | finally: |
| 951 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 952 | |
| 953 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 954 | class WriteTestBase(TarTest): |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 955 | # Put all write tests in here that are supposed to be tested |
| 956 | # in all possible mode combinations. |
| 957 | |
| 958 | def test_fileobj_no_close(self): |
| 959 | fobj = io.BytesIO() |
| 960 | tar = tarfile.open(fileobj=fobj, mode=self.mode) |
| 961 | tar.addfile(tarfile.TarInfo("foo")) |
| 962 | tar.close() |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 963 | self.assertFalse(fobj.closed, "external fileobjs must never closed") |
Serhiy Storchaka | 9fbec7a | 2014-01-18 15:53:05 +0200 | [diff] [blame] | 964 | # Issue #20238: Incomplete gzip output with mode="w:gz" |
| 965 | data = fobj.getvalue() |
| 966 | del tar |
| 967 | support.gc_collect() |
| 968 | self.assertFalse(fobj.closed) |
| 969 | self.assertEqual(data, fobj.getvalue()) |
Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 970 | |
| 971 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 972 | class WriteTest(WriteTestBase, unittest.TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 973 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 974 | prefix = "w:" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 975 | |
| 976 | def test_100_char_name(self): |
| 977 | # The name field in a tar header stores strings of at most 100 chars. |
| 978 | # If a string is shorter than 100 chars it has to be padded with '\0', |
| 979 | # which implies that a string of exactly 100 chars is stored without |
| 980 | # a trailing '\0'. |
| 981 | name = "0123456789" * 10 |
| 982 | tar = tarfile.open(tmpname, self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 983 | try: |
| 984 | t = tarfile.TarInfo(name) |
| 985 | tar.addfile(t) |
| 986 | finally: |
| 987 | tar.close() |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 988 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 989 | tar = tarfile.open(tmpname) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 990 | try: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 991 | self.assertEqual(tar.getnames()[0], name, |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 992 | "failed to store 100 char filename") |
| 993 | finally: |
| 994 | tar.close() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 995 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 996 | def test_tar_size(self): |
| 997 | # Test for bug #1013882. |
| 998 | tar = tarfile.open(tmpname, self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 999 | try: |
| 1000 | path = os.path.join(TEMPDIR, "file") |
| 1001 | with open(path, "wb") as fobj: |
| 1002 | fobj.write(b"aaa") |
| 1003 | tar.add(path) |
| 1004 | finally: |
| 1005 | tar.close() |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1006 | self.assertGreater(os.path.getsize(tmpname), 0, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1007 | "tarfile is empty") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1008 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1009 | # The test_*_size tests test for bug #1167128. |
| 1010 | def test_file_size(self): |
| 1011 | tar = tarfile.open(tmpname, self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1012 | try: |
| 1013 | path = os.path.join(TEMPDIR, "file") |
| 1014 | with open(path, "wb"): |
| 1015 | pass |
| 1016 | tarinfo = tar.gettarinfo(path) |
| 1017 | self.assertEqual(tarinfo.size, 0) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 1018 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1019 | with open(path, "wb") as fobj: |
| 1020 | fobj.write(b"aaa") |
| 1021 | tarinfo = tar.gettarinfo(path) |
| 1022 | self.assertEqual(tarinfo.size, 3) |
| 1023 | finally: |
| 1024 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1025 | |
| 1026 | def test_directory_size(self): |
| 1027 | path = os.path.join(TEMPDIR, "directory") |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 1028 | os.mkdir(path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1029 | try: |
| 1030 | tar = tarfile.open(tmpname, self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1031 | try: |
| 1032 | tarinfo = tar.gettarinfo(path) |
| 1033 | self.assertEqual(tarinfo.size, 0) |
| 1034 | finally: |
| 1035 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1036 | finally: |
| 1037 | os.rmdir(path) |
| 1038 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1039 | @unittest.skipUnless(hasattr(os, "link"), |
| 1040 | "Missing hardlink implementation") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1041 | def test_link_size(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1042 | link = os.path.join(TEMPDIR, "link") |
| 1043 | target = os.path.join(TEMPDIR, "link_target") |
| 1044 | with open(target, "wb") as fobj: |
| 1045 | fobj.write(b"aaa") |
| 1046 | os.link(target, link) |
| 1047 | try: |
| 1048 | tar = tarfile.open(tmpname, self.mode) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1049 | try: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1050 | # Record the link target in the inodes list. |
| 1051 | tar.gettarinfo(target) |
| 1052 | tarinfo = tar.gettarinfo(link) |
| 1053 | self.assertEqual(tarinfo.size, 0) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1054 | finally: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1055 | tar.close() |
| 1056 | finally: |
| 1057 | os.remove(target) |
| 1058 | os.remove(link) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1059 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 1060 | @support.skip_unless_symlink |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1061 | def test_symlink_size(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1062 | path = os.path.join(TEMPDIR, "symlink") |
| 1063 | os.symlink("link_target", path) |
| 1064 | try: |
| 1065 | tar = tarfile.open(tmpname, self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1066 | try: |
| 1067 | tarinfo = tar.gettarinfo(path) |
| 1068 | self.assertEqual(tarinfo.size, 0) |
| 1069 | finally: |
| 1070 | tar.close() |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1071 | finally: |
| 1072 | os.remove(path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1073 | |
| 1074 | def test_add_self(self): |
| 1075 | # Test for #1257255. |
| 1076 | dstname = os.path.abspath(tmpname) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1077 | tar = tarfile.open(tmpname, self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1078 | try: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1079 | self.assertEqual(tar.name, dstname, |
| 1080 | "archive name must be absolute") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1081 | tar.add(dstname) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1082 | self.assertEqual(tar.getnames(), [], |
| 1083 | "added the archive to itself") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1084 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1085 | cwd = os.getcwd() |
| 1086 | os.chdir(TEMPDIR) |
| 1087 | tar.add(dstname) |
| 1088 | os.chdir(cwd) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1089 | self.assertEqual(tar.getnames(), [], |
| 1090 | "added the archive to itself") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1091 | finally: |
| 1092 | tar.close() |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 1093 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 1094 | def test_exclude(self): |
| 1095 | tempdir = os.path.join(TEMPDIR, "exclude") |
| 1096 | os.mkdir(tempdir) |
| 1097 | try: |
| 1098 | for name in ("foo", "bar", "baz"): |
| 1099 | name = os.path.join(tempdir, name) |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 1100 | support.create_empty_file(name) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 1101 | |
Benjamin Peterson | 886af96 | 2010-03-21 23:13:07 +0000 | [diff] [blame] | 1102 | exclude = os.path.isfile |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 1103 | |
| 1104 | tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1105 | try: |
| 1106 | with support.check_warnings(("use the filter argument", |
| 1107 | DeprecationWarning)): |
| 1108 | tar.add(tempdir, arcname="empty_dir", exclude=exclude) |
| 1109 | finally: |
| 1110 | tar.close() |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 1111 | |
| 1112 | tar = tarfile.open(tmpname, "r") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1113 | try: |
| 1114 | self.assertEqual(len(tar.getmembers()), 1) |
| 1115 | self.assertEqual(tar.getnames()[0], "empty_dir") |
| 1116 | finally: |
| 1117 | tar.close() |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 1118 | finally: |
| 1119 | shutil.rmtree(tempdir) |
| 1120 | |
Lars Gustäbel | 049d2aa | 2009-09-12 10:44:00 +0000 | [diff] [blame] | 1121 | def test_filter(self): |
| 1122 | tempdir = os.path.join(TEMPDIR, "filter") |
| 1123 | os.mkdir(tempdir) |
| 1124 | try: |
| 1125 | for name in ("foo", "bar", "baz"): |
| 1126 | name = os.path.join(tempdir, name) |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 1127 | support.create_empty_file(name) |
Lars Gustäbel | 049d2aa | 2009-09-12 10:44:00 +0000 | [diff] [blame] | 1128 | |
| 1129 | def filter(tarinfo): |
| 1130 | if os.path.basename(tarinfo.name) == "bar": |
| 1131 | return |
| 1132 | tarinfo.uid = 123 |
| 1133 | tarinfo.uname = "foo" |
| 1134 | return tarinfo |
| 1135 | |
| 1136 | tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1137 | try: |
| 1138 | tar.add(tempdir, arcname="empty_dir", filter=filter) |
| 1139 | finally: |
| 1140 | tar.close() |
Lars Gustäbel | 049d2aa | 2009-09-12 10:44:00 +0000 | [diff] [blame] | 1141 | |
Raymond Hettinger | a63a312 | 2011-01-26 20:34:14 +0000 | [diff] [blame] | 1142 | # Verify that filter is a keyword-only argument |
| 1143 | with self.assertRaises(TypeError): |
| 1144 | tar.add(tempdir, "empty_dir", True, None, filter) |
| 1145 | |
Lars Gustäbel | 049d2aa | 2009-09-12 10:44:00 +0000 | [diff] [blame] | 1146 | tar = tarfile.open(tmpname, "r") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1147 | try: |
| 1148 | for tarinfo in tar: |
| 1149 | self.assertEqual(tarinfo.uid, 123) |
| 1150 | self.assertEqual(tarinfo.uname, "foo") |
| 1151 | self.assertEqual(len(tar.getmembers()), 3) |
| 1152 | finally: |
| 1153 | tar.close() |
Lars Gustäbel | 049d2aa | 2009-09-12 10:44:00 +0000 | [diff] [blame] | 1154 | finally: |
| 1155 | shutil.rmtree(tempdir) |
| 1156 | |
Lars Gustäbel | bfdfdda | 2009-08-28 19:59:59 +0000 | [diff] [blame] | 1157 | # Guarantee that stored pathnames are not modified. Don't |
| 1158 | # remove ./ or ../ or double slashes. Still make absolute |
| 1159 | # pathnames relative. |
| 1160 | # For details see bug #6054. |
| 1161 | def _test_pathname(self, path, cmp_path=None, dir=False): |
| 1162 | # Create a tarfile with an empty member named path |
| 1163 | # and compare the stored name with the original. |
| 1164 | foo = os.path.join(TEMPDIR, "foo") |
| 1165 | if not dir: |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 1166 | support.create_empty_file(foo) |
Lars Gustäbel | bfdfdda | 2009-08-28 19:59:59 +0000 | [diff] [blame] | 1167 | else: |
| 1168 | os.mkdir(foo) |
| 1169 | |
| 1170 | tar = tarfile.open(tmpname, self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1171 | try: |
| 1172 | tar.add(foo, arcname=path) |
| 1173 | finally: |
| 1174 | tar.close() |
Lars Gustäbel | bfdfdda | 2009-08-28 19:59:59 +0000 | [diff] [blame] | 1175 | |
| 1176 | tar = tarfile.open(tmpname, "r") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1177 | try: |
| 1178 | t = tar.next() |
| 1179 | finally: |
| 1180 | tar.close() |
Lars Gustäbel | bfdfdda | 2009-08-28 19:59:59 +0000 | [diff] [blame] | 1181 | |
| 1182 | if not dir: |
| 1183 | os.remove(foo) |
| 1184 | else: |
| 1185 | os.rmdir(foo) |
| 1186 | |
| 1187 | self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/")) |
| 1188 | |
Senthil Kumaran | be5dbeb | 2011-04-30 06:09:51 +0800 | [diff] [blame] | 1189 | |
| 1190 | @support.skip_unless_symlink |
Senthil Kumaran | 123932f | 2011-04-28 15:38:12 +0800 | [diff] [blame] | 1191 | def test_extractall_symlinks(self): |
| 1192 | # Test if extractall works properly when tarfile contains symlinks |
| 1193 | tempdir = os.path.join(TEMPDIR, "testsymlinks") |
| 1194 | temparchive = os.path.join(TEMPDIR, "testsymlinks.tar") |
| 1195 | os.mkdir(tempdir) |
| 1196 | try: |
| 1197 | source_file = os.path.join(tempdir,'source') |
| 1198 | target_file = os.path.join(tempdir,'symlink') |
| 1199 | with open(source_file,'w') as f: |
| 1200 | f.write('something\n') |
| 1201 | os.symlink(source_file, target_file) |
| 1202 | tar = tarfile.open(temparchive,'w') |
| 1203 | tar.add(source_file) |
| 1204 | tar.add(target_file) |
| 1205 | tar.close() |
| 1206 | # Let's extract it to the location which contains the symlink |
| 1207 | tar = tarfile.open(temparchive,'r') |
| 1208 | # this should not raise OSError: [Errno 17] File exists |
| 1209 | try: |
| 1210 | tar.extractall(path=tempdir) |
| 1211 | except OSError: |
| 1212 | self.fail("extractall failed with symlinked files") |
| 1213 | finally: |
| 1214 | tar.close() |
| 1215 | finally: |
| 1216 | os.unlink(temparchive) |
| 1217 | shutil.rmtree(tempdir) |
Martin v. Löwis | 5dbdc59 | 2005-08-27 10:07:56 +0000 | [diff] [blame] | 1218 | |
Lars Gustäbel | bfdfdda | 2009-08-28 19:59:59 +0000 | [diff] [blame] | 1219 | def test_pathnames(self): |
| 1220 | self._test_pathname("foo") |
| 1221 | self._test_pathname(os.path.join("foo", ".", "bar")) |
| 1222 | self._test_pathname(os.path.join("foo", "..", "bar")) |
| 1223 | self._test_pathname(os.path.join(".", "foo")) |
| 1224 | self._test_pathname(os.path.join(".", "foo", ".")) |
| 1225 | self._test_pathname(os.path.join(".", "foo", ".", "bar")) |
| 1226 | self._test_pathname(os.path.join(".", "foo", "..", "bar")) |
| 1227 | self._test_pathname(os.path.join(".", "foo", "..", "bar")) |
| 1228 | self._test_pathname(os.path.join("..", "foo")) |
| 1229 | self._test_pathname(os.path.join("..", "foo", "..")) |
| 1230 | self._test_pathname(os.path.join("..", "foo", ".", "bar")) |
| 1231 | self._test_pathname(os.path.join("..", "foo", "..", "bar")) |
| 1232 | |
| 1233 | self._test_pathname("foo" + os.sep + os.sep + "bar") |
| 1234 | self._test_pathname("foo" + os.sep + os.sep, "foo", dir=True) |
| 1235 | |
| 1236 | def test_abs_pathnames(self): |
| 1237 | if sys.platform == "win32": |
| 1238 | self._test_pathname("C:\\foo", "foo") |
| 1239 | else: |
| 1240 | self._test_pathname("/foo", "foo") |
| 1241 | self._test_pathname("///foo", "foo") |
| 1242 | |
| 1243 | def test_cwd(self): |
| 1244 | # Test adding the current working directory. |
| 1245 | cwd = os.getcwd() |
| 1246 | os.chdir(TEMPDIR) |
| 1247 | try: |
Lars Gustäbel | bfdfdda | 2009-08-28 19:59:59 +0000 | [diff] [blame] | 1248 | tar = tarfile.open(tmpname, self.mode) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1249 | try: |
| 1250 | tar.add(".") |
| 1251 | finally: |
| 1252 | tar.close() |
Lars Gustäbel | bfdfdda | 2009-08-28 19:59:59 +0000 | [diff] [blame] | 1253 | |
| 1254 | tar = tarfile.open(tmpname, "r") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1255 | try: |
| 1256 | for t in tar: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1257 | if t.name != ".": |
| 1258 | self.assertTrue(t.name.startswith("./"), t.name) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1259 | finally: |
| 1260 | tar.close() |
Lars Gustäbel | bfdfdda | 2009-08-28 19:59:59 +0000 | [diff] [blame] | 1261 | finally: |
| 1262 | os.chdir(cwd) |
| 1263 | |
Serhiy Storchaka | c2d0142 | 2014-01-18 16:14:10 +0200 | [diff] [blame] | 1264 | def test_open_nonwritable_fileobj(self): |
| 1265 | for exctype in OSError, EOFError, RuntimeError: |
| 1266 | class BadFile(io.BytesIO): |
| 1267 | first = True |
| 1268 | def write(self, data): |
| 1269 | if self.first: |
| 1270 | self.first = False |
| 1271 | raise exctype |
| 1272 | |
| 1273 | f = BadFile() |
| 1274 | with self.assertRaises(exctype): |
| 1275 | tar = tarfile.open(tmpname, self.mode, fileobj=f, |
| 1276 | format=tarfile.PAX_FORMAT, |
| 1277 | pax_headers={'non': 'empty'}) |
| 1278 | self.assertFalse(f.closed) |
| 1279 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1280 | class GzipWriteTest(GzipTest, WriteTest): |
| 1281 | pass |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1282 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1283 | class Bz2WriteTest(Bz2Test, WriteTest): |
| 1284 | pass |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1285 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1286 | class LzmaWriteTest(LzmaTest, WriteTest): |
| 1287 | pass |
| 1288 | |
| 1289 | |
| 1290 | class StreamWriteTest(WriteTestBase, unittest.TestCase): |
| 1291 | |
| 1292 | prefix = "w|" |
| 1293 | decompressor = None |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1294 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1295 | def test_stream_padding(self): |
| 1296 | # Test for bug #1543303. |
| 1297 | tar = tarfile.open(tmpname, self.mode) |
| 1298 | tar.close() |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1299 | if self.decompressor: |
| 1300 | dec = self.decompressor() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1301 | with open(tmpname, "rb") as fobj: |
| 1302 | data = fobj.read() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1303 | data = dec.decompress(data) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1304 | self.assertFalse(dec.unused_data, "found trailing data") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1305 | else: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1306 | with self.open(tmpname) as fobj: |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1307 | data = fobj.read() |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1308 | self.assertEqual(data.count(b"\0"), tarfile.RECORDSIZE, |
| 1309 | "incorrect zero padding") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1310 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1311 | @unittest.skipUnless(sys.platform != "win32" and hasattr(os, "umask"), |
| 1312 | "Missing umask implementation") |
Lars Gustäbel | d6eb70b | 2010-04-29 15:37:02 +0000 | [diff] [blame] | 1313 | def test_file_mode(self): |
| 1314 | # Test for issue #8464: Create files with correct |
| 1315 | # permissions. |
Lars Gustäbel | d6eb70b | 2010-04-29 15:37:02 +0000 | [diff] [blame] | 1316 | if os.path.exists(tmpname): |
| 1317 | os.remove(tmpname) |
| 1318 | |
| 1319 | original_umask = os.umask(0o022) |
| 1320 | try: |
| 1321 | tar = tarfile.open(tmpname, self.mode) |
| 1322 | tar.close() |
| 1323 | mode = os.stat(tmpname).st_mode & 0o777 |
| 1324 | self.assertEqual(mode, 0o644, "wrong file permissions") |
| 1325 | finally: |
| 1326 | os.umask(original_umask) |
| 1327 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1328 | class GzipStreamWriteTest(GzipTest, StreamWriteTest): |
| 1329 | pass |
| 1330 | |
| 1331 | class Bz2StreamWriteTest(Bz2Test, StreamWriteTest): |
| 1332 | decompressor = bz2.BZ2Decompressor if bz2 else None |
| 1333 | |
| 1334 | class LzmaStreamWriteTest(LzmaTest, StreamWriteTest): |
| 1335 | decompressor = lzma.LZMADecompressor if lzma else None |
| 1336 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1337 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1338 | class GNUWriteTest(unittest.TestCase): |
| 1339 | # This testcase checks for correct creation of GNU Longname |
| 1340 | # and Longlink extended headers (cp. bug #812325). |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1341 | |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1342 | def _length(self, s): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1343 | blocks = len(s) // 512 + 1 |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1344 | return blocks * 512 |
| 1345 | |
| 1346 | def _calc_size(self, name, link=None): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1347 | # Initial tar header |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1348 | count = 512 |
| 1349 | |
| 1350 | if len(name) > tarfile.LENGTH_NAME: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1351 | # GNU longname extended header + longname |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1352 | count += 512 |
| 1353 | count += self._length(name) |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1354 | if link is not None and len(link) > tarfile.LENGTH_LINK: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1355 | # GNU longlink extended header + longlink |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1356 | count += 512 |
| 1357 | count += self._length(link) |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1358 | return count |
| 1359 | |
| 1360 | def _test(self, name, link=None): |
| 1361 | tarinfo = tarfile.TarInfo(name) |
| 1362 | if link: |
| 1363 | tarinfo.linkname = link |
| 1364 | tarinfo.type = tarfile.LNKTYPE |
| 1365 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1366 | tar = tarfile.open(tmpname, "w") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1367 | try: |
| 1368 | tar.format = tarfile.GNU_FORMAT |
| 1369 | tar.addfile(tarinfo) |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1370 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1371 | v1 = self._calc_size(name, link) |
| 1372 | v2 = tar.offset |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1373 | self.assertEqual(v1, v2, "GNU longname/longlink creation failed") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1374 | finally: |
| 1375 | tar.close() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1376 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1377 | tar = tarfile.open(tmpname) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1378 | try: |
| 1379 | member = tar.next() |
| 1380 | self.assertIsNotNone(member, |
| 1381 | "unable to read longname member") |
| 1382 | self.assertEqual(tarinfo.name, member.name, |
| 1383 | "unable to read longname member") |
| 1384 | self.assertEqual(tarinfo.linkname, member.linkname, |
| 1385 | "unable to read longname member") |
| 1386 | finally: |
| 1387 | tar.close() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1388 | |
Neal Norwitz | 0662f8a | 2004-07-20 21:54:18 +0000 | [diff] [blame] | 1389 | def test_longname_1023(self): |
| 1390 | self._test(("longnam/" * 127) + "longnam") |
| 1391 | |
| 1392 | def test_longname_1024(self): |
| 1393 | self._test(("longnam/" * 127) + "longname") |
| 1394 | |
| 1395 | def test_longname_1025(self): |
| 1396 | self._test(("longnam/" * 127) + "longname_") |
| 1397 | |
| 1398 | def test_longlink_1023(self): |
| 1399 | self._test("name", ("longlnk/" * 127) + "longlnk") |
| 1400 | |
| 1401 | def test_longlink_1024(self): |
| 1402 | self._test("name", ("longlnk/" * 127) + "longlink") |
| 1403 | |
| 1404 | def test_longlink_1025(self): |
| 1405 | self._test("name", ("longlnk/" * 127) + "longlink_") |
| 1406 | |
| 1407 | def test_longnamelink_1023(self): |
| 1408 | self._test(("longnam/" * 127) + "longnam", |
| 1409 | ("longlnk/" * 127) + "longlnk") |
| 1410 | |
| 1411 | def test_longnamelink_1024(self): |
| 1412 | self._test(("longnam/" * 127) + "longname", |
| 1413 | ("longlnk/" * 127) + "longlink") |
| 1414 | |
| 1415 | def test_longnamelink_1025(self): |
| 1416 | self._test(("longnam/" * 127) + "longname_", |
| 1417 | ("longlnk/" * 127) + "longlink_") |
| 1418 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1419 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1420 | @unittest.skipUnless(hasattr(os, "link"), "Missing hardlink implementation") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1421 | class HardlinkTest(unittest.TestCase): |
| 1422 | # Test the creation of LNKTYPE (hardlink) members in an archive. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1423 | |
| 1424 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1425 | self.foo = os.path.join(TEMPDIR, "foo") |
| 1426 | self.bar = os.path.join(TEMPDIR, "bar") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1428 | with open(self.foo, "wb") as fobj: |
| 1429 | fobj.write(b"foo") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1430 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1431 | os.link(self.foo, self.bar) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1432 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1433 | self.tar = tarfile.open(tmpname, "w") |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 1434 | self.tar.add(self.foo) |
| 1435 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1436 | def tearDown(self): |
Hirokazu Yamamoto | af079d4 | 2008-09-21 11:50:03 +0000 | [diff] [blame] | 1437 | self.tar.close() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1438 | support.unlink(self.foo) |
| 1439 | support.unlink(self.bar) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1440 | |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 1441 | def test_add_twice(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1442 | # The same name will be added as a REGTYPE every |
| 1443 | # time regardless of st_nlink. |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 1444 | tarinfo = self.tar.gettarinfo(self.foo) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1445 | self.assertEqual(tarinfo.type, tarfile.REGTYPE, |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 1446 | "add file as regular failed") |
| 1447 | |
| 1448 | def test_add_hardlink(self): |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 1449 | tarinfo = self.tar.gettarinfo(self.bar) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1450 | self.assertEqual(tarinfo.type, tarfile.LNKTYPE, |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 1451 | "add file as hardlink failed") |
| 1452 | |
| 1453 | def test_dereference_hardlink(self): |
| 1454 | self.tar.dereference = True |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 1455 | tarinfo = self.tar.gettarinfo(self.bar) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1456 | self.assertEqual(tarinfo.type, tarfile.REGTYPE, |
Neal Norwitz | b0e32e2 | 2005-10-20 04:50:13 +0000 | [diff] [blame] | 1457 | "dereferencing hardlink failed") |
| 1458 | |
Neal Norwitz | a4f651a | 2004-07-20 22:07:44 +0000 | [diff] [blame] | 1459 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1460 | class PaxWriteTest(GNUWriteTest): |
Martin v. Löwis | 78be7df | 2005-03-05 12:47:42 +0000 | [diff] [blame] | 1461 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1462 | def _test(self, name, link=None): |
| 1463 | # See GNUWriteTest. |
| 1464 | tarinfo = tarfile.TarInfo(name) |
| 1465 | if link: |
| 1466 | tarinfo.linkname = link |
| 1467 | tarinfo.type = tarfile.LNKTYPE |
Andrew M. Kuchling | d4f2552 | 2004-10-20 11:47:01 +0000 | [diff] [blame] | 1468 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1469 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1470 | try: |
| 1471 | tar.addfile(tarinfo) |
| 1472 | finally: |
| 1473 | tar.close() |
Andrew M. Kuchling | d4f2552 | 2004-10-20 11:47:01 +0000 | [diff] [blame] | 1474 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1475 | tar = tarfile.open(tmpname) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1476 | try: |
| 1477 | if link: |
| 1478 | l = tar.getmembers()[0].linkname |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1479 | self.assertEqual(link, l, "PAX longlink creation failed") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1480 | else: |
| 1481 | n = tar.getmembers()[0].name |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1482 | self.assertEqual(name, n, "PAX longname creation failed") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1483 | finally: |
| 1484 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1485 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1486 | def test_pax_global_header(self): |
| 1487 | pax_headers = { |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 1488 | "foo": "bar", |
| 1489 | "uid": "0", |
| 1490 | "mtime": "1.23", |
Guido van Rossum | a055770 | 2007-08-07 23:19:53 +0000 | [diff] [blame] | 1491 | "test": "\xe4\xf6\xfc", |
| 1492 | "\xe4\xf6\xfc": "test"} |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1493 | |
Benjamin Peterson | 886af96 | 2010-03-21 23:13:07 +0000 | [diff] [blame] | 1494 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1495 | pax_headers=pax_headers) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1496 | try: |
| 1497 | tar.addfile(tarfile.TarInfo("test")) |
| 1498 | finally: |
| 1499 | tar.close() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1500 | |
| 1501 | # Test if the global header was written correctly. |
| 1502 | tar = tarfile.open(tmpname, encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1503 | try: |
| 1504 | self.assertEqual(tar.pax_headers, pax_headers) |
| 1505 | self.assertEqual(tar.getmembers()[0].pax_headers, pax_headers) |
| 1506 | # Test if all the fields are strings. |
| 1507 | for key, val in tar.pax_headers.items(): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1508 | self.assertIsNot(type(key), bytes) |
| 1509 | self.assertIsNot(type(val), bytes) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1510 | if key in tarfile.PAX_NUMBER_FIELDS: |
| 1511 | try: |
| 1512 | tarfile.PAX_NUMBER_FIELDS[key](val) |
| 1513 | except (TypeError, ValueError): |
| 1514 | self.fail("unable to convert pax header field") |
| 1515 | finally: |
| 1516 | tar.close() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1517 | |
| 1518 | def test_pax_extended_header(self): |
| 1519 | # The fields from the pax header have priority over the |
| 1520 | # TarInfo. |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 1521 | pax_headers = {"path": "foo", "uid": "123"} |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1522 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1523 | tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, |
| 1524 | encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1525 | try: |
| 1526 | t = tarfile.TarInfo() |
| 1527 | t.name = "\xe4\xf6\xfc" # non-ASCII |
| 1528 | t.uid = 8**8 # too large |
| 1529 | t.pax_headers = pax_headers |
| 1530 | tar.addfile(t) |
| 1531 | finally: |
| 1532 | tar.close() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1533 | |
| 1534 | tar = tarfile.open(tmpname, encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1535 | try: |
| 1536 | t = tar.getmembers()[0] |
| 1537 | self.assertEqual(t.pax_headers, pax_headers) |
| 1538 | self.assertEqual(t.name, "foo") |
| 1539 | self.assertEqual(t.uid, 123) |
| 1540 | finally: |
| 1541 | tar.close() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1542 | |
| 1543 | |
| 1544 | class UstarUnicodeTest(unittest.TestCase): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1545 | |
| 1546 | format = tarfile.USTAR_FORMAT |
| 1547 | |
| 1548 | def test_iso8859_1_filename(self): |
| 1549 | self._test_unicode_filename("iso8859-1") |
| 1550 | |
| 1551 | def test_utf7_filename(self): |
| 1552 | self._test_unicode_filename("utf7") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1553 | |
| 1554 | def test_utf8_filename(self): |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 1555 | self._test_unicode_filename("utf-8") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1556 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1557 | def _test_unicode_filename(self, encoding): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1558 | tar = tarfile.open(tmpname, "w", format=self.format, |
| 1559 | encoding=encoding, errors="strict") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1560 | try: |
| 1561 | name = "\xe4\xf6\xfc" |
| 1562 | tar.addfile(tarfile.TarInfo(name)) |
| 1563 | finally: |
| 1564 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1565 | |
| 1566 | tar = tarfile.open(tmpname, encoding=encoding) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1567 | try: |
| 1568 | self.assertEqual(tar.getmembers()[0].name, name) |
| 1569 | finally: |
| 1570 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1571 | |
| 1572 | def test_unicode_filename_error(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1573 | tar = tarfile.open(tmpname, "w", format=self.format, |
| 1574 | encoding="ascii", errors="strict") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1575 | try: |
| 1576 | tarinfo = tarfile.TarInfo() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1577 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1578 | tarinfo.name = "\xe4\xf6\xfc" |
| 1579 | self.assertRaises(UnicodeError, tar.addfile, tarinfo) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1580 | |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1581 | tarinfo.name = "foo" |
| 1582 | tarinfo.uname = "\xe4\xf6\xfc" |
| 1583 | self.assertRaises(UnicodeError, tar.addfile, tarinfo) |
| 1584 | finally: |
| 1585 | tar.close() |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1586 | |
| 1587 | def test_unicode_argument(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1588 | tar = tarfile.open(tarname, "r", |
| 1589 | encoding="iso8859-1", errors="strict") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1590 | try: |
| 1591 | for t in tar: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1592 | self.assertIs(type(t.name), str) |
| 1593 | self.assertIs(type(t.linkname), str) |
| 1594 | self.assertIs(type(t.uname), str) |
| 1595 | self.assertIs(type(t.gname), str) |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1596 | finally: |
| 1597 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1598 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1599 | def test_uname_unicode(self): |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 1600 | t = tarfile.TarInfo("foo") |
| 1601 | t.uname = "\xe4\xf6\xfc" |
| 1602 | t.gname = "\xe4\xf6\xfc" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1603 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1604 | tar = tarfile.open(tmpname, mode="w", format=self.format, |
| 1605 | encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1606 | try: |
| 1607 | tar.addfile(t) |
| 1608 | finally: |
| 1609 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1610 | |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 1611 | tar = tarfile.open(tmpname, encoding="iso8859-1") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1612 | try: |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1613 | t = tar.getmember("foo") |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1614 | self.assertEqual(t.uname, "\xe4\xf6\xfc") |
| 1615 | self.assertEqual(t.gname, "\xe4\xf6\xfc") |
| 1616 | |
| 1617 | if self.format != tarfile.PAX_FORMAT: |
Antoine Pitrou | ab58b5f | 2010-09-23 19:39:35 +0000 | [diff] [blame] | 1618 | tar.close() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1619 | tar = tarfile.open(tmpname, encoding="ascii") |
| 1620 | t = tar.getmember("foo") |
| 1621 | self.assertEqual(t.uname, "\udce4\udcf6\udcfc") |
| 1622 | self.assertEqual(t.gname, "\udce4\udcf6\udcfc") |
| 1623 | finally: |
| 1624 | tar.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1625 | |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 1626 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1627 | class GNUUnicodeTest(UstarUnicodeTest): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1628 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1629 | format = tarfile.GNU_FORMAT |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1630 | |
Lars Gustäbel | 1465cc2 | 2010-05-17 18:02:50 +0000 | [diff] [blame] | 1631 | def test_bad_pax_header(self): |
| 1632 | # Test for issue #8633. GNU tar <= 1.23 creates raw binary fields |
| 1633 | # without a hdrcharset=BINARY header. |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1634 | for encoding, name in ( |
| 1635 | ("utf-8", "pax/bad-pax-\udce4\udcf6\udcfc"), |
Lars Gustäbel | 1465cc2 | 2010-05-17 18:02:50 +0000 | [diff] [blame] | 1636 | ("iso8859-1", "pax/bad-pax-\xe4\xf6\xfc"),): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1637 | with tarfile.open(tarname, encoding=encoding, |
| 1638 | errors="surrogateescape") as tar: |
Lars Gustäbel | 1465cc2 | 2010-05-17 18:02:50 +0000 | [diff] [blame] | 1639 | try: |
| 1640 | t = tar.getmember(name) |
| 1641 | except KeyError: |
| 1642 | self.fail("unable to read bad GNU tar pax header") |
| 1643 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1644 | |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 1645 | class PAXUnicodeTest(UstarUnicodeTest): |
| 1646 | |
| 1647 | format = tarfile.PAX_FORMAT |
| 1648 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1649 | # PAX_FORMAT ignores encoding in write mode. |
| 1650 | test_unicode_filename_error = None |
| 1651 | |
Lars Gustäbel | 1465cc2 | 2010-05-17 18:02:50 +0000 | [diff] [blame] | 1652 | def test_binary_header(self): |
| 1653 | # Test a POSIX.1-2008 compatible header with a hdrcharset=BINARY field. |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1654 | for encoding, name in ( |
| 1655 | ("utf-8", "pax/hdrcharset-\udce4\udcf6\udcfc"), |
Lars Gustäbel | 1465cc2 | 2010-05-17 18:02:50 +0000 | [diff] [blame] | 1656 | ("iso8859-1", "pax/hdrcharset-\xe4\xf6\xfc"),): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1657 | with tarfile.open(tarname, encoding=encoding, |
| 1658 | errors="surrogateescape") as tar: |
Lars Gustäbel | 1465cc2 | 2010-05-17 18:02:50 +0000 | [diff] [blame] | 1659 | try: |
| 1660 | t = tar.getmember(name) |
| 1661 | except KeyError: |
| 1662 | self.fail("unable to read POSIX.1-2008 binary header") |
| 1663 | |
Lars Gustäbel | 3741eff | 2007-08-21 12:17:05 +0000 | [diff] [blame] | 1664 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1665 | class AppendTestBase: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1666 | # Test append mode (cp. patch #1652681). |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1667 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1668 | def setUp(self): |
| 1669 | self.tarname = tmpname |
| 1670 | if os.path.exists(self.tarname): |
| 1671 | os.remove(self.tarname) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 1672 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1673 | def _create_testtar(self, mode="w:"): |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1674 | with tarfile.open(tarname, encoding="iso8859-1") as src: |
| 1675 | t = src.getmember("ustar/regtype") |
| 1676 | t.name = "foo" |
Lars Gustäbel | 7a919e9 | 2012-05-05 18:15:03 +0200 | [diff] [blame] | 1677 | with src.extractfile(t) as f: |
Antoine Pitrou | e1eca4e | 2010-10-29 23:49:49 +0000 | [diff] [blame] | 1678 | with tarfile.open(self.tarname, mode) as tar: |
| 1679 | tar.addfile(t, f) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 1680 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1681 | def test_append_compressed(self): |
| 1682 | self._create_testtar("w:" + self.suffix) |
| 1683 | self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a") |
| 1684 | |
| 1685 | class AppendTest(AppendTestBase, unittest.TestCase): |
| 1686 | test_append_compressed = None |
| 1687 | |
| 1688 | def _add_testfile(self, fileobj=None): |
| 1689 | with tarfile.open(self.tarname, "a", fileobj=fileobj) as tar: |
| 1690 | tar.addfile(tarfile.TarInfo("bar")) |
| 1691 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1692 | def _test(self, names=["bar"], fileobj=None): |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1693 | with tarfile.open(self.tarname, fileobj=fileobj) as tar: |
| 1694 | self.assertEqual(tar.getnames(), names) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1695 | |
| 1696 | def test_non_existing(self): |
| 1697 | self._add_testfile() |
| 1698 | self._test() |
| 1699 | |
| 1700 | def test_empty(self): |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 1701 | tarfile.open(self.tarname, "w:").close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1702 | self._add_testfile() |
| 1703 | self._test() |
| 1704 | |
| 1705 | def test_empty_fileobj(self): |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 1706 | fobj = io.BytesIO(b"\0" * 1024) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1707 | self._add_testfile(fobj) |
| 1708 | fobj.seek(0) |
| 1709 | self._test(fileobj=fobj) |
| 1710 | |
| 1711 | def test_fileobj(self): |
| 1712 | self._create_testtar() |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1713 | with open(self.tarname, "rb") as fobj: |
| 1714 | data = fobj.read() |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 1715 | fobj = io.BytesIO(data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1716 | self._add_testfile(fobj) |
| 1717 | fobj.seek(0) |
| 1718 | self._test(names=["foo", "bar"], fileobj=fobj) |
| 1719 | |
| 1720 | def test_existing(self): |
| 1721 | self._create_testtar() |
| 1722 | self._add_testfile() |
| 1723 | self._test(names=["foo", "bar"]) |
| 1724 | |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 1725 | # Append mode is supposed to fail if the tarfile to append to |
| 1726 | # does not end with a zero block. |
| 1727 | def _test_error(self, data): |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 1728 | with open(self.tarname, "wb") as fobj: |
| 1729 | fobj.write(data) |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 1730 | self.assertRaises(tarfile.ReadError, self._add_testfile) |
| 1731 | |
| 1732 | def test_null(self): |
| 1733 | self._test_error(b"") |
| 1734 | |
| 1735 | def test_incomplete(self): |
| 1736 | self._test_error(b"\0" * 13) |
| 1737 | |
| 1738 | def test_premature_eof(self): |
| 1739 | data = tarfile.TarInfo("foo").tobuf() |
| 1740 | self._test_error(data) |
| 1741 | |
| 1742 | def test_trailing_garbage(self): |
| 1743 | data = tarfile.TarInfo("foo").tobuf() |
| 1744 | self._test_error(data + b"\0" * 13) |
| 1745 | |
| 1746 | def test_invalid(self): |
| 1747 | self._test_error(b"a" * 512) |
| 1748 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1749 | class GzipAppendTest(GzipTest, AppendTestBase, unittest.TestCase): |
| 1750 | pass |
| 1751 | |
| 1752 | class Bz2AppendTest(Bz2Test, AppendTestBase, unittest.TestCase): |
| 1753 | pass |
| 1754 | |
| 1755 | class LzmaAppendTest(LzmaTest, AppendTestBase, unittest.TestCase): |
| 1756 | pass |
| 1757 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1758 | |
| 1759 | class LimitsTest(unittest.TestCase): |
| 1760 | |
| 1761 | def test_ustar_limits(self): |
| 1762 | # 100 char name |
| 1763 | tarinfo = tarfile.TarInfo("0123456789" * 10) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1764 | tarinfo.tobuf(tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1765 | |
| 1766 | # 101 char name that cannot be stored |
| 1767 | tarinfo = tarfile.TarInfo("0123456789" * 10 + "0") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1768 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1769 | |
| 1770 | # 256 char name with a slash at pos 156 |
| 1771 | tarinfo = tarfile.TarInfo("123/" * 62 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1772 | tarinfo.tobuf(tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1773 | |
| 1774 | # 256 char name that cannot be stored |
| 1775 | tarinfo = tarfile.TarInfo("1234567/" * 31 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1776 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1777 | |
| 1778 | # 512 char name |
| 1779 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1780 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1781 | |
| 1782 | # 512 char linkname |
| 1783 | tarinfo = tarfile.TarInfo("longlink") |
| 1784 | tarinfo.linkname = "123/" * 126 + "longname" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1785 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1786 | |
| 1787 | # uid > 8 digits |
| 1788 | tarinfo = tarfile.TarInfo("name") |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1789 | tarinfo.uid = 0o10000000 |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1790 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1791 | |
| 1792 | def test_gnu_limits(self): |
| 1793 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1794 | tarinfo.tobuf(tarfile.GNU_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1795 | |
| 1796 | tarinfo = tarfile.TarInfo("longlink") |
| 1797 | tarinfo.linkname = "123/" * 126 + "longname" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1798 | tarinfo.tobuf(tarfile.GNU_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1799 | |
| 1800 | # uid >= 256 ** 7 |
| 1801 | tarinfo = tarfile.TarInfo("name") |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1802 | tarinfo.uid = 0o4000000000000000000 |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1803 | self.assertRaises(ValueError, tarinfo.tobuf, tarfile.GNU_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1804 | |
| 1805 | def test_pax_limits(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1806 | tarinfo = tarfile.TarInfo("123/" * 126 + "longname") |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1807 | tarinfo.tobuf(tarfile.PAX_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1808 | |
| 1809 | tarinfo = tarfile.TarInfo("longlink") |
| 1810 | tarinfo.linkname = "123/" * 126 + "longname" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1811 | tarinfo.tobuf(tarfile.PAX_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1812 | |
| 1813 | tarinfo = tarfile.TarInfo("name") |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1814 | tarinfo.uid = 0o4000000000000000000 |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 1815 | tarinfo.tobuf(tarfile.PAX_FORMAT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1816 | |
| 1817 | |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 1818 | class MiscTest(unittest.TestCase): |
| 1819 | |
| 1820 | def test_char_fields(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1821 | self.assertEqual(tarfile.stn("foo", 8, "ascii", "strict"), |
| 1822 | b"foo\0\0\0\0\0") |
| 1823 | self.assertEqual(tarfile.stn("foobar", 3, "ascii", "strict"), |
| 1824 | b"foo") |
| 1825 | self.assertEqual(tarfile.nts(b"foo\0\0\0\0\0", "ascii", "strict"), |
| 1826 | "foo") |
| 1827 | self.assertEqual(tarfile.nts(b"foo\0bar\0", "ascii", "strict"), |
| 1828 | "foo") |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 1829 | |
Lars Gustäbel | ac3d137 | 2011-10-14 12:46:40 +0200 | [diff] [blame] | 1830 | def test_read_number_fields(self): |
| 1831 | # Issue 13158: Test if GNU tar specific base-256 number fields |
| 1832 | # are decoded correctly. |
| 1833 | self.assertEqual(tarfile.nti(b"0000001\x00"), 1) |
| 1834 | self.assertEqual(tarfile.nti(b"7777777\x00"), 0o7777777) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1835 | self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\x00\x20\x00\x00"), |
| 1836 | 0o10000000) |
| 1837 | self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\xff\xff\xff\xff"), |
| 1838 | 0xffffffff) |
| 1839 | self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\xff"), |
| 1840 | -1) |
| 1841 | self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\x9c"), |
| 1842 | -100) |
| 1843 | self.assertEqual(tarfile.nti(b"\xff\x00\x00\x00\x00\x00\x00\x00"), |
| 1844 | -0x100000000000000) |
Lars Gustäbel | ac3d137 | 2011-10-14 12:46:40 +0200 | [diff] [blame] | 1845 | |
| 1846 | def test_write_number_fields(self): |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 1847 | self.assertEqual(tarfile.itn(1), b"0000001\x00") |
Lars Gustäbel | ac3d137 | 2011-10-14 12:46:40 +0200 | [diff] [blame] | 1848 | self.assertEqual(tarfile.itn(0o7777777), b"7777777\x00") |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1849 | self.assertEqual(tarfile.itn(0o10000000), |
| 1850 | b"\x80\x00\x00\x00\x00\x20\x00\x00") |
| 1851 | self.assertEqual(tarfile.itn(0xffffffff), |
| 1852 | b"\x80\x00\x00\x00\xff\xff\xff\xff") |
| 1853 | self.assertEqual(tarfile.itn(-1), |
| 1854 | b"\xff\xff\xff\xff\xff\xff\xff\xff") |
| 1855 | self.assertEqual(tarfile.itn(-100), |
| 1856 | b"\xff\xff\xff\xff\xff\xff\xff\x9c") |
| 1857 | self.assertEqual(tarfile.itn(-0x100000000000000), |
| 1858 | b"\xff\x00\x00\x00\x00\x00\x00\x00") |
Lars Gustäbel | ac3d137 | 2011-10-14 12:46:40 +0200 | [diff] [blame] | 1859 | |
| 1860 | def test_number_field_limits(self): |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 1861 | with self.assertRaises(ValueError): |
| 1862 | tarfile.itn(-1, 8, tarfile.USTAR_FORMAT) |
| 1863 | with self.assertRaises(ValueError): |
| 1864 | tarfile.itn(0o10000000, 8, tarfile.USTAR_FORMAT) |
| 1865 | with self.assertRaises(ValueError): |
| 1866 | tarfile.itn(-0x10000000001, 6, tarfile.GNU_FORMAT) |
| 1867 | with self.assertRaises(ValueError): |
| 1868 | tarfile.itn(0x10000000000, 6, tarfile.GNU_FORMAT) |
Lars Gustäbel | b506dc3 | 2007-08-07 18:36:16 +0000 | [diff] [blame] | 1869 | |
| 1870 | |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 1871 | class CommandLineTest(unittest.TestCase): |
| 1872 | |
Serhiy Storchaka | 255493c | 2014-02-05 20:54:43 +0200 | [diff] [blame] | 1873 | def tarfilecmd(self, *args, **kwargs): |
| 1874 | rc, out, err = script_helper.assert_python_ok('-m', 'tarfile', *args, |
| 1875 | **kwargs) |
Antoine Pitrou | 3b7b1e5 | 2013-11-24 01:55:05 +0100 | [diff] [blame] | 1876 | return out.replace(os.linesep.encode(), b'\n') |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 1877 | |
| 1878 | def tarfilecmd_failure(self, *args): |
| 1879 | return script_helper.assert_python_failure('-m', 'tarfile', *args) |
| 1880 | |
| 1881 | def make_simple_tarfile(self, tar_name): |
| 1882 | files = [support.findfile('tokenize_tests.txt'), |
| 1883 | support.findfile('tokenize_tests-no-coding-cookie-' |
| 1884 | 'and-utf8-bom-sig-only.txt')] |
| 1885 | self.addCleanup(support.unlink, tar_name) |
| 1886 | with tarfile.open(tar_name, 'w') as tf: |
| 1887 | for tardata in files: |
| 1888 | tf.add(tardata, arcname=os.path.basename(tardata)) |
| 1889 | |
| 1890 | def test_test_command(self): |
Serhiy Storchaka | 5e8c809 | 2013-11-24 02:30:59 +0200 | [diff] [blame] | 1891 | for tar_name in testtarnames: |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 1892 | for opt in '-t', '--test': |
| 1893 | out = self.tarfilecmd(opt, tar_name) |
| 1894 | self.assertEqual(out, b'') |
| 1895 | |
| 1896 | def test_test_command_verbose(self): |
Serhiy Storchaka | 5e8c809 | 2013-11-24 02:30:59 +0200 | [diff] [blame] | 1897 | for tar_name in testtarnames: |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 1898 | for opt in '-v', '--verbose': |
| 1899 | out = self.tarfilecmd(opt, '-t', tar_name) |
| 1900 | self.assertIn(b'is a tar archive.\n', out) |
| 1901 | |
| 1902 | def test_test_command_invalid_file(self): |
| 1903 | zipname = support.findfile('zipdir.zip') |
| 1904 | rc, out, err = self.tarfilecmd_failure('-t', zipname) |
| 1905 | self.assertIn(b' is not a tar archive.', err) |
| 1906 | self.assertEqual(out, b'') |
| 1907 | self.assertEqual(rc, 1) |
| 1908 | |
Serhiy Storchaka | 5e8c809 | 2013-11-24 02:30:59 +0200 | [diff] [blame] | 1909 | for tar_name in testtarnames: |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 1910 | with self.subTest(tar_name=tar_name): |
| 1911 | with open(tar_name, 'rb') as f: |
| 1912 | data = f.read() |
| 1913 | try: |
| 1914 | with open(tmpname, 'wb') as f: |
| 1915 | f.write(data[:511]) |
| 1916 | rc, out, err = self.tarfilecmd_failure('-t', tmpname) |
| 1917 | self.assertEqual(out, b'') |
| 1918 | self.assertEqual(rc, 1) |
| 1919 | finally: |
| 1920 | support.unlink(tmpname) |
| 1921 | |
| 1922 | def test_list_command(self): |
Serhiy Storchaka | 255493c | 2014-02-05 20:54:43 +0200 | [diff] [blame] | 1923 | for tar_name in testtarnames: |
| 1924 | with support.captured_stdout() as t: |
| 1925 | with tarfile.open(tar_name, 'r') as tf: |
| 1926 | tf.list(verbose=False) |
| 1927 | expected = t.getvalue().encode('ascii', 'backslashreplace') |
| 1928 | for opt in '-l', '--list': |
| 1929 | out = self.tarfilecmd(opt, tar_name, |
| 1930 | PYTHONIOENCODING='ascii') |
| 1931 | self.assertEqual(out, expected) |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 1932 | |
| 1933 | def test_list_command_verbose(self): |
Serhiy Storchaka | 255493c | 2014-02-05 20:54:43 +0200 | [diff] [blame] | 1934 | for tar_name in testtarnames: |
| 1935 | with support.captured_stdout() as t: |
| 1936 | with tarfile.open(tar_name, 'r') as tf: |
| 1937 | tf.list(verbose=True) |
| 1938 | expected = t.getvalue().encode('ascii', 'backslashreplace') |
| 1939 | for opt in '-v', '--verbose': |
| 1940 | out = self.tarfilecmd(opt, '-l', tar_name, |
| 1941 | PYTHONIOENCODING='ascii') |
| 1942 | self.assertEqual(out, expected) |
Serhiy Storchaka | d27b455 | 2013-11-24 01:53:29 +0200 | [diff] [blame] | 1943 | |
| 1944 | def test_list_command_invalid_file(self): |
| 1945 | zipname = support.findfile('zipdir.zip') |
| 1946 | rc, out, err = self.tarfilecmd_failure('-l', zipname) |
| 1947 | self.assertIn(b' is not a tar archive.', err) |
| 1948 | self.assertEqual(out, b'') |
| 1949 | self.assertEqual(rc, 1) |
| 1950 | |
| 1951 | def test_create_command(self): |
| 1952 | files = [support.findfile('tokenize_tests.txt'), |
| 1953 | support.findfile('tokenize_tests-no-coding-cookie-' |
| 1954 | 'and-utf8-bom-sig-only.txt')] |
| 1955 | for opt in '-c', '--create': |
| 1956 | try: |
| 1957 | out = self.tarfilecmd(opt, tmpname, *files) |
| 1958 | self.assertEqual(out, b'') |
| 1959 | with tarfile.open(tmpname) as tar: |
| 1960 | tar.getmembers() |
| 1961 | finally: |
| 1962 | support.unlink(tmpname) |
| 1963 | |
| 1964 | def test_create_command_verbose(self): |
| 1965 | files = [support.findfile('tokenize_tests.txt'), |
| 1966 | support.findfile('tokenize_tests-no-coding-cookie-' |
| 1967 | 'and-utf8-bom-sig-only.txt')] |
| 1968 | for opt in '-v', '--verbose': |
| 1969 | try: |
| 1970 | out = self.tarfilecmd(opt, '-c', tmpname, *files) |
| 1971 | self.assertIn(b' file created.', out) |
| 1972 | with tarfile.open(tmpname) as tar: |
| 1973 | tar.getmembers() |
| 1974 | finally: |
| 1975 | support.unlink(tmpname) |
| 1976 | |
| 1977 | def test_create_command_dotless_filename(self): |
| 1978 | files = [support.findfile('tokenize_tests.txt')] |
| 1979 | try: |
| 1980 | out = self.tarfilecmd('-c', dotlessname, *files) |
| 1981 | self.assertEqual(out, b'') |
| 1982 | with tarfile.open(dotlessname) as tar: |
| 1983 | tar.getmembers() |
| 1984 | finally: |
| 1985 | support.unlink(dotlessname) |
| 1986 | |
| 1987 | def test_create_command_dot_started_filename(self): |
| 1988 | tar_name = os.path.join(TEMPDIR, ".testtar") |
| 1989 | files = [support.findfile('tokenize_tests.txt')] |
| 1990 | try: |
| 1991 | out = self.tarfilecmd('-c', tar_name, *files) |
| 1992 | self.assertEqual(out, b'') |
| 1993 | with tarfile.open(tar_name) as tar: |
| 1994 | tar.getmembers() |
| 1995 | finally: |
| 1996 | support.unlink(tar_name) |
| 1997 | |
| 1998 | def test_extract_command(self): |
| 1999 | self.make_simple_tarfile(tmpname) |
| 2000 | for opt in '-e', '--extract': |
| 2001 | try: |
| 2002 | with support.temp_cwd(tarextdir): |
| 2003 | out = self.tarfilecmd(opt, tmpname) |
| 2004 | self.assertEqual(out, b'') |
| 2005 | finally: |
| 2006 | support.rmtree(tarextdir) |
| 2007 | |
| 2008 | def test_extract_command_verbose(self): |
| 2009 | self.make_simple_tarfile(tmpname) |
| 2010 | for opt in '-v', '--verbose': |
| 2011 | try: |
| 2012 | with support.temp_cwd(tarextdir): |
| 2013 | out = self.tarfilecmd(opt, '-e', tmpname) |
| 2014 | self.assertIn(b' file is extracted.', out) |
| 2015 | finally: |
| 2016 | support.rmtree(tarextdir) |
| 2017 | |
| 2018 | def test_extract_command_different_directory(self): |
| 2019 | self.make_simple_tarfile(tmpname) |
| 2020 | try: |
| 2021 | with support.temp_cwd(tarextdir): |
| 2022 | out = self.tarfilecmd('-e', tmpname, 'spamdir') |
| 2023 | self.assertEqual(out, b'') |
| 2024 | finally: |
| 2025 | support.rmtree(tarextdir) |
| 2026 | |
| 2027 | def test_extract_command_invalid_file(self): |
| 2028 | zipname = support.findfile('zipdir.zip') |
| 2029 | with support.temp_cwd(tarextdir): |
| 2030 | rc, out, err = self.tarfilecmd_failure('-e', zipname) |
| 2031 | self.assertIn(b' is not a tar archive.', err) |
| 2032 | self.assertEqual(out, b'') |
| 2033 | self.assertEqual(rc, 1) |
| 2034 | |
| 2035 | |
Lars Gustäbel | 0138581 | 2010-03-03 12:08:54 +0000 | [diff] [blame] | 2036 | class ContextManagerTest(unittest.TestCase): |
| 2037 | |
| 2038 | def test_basic(self): |
| 2039 | with tarfile.open(tarname) as tar: |
| 2040 | self.assertFalse(tar.closed, "closed inside runtime context") |
| 2041 | self.assertTrue(tar.closed, "context manager failed") |
| 2042 | |
| 2043 | def test_closed(self): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 2044 | # The __enter__() method is supposed to raise OSError |
Lars Gustäbel | 0138581 | 2010-03-03 12:08:54 +0000 | [diff] [blame] | 2045 | # if the TarFile object is already closed. |
| 2046 | tar = tarfile.open(tarname) |
| 2047 | tar.close() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 2048 | with self.assertRaises(OSError): |
Lars Gustäbel | 0138581 | 2010-03-03 12:08:54 +0000 | [diff] [blame] | 2049 | with tar: |
| 2050 | pass |
| 2051 | |
| 2052 | def test_exception(self): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 2053 | # Test if the OSError exception is passed through properly. |
Lars Gustäbel | 0138581 | 2010-03-03 12:08:54 +0000 | [diff] [blame] | 2054 | with self.assertRaises(Exception) as exc: |
| 2055 | with tarfile.open(tarname) as tar: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 2056 | raise OSError |
| 2057 | self.assertIsInstance(exc.exception, OSError, |
Lars Gustäbel | 0138581 | 2010-03-03 12:08:54 +0000 | [diff] [blame] | 2058 | "wrong exception raised in context manager") |
| 2059 | self.assertTrue(tar.closed, "context manager failed") |
| 2060 | |
| 2061 | def test_no_eof(self): |
| 2062 | # __exit__() must not write end-of-archive blocks if an |
| 2063 | # exception was raised. |
| 2064 | try: |
| 2065 | with tarfile.open(tmpname, "w") as tar: |
| 2066 | raise Exception |
| 2067 | except: |
| 2068 | pass |
| 2069 | self.assertEqual(os.path.getsize(tmpname), 0, |
| 2070 | "context manager wrote an end-of-archive block") |
| 2071 | self.assertTrue(tar.closed, "context manager failed") |
| 2072 | |
| 2073 | def test_eof(self): |
| 2074 | # __exit__() must write end-of-archive blocks, i.e. call |
| 2075 | # TarFile.close() if there was no error. |
| 2076 | with tarfile.open(tmpname, "w"): |
| 2077 | pass |
| 2078 | self.assertNotEqual(os.path.getsize(tmpname), 0, |
| 2079 | "context manager wrote no end-of-archive block") |
| 2080 | |
| 2081 | def test_fileobj(self): |
| 2082 | # Test that __exit__() did not close the external file |
| 2083 | # object. |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 2084 | with open(tmpname, "wb") as fobj: |
| 2085 | try: |
| 2086 | with tarfile.open(fileobj=fobj, mode="w") as tar: |
| 2087 | raise Exception |
| 2088 | except: |
| 2089 | pass |
| 2090 | self.assertFalse(fobj.closed, "external file object was closed") |
| 2091 | self.assertTrue(tar.closed, "context manager failed") |
Lars Gustäbel | 0138581 | 2010-03-03 12:08:54 +0000 | [diff] [blame] | 2092 | |
| 2093 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2094 | @unittest.skipIf(hasattr(os, "link"), "requires os.link to be missing") |
| 2095 | class LinkEmulationTest(ReadTest, unittest.TestCase): |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 2096 | |
| 2097 | # Test for issue #8741 regression. On platforms that do not support |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2098 | # symbolic or hard links tarfile tries to extract these types of members |
| 2099 | # as the regular files they point to. |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 2100 | def _test_link_extraction(self, name): |
| 2101 | self.tar.extract(name, TEMPDIR) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2102 | with open(os.path.join(TEMPDIR, name), "rb") as f: |
| 2103 | data = f.read() |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 2104 | self.assertEqual(md5sum(data), md5_regtype) |
| 2105 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2106 | # See issues #1578269, #8879, and #17689 for some history on these skips |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2107 | @unittest.skipIf(hasattr(os.path, "islink"), |
| 2108 | "Skip emulation - has os.path.islink but not os.link") |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 2109 | def test_hardlink_extraction1(self): |
| 2110 | self._test_link_extraction("ustar/lnktype") |
| 2111 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2112 | @unittest.skipIf(hasattr(os.path, "islink"), |
| 2113 | "Skip emulation - has os.path.islink but not os.link") |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 2114 | def test_hardlink_extraction2(self): |
| 2115 | self._test_link_extraction("./ustar/linktest2/lnktype") |
| 2116 | |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 2117 | @unittest.skipIf(hasattr(os, "symlink"), |
| 2118 | "Skip emulation if symlink exists") |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 2119 | def test_symlink_extraction1(self): |
| 2120 | self._test_link_extraction("ustar/symtype") |
| 2121 | |
Brian Curtin | 74e4561 | 2010-07-09 15:58:59 +0000 | [diff] [blame] | 2122 | @unittest.skipIf(hasattr(os, "symlink"), |
| 2123 | "Skip emulation if symlink exists") |
Lars Gustäbel | 1b51272 | 2010-06-03 12:45:16 +0000 | [diff] [blame] | 2124 | def test_symlink_extraction2(self): |
| 2125 | self._test_link_extraction("./ustar/linktest2/symtype") |
| 2126 | |
| 2127 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2128 | class Bz2PartialReadTest(Bz2Test, unittest.TestCase): |
Lars Gustäbel | 42e0091 | 2009-03-22 20:34:29 +0000 | [diff] [blame] | 2129 | # Issue5068: The _BZ2Proxy.read() method loops forever |
| 2130 | # on an empty or partial bzipped file. |
| 2131 | |
| 2132 | def _test_partial_input(self, mode): |
| 2133 | class MyBytesIO(io.BytesIO): |
| 2134 | hit_eof = False |
| 2135 | def read(self, n): |
| 2136 | if self.hit_eof: |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2137 | raise AssertionError("infinite loop detected in " |
| 2138 | "tarfile.open()") |
Lars Gustäbel | 42e0091 | 2009-03-22 20:34:29 +0000 | [diff] [blame] | 2139 | self.hit_eof = self.tell() == len(self.getvalue()) |
| 2140 | return super(MyBytesIO, self).read(n) |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 2141 | def seek(self, *args): |
| 2142 | self.hit_eof = False |
| 2143 | return super(MyBytesIO, self).seek(*args) |
Lars Gustäbel | 42e0091 | 2009-03-22 20:34:29 +0000 | [diff] [blame] | 2144 | |
| 2145 | data = bz2.compress(tarfile.TarInfo("foo").tobuf()) |
| 2146 | for x in range(len(data) + 1): |
Lars Gustäbel | 9520a43 | 2009-11-22 18:48:49 +0000 | [diff] [blame] | 2147 | try: |
| 2148 | tarfile.open(fileobj=MyBytesIO(data[:x]), mode=mode) |
| 2149 | except tarfile.ReadError: |
| 2150 | pass # we have no interest in ReadErrors |
Lars Gustäbel | 42e0091 | 2009-03-22 20:34:29 +0000 | [diff] [blame] | 2151 | |
| 2152 | def test_partial_input(self): |
| 2153 | self._test_partial_input("r") |
| 2154 | |
| 2155 | def test_partial_input_bz2(self): |
| 2156 | self._test_partial_input("r:bz2") |
| 2157 | |
| 2158 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2159 | def setUpModule(): |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 2160 | support.unlink(TEMPDIR) |
Antoine Pitrou | 941ee88 | 2009-11-11 20:59:38 +0000 | [diff] [blame] | 2161 | os.makedirs(TEMPDIR) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 2162 | |
Serhiy Storchaka | 5e8c809 | 2013-11-24 02:30:59 +0200 | [diff] [blame] | 2163 | global testtarnames |
| 2164 | testtarnames = [tarname] |
Antoine Pitrou | 95f5560 | 2010-09-23 18:36:46 +0000 | [diff] [blame] | 2165 | with open(tarname, "rb") as fobj: |
| 2166 | data = fobj.read() |
Neal Norwitz | a4f651a | 2004-07-20 22:07:44 +0000 | [diff] [blame] | 2167 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2168 | # Create compressed tarfiles. |
| 2169 | for c in GzipTest, Bz2Test, LzmaTest: |
| 2170 | if c.open: |
| 2171 | support.unlink(c.tarname) |
Serhiy Storchaka | 5e8c809 | 2013-11-24 02:30:59 +0200 | [diff] [blame] | 2172 | testtarnames.append(c.tarname) |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2173 | with c.open(c.tarname, "wb") as tar: |
| 2174 | tar.write(data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2175 | |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2176 | def tearDownModule(): |
| 2177 | if os.path.exists(TEMPDIR): |
| 2178 | shutil.rmtree(TEMPDIR) |
Neal Norwitz | b9ef4ae | 2003-01-05 23:19:43 +0000 | [diff] [blame] | 2179 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 2180 | if __name__ == "__main__": |
Serhiy Storchaka | 8b56292 | 2013-06-17 15:38:50 +0300 | [diff] [blame] | 2181 | unittest.main() |