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