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